Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --locale and --no-docs flags #223

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions Generator/Sources/SwiftWinRT/CommandLineArguments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
45 changes: 23 additions & 22 deletions Generator/Sources/SwiftWinRT/createProjection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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[..<lastPathSeparator])
let assemblyFileName = String(path[path.index(after: lastPathSeparator)...])
if let extensionDot = assemblyFileName.lastIndex(of: ".") {
let assemblyFileNameWithoutExtension = String(assemblyFileName[..<extensionDot])
let sideBySideDocsPath = "\(assemblyDirectoryPath)\\\(assemblyFileNameWithoutExtension).xml"
if FileManager.default.fileExists(atPath: sideBySideDocsPath) {
docs = try AssemblyDocumentation(readingFileAtPath: sideBySideDocsPath)
}
else if let languageCode {
let languageNestedDocsPath = "\(assemblyDirectoryPath)\\\(languageCode)\\\(assemblyFileNameWithoutExtension).xml"
if FileManager.default.fileExists(atPath: languageNestedDocsPath) {
docs = try AssemblyDocumentation(readingFileAtPath: languageNestedDocsPath)
}
}
fileprivate func tryLoadDocumentation(assemblyPath: String, locales: [String]) throws -> AssemblyDocumentation? {
let lastPathSeparator = assemblyPath.lastIndex(where: { $0 == "\\" || $0 == "/" })

let assemblyDirectoryPath = lastPathSeparator == nil ? "." : String(assemblyPath[..<lastPathSeparator!])
let assemblyFileName = lastPathSeparator == nil ? assemblyPath : String(assemblyPath[assemblyPath.index(after: lastPathSeparator!)...])

guard let extensionDot = assemblyFileName.lastIndex(of: ".") else { return nil }
let assemblyFileNameWithoutExtension = String(assemblyFileName[..<extensionDot])

for locale in locales {
let languageNestedDocsPath = "\(assemblyDirectoryPath)\\\(locale)\\\(assemblyFileNameWithoutExtension).xml"
if FileManager.default.fileExists(atPath: languageNestedDocsPath) {
return try AssemblyDocumentation(readingFileAtPath: languageNestedDocsPath)
}
}
return (assembly, docs)

let sideBySideDocsPath = "\(assemblyDirectoryPath)\\\(assemblyFileNameWithoutExtension).xml"
if FileManager.default.fileExists(atPath: sideBySideDocsPath) {
return try AssemblyDocumentation(readingFileAtPath: sideBySideDocsPath)
}

return nil
}
Loading