Skip to content

Commit

Permalink
refine control
Browse files Browse the repository at this point in the history
  • Loading branch information
omochi committed Dec 20, 2022
1 parent d758739 commit 73ad8c0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 22 deletions.
24 changes: 10 additions & 14 deletions Sources/CodegenKitCLI/ManifestoCode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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)

Expand All @@ -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
Expand Down
32 changes: 24 additions & 8 deletions Sources/CodegenKitCLI/RepositoryInitializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,32 @@ 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()
try manifesto.write()
}
}

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
Expand All @@ -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,
Expand Down

0 comments on commit 73ad8c0

Please sign in to comment.