Skip to content

Commit

Permalink
Added ability to add offset to the build number.
Browse files Browse the repository at this point in the history
  • Loading branch information
samdeane committed Nov 26, 2024
1 parent 8a7c67b commit 5fd3475
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
4 changes: 3 additions & 1 deletion Sources/ReleaseTools/Commands/ArchiveCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ struct ArchiveCommand: AsyncParsableCommand {
@OptionGroup() var scheme: SchemeOption
@OptionGroup() var platform: PlatformOption
@OptionGroup() var options: CommonOptions
@OptionGroup() var buildOptions: BuildOptions

func run() async throws {
let parsed = try OptionParser(
options: options,
command: Self.configuration,
scheme: scheme,
platform: platform
platform: platform,
buildOptions: buildOptions
)

try await Self.archive(parsed: parsed, xcconfig: xcconfig)
Expand Down
19 changes: 13 additions & 6 deletions Sources/ReleaseTools/Commands/UpdateBuildCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ struct UpdateBuildCommand: AsyncParsableCommand {
@Option(help: "The git repo to derive the build number from.") var repo: String?

@OptionGroup() var options: CommonOptions
@OptionGroup() var buildOptions: BuildOptions

func run() async throws {
let parsed = try OptionParser(
options: options,
command: Self.configuration
command: Self.configuration,
buildOptions: buildOptions
)

if let header = header, let repo = repo {
Expand All @@ -58,14 +60,19 @@ struct UpdateBuildCommand: AsyncParsableCommand {
}
}

static func getBuild(in url: URL, using git: GitRunner) async throws -> (String, String) {
static func getBuild(in url: URL, using git: GitRunner, offset: UInt) async throws -> (String, String) {
git.cwd = url
chdir(url.path)

var result = git.run(["rev-list", "--count", "HEAD"])
try await result.throwIfFailed(UpdateBuildError.gettingBuildFailed)

let build = await result.stdout.string.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
var build = await result.stdout.string.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)

// apply an offset to the build number if required
if offset > 0, let number = UInt(build) {
build = String(number + offset)
}

result = git.run(["rev-list", "--max-count", "1", "HEAD"])
try await result.throwIfFailed(UpdateBuildError.gettingCommitFailed)
Expand All @@ -82,7 +89,7 @@ struct UpdateBuildCommand: AsyncParsableCommand {
let info = try PropertyListSerialization.propertyList(from: data, options: [], format: nil)

let git = GitRunner()
let (build, commit) = try await getBuild(in: repoURL, using: git)
let (build, commit) = try await getBuild(in: repoURL, using: git, offset: parsed.buildOffset)

if var info = info as? [String: Any] {
print(info)
Expand All @@ -108,7 +115,7 @@ struct UpdateBuildCommand: AsyncParsableCommand {
let repoURL = URL(fileURLWithPath: repo)

let git = GitRunner()
let (build, commit) = try await getBuild(in: repoURL, using: git)
let (build, commit) = try await getBuild(in: repoURL, using: git, offset: parsed.buildOffset)
parsed.log("Setting build number to \(build).")
let header =
"#define BUILD \(build)\n#define CURRENT_PROJECT_VERSION \(build)\n#define COMMIT \(commit)"
Expand All @@ -131,7 +138,7 @@ struct UpdateBuildCommand: AsyncParsableCommand {
}

let git = GitRunner()
let (build, commit) = try await getBuild(in: configURL.deletingLastPathComponent(), using: git)
let (build, commit) = try await getBuild(in: configURL.deletingLastPathComponent(), using: git, offset: parsed.buildOffset)
let new = "BUILD_NUMBER = \(build)\nBUILD_COMMIT = \(commit)"

if let existing = try? String(contentsOf: configURL), existing == new {
Expand Down
7 changes: 7 additions & 0 deletions Sources/ReleaseTools/OptionParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class OptionParser {
var apiIssuer: String = ""
var package: String = ""
var workspace: String = ""
var buildOffset: UInt = 0
var archive: XcodeArchive!

let rootURL = URL(fileURLWithPath: FileManager.default.currentDirectoryPath)
Expand Down Expand Up @@ -143,6 +144,7 @@ class OptionParser {
apiKey: ApiKeyOption? = nil,
apiIssuer: ApiIssuerOption? = nil,
platform: PlatformOption? = nil,
buildOptions: BuildOptions? = nil,
setDefaultPlatform: Bool = true
) throws {

Expand All @@ -153,6 +155,11 @@ class OptionParser {
self.platform = platform.platform ?? (setDefaultPlatform ? "macOS" : "")
}

// remember the build offset if it was supplied
if let buildOptions, let offset = buildOptions.offset {
buildOffset = offset
}

// if we've specified the scheme or user, we also need the workspace
if requirements.contains(.workspace) || scheme != nil || user != nil {
if let workspace = options.workspace ?? defaultWorkspace {
Expand Down
4 changes: 4 additions & 0 deletions Sources/ReleaseTools/SharedOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,7 @@ struct CommonOptions: ParsableArguments {
@Option(help: "The workspace we're operating on.")
var workspace: String?
}

struct BuildOptions: ParsableArguments {
@Option(name: .customLong("offset"), help: "Integer offset to apply to the build number.") var offset: UInt?
}

0 comments on commit 5fd3475

Please sign in to comment.