Skip to content

Commit

Permalink
version number fix (#340)
Browse files Browse the repository at this point in the history
  • Loading branch information
tanner0101 authored Aug 20, 2020
1 parent 6f29a1b commit 7edba49
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 53 deletions.
19 changes: 6 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
VERSION := $(shell ./scripts/current-version.sh)
DEST := /usr/local/bin/vapor

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
swiftc ./scripts/build.swift
./build
rm ./build
install: build
mv .build/release/vapor /usr/local/bin/vapor-make
mv .build/release/vapor ${DEST}
uninstall:
rm /usr/local/bin/vapor-make
rm ${DEST}
clean:
rm -rf .build
82 changes: 82 additions & 0 deletions scripts/build.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env swift
import Foundation

try build()

func build() throws {
try withVersion(in: "Sources/VaporToolbox/Version.swift", as: currentVersion()) {
try foregroundShell(
"swift", "build",
"--disable-sandbox",
"--configuration", "release",
"-Xswiftc", "-cross-module-optimization",
"--enable-test-discovery"
)
}
}

func withVersion(in file: String, as version: String, _ closure: () throws -> ()) throws {
let fileURL = URL(fileURLWithPath: file)
let originalFileContents = try String(contentsOf: fileURL, encoding: .utf8)
// set version
try originalFileContents
.replacingOccurrences(of: "nil", with: "\"\(version)\"")
.write(to: fileURL, atomically: true, encoding: .utf8)
defer {
// undo set version
try! originalFileContents
.write(to: fileURL, atomically: true, encoding: .utf8)
}
// run closure
try closure()
}

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

func foregroundShell(_ args: String...) throws {
print("$", args.joined(separator: " "))
let task = Process()
task.launchPath = "/usr/bin/env"
task.arguments = args
task.launch()
task.waitUntilExit()

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

@discardableResult
func backgroundShell(_ 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: Swift.Error {
var terminationStatus: Int32
}
40 changes: 0 additions & 40 deletions scripts/current-version.swift

This file was deleted.

0 comments on commit 7edba49

Please sign in to comment.