Share
Embed
Transcript
36 slides
-
Pol Piella Abadia - 04/07/2022 Code generation using Swift Package
Plugins ๐ NSManchester ๐ -
Letโs get started! ๐ - Context โข A new package
exposes a protocol - very similar to decodable. โข Many features implement types which conform to it. New package decodes these types from data. โข A fallback json fi le is shipped with the app. It must always work with all models. โข Decoding errors are all runtime and would cause issues when certain screens are accessed. โข We gained con fi dence by testing, but doing this manually is not very scalableโฆ โข Very di ff i cult to automate at the time as we have a mix of Swift Packages and Xcode projects. โข Xcode 14 will allow us to use plugins for Xcode projects too! ๐ -
Letโs get started! ๐ - Specifics โข A build tool
Swift Package Plugin. โข Uses SourceKitten (SourceKit under the hood) to fi nd all types conforming to a speci fi c protocol. โข Automatically generates tests for each type which conforms to such protocol. -
๐ Package.swift // swift-tools-version: 5.6 // The swift-tools-version declares the
minimum version of Swift required to build this package. import PackageDescription let package = Package( name: "CodeGenSample", platforms: [.macOS(.v10_11)], products: [ .library( name: "CodeGenSample", targets: ["CodeGenSample"]), ], dependencies: [ ], targets: [ .target( name: "CodeGenSample", dependencies: [] ), .testTarget( name: "CodeGenSampleTests", dependencies: ["CodeGenSample"] ) ] ) Package.swift ๐ Sources ๐ CodeGenSample CodeGenSample.swift ๐ Tests ๐ CodeGenSampleTests Empty.swift -
The executable ๐ Extract types conforming to the protocol ๐
Scan target for Swift Files ๐ -
The executable ๐ Extract types conforming to the protocol ๐
Generate a XCTestCase ๐จ Scan target for Swift Files ๐ -
๐ Package.swift // swift-tools-version: 5.6 // The swift-tools-version declares the
minimum version of Swift required to build this package. import PackageDescription let package = Package( name: "CodeGenSample", platforms: [.macOS(.v10_11)], products: [ .library( name: "CodeGenSample", targets: ["CodeGenSample"]), ], dependencies: [ ], targets: [ .target( name: "CodeGenSample", dependencies: [] ), .testTarget( name: "CodeGenSampleTests", dependencies: ["CodeGenSample"] ) ] ) Package.swift ๐ Sources ๐ CodeGenSample CodeGenSample.swift ๐ Tests ๐ CodeGenSampleTests Empty.swift -
๐ Package.swift // swift-tools-version: 5.6 // The swift-tools-version declares the
minimum version of Swift required to build this package. import PackageDescription let package = Package( name: "CodeGenSample", platforms: [.macOS(.v10_11)], products: [ .library( name: "CodeGenSample", targets: ["CodeGenSample"]), ], dependencies: [ .package(url: "https://github.com/jpsim/SourceKitten.git", exact: "0.32.0"), .package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.0") ], targets: [ .target( name: "CodeGenSample", dependencies: [] ), .testTarget( name: "CodeGenSampleTests", dependencies: ["CodeGenSample"] ) ] ) Package.swift ๐ Sources ๐ CodeGenSample CodeGenSample.swift ๐ Tests ๐ CodeGenSampleTests Empty.swift -
๐ Package.swift // swift-tools-version: 5.6 // The swift-tools-version declares the
minimum version of Swift required to build this package. import PackageDescription let package = Package( name: "CodeGenSample", platforms: [.macOS(.v10_11)], products: [ .library( name: "CodeGenSample", targets: ["CodeGenSample"]), ], dependencies: [ .package(url: "https://github.com/jpsim/SourceKitten.git", exact: "0.32.0"), .package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.0") ], targets: [ .target( name: "CodeGenSample", dependencies: [] ), .testTarget( name: "CodeGenSampleTests", dependencies: ["CodeGenSample"] ), .executableTarget( name: "PluginExecutable", dependencies: [ .product(name: "SourceKittenFramework", package: "SourceKitten"), .product(name: "ArgumentParser", package: "swift-argument-parser") ] ) ] ) Package.swift ๐ Sources ๐ CodeGenSample CodeGenSample.swift ๐ PluginExecutable PluginExecutable.swift ๐ Tests ๐ CodeGenSampleTests Empty.swift -
๐ PluginExecutable.swift import SourceKittenFramework import ArgumentParser import Foundation @main struct
PluginExecutable: ParsableCommand { } Package.swift ๐ Sources ๐ CodeGenSample CodeGenSample.swift ๐ PluginExecutable PluginExecutable.swift ๐ Tests ๐ CodeGenSampleTests Empty.swift -
๐ PluginExecutable.swift import SourceKittenFramework import ArgumentParser import Foundation @main struct
PluginExecutable: ParsableCommand { @Argument(help: "The protocol name to match") var protocolName: String @Argument(help: "The module's name") var moduleName: String @Option(help: "Directory containing the swift files") var input: String @Option(help: "The path where the generated files will be created") var output: String } Package.swift ๐ Sources ๐ CodeGenSample CodeGenSample.swift ๐ PluginExecutable PluginExecutable.swift ๐ Tests ๐ CodeGenSampleTests Empty.swift -
๐ PluginExecutable.swift import SourceKittenFramework import ArgumentParser import Foundation @main struct
PluginExecutable: ParsableCommand { @Argument(help: "The protocol name to match") var protocolName: String @Argument(help: "The module's name") var moduleName: String @Option(help: "Directory containing the swift files") var input: String @Option(help: "The path where the generated files will be created") var output: String func run() throws { } } Package.swift ๐ Sources ๐ CodeGenSample CodeGenSample.swift ๐ PluginExecutable PluginExecutable.swift ๐ Tests ๐ CodeGenSampleTests Empty.swift -
๐ PluginExecutable.swift import SourceKittenFramework import ArgumentParser import Foundation @main struct
PluginExecutable: ParsableCommand { @Argument(help: "The protocol name to match") var protocolName: String @Argument(help: "The module's name") var moduleName: String @Option(help: "Directory containing the swift files") var input: String @Option(help: "The path where the generated files will be created") var output: String func run() throws { let files = try deepSearch(URL(fileURLWithPath: input, isDirectory: true)) } } Package.swift ๐ Sources ๐ CodeGenSample CodeGenSample.swift ๐ PluginExecutable PluginExecutable.swift ๐ Tests ๐ CodeGenSampleTests Empty.swift -
๐ PluginExecutable.swift import SourceKittenFramework import ArgumentParser import Foundation @main struct
PluginExecutable: ParsableCommand { @Argument(help: "The protocol name to match") var protocolName: String @Argument(help: "The module's name") var moduleName: String @Option(help: "Directory containing the swift files") var input: String @Option(help: "The path where the generated files will be created") var output: String func run() throws { let files = try deepSearch(URL(fileURLWithPath: input, isDirectory: true)) let structures = try files.map { try Structure(file: File(path: $0.path)!) } } } Package.swift ๐ Sources ๐ CodeGenSample CodeGenSample.swift ๐ PluginExecutable PluginExecutable.swift ๐ Tests ๐ CodeGenSampleTests Empty.swift -
๐ PluginExecutable.swift import SourceKittenFramework import ArgumentParser import Foundation @main struct
PluginExecutable: ParsableCommand { @Argument(help: "The protocol name to match") var protocolName: String @Argument(help: "The module's name") var moduleName: String @Option(help: "Directory containing the swift files") var input: String @Option(help: "The path where the generated files will be created") var output: String func run() throws { let files = try deepSearch(URL(fileURLWithPath: input, isDirectory: true)) let structures = try files.map { try Structure(file: File(path: $0.path)!) } var matchedTypes = [String]() structures.forEach { walkTree(dictionary: $0.dictionary, acc: &matchedTypes) } } } Package.swift ๐ Sources ๐ CodeGenSample CodeGenSample.swift ๐ PluginExecutable PluginExecutable.swift ๐ Tests ๐ CodeGenSampleTests Empty.swift -
๐ PluginExecutable.swift import SourceKittenFramework import ArgumentParser import Foundation @main struct
PluginExecutable: ParsableCommand { @Argument(help: "The protocol name to match") var protocolName: String @Argument(help: "The module's name") var moduleName: String @Option(help: "Directory containing the swift files") var input: String @Option(help: "The path where the generated files will be created") var output: String func run() throws { let files = try deepSearch(URL(fileURLWithPath: input, isDirectory: true)) let structures = try files.map { try Structure(file: File(path: $0.path)!) } var matchedTypes = [String]() structures.forEach { walkTree(dictionary: $0.dictionary, acc: &matchedTypes) } try createOutputFile(withContent: matchedTypes) } } Package.swift ๐ Sources ๐ CodeGenSample CodeGenSample.swift ๐ PluginExecutable PluginExecutable.swift ๐ Tests ๐ CodeGenSampleTests Empty.swift -
๐ PluginExecutable.swift import SourceKittenFramework import ArgumentParser import Foundation @main struct
PluginExecutable: ParsableCommand { @Argument(help: "The protocol name to match") var protocolName: String @Argument(help: "The module's name") var moduleName: String @Option(help: "Directory containing the swift files") var input: String @Option(help: "The path where the generated files will be created") var output: String func run() throws { // Needed to ensure that sourcekit runs in a single process setenv("IN_PROCESS_SOURCEKIT", "YES", 1) let files = try deepSearch(URL(fileURLWithPath: input, isDirectory: true)) let structures = try files.map { try Structure(file: File(path: $0.path)!) } var matchedTypes = [String]() structures.forEach { walkTree(dictionary: $0.dictionary, acc: &matchedTypes) } try createOutputFile(withContent: matchedTypes) } } Package.swift ๐ Sources ๐ CodeGenSample CodeGenSample.swift ๐ PluginExecutable PluginExecutable.swift ๐ Tests ๐ CodeGenSampleTests Empty.swift -
More contextโฆ โญ Thanks to @jozasvalancius, which did all the
work for this. ๐ This is also a fi x for getting SwiftLint to work with SPM plugins ๐ Link to the PR: https://github.com/jpsim/SourceKitten/pull/728 -
๐ Package.swift // swift-tools-version: 5.6 // The swift-tools-version declares the
minimum version of Swift required to build this package. import PackageDescription let package = Package( name: "CodeGenSample", platforms: [.macOS(.v10_11)], products: [ .library( name: "CodeGenSample", targets: ["CodeGenSample"]), ], dependencies: [ .package(url: "https://github.com/jpsim/SourceKitten.git", exact: "0.32.0"), .package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.0") ], targets: [ .target( name: "CodeGenSample", dependencies: [] ), .testTarget( name: "CodeGenSampleTests", dependencies: ["CodeGenSample"] ), .executableTarget( name: "PluginExecutable", dependencies: [ .product(name: "SourceKittenFramework", package: "SourceKitten"), .product(name: "ArgumentParser", package: "swift-argument-parser") ] ) ] ) Package.swift ๐ Sources ๐ CodeGenSample CodeGenSample.swift ๐ PluginExecutable PluginExecutable.swift ๐ Tests ๐ CodeGenSampleTests Empty.swift -
๐ Package.swift // swift-tools-version: 5.6 // The swift-tools-version declares the
minimum version of Swift required to build this package. import PackageDescription let package = Package( name: "CodeGenSample", platforms: [.macOS(.v10_11)], products: [ .library( name: "CodeGenSample", targets: ["CodeGenSample"]), ], dependencies: [ .package(url: "https://github.com/jpsim/SourceKitten.git", exact: "0.32.0"), .package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.0") ], targets: [ .target( name: "CodeGenSample", dependencies: [] ), .testTarget( name: "CodeGenSampleTests", dependencies: ["CodeGenSample"] ), .executableTarget( name: "PluginExecutable", dependencies: [ .product(name: "SourceKittenFramework", package: "SourceKitten"), .product(name: "ArgumentParser", package: "swift-argument-parser") ] ), .plugin( name: "SourceKitPlugin", capability: .buildTool(), dependencies: [.target(name: "PluginExecutable")] ) ] ) Package.swift ๐ Sources ๐ CodeGenSample CodeGenSample.swift ๐ PluginExecutable PluginExecutable.swift ๐ Tests ๐ CodeGenSampleTests Empty.swift ๐ Plugins ๐ SourceKitPlugin SourceKitPlugin.swift -
๐ Package.swift // swift-tools-version: 5.6 // The swift-tools-version declares the
minimum version of Swift required to build this package. import PackageDescription let package = Package( name: "CodeGenSample", platforms: [.macOS(.v10_11)], products: [ .library( name: "CodeGenSample", targets: ["CodeGenSample"]), ], dependencies: [ .package(url: "https://github.com/jpsim/SourceKitten.git", exact: "0.32.0"), .package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.0") ], targets: [ .target( name: "CodeGenSample", dependencies: [] ), .testTarget( name: "CodeGenSampleTests", dependencies: [โCodeGenSample"], plugins: [โSourceKitPluginโ], ), .executableTarget( name: "PluginExecutable", dependencies: [ .product(name: "SourceKittenFramework", package: "SourceKitten"), .product(name: "ArgumentParser", package: "swift-argument-parser") ] ), .plugin( name: "SourceKitPlugin", capability: .buildTool(), dependencies: [.target(name: "PluginExecutable")] ) ] ) Package.swift ๐ Sources ๐ CodeGenSample CodeGenSample.swift ๐ PluginExecutable PluginExecutable.swift ๐ Tests ๐ CodeGenSampleTests Empty.swift ๐ Plugins ๐ SourceKitPlugin SourceKitPlugin.swift -
๐ SourceKitPlugin.swift import PackagePlugin Package.swift ๐ Sources ๐ CodeGenSample CodeGenSample.swift
๐ PluginExecutable PluginExecutable.swift ๐ Tests ๐ CodeGenSampleTests Empty.swift ๐ Plugins ๐ SourceKitPlugin SourceKitPlugin.swift -
๐ SourceKitPlugin.swift import PackagePlugin @main struct SourceKitPlugin: BuildToolPlugin { func
createBuildCommands(context: PluginContext, target: Target) async throws -> [Command] { } } Package.swift ๐ Sources ๐ CodeGenSample CodeGenSample.swift ๐ PluginExecutable PluginExecutable.swift ๐ Tests ๐ CodeGenSampleTests Empty.swift ๐ Plugins ๐ SourceKitPlugin SourceKitPlugin.swift -
๐ SourceKitPlugin.swift import PackagePlugin @main struct SourceKitPlugin: BuildToolPlugin { func
createBuildCommands(context: PluginContext, target: Target) async throws -> [Command] { return [ .buildCommand( displayName: "Protocol Extraction!", executable: try context.tool(named: "PluginExecutable").path, arguments: [ "FindThis", ๐คท, "--input", ๐คท, "--output", ๐คท ], environment: ["IN_PROCESS_SOURCEKIT": "YES"], outputFiles: [๐คท] ) ] } } Package.swift ๐ Sources ๐ CodeGenSample CodeGenSample.swift ๐ PluginExecutable PluginExecutable.swift ๐ Tests ๐ CodeGenSampleTests Empty.swift ๐ Plugins ๐ SourceKitPlugin SourceKitPlugin.swift -
๐ SourceKitPlugin.swift import PackagePlugin @main struct SourceKitPlugin: BuildToolPlugin { func
createBuildCommands(context: PluginContext, target: Target) async throws -> [Command] { let outputPath = context.pluginWorkDirectory.appending(โGeneratedTests.swiftโ) return [ .buildCommand( displayName: "Protocol Extraction!", executable: try context.tool(named: "PluginExecutable").path, arguments: [ "FindThis", ๐คท, "--input", ๐คท, "--output", outputPath ], environment: ["IN_PROCESS_SOURCEKIT": "YES"], outputFiles: [outputPath] ) ] } } Package.swift ๐ Sources ๐ CodeGenSample CodeGenSample.swift ๐ PluginExecutable PluginExecutable.swift ๐ Tests ๐ CodeGenSampleTests Empty.swift ๐ Plugins ๐ SourceKitPlugin SourceKitPlugin.swift -
๐ SourceKitPlugin.swift import PackagePlugin @main struct SourceKitPlugin: BuildToolPlugin { func
createBuildCommands(context: PluginContext, target: Target) async throws -> [Command] { let outputPath = context.pluginWorkDirectory.appending(โGeneratedTests.swiftโ) let dependencyTarget = target .dependencies return [ .buildCommand( displayName: "Protocol Extraction!", executable: try context.tool(named: "PluginExecutable").path, arguments: [ "FindThis", ๐คท, "--input", ๐คท, "--output", outputPath ], environment: ["IN_PROCESS_SOURCEKIT": "YES"], outputFiles: [outputPath] ) ] } } Package.swift ๐ Sources ๐ CodeGenSample CodeGenSample.swift ๐ PluginExecutable PluginExecutable.swift ๐ Tests ๐ CodeGenSampleTests Empty.swift ๐ Plugins ๐ SourceKitPlugin SourceKitPlugin.swift -
๐ SourceKitPlugin.swift import PackagePlugin @main struct SourceKitPlugin: BuildToolPlugin { func
createBuildCommands(context: PluginContext, target: Target) async throws -> [Command] { let outputPath = context.pluginWorkDirectory.appending(โGeneratedTests.swiftโ) let dependencyTarget = target .dependencies .compactMap { dependency -> Target? in switch dependency { case .target(let target): return target default: return nil } } return [ .buildCommand( displayName: "Protocol Extraction!", executable: try context.tool(named: "PluginExecutable").path, arguments: [ "FindThis", ๐คท, "--input", ๐คท, "--output", outputPath ], environment: ["IN_PROCESS_SOURCEKIT": "YES"], outputFiles: [outputPath] ) ] } } Package.swift ๐ Sources ๐ CodeGenSample CodeGenSample.swift ๐ PluginExecutable PluginExecutable.swift ๐ Tests ๐ CodeGenSampleTests Empty.swift ๐ Plugins ๐ SourceKitPlugin SourceKitPlugin.swift -
๐ SourceKitPlugin.swift import PackagePlugin @main struct SourceKitPlugin: BuildToolPlugin { func
createBuildCommands(context: PluginContext, target: Target) async throws -> [Command] { let outputPath = context.pluginWorkDirectory.appending(โGeneratedTests.swiftโ) let dependencyTarget = target .dependencies .compactMap { dependency -> Target? in switch dependency { case .target(let target): return target default: return nil } } .filter { "\($0.name)Tests" == target.name } .first return [ .buildCommand( displayName: "Protocol Extraction!", executable: try context.tool(named: "PluginExecutable").path, arguments: [ "FindThis", ๐คท, "--input", ๐คท, "--output", outputPath ], environment: ["IN_PROCESS_SOURCEKIT": "YES"], outputFiles: [outputPath] ) ] } } Package.swift ๐ Sources ๐ CodeGenSample CodeGenSample.swift ๐ PluginExecutable PluginExecutable.swift ๐ Tests ๐ CodeGenSampleTests Empty.swift ๐ Plugins ๐ SourceKitPlugin SourceKitPlugin.swift -
๐ SourceKitPlugin.swift import PackagePlugin @main struct SourceKitPlugin: BuildToolPlugin { func
createBuildCommands(context: PluginContext, target: Target) async throws -> [Command] { let outputPath = context.pluginWorkDirectory.appending(โGeneratedTests.swiftโ) guard let dependencyTarget = target .dependencies .compactMap { dependency -> Target? in switch dependency { case .target(let target): return target default: return nil } } .filter { "\($0.name)Tests" == target.name } .first else { Diagnostics.error("Could not get a dependency to scan!โ) return [] } return [ .buildCommand( displayName: "Protocol Extraction!", executable: try context.tool(named: "PluginExecutable").path, arguments: [ "FindThis", ๐คท, "--input", ๐คท, "--output", outputPath ], environment: ["IN_PROCESS_SOURCEKIT": "YES"], outputFiles: [outputPath] ) ] } } Package.swift ๐ Sources ๐ CodeGenSample CodeGenSample.swift ๐ PluginExecutable PluginExecutable.swift ๐ Tests ๐ CodeGenSampleTests Empty.swift ๐ Plugins ๐ SourceKitPlugin SourceKitPlugin.swift -
๐ SourceKitPlugin.swift import PackagePlugin @main struct SourceKitPlugin: BuildToolPlugin { func
createBuildCommands(context: PluginContext, target: Target) async throws -> [Command] { let outputPath = context.pluginWorkDirectory.appending(โGeneratedTests.swiftโ) guard let dependencyTarget = target .dependencies .compactMap { dependency -> Target? in switch dependency { case .target(let target): return target default: return nil } } .filter { "\($0.name)Tests" == target.name } .first else { Diagnostics.error("Could not get a dependency to scan!โ) return [] } return [ .buildCommand( displayName: "Protocol Extraction!", executable: try context.tool(named: "PluginExecutable").path, arguments: [ "FindThis", dependencyTarget.name, "--input", dependencyTarget.directory, "--output", outputPath ], environment: ["IN_PROCESS_SOURCEKIT": "YES"], outputFiles: [outputPath] ) ] } } Package.swift ๐ Sources ๐ CodeGenSample CodeGenSample.swift ๐ PluginExecutable PluginExecutable.swift ๐ Tests ๐ CodeGenSampleTests Empty.swift ๐ Plugins ๐ SourceKitPlugin SourceKitPlugin.swift