diff --git a/Sources/CodegenKitCLI/ManifestoCode.swift b/Sources/CodegenKitCLI/ManifestoCode.swift index 30d1b44..49ff090 100644 --- a/Sources/CodegenKitCLI/ManifestoCode.swift +++ b/Sources/CodegenKitCLI/ManifestoCode.swift @@ -82,13 +82,19 @@ struct ManifestoCode { } - private func targetsArray() throws -> ArrayExprSyntax { + func targetsArray() throws -> ArrayExprSyntax { let syntax = try self.parse() let packageCall = try self.packageCall(source: syntax) return try self.targetsArray(packageCall: packageCall) } - private func targetName(_ target: ArrayElementSyntax) -> String? { + func target(name: String) throws -> ArrayElementSyntax? { + return try targetsArray().elements.first { + targetName($0) == name + } + } + + func targetName(_ target: ArrayElementSyntax) -> String? { guard let call = target.expression.as(FunctionCallExprSyntax.self), let member = call.calledExpression.as(MemberAccessExprSyntax.self), member.base == nil else { return nil } @@ -106,15 +112,10 @@ struct ManifestoCode { return nil } - mutating func addExecutableIfNone( + mutating func addExecutable( executableName: String ) throws { let targets = try self.targetsArray() - if targets.elements.contains(where: { targetName($0) == executableName }) { - return - } - - print("add \(executableName) executable") let position = try targets.leftSquare.endPosition.samePosition(in: source) @@ -129,16 +130,11 @@ struct ManifestoCode { self.source.insert(contentsOf: "\n" + patch, at: position) } - mutating func addPluginIfNone( + mutating func addPlugin( executableName: String, pluginName: String ) throws { let targets = try self.targetsArray() - if targets.elements.contains(where: { targetName($0) == pluginName }) { - return - } - - print("add \(pluginName) plugin") guard let executableTarget = targets.elements.first(where: { targetName($0) == executableName diff --git a/Sources/CodegenKitCLI/RepositoryInitializer.swift b/Sources/CodegenKitCLI/RepositoryInitializer.swift index 7a55e4e..e234dae 100644 --- a/Sources/CodegenKitCLI/RepositoryInitializer.swift +++ b/Sources/CodegenKitCLI/RepositoryInitializer.swift @@ -30,10 +30,8 @@ public struct RepositoryInitializer { ) let originalManifesto = manifesto - try manifesto.addExecutableIfNone(executableName: executableName) - try createExecutableDirectory() - try manifesto.addPluginIfNone(executableName: executableName, pluginName: pluginName) - try createPluginDirectory() + try addExecutable(manifesto: &manifesto) + try addPlugin(manifesto: &manifesto) if manifesto.source != originalManifesto.source { try manifesto.format() @@ -41,14 +39,23 @@ public struct RepositoryInitializer { } } + private func addExecutable(manifesto: inout ManifestoCode) throws { + if let _ = try manifesto.target(name: executableName) { + return + } + + print(#"add "\#(executableName)" executable"#) + + try manifesto.addExecutable(executableName: executableName) + try createExecutableDirectory() + } + private func createExecutableDirectory() throws { let dir = self.directory.appendingPathComponent("Sources/\(executableName)") if fileManager.directoryExists(atPath: dir.path) { return } - print("create \(executableName) directory") - try fileManager.createDirectory(at: dir, withIntermediateDirectories: true) let source = """ import Foundation @@ -64,14 +71,23 @@ try runner.run(directories: [dir]) try source.write(to: file, atomically: true, encoding: .utf8) } + private func addPlugin(manifesto: inout ManifestoCode) throws { + if let _ = try manifesto.target(name: pluginName) { + return + } + + print(#"add "\#(pluginName)" plugin"#) + + try manifesto.addPlugin(executableName: executableName, pluginName: pluginName) + try createPluginDirectory() + } + private func createPluginDirectory() throws { let dir = self.directory.appendingPathComponent("Plugins/\(pluginName)") if fileManager.directoryExists(atPath: dir.path) { return } - print("create \(pluginName) directory") - let builder = PluginDirectoryBuilder( fileManager: fileManager, dir: dir,