GenerateManualPlugin.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // This source file is part of the Swift Argument Parser open source project
  4. //
  5. // Copyright (c) 2021 Apple Inc. and the Swift project authors
  6. // Licensed under Apache License v2.0 with Runtime Library Exception
  7. //
  8. // See https://swift.org/LICENSE.txt for license information
  9. //
  10. //===----------------------------------------------------------------------===//
  11. import PackagePlugin
  12. @main
  13. struct GenerateManualPlugin: CommandPlugin {
  14. func performCommand(
  15. context: PluginContext,
  16. arguments: [String]
  17. ) async throws {
  18. // Locate generation tool.
  19. let generationTool = "generate-manual"
  20. let generationToolFile = try context.tool(named: generationTool).path
  21. // Create an extractor to extract plugin-only arguments from the `arguments`
  22. // array.
  23. var extractor = ArgumentExtractor(arguments)
  24. // Run generation tool once if help is requested.
  25. if extractor.helpRequest() {
  26. try generationToolFile.exec(arguments: ["--help"])
  27. print(
  28. """
  29. ADDITIONAL OPTIONS:
  30. --configuration <configuration>
  31. Tool build configuration used to generate the
  32. manual. (default: release)
  33. NOTE: The "GenerateManual" plugin handles passing the "<tool>" and
  34. "--output-directory <output-directory>" arguments. Manually supplying
  35. these arguments will result in a runtime failure.
  36. """)
  37. return
  38. }
  39. // Extract configuration argument before making it to the
  40. // "generate-manual" tool.
  41. let configuration = try extractor.configuration()
  42. // Build all products first.
  43. print("Building package in \(configuration) mode...")
  44. let buildResult = try packageManager.build(
  45. .all(includingTests: false),
  46. parameters: .init(configuration: configuration))
  47. guard buildResult.succeeded else {
  48. throw GenerateManualPluginError.buildFailed(buildResult.logText)
  49. }
  50. print("Built package in \(configuration) mode")
  51. // Run generate-manual on all executable artifacts.
  52. for builtArtifact in buildResult.builtArtifacts {
  53. // Skip non-executable targets
  54. guard builtArtifact.kind == .executable else { continue }
  55. // Skip executables without a matching product.
  56. guard let product = builtArtifact.matchingProduct(context: context)
  57. else { continue }
  58. // Skip products without a dependency on ArgumentParser.
  59. guard product.hasDependency(named: "ArgumentParser") else { continue }
  60. // Get the artifacts name.
  61. let executableName = builtArtifact.path.lastComponent
  62. print("Generating manual for \(executableName)...")
  63. // Create output directory.
  64. let outputDirectory = context
  65. .pluginWorkDirectory
  66. .appending(executableName)
  67. try outputDirectory.createOutputDirectory()
  68. // Create generation tool arguments.
  69. var generationToolArguments = [
  70. builtArtifact.path.string,
  71. "--output-directory",
  72. outputDirectory.string,
  73. ]
  74. generationToolArguments.append(
  75. contentsOf: extractor.remainingArguments)
  76. // Spawn generation tool.
  77. try generationToolFile.exec(arguments: generationToolArguments)
  78. print("Generated manual in '\(outputDirectory)'")
  79. }
  80. }
  81. }