Share
Embed
Transcript
79 slides
-
SE-0303 Extensible Build Tools Implemented in 5.6 • Swift PMϏϧυதͷΧελϜΞΫγϣϯΛఏڙ͍ͯ͠ͳ͍
• Ϗϧυ࣌ʹίʔυੜΛߦ͏Package PluginΛՃ • e.g.) SwiftProtobuf, SwiftGen, SwiftSyntax • ଞʹকདྷతʹ૿͍ͨ͠ϓϥάΠϯ͋Δ͕ɺ͜ͷProposalͷର֎ -
SE-0303 Extensible Build Tools Client MyPackage ᵓ Package.swift ᵓ Plugins
│ └ MySourceGenPlugin │ └ plugin.swift └ Sources ᵓ MyExe │ │ fi le.dat │ └ main.swift └ MySourceGenTool └ main.swift // swift-tools-version: 5.6 import PackageDescription let package = Package( name: "MyPackage", targets: [ .executable( name: "MyExe", plugins: [ .plugin("MySourceGenPlugin") ] ), .plugin( name: "MySourceGenPlugin", capability: .buildTool(), dependencies: ["MySourceGenTool"] ), .executableTarget( name: "MySourceGenTool" ), ] ) -
SE-0303 Extensible Build Tools Plugin • Target, executableTarget, testTargetʹpluginsΛՃ •
Package resolve / validationͷ͋ͱʹಈ࡞͢Δ • ҎԼͷΞΫηε͕Մೳ • plugin͕ద༻͞ΕΔλʔήοτͷใΛఏڙ͢ΔInput context • TargetͷSource directoryͷread-only access • Output directory -
SE-0303 Extensible Build Tools Plugin - Limitation • Target, executableTarget,
testTargetʹpluginsΛՃ͠ɺbuild tool pluginΛར༻Ͱ͖ΔΑ͏ʹ͢Δɻ • binaryTarget, systemLibrarypsudo-targetͳͷͰɺ͜ͷproposalͰαϙʔτ͠ͳ͍ • Package resolve / validationͷ͋ͱʹಈ࡞͢Δ • ҎԼͷΞΫηε͕Մೳ • plugin͕ద༻͞ΕΔλʔήοτͷใΛఏڙ͢ΔInput context • TargetͷSource directoryͷread-only access • Output directory • Package plugin targetManifestͰproductΛએݴ͠ͳͯ͘ɺಉPackageͷଞͷTargetͰར༻Ͱ͖Δɻ -
Examples SwiftGen - Client // swift-tools-version: 999.0 import PackageDescription let
package = Package( name: "MyPackage", dependencies: [ .package(url: "https://github.com/SwiftGen/SwiftGen", from: "6.4.0") ] targets: [ .executable( name: "MyLibrary", plugins: [ .plugin(name: "SwiftGenPlugin", package: "SwiftGen") ] ) ] ) MyPackage ᵓ Package.swift ᵓ swiftgen.yml └ Sources └ MyLibrary ᵓ Assets.xcassets └ SourceFile.swift -
Examples SwiftGen - Plugin // swift-tools-version: 5.6 import PackageDescription let
package = Package( name: "SwiftGen", targets: [ .plugin( name: "SwiftGenPlugin", capability: .buildTool(), dependencies: ["SwiftGen"] ), .binaryTarget( name: "SwiftGen", url: "https://url/to/the/built/swiftgen-executables.zip", checksum: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" ), ] ) SwiftGen ᵓ Package.swift ᵓ Plugins │ └ SwiftGenPlugin │ └ plugin.swift └ Sources └ SwiftGen └ ... -
Examples SwiftGen - Plugin import PackagePlugin @main struct SwiftGenPlugin: BuildToolPlugin
{ func createBuildCommands(context: TargetBuildContext) throws -> [Command] { let swiftGenConfigFile = context.packageDirectory.appending("swiftgen.yml") let genSourcesDir = context.pluginWorkDirectory.appending("GeneratedSources") return [.prebuildCommand( displayName: "Running SwiftGen", executable: try context.tool(named: "swiftgen").path, arguments: [ "config", "run", "--config", "\(swiftGenConfigFile)" ], environment: [ "PROJECT_DIR": "\(context.packageDirectory)", "TARGET_NAME": "\(context.targetName)", "DERIVED_SOURCES_DIR": "\(genSourcesDir)", ], outputFilesDirectory: genSourcesDir)] } } SwiftGen ᵓ Package.swift ᵓ Plugins │ └ SwiftGenPlugin │ └ plugin.swift └ Sources └ SwiftGen └ ... -
Examples SwiftProtobuf - Client // swift-tools-version: 5.6 import PackageDescription let
package = Package( name: "MyPackage", dependencies: [ .package(url: "https://github.com/apple/swift-protobuf", from: "1.15.0") ] targets: [ .executable( name: "MyExe", dependencies: [ .product(name: "SwiftProtobufRuntimeLib", package: "swift-protobuf") ], plugins: [ .plugin(name: "SwiftProtobuf", package: "swift-protobuf") ] ) ] ) MyPackage ᵓ Package.swift └ Sources └ MyExe ᵓ messages.proto └ main.swift -
Examples SwiftProtobuf - Plugin // swift-tools-version: 5.6 import PackageDescription let
package = Package( name: "SwiftProtobuf", targets: [ .plugin( name: "SwiftProtobuf", capability: .buildTool(), dependencies: ["protoc", "protoc-gen-swift"] ), .binaryTarget( name: "protoc", url: "https://url/to/the/built/protoc-executables.zip", checksum: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" ), .executableTarget( name: "protoc-gen-swift" ), .libraryTarget( name: "SwiftProtobufRuntimeLib" ), ] ) SwiftProtobuf ᵓ Package.swift ᵓ Plugins │ └ SwiftProtobuf │ └ plugin.swift └ Sources ᵓ SwiftProtobufRuntimeLib │ └ ... └ protoc-gen-swift └ ... -
Examples SwiftProtobuf - Plugin import PackagePlugin import Foundation @main struct
MyPlugin: BuildToolPlugin { func createBuildCommands(context: TargetBuildContext) throws -> [Command] { let protocTool = try context.tool(named: "protoc") let protocGenSwiftTool = try context.tool(named: "protoc-gen-swift") var protoSearchPaths = context.dependencies.map { target in target.targetDirectory.appending("protos") } let genSourcesDir = context.pluginWorkDirectory.appending("GeneratedSources") let otherFilesDir = context.pluginWorkDirectory.appending("OtherFiles") protoSearchPaths.append(protocTool.path.removingLastComponent().appending("system-protos")) let moduleMappingsFile = otherFilesDir.appending("module-mappings") let outputString = ". . . module mappings file . . ." let outputData = outputString.data(using: .utf8) FileManager.default.createFile(atPath: moduleMappingsFile.string, contents: outputData) let inputFiles = context.inputFiles.filter { $0.path.extension == "proto" } return inputFiles.map { inputFile in let outputName = inputFile.path.stem + ".swift" let outputPath = genSourcesDir.appending(outputName) let inputFiles = [inputFile.path] let outputFiles = [outputPath] var commandArgs = [ "--plugin=protoc-gen-swift=\(protocGenSwiftTool.path)", "--swift_out=\(genSourcesDir)", "--swift_opt=ProtoPathModuleMappings=\(moduleMappingsFile)" ] commandArgs.append(contentsOf: protoSearchPaths.flatMap { ["-I", "\($0)"] }) commandArgs.append("\(inputFile.path)") return .buildCommand( displayName: "Generating \(outputName) from \(inputFile.path.stem)", executable: protocTool.path, arguments: commandArgs, inputFiles: inputFiles, outputFiles: outputFiles) } } } SwiftProtobuf ᵓ Package.swift ᵓ Plugins │ └ SwiftProtobuf │ └ plugin.swift └ Sources ᵓ SwiftProtobufRuntimeLib │ └ ... └ protoc-gen-swift └ ... -
SE-0303 Extensible Build Tools Future Directions • ΫϥΠΞϯτͱҟͳΔόʔδϣϯͷdependencyΛར༻Ͱ͖Δػೳ • Build
graphͷΞΫηε • Prebuild actions • ଞͷλʔήοτͰఏڙ͞ΕΔϥΠϒϥϦΛར༻ͨ͠ϓϥάΠϯεΫϦϓτ • Ϗϧυதʹ৽͍͠λεΫΛੜ͢Δػೳ • Type-safe option • Ϗϧυͱςετͷྃޙʹ࣮ߦ͢ΔίϚϯυΛఆٛ͢Δػೳ • Swift packageϓϥάΠϯεΫϦϓτͷςεταϙʔτ • Linter / Formatterͷαϙʔτ -
SE-0305 Binary Target Improvements Implemented in 5.6 • ҰൠతʹCLISwiftͰϏϧυ͞Εͳ͍ͷͰɺࣄલʹϏϧυ͓ͯ͘͠ඞཁ͕͋ Δɻ
• Pre buildதʹ࣮ߦ͢ΔπʔϧࣗମΛϏϧυதʹϏϧυ͢Δ͜ͱͰ͖ͳ͍ • binaryTarget͕αϙʔτ͢Δformatʹ `artifact bundle` ΛՃ͢Δ • Remote URLͱͯ͠ɺartifact bundle indexϑΝΠϧࢀরͰ͖ΔΑ͏ʹ͢Δ • એݴ͞ΕͨvariantPackage pluginͰར༻Ͱ͖Δ -
SE-0305 Binary Target Improvements Artifact bundle • ෳͷartifactؚ͕·Εɺ໊લϢχʔΫ • Plugin
APIͰartifactͷݕࡧʹΘΕΔ • artifactʹunique identi fi erΛͭvariant • ݸʑͷartifactͱvariantʹؔ͢ΔManifest json <name>.artifactbundle ᵓ info.json ᵓ <artifact> │ ᵓ <variant> │ │ ᵓ <executable> │ │ └ <other fi les> │ └ <variant> │ ᵓ <executable> │ └ <other fi les> ᵓ <artifact> │ └ <variant> │ ᵓ <executable> │ └ <other fi les> │ <artifact> ᴽ └ᴻ -
SE-0305 Binary Target Improvements Artifact bundle manifest • ArtifactͷmetadataΛཧ͢Δ •
Version • Type: executable (reserved) • Variants: • Path: • Supported triples: <arch><sub>-<vendor>-<sys>-<abi> arm64-apple-ios-simulator { "schemaVersion": " 1.0 ", "artifacts": { "<identifier>": { "version": " <version number> ", "type": " executable ", "variants": [ { "path": " <relative-path-to-executable> ", "supportedTriples": [ " <triple1>", ... ] }, ... ] }ɺ... } } -
SE-0305 Binary Target Improvements Artifact bundleͷॲཧ 1. ZipϑΝΠϧΛμϯϩʔυ͢Δ 2. ղౚ͢Δ
3. .xcframeworkؚ͕·Ε͍ͯͳ͚Εɺ.artifactbundleΛ୳͢ .artifactbundleͱ.xcframework͕྆ํؚ·Ε͍ͯΔͱΤϥʔ 4. info.jsonΛಡΈࠐΉɻschemaVersion͕ͳ͚Εऴྃɻ͋Δ߹artifactΛ ొ͢Δɻ -
SE-0305 Binary Target Improvements Artifact bundle Index • ෆཁͳϑΝΠϧͷμϯϩʔυΛճආ ͢ΔͨΊʹɺartifact
bundleΛvariant͝ ͱʹෳͷόϯυϧʹׂͰ͖Δɻ • *.artifactbundleindex • .artifactbundleindexͱಉ͡σΟϨΫτ ϦʹݸʑͷzipΛஔ͢Δ { "schemaVersion": "1.0", "bundles": [ { "fileName": "<name of .zip file containing bundle>", "checksum": "<checksum of .zip file>", "supportedTriples": [ "<triple1>", ... ] }, ... ] } -
SE-0325 Additional Plugin APIs Accepted: ৄࡉׂѪ • ίʔυੜʹ੍ݶ͍ͯͨ͠Plugin APIʹॊೈੑΛ࣋ͨͤΔมߋ •
PluginContextΛಋೖͯ͠ɺPackage, TargetͷใΛऔΓग़ͯͦ͘͢͠͠͏ • Identi fi ableʹconform͕͍ͨͬͯ͠Δ͕ɺProtocol͕ExistentialʹͳΔ (SE-0309)ͷΛ͍ͬͯΔ • ԿΛಋೖ͕͍ͨͬͯ͠Δͷ͔͍·͍ͪݟ͑ͳ͍ͷͰɺSwiftUIͷResult Builderͱಉ͍͕͢͡Δ -
SE-0332 Command Plugins Accepted with modi fi cations • SE-0303ͰϏϧυதʹࣗಈͰ࣮ߦ͞ΕΔΧελϜπʔϧΛʢίʔυੜʹ
͚ͯʣ࡞ͬͨɻͦͷͨΊɺPackage directoryͷϑΝΠϧΛมߋͰ͖ͳ͍ͳ ͲɺϓϥάΠϯͷ࣮ߦʹ੍ݶ͕͋Δ • ͜͜ͰɺϢʔβʔ͕CLIIDEͰΧελϜΞΫγϣϯΛ࣮ߦͰ͖Δ Command PluginΛఆٛ͢Δɻ • υΩϡϝϯτͷੜɺιʔείʔυͷformatɺUnit test reportɺbuild artifact ͷޙॲཧͳͲɺϏϧυγεςϜ͔Βಠཱͯ͠ϫʔΫϑϩʔΛ֦ுͰ͖Δɻ -
Command • swift package <verb: command> Ͱ࣮ߦͰ͖Δ • intentΛࢦఆ •
Prede fi ned: Generate documantion / source code formatting • Custom: • ࣮ߦʹඞཁͳpermissionఆٛͰ͖Δ -
Command Manifest API extension PluginCapability { public static func command(
intent: PluginCommandIntent, permissions: [PluginPermission] = [] ) -> PluginCapability } public struct PluginCommandIntent { /// Invoked by a `generate-documentation` verb to `swift package`. public static func documentationGeneration() -> PluginCommandIntent /// Invoked by a `format-source-code` verb to `swift package`. public static func sourceCodeFormatting() -> PluginCommandIntent /// An intent that doesn't fit into any of the other categories, with a custom /// verb through which it can be invoked. public static func custom(verb: String, description: String) -> PluginCommandIntent } public struct PluginPermission { /// The command plugin wants permission to modify the files under the package /// directory. The `reason` string is shown to the user at the time of request /// for approval, explaining why the plugin is requesting this access. public static func writeToPackageDirectory(reason: String) -> PluginPermission } -
Command Plugin APIͷ৽͍͠Entry pointͷఆٛ public protocol CommandPlugin: Plugin { func
performCommand( /// Plugin͕࣮ߦ͞ΕΔίϯςΩετɻPackage graphͷΞΫηεɺσΟϨΫτϦͷΩϟογϡͳͲ͕ఏڙ͞ΕΔɻ context: PluginContext, /// ίϚϯυ͕ద༻͞ΕΔλʔήοτɻࢦఆ͍ͯ͠ͳ͍߹ɺͯ͢ͷλʔήοτʹͳΔɻ targets: [Target], /// verbͷޙʹଓ͘ϦςϥϧҾ arguments: [String], ) async throws /// ίϚϯυϓϥάΠϯΛϗετ͍ͯ͠ΔSwift Package ManagerIDEͷϓϩΩγɻ /// ϓϥάΠϯ͕ಛఆͷใΞΫγϣϯΛཁٻͰ͖ΔΑ͏ʹ͢Δɻ var packageManager: PackageManager { get } } -
public struct PackageManager { public func build( _ subset: BuildSubset,
parameters: BuildParameters ) async throws -> BuildResult public enum BuildSubset { /// Represents the subset consisting of all products and of either all /// targets or (if `includingTests` is false) just non-test targets. case all(includingTests: Bool) /// Represents the product with the specified name. case product(String) /// Represents the target with the specified name. case target(String) } public struct BuildParameters { public var configuration: BuildConfiguration = .debug public var logging: BuildLogVerbosity = .concise public var otherCFlags: [String] = [] public var otherCxxFlags: [String] = [] public var otherSwiftcFlags: [String] = [] public var otherLinkerFlags: [String] = [] /// Future proposals should add more controls over the build. } public enum BuildConfiguration { case debug, release } public enum BuildLogVerbosity { case concise, verbose, debug } public struct BuildResult { public var succeeded: Bool public var logText: String public var builtArtifacts: [BuiltArtifact] /// Represents a single artifact produced during a build. public struct BuiltArtifact { public var path: Path public var kind: Kind /// Represents the kind of artifact that was built. The specific file /// formats may vary from platform to platform — for example, on macOS /// a dynamic library may in fact be built as a framework. public enum Kind { case executable, dynamicLibrary, staticLibrary } } } -
Invoke Command Plugins • swift package do-something • swift package
--target Foo --target Bar do-something • swift package --target Foo --target Bar do-something aParam -aFlag • swift package --allow-writing-to-package-directory do-something • কདྷతʹSwiftArgumentParserͷΑ͏ͳελΠϧͰએݴͰ͖ΔΑ͏ʹͳΕɺIDE͕࠷దͳUIΛදࣔ͢ΔՄೳੑ͋Δɻ -
Discovering command plugins • swift package plugin --list --capability=buildTool •
swift package plugin --list --capability=command --json -
Examples DocC // swift-tools-version: 5.6 import PackageDescription let package =
Package( name: "MyDocCPlugin", products: [ // Declaring the plugin product vends the plugin to clients of the package. .plugin( name: "MyDocCPlugin", targets: ["MyDocCPlugin"] ), ], targets: [ // This is the target that implements the command plugin. .plugin( name: "MyDocCPlugin", capability: .command( intent: .documentationGeneration() ) ) ] ) -
import PackagePlugin import Foundation @main struct MyDocCPlugin: CommandPlugin { func
performCommand( context: PluginContext, targets: [Target], arguments: [String] ) async throws { // We'll be creating commands that invoke `docc`, so start by locating it. let doccTool = try context.tool(named: "docc") let outputDir = context.pluginWorkDirectory.appending("Outputs") for target in targets { guard let target = target as? SourceModuleTarget else { continue } let doccCatalog = target.sourceFiles.first { $0.path.extension == "docc" } let symbolGraphInfo = try await packageManager.getSymbolGraph(for: target, options: .init( minimumAccessLevel: .public, includeSynthesized: false, includeSPI: false)) let doccExec = URL(fileURLWithPath: doccTool.path.string) var doccArgs = ["convert"] if let doccCatalog = doccCatalog { doccArgs += ["\(doccCatalog.path)"] } doccArgs += [ "--fallback-display-name", target.name, "--fallback-bundle-identifier", target.name, "--fallback-bundle-version", "0", "--additional-symbol-graph-dir", "\(symbolGraphInfo.directoryPath)", "--output-dir", "\(outputDir)", ] let process = try Process.run(doccExec, arguments: doccArgs) process.waitUntilExit() // Check whether the subprocess invocation was successful. if process.terminationReason == .exit && process.terminationStatus == 0 { print("Generated documentation at \(outputDir).") } else { let problem = "\(process.terminationReason):\(process.terminationStatus)" Diagnostics.error("docc invocation failed: \(problem)") } } } } -
Examples Formatting Source code // swift-tools-version: 5.6 import PackageDescription let
package = Package( name: "MyFormatterPlugin", dependencies: [ .package(url: "https://github.com/apple/swift-format.git", from: "0.50500.0"), ], targets: [ .plugin( "MyFormatterPlugin", capability: .command( intent: .sourceCodeFormatting(), permissions: [ .writeToPackageDirectory(reason: "This command reformats source files") ] ), dependencies: [ .product(name: "swift-format", package: "swift-format"), ] ) ] ) -
import PackagePlugin import Foundation @main struct MyFormatterPlugin: CommandPlugin { func
performCommand( context: PluginContext, targets: [Target], arguments: [String] ) async throws { // We'll be invoking `swift-format`, so start by locating it. let swiftFormatTool = try context.tool(named: "swift-format") // By convention, use a configuration file in the package directory. let configFile = context.package.directory.appending(".swift-format.json") // Iterate over the targets we've been asked to format. for target in targets { // Skip any type of target that doesn't have source files. // Note: We could choose to instead emit a warning or error here. guard let target = target as? SourceModuleTarget else { continue } // Invoke `swift-format` on the target directory, passing a configuration // file from the package directory. let swiftFormatExec = URL(fileURLWithPath: swiftFormatTool.path.string) let swiftFormatArgs = [ "--configuration", "\(configFile)", "--in-place", "--recursive", "\(target.directory)" ] let process = try Process.run(swiftFormatExec, arguments: swiftFormatArgs) process.waitUntilExit() // Check whether the subprocess invocation was successful. if process.terminationReason == .exit && process.terminationStatus == 0 { print("Formatted the source code in \(target.directory).") } else { let problem = "\(process.terminationReason):\(process.terminationStatus)" Diagnostics.error("swift-format invocation failed: \(problem)") } } } } -
Examples Building Deployment Artifacts // swift-tools-version: 5.6 import PackageDescription let
package = Package( name: "MyExecutable", products: [ .executable(name: "MyExec", targets: ["MyExec"]) ], targets: [ // This is the hypothetical executable we want to distribute. .executableTarget( "MyExec" ), // This is the plugin that defines a custom command to distribute the executable. .plugin( "MyDistributionArchiveCreator", capability: .command( intent: .custom( verb: "create-distribution-archive", description: "Creates a .zip containing release builds of products" ) ), ) ] ) -
import PackagePlugin import Foundation @main struct MyDistributionArchiveCreator: CommandPlugin { func
performCommand( context: PluginContext, targets: [Target], arguments: [String] ) async throws { // Check that we were given the name of a product as the first argument // and the name of an archive as the second. guard arguments.count == 2 else { throw Error("Expected two arguments: product name and archive name") } let productName = arguments[0] let archiveName = arguments[1] // Ask the plugin host (SwiftPM or an IDE) to build our product. let result = try await packageManager.build( .product(productName), parameters: .init(configuration: .release, logging: .concise) ) // Check the result. Ideally this would report more details. guard result.succeeded else { throw Error("couldn't build product") } // Get the list of built executables from the build result. let builtExecutables = result.builtArtifacts.filter{ $0.kind == .executable } // Decide on the output path for the archive. let outputPath = context.pluginWorkDirectory.appending("\(archiveName).zip") // Use Foundation to run `zip`. The exact details of using the Foundation // API aren't relevant; the point is that the built artifacts can be used // by the script. let zipTool = try context.tool(named: "zip") let zipArgs = ["-j", outputPath.string] + builtExecutables.map{ $0.path.string } let zipToolURL = URL(fileURLWithPath: zipTool.path.string) let process = try Process.run(zipToolURL, arguments: zipArgs) process.waitUntilExit() // Check whether the subprocess invocation was successful. if process.terminationReason == .exit && process.terminationStatus == 0 { print("Created distribution archive at \(outputPath).") } else { let problem = "\(process.terminationReason):\(process.terminationStatus)" Diagnostics.error("zip invocation failed: \(problem)") } } } -
Package Plugins Summary • Swift PMͰར༻Ͱ͖ΔϓϥάΠϯ • Build Tool Plugin
(Swift 5.6): Ϗϧυ࣌ʹࣗಈͰ࣮ߦ͞ΕΔɻ • SwiftGen / SwiftProtobuf / SwiftSyntaxͷΑ͏ͳίʔυੜ͚ • Command Plugin (Accepted) : CLI / IDEͰར༻Ͱ͖ΔΧελϜΞΫγϣϯ • υΩϡϝϯτͷੜɺformat͕SwiftPM͚ͩͰ࣮ߦͰ͖Δ༧ఆ • xcodebuildxcrunΛcommand pluginʹஔ͖͑Δະདྷ͋Δͷ͔…ʁ • কདྷతʹ͍ΖΜͳϓϥάΠϯ͕Ճ͞ΕΔ༧ఆ • Authorͯ͢AppleͷAnders Bertelurd • Կ͔࣍ͷେ͖ͳൃදͷ෬ઢ͔ɺ୯७ʹͦ͏͍͏ඪઃఆͳͷ͔ -
SE-0301 Editing Commands Accepted • Product / Target / DependencyΛՃͨ͠ΒPackage.swiftʹө͠ɺखಈͰ
ฤू͠ͳͯ͘ࡁΉΑ͏ʹ͢Δ • ϞδϡʔϧΛՃͨ͠ΒɺࣗಈͰtargetʹө͢Δ • importΛॻ͍ͨΒɺෆ͍ͯ͠ΔdependencyΛՃͨ͠ΓɺPackage collection͔ΒՃ͢Δ c.f.) npm / cargo / elm -
SE-0339 Module Aliasing For Disambiguation Active Review … 2022/1/31 •
ݱࡏϞδϡʔϧʹ൚༻తͳ໊લ (e.g. Core, Utils)Λ͚ͭΔͱিಥ͢ΔͷͰ rename͢Δඞཁ͕͋Δ Vapor/Logging: error: multiple products named 'Logging' in: Console, swift-log error: multiple targets named 'Logging' in: Console, swift-log • ̎ͭͷόʔδϣϯͷҟͳΔύοέʔδ͕ଘࡏ͢Δ߹ɺยํΛἧ͑Α͏ͱ͢ ΔͱյΕΔՄೳੑ͕͋ΔͷͰɺڞଘ͍ͤͨ͞ -
Proposed Solution • Module AliasΛಋೖͯ͠ɺϞδϡʔϧʹผ໊Λ͚ͭΒΕΔΑ͏ʹ͢Δ targets: [ .executableTarget( name: "App",
dependencies: [ .product(name: "Game", *moduleAliases*: ["Utils": "GameUtils"], package: "swift-game"), .product(name: "Utils", package: "swift-draw"), ]) ] [App] import GameUtils import Utils SE-0339 Module Aliasing For Disambiguation -
Detailed Design • Frontend: -module-alias [name] = [new_name] • module[new_name].swiftmoduleͱͯ͠ੜ͞ΕΔ
• Symbol manglingserializationʹӨڹ͢Δ SE-0339 Module Aliasing For Disambiguation -
Requirements / Limitations • ObjC / C / C++ /
AsmͰγϯϘϧͷিಥͷՄೳੑ͕͋ΔͷͰར༻Ͱ͖ͳ͍ɻ • @objc(name) ਪ͞Εͳ͍ • ManglingSerializationʹӨڹ͢ΔͨΊɺιʔείʔυͷϏϧυʹݶఆɻDistributed BinaryͰར༻ Ͱ͖ͳ͍ɻ -> Binaryʹ൚༻Ϟδϡʔϧ໊͕ೖ͍ͬͯΔͱ٧Έɻ • NSClassFromString(…) ͰͷStringͷมࣦഊ͢Δ • Resources: Asset CatalogͱLocalized StringsͷΈར༻Մೳ • Retroactive Conformanceආ͚Δɻ • SE-0339 Module Aliasing For Disambiguation -
• Retroactive conformance: extending a class, struct, or enum from
another module to conform to a protocol from another module. The protocol and the conforming type may be from the same module or from di ff erent modules; the important note is that neither is in the same module as the extension. // Framework A extension SomeStruct: CustomStringConvertible { var description: String { return "SomeStruct, via A" } } // Framework B extension SomeStruct: CustomStringConvertible { var description: String { return "SomeStruct, via B" } } // main.swift import A import B print(SomeStruct()) // ??? Appendix: Retroactive Conformance -
SE-0292 Package Registry Service Accepted • DependencyͷdownloadʹιʔεϦϙδτϦͷURLΛࢦఆͯ͠gitΛ͏ͷద͍ͯ͠ͳ͍ • ࠶ݱੑ: όʔδϣϯλά͍ͭͰผίϛοτʹׂΓͯՄೳͳͷͰɺϏϧυ͞ΕͨλΠϛϯάʹΑͬͯҟͳΔ
ϏϧυՌʹͳΔՄೳੑ͕͋Δ • Մ༻ੑ: ϦϙδτϦҠಈͨ͠Γআ͞ΕͨΓ͢Δ • ޮ: ύοέʔδͷͯ͢ͷόʔδϣϯ͕μϯϩʔυ͞Εͯ͠·͏ • : history͕େ͖͍ͱ͘ͳΔɻΫϩʔϯαʔόʔʹΫϥΠΞϯτʹίετɻ • Package registry: RubyGems / PyPl / npm / crates.io • SwiftPMͷpackage registrygitΑΓߴͰ৴པੑͷߴ͍ґଘؔղܾ͕ΊΔɻ • Package search, Security audit, local o ff l ine cache -
Package registry service Method Path Description GET /{scope}/{name} List package
releases GET /{scope}/{name}/{version} Fetch metadata for a package release GET /{scope}/{name}/{version}/Package.swift{?swift-version} Fetch manifest for a package release GET /{scope}/{name}/{version}.zip Download source archive for a package release GET /identi fi ers{?url} Lookup package identi fi ers registered for a URL -
Changes to Swift Package Manager Package Identity • ݱࡏpackage IDURLͷlast
path components • scope.package-name ʹมߋ͢Δ • scopenamespaceͰ࠷େ39จࣈ • package-namescopeͷϢχʔΫͳ໊લͰ࠷େ100จࣈ -
Changes to Swift Package Manager Dependency graph resolution • PackageContainerΛpackage
resolutionͷtop-level unitͱ͢Δ • RegistryPackageContainer͕HTTPϦ ΫΤετͱಉͷૢ࡞Λߦ͏ public protocol PackageContainer { /// The identifier for the package. var package: PackageReference { get } func isToolsVersionCompatible(at version: Version) -> Bool func toolsVersion(for version: Version) throws -> ToolsVersion /// Get the list of versions which are available for the package. /// /// The list will be returned in sorted order, with the latest version *first*. /// All versions will not be requested at once. Resolver will request the next one only /// if the previous one did not satisfy all constraints. func toolsVersionsAppropriateVersionsDescending() throws -> [Version] /// Get the list of versions in the repository sorted in the ascending order, that is the earliest /// version appears first. func versionsAscending() throws -> [Version] func versionsDescending() throws -> [Version] // FIXME: We should perhaps define some particularly useful error codes // here, so the resolver can handle errors more meaningfully. // /// Fetch the declared dependencies for a particular version. /// /// This property is expected to be efficient to access, and cached by the /// client if necessary. /// /// - Precondition: `versions.contains(version)` /// - Throws: If the version could not be resolved; this will abort /// dependency resolution completely. func getDependencies(at version: Version, productFilter: ProductFilter) throws -> [PackageContainerConstraint] /// Fetch the declared dependencies for a particular revision. /// /// This property is expected to be efficient to access, and cached by the /// client if necessary. /// /// - Throws: If the revision could not be resolved; this will abort /// dependency resolution completely. func getDependencies(at revision: String, productFilter: ProductFilter) throws -> [PackageContainerConstraint] /// Fetch the dependencies of an unversioned package container. /// /// NOTE: This method should not be called on a versioned container. func getUnversionedDependencies(productFilter: ProductFilter) throws -> [PackageContainerConstraint] /// Get the updated identifier at a bound version. /// /// This can be used by the containers to fill in the missing information that is obtained /// after the container is available. The updated identifier is returned in result of the /// dependency resolution. func loadPackageReference(at boundVersion: BoundVersion) throws -> PackageReference } -
Changes to Swift Package Manager Tasks performed by Swift Package
Manager during dependency resolution Task Git operation Registry request Fetch the contents of a package git clone && git checkout GET /{scope}/{name}/{version}.zip List the available tags for a package git tag GET /{scope}/{name} Fetch a package manifest git clone GET /{scope}/{name}/{version}/Package.swift -
Appendix: HTTPClient in SwiftPM • Apple/swift-package-managerͷSources/Basics/HTTPClient • ඪ४తͳHTTPClientͷ࣮ͱͯ͠Α͍͔ • ͦͷଞSwiftPMllbuildͷcommon
infrastructure ͱͯ͠ apple/swift-tools-support-core ͱ͍͏ͷ͋Δ • https://github.com/apple/swift-tools-support-core -
Changes to Package.resolved • Swift Package RegistryͷϦϦʔεZipϑΝΠϧͱͯ͠ΞʔΧΠϒ͞ΕΔɻ • ֎෦ύοέʔδͷґଘ͕ؔRegistryΛհͯ͠μϯϩʔυ͞ΕΔͱɺPackage.resolvedͷchecksum ͱɺswift
package compute-checksumίϚϯυͷchecksumͱൺֱ͢ΔɻPackage.resolvedʹͳ͚Ε هɻ $ swift package compute-checksum LinkedList-1.2.0.zip 1feec3d8d144814e99e694cd1d785928878d8d6892c4e59d12569e179252c535 { "object": { "pins": [ { "package": "mona.LinkedList", "state": { "checksum": "ed008d5af44c1d0ea0e3668033cae9b695235f18b1a99240b7cf0f3d9559a30d", "version": "1.2.0" } } ] }, "version": 1 } -
Changes to Package.resolved • Checksum͕ҟͳ͍ͬͯΕɺμϯϩʔυΛڋ൱͢Δɻ $ swift build error: checksum
of downloaded source archive of dependency 'mona.LinkedList' (c2b934fe66e55747d912f1cfd03150883c4f037370c40ca2ad4203805db79457) does not match checksum speci fi ed by the manifest (ed008d5af44c1d0ea0e3668033cae9b695235f18b1a99240b7cf0f3d9559a30d) -
Archive-source subcommand • ଞͷPackage ManagerͷࣄྫΛݟΔͱɺChecksumͷෆҰகɺِഁଛΑΓɺArchiveͷ࡞ɺ ChecksumͷෆҰக͕ݪҼͷ͜ͱͷํ͕ଟ͍ • swift package archive-source
ίϚϯυඪ४ͷsource archiveΛ࡞͢Δํ๏Λఏڙ͢Δ SYNOPSIS swift package archive-source [--output=< fi le>] OPTIONS -o < fi le>, --output=< fi le> Write the archive to < fi le>. If unspeci fi ed, the package is written to `\(PackageName).zip`. -
Archive-source subcommand • ίϚϯυΛrootͰ࣮ߦ͢ΔͱɺݱࡏͷWorking treeͷsource archiveΛ࡞͢Δ $ tree -a -L
1 LinkedList ᵓ── .git ᵓ── Package.swift ᵓ── README.md ᵓ── Sources └── Tests $ head -n 5 Package.swift // swift-tools-version:5.3 import PackageDescription let package = Package( name: "LinkedList", $ swift package archive-source Created LinkedList.zip -
Archive-source subcommand • ίϚϯυΛrootͰ࣮ߦ͢ΔͱɺݱࡏͷWorking treeͷsource archiveΛ࡞͢ Δ $ tree -a
-L 1 LinkedList ᵓ── .git ᵓ── Package.swift ᵓ── README.md ᵓ── Sources └── Tests $ head -n 5 Package.swift // swift-tools-version:5.3 import PackageDescription let package = Package( name: "LinkedList", $ swift package archive-source Created LinkedList.zip -
Archive-source subcommand • σϑΥϧτͰɺ<Package name>.zip ʹͳΔɻ- - outputͰมߋՄೳ • Archive-source
git-archive ʹzip formatΛࢦఆ͠ɺdefault compression levelΛࢦఆͨ͠ͷͱಉɻ Note: export-ignoreଐੑͷϑΝΠϧແࢹ͞ΕΔɻ.git.buildͷΑ͏ͳӅ͠ϑΝΠϧ / σΟϨΫτϦؚ Ήɻ $ git checkout 1.2.0 $ swift package archive-source --output="LinkedList-1.2.0.zip" # Created LinkedList-1.2.0.zip $ git archive --format zip \ --pre fi x LinkedList-1.2.0 --output LinkedList-1.2.0.zip \ 1.2.0 -
Registry configuration subcommands swift package-registry • registryͷཧΛߦ͏αϒίϚϯυ • Private dependencies:
Public Registry͔Βfetch͞Εͨpackageʹprivate packageΛΈࠐΉ͜ͱ͕Ͱ͖Δ • Geographic colocation: ωοτϫʔΫ͕ශऑͳ߹ʹɺϛϥʔΛϗετ͢Δ • Policy enforcement: ΧελϜϨδετϦΛհͯ͠ঝೝ͞ΕͨύοέʔδͷΈΛར༻ͤ͞Δ • Auditing: ΧελϜϨδετϦɺϥϯΩϯάϥΠηϯεྉͷٻΛతͱͯ͠ɺύοέʔδͷΞΫηεͷޮՌଌఆΛ͢ Δ߹͕͋Δ SYNOPSIS swift package-registry set <url> [options] OPTIONS: --global Apply settings to all projects for this user --scope Associate the registry with a given scope --login Specify a user name for the remote machine --password Supply a password for the remote machine -
Package Registry Summary • GitΛΘͣʹɺՌϕʔεͷμϯϩʔυʹͳΔͷͰɺґଘղܾ͕ૣ͘ͳ ΔՄೳੑ͕ߴ͍ • AuthorMattt͞Μ͡ΊશһGitHubͳͷͰɺ͜ͷProposalͷϞνϕʔγϣ ϯߴͦ͏ •
Package PluginͷͱͷॏෳͰɺଟগมߋ͕͋Δ͔͠Εͳ͍