From 9067e00624efb2719eba373770a6bbcd541d390d Mon Sep 17 00:00:00 2001 From: Tristan Labelle Date: Fri, 26 Jul 2024 19:30:23 -0400 Subject: [PATCH] Add --locale and --no-docs flags --- .../SwiftWinRT/CommandLineArguments.swift | 18 +++++--- .../Sources/SwiftWinRT/createProjection.swift | 45 ++++++++++--------- 2 files changed, 35 insertions(+), 28 deletions(-) diff --git a/Generator/Sources/SwiftWinRT/CommandLineArguments.swift b/Generator/Sources/SwiftWinRT/CommandLineArguments.swift index 05e1e62f..ceebf983 100644 --- a/Generator/Sources/SwiftWinRT/CommandLineArguments.swift +++ b/Generator/Sources/SwiftWinRT/CommandLineArguments.swift @@ -7,24 +7,30 @@ struct CommandLineArguments: ParsableCommand { abstract: "A WinRT projections generator for Swift.") } - @Option(name: .customLong("reference"), help: "A path to a .winmd file with the APIs to project.") + @Option(name: .customLong("reference"), help: .init("A path to a .winmd file with the APIs to project.", valueName: "file")) var references: [String] = [] - @Option(name: .customLong("winsdk"), help: "A Windows SDK version with the APIs to project.") + @Option(name: .customLong("winsdk"), help: .init("A Windows SDK version with the APIs to project.", valueName: "version")) var windowsSdkVersion: String? = nil - @Option(name: .customLong("config"), help: "A path to a json projection configuration file to use.") + @Option(name: .customLong("config"), help: .init("A path to a json projection configuration file to use.", valueName: "file")) var configFilePath: String? = nil - @Option(name: .customLong("out"), help: "A path to the output directory.") + @Option(name: .customLong("locale"), help: .init("The locale(s) to prefer for documentation comments.", valueName: "code")) + var locales: [String] = ["en-us", "en"] + + @Flag(name: .customLong("no-docs"), help: "Don't generate documentation comments.") + var noDocs: Bool = false + + @Option(name: .customLong("out"), help: .init("A path to the output directory.", valueName: "dir")) var outputDirectoryPath: String @Flag(name: .customLong("spm"), help: "Generate a package.swift file for building with SPM.") var generatePackageDotSwift: Bool = false - @Option(name: .customLong("support"), help: "The directory path or url:branch or url@revision of the support package to use.") + @Option(name: .customLong("support"), help: .init("The directory path or url:branch or url@revision of the support package to use.", valueName: "dir-or-url")) var supportPackageLocation: String = "https://github.com/tristanlabelle/swift-winrt.git:main" - @Option(name: .customLong("out-manifest"), help: "Path to generate an embeddable exe manifest file to.") + @Option(name: .customLong("out-manifest"), help: .init("Path to generate an embeddable exe manifest file to.", valueName: "file")) var exeManifestPath: String? = nil } diff --git a/Generator/Sources/SwiftWinRT/createProjection.swift b/Generator/Sources/SwiftWinRT/createProjection.swift index 800a5cad..a05add50 100644 --- a/Generator/Sources/SwiftWinRT/createProjection.swift +++ b/Generator/Sources/SwiftWinRT/createProjection.swift @@ -17,7 +17,9 @@ internal func createProjection(commandLineArguments: CommandLineArguments, proje for filePath in winMDFilePaths.sorted(by: { $0.lastPathComponent < $1.lastPathComponent }) { print("Loading assembly \(filePath.lastPathComponent)...") - let (assembly, assemblyDocumentation) = try loadAssemblyAndDocumentation(path: filePath, into: assemblyLoadContext) + let assembly = try assemblyLoadContext.load(path: filePath) + let assemblyDocumentation = commandLineArguments.noDocs ? nil + : try tryLoadDocumentation(assemblyPath: filePath, locales: commandLineArguments.locales) let (moduleName, moduleConfig) = projectionConfig.getModule(assemblyName: assembly.name) let module = projection.modulesByName[moduleName] ?? projection.addModule(name: moduleName, flattenNamespaces: moduleConfig.flattenNamespaces) @@ -104,27 +106,26 @@ fileprivate func getWindowsSdkWinMDPaths(sdkVersion: String) throws -> [String] return winmdPaths } -fileprivate func loadAssemblyAndDocumentation( - path: String, languageCode: String? = "en", - into context: AssemblyLoadContext) throws -> (assembly: Assembly, docs: AssemblyDocumentation?) { - let assembly = try context.load(path: path) - var docs: AssemblyDocumentation? = nil - if let lastPathSeparator = path.lastIndex(where: { $0 == "\\" || $0 == "/" }) { - let assemblyDirectoryPath = String(path[.. AssemblyDocumentation? { + let lastPathSeparator = assemblyPath.lastIndex(where: { $0 == "\\" || $0 == "/" }) + + let assemblyDirectoryPath = lastPathSeparator == nil ? "." : String(assemblyPath[..