Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 19 additions & 1 deletion Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ let package = Package(
url: "https://github.com/davecom/SwiftGraph",
from: "3.1.0"
),
.package(
url: "https://github.com/kylef/PathKit.git",
exact: "1.0.1"
),
],
targets: [
.target(
Expand Down Expand Up @@ -60,6 +64,7 @@ let package = Package(
name: "SwordCommand",
dependencies: [
"SwordGenerator",
"PathKit",
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftParser", package: "swift-syntax"),
.product(name: "ArgumentParser", package: "swift-argument-parser"),
Expand Down
58 changes: 22 additions & 36 deletions Sources/SwordCommand/SwordCommand.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ArgumentParser
import Foundation
import PathKit
import SwiftParser
import SwiftSyntax
import SwordGenerator
Expand All @@ -18,14 +19,23 @@ struct SwordCommand: AsyncParsableCommand {
try loadLocalPackagesIfNeeded()

// Parse files in current working directory if no inputs were specified.
let allInputs = inputs.isEmpty ? [""] : inputs
let sourceFiles = try allInputs.flatMap { input in
paths(in: input)
let allInputs = inputs.isEmpty ? ["."] : inputs
let sourceFilePaths = try allInputs.flatMap { input -> [URL] in
let path = Path(input)
return if path.isFile {
[path.absolute().url]
} else {
try path.recursiveChildren().compactMap { child in
guard child.extension == "swift" else { return nil }

return child.absolute().url
}
}
}
.map { url in
let source = try String(contentsOf: url)
let sourceFiles = try sourceFilePaths.map { sourceFilePath in
let source = try String(contentsOf: sourceFilePath, encoding: .utf8)
return SourceFile(
path: url.path(),
path: sourceFilePath.path(),
tree: Parser.parse(source: source)
)
}
Expand All @@ -47,43 +57,19 @@ struct SwordCommand: AsyncParsableCommand {
}

mutating private func loadLocalPackagesIfNeeded() throws {
let fileManager = FileManager.default
let configurationFilePath = URL(filePath: fileManager.currentDirectoryPath).appending(path: ".sword.yml")
let configurationPath = Path.current + ".sword.yml"
guard configurationPath.exists else { return }

guard fileManager.fileExists(atPath: configurationFilePath.path()) else { return }

let data = try Data(contentsOf: configurationFilePath)
let data = try configurationPath.read()
let configuration = try YAMLDecoder().decode(Configuration.self, from: data)

for localPackage in configuration.localPackages {
targets.append(contentsOf: localPackage.targets)

let inputPath = URL(filePath: fileManager.currentDirectoryPath)
.appending(path: localPackage.path)
.appending(path: "Sources")
if let enumerator = fileManager.enumerator(atPath: inputPath.path()) {
for case let filePath as String in enumerator {
if filePath.hasSuffix(".swift") {
let fullFilePath = inputPath.appending(path: filePath)
inputs.append(fullFilePath.path())
}
}
let sourcesPath = Path.current + localPackage.path + "Sources"
for swiftFile in sourcesPath.glob("**/*.swift") {
inputs.append(swiftFile.string)
}
}
}

private func paths(in path: String) -> [URL] {
if path.isFile {
return [URL(filePath: path)]
}

let fileManager = FileManager.default
let absolutePath = URL(filePath: fileManager.currentDirectoryPath).appending(path: path)
return fileManager.subpaths(atPath: absolutePath.path())?.compactMap { element -> URL? in
guard element.hasSuffix(".swift") else { return nil }

let elementAbsolutePath = absolutePath.appending(path: element)
return elementAbsolutePath.path().isFile ? elementAbsolutePath : nil
} ?? []
}
}