Skip to content

Commit

Permalink
Add Makefile + optional static version (#339)
Browse files Browse the repository at this point in the history
* Add Makefile + optional static version

* add commit hash to version

* use swift instead of bash
  • Loading branch information
tanner0101 authored Aug 20, 2020
1 parent 150526c commit 6f29a1b
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 29 deletions.
19 changes: 19 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
VERSION := $(shell ./scripts/current-version.sh)

build:
# Set version
sed -i '' 's/nil/"${VERSION}"/g' Sources/VaporToolbox/Version.swift
# Build
swift build \
--disable-sandbox \
--configuration release \
-Xswiftc -cross-module-optimization \
--enable-test-discovery
# Reset version
sed -i '' 's/"${VERSION}"/nil/g' Sources/VaporToolbox/Version.swift
install: build
mv .build/release/vapor /usr/local/bin/vapor-make
uninstall:
rm /usr/local/bin/vapor-make
clean:
rm -rf .build
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ConsoleKit
import Foundation

final class Main: CommandGroup {
final class Toolbox: CommandGroup {
struct Signature: CommandSignature {
@Flag(name: "version", help: "Prints Vapor toolbox and framework versions.")
var version: Bool
Expand All @@ -22,33 +22,8 @@ final class Main: CommandGroup {
func run(using context: inout CommandContext) throws {
let signature = try Signature(from: &context.input)
if signature.version {
do {
let packageString = try Process.shell.run("cat", "Package.resolved")
let package = try JSONDecoder().decode(PackageResolved.self, from: .init(packageString.utf8))
if let vapor = package.object.pins.filter({ $0.package == "vapor" }).first {
context.console.output(key: "framework", value: vapor.state.version)
} else {
context.console.output("\("note:", style: .warning) this Swift project does not depend on Vapor.")
context.console.output(key: "framework", value: "not found")
}
} catch {
context.console.output("\("note:", style: .warning) no Package.resolved file was found.")
context.console.output(key: "framework", value: "not found")
}
do {
let brewString = try Process.shell.run("brew", "info", "vapor")
let versionFinder = try NSRegularExpression(pattern: #"(\d+\.)(\d+\.)(\d)"#)
let versionString = String(brewString.split(separator: "\n")[0])
if let match = versionFinder.firstMatch(in: versionString, options: [], range: .init(location: 0, length: versionString.utf16.count)) {
let version = versionString[Range(match.range, in: versionString)!]
context.console.output(key: "toolbox", value: "\(version)")
} else {
context.console.output(key: "toolbox", value: versionString)
}
} catch {
context.console.output("\("note:", style: .warning) could not determine toolbox version.")
context.console.output(key: "toolbox", value: "not found")
}
self.outputFrameworkVersion(context: context)
self.outputToolboxVersion(context: context)
} else if let command = try self.commmand(using: &context) {
try command.run(using: &context)
} else if let `default` = self.defaultCommand {
Expand All @@ -73,6 +48,46 @@ final class Main: CommandGroup {
return nil
}
}


private func outputFrameworkVersion(context: CommandContext) {
do {
let packageString = try Process.shell.run("cat", "Package.resolved")
let package = try JSONDecoder().decode(PackageResolved.self, from: .init(packageString.utf8))
if let vapor = package.object.pins.filter({ $0.package == "vapor" }).first {
context.console.output(key: "framework", value: vapor.state.version)
} else {
context.console.output("\("note:", style: .warning) this Swift project does not depend on Vapor.")
context.console.output(key: "framework", value: "not found")
}
} catch {
context.console.output("\("note:", style: .warning) no Package.resolved file was found.")
context.console.output(key: "framework", value: "not found")
}
}

private func outputToolboxVersion(context: CommandContext) {
do {
if let version = staticVersion {
// compiled with static version, use that
context.console.output(key: "toolbox", value: version)
} else {
// determine version through homebrew
let brewString = try Process.shell.run("brew", "info", "vapor")
let versionFinder = try NSRegularExpression(pattern: #"(\d+\.)(\d+\.)(\d)"#)
let versionString = String(brewString.split(separator: "\n")[0])
if let match = versionFinder.firstMatch(in: versionString, options: [], range: .init(location: 0, length: versionString.utf16.count)) {
let version = versionString[Range(match.range, in: versionString)!]
context.console.output(key: "toolbox", value: "\(version)")
} else {
context.console.output(key: "toolbox", value: versionString)
}
}
} catch {
context.console.output("\("note:", style: .warning) could not determine toolbox version.")
context.console.output(key: "toolbox", value: "not found")
}
}
}

private struct PackageResolved: Codable {
Expand Down Expand Up @@ -104,7 +119,7 @@ public func run() throws {
let console = Terminal()
let input = CommandInput(arguments: CommandLine.arguments)
do {
try console.run(Main(), input: input)
try console.run(Toolbox(), input: input)
}
// Handle deprecated commands. Done this way instead of by implementing them as Commands because otherwise
// there's no way to avoid them showing up in the --help, which is exactly the opposite of what we want.
Expand Down
3 changes: 3 additions & 0 deletions Sources/VaporToolbox/Version.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file is edited by Makefile
// Do not change manually.
let staticVersion: String? = nil
40 changes: 40 additions & 0 deletions scripts/current-version.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env swift
import Foundation

try print(currentVersion())

func currentVersion() throws -> String {
do {
let tag = try shell("git", "describe", "--tags", "--exact-match")
return tag
} catch {
let branch = try shell("git", "symbolic-ref", "-q", "--short", "HEAD")
let commit = try shell("git", "rev-parse", "--short", "HEAD")
return "\(branch) (\(commit))"
}
}

func shell(_ args: String...) throws -> String {
let task = Process()
task.launchPath = "/usr/bin/env"
task.arguments = args
// grab stdout
let output = Pipe()
task.standardOutput = output
// ignore stderr
let error = Pipe()
task.standardError = error
task.launch()
task.waitUntilExit()

guard task.terminationStatus == 0 else {
throw ShellError(terminationStatus: task.terminationStatus)
}

return String(decoding: output.fileHandleForReading.readDataToEndOfFile(), as: UTF8.self)
.trimmingCharacters(in: .whitespacesAndNewlines)
}

struct ShellError: Error {
var terminationStatus: Int32
}

0 comments on commit 6f29a1b

Please sign in to comment.