Skip to content

Commit

Permalink
Rename --support to --spm-support-package and improve format of url r…
Browse files Browse the repository at this point in the history
…eferences (#311)
  • Loading branch information
tristanlabelle committed Sep 21, 2024
1 parent c61bf99 commit d064632
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 25 deletions.
4 changes: 2 additions & 2 deletions Generator/Sources/SwiftWinRT/CommandLineArguments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ struct CommandLineArguments: ParsableCommand {
@Flag(name: .customLong("spm"), help: "Generate a package.swift file for building with SPM.")
var generatePackageDotSwift: Bool = false

@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("spm-support-package"), help: .init("The directory path or '<url>#branch=<branch>' of the support package to reference.", valueName: "dir-or-url"))
var spmSupportPackageReference: String = "https://github.com/tristanlabelle/swift-winrt.git#branch=main"

@Flag(name: .customLong("cmakelists"), help: "Generate a CMakeLists.txt files for building with CMake.")
var generateCMakeLists: Bool = false
Expand Down
33 changes: 20 additions & 13 deletions Generator/Sources/SwiftWinRT/Writing/SwiftPackageFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import struct Foundation.URL

func writeSwiftPackageFile(
_ projection: Projection,
supportPackageLocation: String,
supportPackageReference: String,
excludeCMakeLists: Bool,
dynamicLibraries: Bool,
toPath path: String) {
var package = SwiftPackage(name: "Projection")
package.dependencies.append(getSupportPackageDependency(location: supportPackageLocation))
package.dependencies.append(getSupportPackageDependency(reference: supportPackageReference))

for module in projection.modulesByName.values {
guard !module.isEmpty else { continue }
Expand Down Expand Up @@ -77,19 +77,26 @@ func writeSwiftPackageFile(
package.write(version: "5.10", to: FileTextOutputStream(path: path, directoryCreation: .ancestors))
}

fileprivate func getSupportPackageDependency(location: String) -> SwiftPackage.Dependency {
if location.starts(with: "https://") {
if let separatorIndex = location.lastIndex(of: ":"),
let lastSlashIndex = location.lastIndex(of: "/"),
separatorIndex > lastSlashIndex {
let url = String(location[..<separatorIndex])
let branch = String(location[location.index(after: separatorIndex)...])
return .package(url: url, branch: branch)
fileprivate func getSupportPackageDependency(reference: String) -> SwiftPackage.Dependency {
if reference.starts(with: "https://") {
guard let fragmentSeparatorIndex = reference.lastIndex(of: "#") else {
fatalError("Package URL reference should include # fragment: \(reference)")
}
else {
fatalError("Unexpected support package location format: \(location)")

let url = reference[..<fragmentSeparatorIndex]
let fragment = reference[reference.index(after: fragmentSeparatorIndex)...]
guard let equalIndex = fragment.firstIndex(of: "=") else {
fatalError("Package URL fragment should include an assignment: \(reference)")
}

let lhs = fragment[..<equalIndex]
let rhs = fragment[fragment.index(after: equalIndex)...]
guard lhs == "branch" else {
fatalError("Package URL fragment should include a branch assignment: \(reference)")
}

return .package(url: String(url), branch: String(rhs))
} else {
return .package(name: "Support", path: location)
return .package(name: "Support", path: reference)
}
}
2 changes: 1 addition & 1 deletion Generator/Sources/SwiftWinRT/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ do {
if commandLineArguments.generatePackageDotSwift {
writeSwiftPackageFile(
projection,
supportPackageLocation: commandLineArguments.supportPackageLocation,
supportPackageReference: commandLineArguments.spmSupportPackageReference,
excludeCMakeLists: commandLineArguments.generateCMakeLists,
dynamicLibraries: commandLineArguments.dynamicLibraries,
toPath: "\(commandLineArguments.outputDirectoryPath)\\Package.swift")
Expand Down
2 changes: 1 addition & 1 deletion InteropTests/Build-WinRTComponentProjection.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Write-Host -ForegroundColor Cyan "Generating Swift projection for WinRT componen
-D "WINRTCOMPONENT_WINMD=$WinRTComponentBinDir\WinRTComponent.winmd" `
-D "PROJECTION_JSON=$PSScriptRoot\projection.json" `
-D "PROJECTION_DIR=$PSScriptRoot\Generated" `
-D "SPM_SUPPORT_MODULE_DIR=$PSScriptRoot\.." `
-D "SPM_SUPPORT_PACKAGE_DIR=$PSScriptRoot\.." `
-P "$PSScriptRoot\GenerateProjection.cmake"

Write-Host -ForegroundColor Cyan "Copying the WinRT component dll next to the test..."
Expand Down
2 changes: 1 addition & 1 deletion InteropTests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ generate_projection(
WINRTCOMPONENT_WINMD "${WINRTCOMPONENT_WINMD}"
PROJECTION_JSON "${CMAKE_CURRENT_SOURCE_DIR}/projection.json"
PROJECTION_DIR "${CMAKE_CURRENT_BINARY_DIR}/Projection/Sources"
SPM_SUPPORT_MODULE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/..")
SPM_SUPPORT_PACKAGE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/..")

# Define WinRTComponent build (requires the cl.exe compiler)
include(ExternalProject)
Expand Down
14 changes: 7 additions & 7 deletions InteropTests/GenerateProjection.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Invokes SwiftWinRT to generate a Swift projection for a WinRT component
# This file can be called as a CMake script.
function(generate_projection)
cmake_parse_arguments("ARG" "" "SWIFTWINRT_EXE;WINRTCOMPONENT_WINMD;PROJECTION_JSON;PROJECTION_DIR;SPM_SUPPORT_MODULE_DIR" "" ${ARGN})
cmake_parse_arguments("ARG" "" "SWIFTWINRT_EXE;WINRTCOMPONENT_WINMD;PROJECTION_JSON;PROJECTION_DIR;SPM_SUPPORT_PACKAGE_DIR" "" ${ARGN})

if ("${ARG_SWIFTWINRT_EXE}" STREQUAL "")
message(FATAL_ERROR "SWIFTWINRT_EXE argument is required")
Expand All @@ -19,25 +19,25 @@ function(generate_projection)
message(FATAL_ERROR "PROJECTION_DIR argument is required")
endif()

if("${ARG_SPM_SUPPORT_MODULE_DIR}" STREQUAL "")
message(FATAL_ERROR "SPM_SUPPORT_MODULE_DIR argument is required")
if("${ARG_SPM_SUPPORT_PACKAGE_DIR}" STREQUAL "")
message(FATAL_ERROR "SPM_SUPPORT_PACKAGE_DIR argument is required")
endif()

# SPM won't handle an absolute path with a ".." component.
cmake_path(ABSOLUTE_PATH ARG_SPM_SUPPORT_MODULE_DIR NORMALIZE)
cmake_path(ABSOLUTE_PATH ARG_SPM_SUPPORT_PACKAGE_DIR NORMALIZE)

cmake_path(CONVERT "${ARG_PROJECTION_JSON}" TO_NATIVE_PATH_LIST PROJECTION_JSON_NATIVE)
string(REPLACE "\\" "" WINDOWS_SDK_VERSION "$ENV{WindowsSDKVersion}") # Remove trailing slash
cmake_path(CONVERT "${ARG_WINRTCOMPONENT_WINMD}" TO_NATIVE_PATH_LIST WINRTCOMPONENT_WINMD_NATIVE)
cmake_path(CONVERT "${ARG_SPM_SUPPORT_MODULE_DIR}" TO_NATIVE_PATH_LIST SPM_SUPPORT_MODULE_DIR_NATIVE)
cmake_path(CONVERT "${ARG_SPM_SUPPORT_PACKAGE_DIR}" TO_NATIVE_PATH_LIST SPM_SUPPORT_PACKAGE_DIR_NATIVE)
cmake_path(CONVERT "${ARG_PROJECTION_DIR}" TO_NATIVE_PATH_LIST PROJECTION_DIR_NATIVE)
execute_process(
COMMAND "${ARG_SWIFTWINRT_EXE}"
--config "${PROJECTION_JSON_NATIVE}"
--winsdk "${WINDOWS_SDK_VERSION}"
--reference "${WINRTCOMPONENT_WINMD_NATIVE}"
--spm
--support "${SPM_SUPPORT_MODULE_DIR_NATIVE}"
--spm-support-package "${SPM_SUPPORT_PACKAGE_DIR_NATIVE}"
--cmakelists
--out "${PROJECTION_DIR_NATIVE}"
--out-manifest "${PROJECTION_DIR_NATIVE}\\WinRTComponent.manifest"
Expand All @@ -51,5 +51,5 @@ if(CMAKE_SCRIPT_MODE_FILE AND NOT CMAKE_PARENT_LIST_FILE)
WINRTCOMPONENT_WINMD "${WINRTCOMPONENT_WINMD}"
PROJECTION_JSON "${PROJECTION_JSON}"
PROJECTION_DIR "${PROJECTION_DIR}"
SPM_SUPPORT_MODULE_DIR "${SPM_SUPPORT_MODULE_DIR}")
SPM_SUPPORT_PACKAGE_DIR "${SPM_SUPPORT_PACKAGE_DIR}")
endif()

0 comments on commit d064632

Please sign in to comment.