Skip to content

Commit

Permalink
Detect path of swift-sh tool dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
FlineDevPublic committed Apr 1, 2022
1 parent ae94a28 commit c4eb211
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 14 deletions.
16 changes: 8 additions & 8 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@ let package = Package(
.package(url: "https://github.com/jakeheis/SwiftCLI.git", from: "6.0.1"),
],
targets: [
.executableTarget(
name: "AnyLintCLI",
dependencies: ["Rainbow", "SwiftCLI", "Utility"]
),
.target(
name: "AnyLint",
dependencies: ["Utility"]
),
.target(
name: "Utility",
dependencies: ["Rainbow"]
),
.testTarget(
name: "AnyLintTests",
dependencies: ["AnyLint"]
),
.executableTarget(
name: "AnyLintCLI",
dependencies: ["Rainbow", "SwiftCLI", "Utility"]
),
.testTarget(
name: "AnyLintCLITests",
dependencies: ["AnyLintCLI"]
),
.target(
name: "Utility",
dependencies: ["Rainbow"]
),
.testTarget(
name: "UtilityTests",
dependencies: ["Utility"]
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ anylint --init blank
This will create the Swift script file `lint.swift` with something like the following contents:

```swift
#!/usr/local/bin/swift-sh
#!/opt/local/bin/swift-sh
import AnyLint // @Flinesoft

Lint.logSummaryAndExit(arguments: CommandLine.arguments) {
Expand Down Expand Up @@ -399,7 +399,7 @@ If you want to use regexes in your custom code, you can learn more about how you
When using the `customCheck`, you might want to also include some Swift packages for [easier file handling](https://github.com/JohnSundell/Files) or [running shell commands](https://github.com/JohnSundell/ShellOut). You can do so by adding them at the top of the file like so:

```swift
#!/usr/local/bin/swift-sh
#!/opt/local/bin/swift-sh
import AnyLint // @Flinesoft
import ShellOut // @JohnSundell

Expand Down
46 changes: 45 additions & 1 deletion Sources/AnyLintCLI/Globals/CLIConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,49 @@ enum CLIConstants {
static let commandName: String = "anylint"
static let defaultConfigFileName: String = "lint.swift"
static let initTemplateCases: String = InitTask.Template.allCases.map { $0.rawValue }.joined(separator: ", ")
static let swiftShPath: String = "/usr/local/bin/swift-sh"
static var swiftShPath: String {
switch self.getPlatform() {
case .intel:
return "/usr/local/bin/swift-sh"

case .appleSilicon:
return "/opt/homebrew/bin/swift-sh"

case .linux:
return "/home/linuxbrew/.linuxbrew/bin/swift-sh"
}
}
}

extension CLIConstants {
fileprivate enum Platform {
case intel
case appleSilicon
case linux
}

fileprivate static func getPlatform() -> Platform {
#if os(Linux)
return .linux
#else
// Source: https://stackoverflow.com/a/69624732
var systemInfo = utsname()
let exitCode = uname(&systemInfo)

let fallbackPlatform: Platform = .appleSilicon
guard exitCode == EXIT_SUCCESS else { return fallbackPlatform }

let cpuArchitecture = String(cString: &systemInfo.machine.0, encoding: .utf8)
switch cpuArchitecture {
case "x86_64":
return .intel

case "arm64":
return .appleSilicon

default:
return fallbackPlatform
}
#endif
}
}
4 changes: 2 additions & 2 deletions Sources/AnyLintCLI/Globals/ValidateOrFail.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ enum ValidateOrFail {
static func swiftShInstalled() {
guard fileManager.fileExists(atPath: CLIConstants.swiftShPath) else {
log.message(
"swift-sh not installed – please follow instructions on https://github.com/mxcl/swift-sh#installation to install.",
"swift-sh not installed – please try `brew install swift-sh` or follow instructions on https://github.com/mxcl/swift-sh#installation",
level: .error
)
log.exit(status: .failure)
Expand All @@ -18,7 +18,7 @@ enum ValidateOrFail {
static func configFileExists(at configFilePath: String) throws {
guard fileManager.fileExists(atPath: configFilePath) else {
log.message(
"No configuration file found at \(configFilePath) – consider running `\(CLIConstants.commandName) --init` with a template.",
"No configuration file found at \(configFilePath) – consider running `--init` with a template, e.g.`\(CLIConstants.commandName) --init blank`.",
level: .error
)
log.exit(status: .failure)
Expand Down
2 changes: 2 additions & 0 deletions Sources/AnyLintCLI/Tasks/InitTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ extension InitTask: TaskHandler {
return // only reachable in unit tests
}

ValidateOrFail.swiftShInstalled()

log.message("Making sure config file directory exists ...", level: .info)
try Task.run(bash: "mkdir -p '\(configFilePath.parentDirectoryPath)'")

Expand Down
2 changes: 1 addition & 1 deletion lint.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/local/bin/swift-sh
#!/opt/local/bin/swift-sh
import AnyLint // .
import Utility
import ShellOut // @JohnSundell
Expand Down

0 comments on commit c4eb211

Please sign in to comment.