Skip to content

Commit

Permalink
seems to build
Browse files Browse the repository at this point in the history
  • Loading branch information
Full Queue Developer committed Oct 8, 2023
1 parent eb320c3 commit 6a6cba5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
20 changes: 17 additions & 3 deletions Plugins/GenerateTemplatesPlugin/GenerateTemplatesPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ struct GenerateTemplatesPlugin: BuildToolPlugin {

let contents = try FileManager.default.contentsOfDirectory(atPath: templatesDir.string)

print(contents)

let enumerator = FileManager.default.enumerator(atPath: templatesDir.string)


let inputFiles = paths(fromEnumerator: enumerator!).map { path in
templatesDir.appending(path)
}

return [
.buildCommand(
Expand All @@ -19,13 +25,21 @@ struct GenerateTemplatesPlugin: BuildToolPlugin {
templatesDir.string,
context.pluginWorkDirectory.string
],
inputFiles: contents.map { Path($0) },
inputFiles: inputFiles,
outputFiles: [
context.pluginWorkDirectory.appending("Templates.swift"),
] + contents.map({ templateName in
Path("\(templateName)Template.swift")
context.pluginWorkDirectory.appending("\(templateName)Template.swift")
})
),
]
}

func paths(fromEnumerator enumerator: FileManager.DirectoryEnumerator) -> [String] {
var result: [String] = []
while let file = enumerator.nextObject() as? String {
result += [file]
}
return result
}
}
19 changes: 16 additions & 3 deletions Sources/GenerateTemplatesTool/GenerateTemplatesTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import ArgumentParser
@main
struct GenerateTemplatesTool: ParsableCommand {


@Argument(help: "input dir to read templates from")
var templatesDir: String

Expand Down Expand Up @@ -68,19 +67,23 @@ struct GenerateTemplatesTool: ParsableCommand {

return try templateNames.reduce([:]) { result, next in

let enumerator = FileManager.default.enumerator(atPath: "templates/\(next)")
let enumerator = FileManager.default.enumerator(atPath: "\(templatesDir)/\(next)")
guard let enumerator = enumerator else {
struct CouldNotCreateEnumerator: Error {
let path: String
}
throw CouldNotCreateEnumerator(path: "templates/\(next)")
throw CouldNotCreateEnumerator(path: "\(templatesDir)/\(next)")
}

var files: [ScaffoldFile] = []
let rootPath = FilePath("\(templatesDir)/\(next)/")
while let file = enumerator.nextObject() as? String {

let filePath = rootPath.appending(file)//"templates/\(next)/\(file)"

guard !filePath.isDirectory else {
continue
}
do {
let contents = try String(contentsOfFile: filePath.string)
let name = filePath.lastComponent!
Expand All @@ -105,6 +108,16 @@ struct GenerateTemplatesTool: ParsableCommand {
}
}

extension FilePath {
var isDirectory: Bool {
var directory = ObjCBool(false)
guard FileManager.default.fileExists(atPath: self.string, isDirectory: &directory) else {
return false
}
return directory.boolValue
}
}

extension String {
var isDirectory: Bool {
var directory = ObjCBool(false)
Expand Down

0 comments on commit 6a6cba5

Please sign in to comment.