Skip to content

Commit

Permalink
Fix shellcheck warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
DimaRU committed Feb 9, 2024
1 parent 7bb46d1 commit 3533e83
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 1 deletion.
21 changes: 21 additions & 0 deletions Binaries/PackageBuildInfo.artifactbundle/info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"schemaVersion": "1.0",
"artifacts": {
"PackageBuildInfo": {
"type": "executable",
"version": "1.0.2",
"variants": [
{
"path": "universal/packageBuildInfo.sh",
"supportedTriples": [
"x86_64-unknown-linux-gnu",
"arm64-unknown-linux-gnu",
"x86_64-apple-macosx",
"arm64-apple-macosx",
"aarch64-unknown-linux-gnu"
]
}
]
}
}
}
123 changes: 123 additions & 0 deletions Binaries/PackageBuildInfo.artifactbundle/universal/packageBuildInfo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#!/bin/bash
#
# packageBuildInfo.sh
# Generate .swift file with info from git repo
# Part of PackageBuildInfo plugin (https://github.com/DimaRU/PackageBuildInfo)
#
# Copyright © 2024 Dmitriy Borovikov. All rights reserved.
#
if [[ $# != 2 ]]; then
echo "USAGE: packageBuildInfo <git directory> <output file>"
exit 1
fi

gitDirectory=$1
cd "$gitDirectory" || exit 2

function trim() {
local var="$*"
# remove leading whitespace characters
var="${var#"${var%%[![:space:]]*}"}"

# remove trailing whitespace characters
var="${var%"${var##*[![:space:]]}"}"
output=$var
}

function runGit() {
out=$($1 2>&1)
exitCode=$?
trim "$out"
}

function getGitInfo() {
runGit "git status --porcelain -uno"
if [[ $exitCode != 0 ]]; then
echo "$output" >&2
return
fi
if [[ ${#output} == 0 ]]; then
isDirty="false"
fi

runGit "git describe --tags --abbrev=0"
if [[ $exitCode == 0 && ${#output} != 0 ]]; then
tag=\"$output\"
runGit "git rev-list --count $output..HEAD"
if [[ ${#output} != 0 ]]; then
countSinceTag=$output
fi
fi

runGit "git rev-parse --abbrev-ref HEAD"
if [[ $exitCode == 0 && ${#output} != 0 ]]; then
branch=\"$output\"
fi

runGit "git show -s --format=%H:%ct"
if [[ $exitCode == 0 && ${#output} != 0 ]]; then
digest=${output%:*}
date=${output#*:}
fi

runGit "git rev-list --count HEAD"
if [[ $exitCode == 0 && ${#output} != 0 ]]; then
count=$output
fi

if [[ ${#digest} != 0 ]]; then
digestS="[0x${digest:0:2}"
for (( start=2; start < ${#digest}; start+=2 ))
do
digestS+=", 0x${digest:$start:2}"
done
digestS+="]"
fi
}

isDirty="true"
date=$(date +%s)
count="0"
branch="nil"
countSinceTag="0"
tag="nil"
digest=""
digestS="[]"

TZO=$(date +%z)
TZSIGN=${TZO:0:1}1
TZHOUR=10#${TZO:1:2}
TZMIN=10#${TZO:3:2}
timeZone=$((TZSIGN*((TZHOUR*60)+TZMIN)*60))

getGitInfo

echo "/////
//// Package Build info
/// Code generated by PackageBuildInfo. DO NOT EDIT.
//
import Foundation
public struct PackageBuild {
let isDirty: Bool // Dirty build - git directory is't clean.
let timeStamp: Date // Time of last commit
let timeZone: TimeZone // Time Zone
let count: Int // Total commit count
let tag: String? // Tag, if exist
let countSinceTag: Int // Commit count since tag
let branch: String? // Git branch name
let digest: [UInt8] // Latest commit sha1 digest (20 bytes)
var commit: String {
digest.reduce(\"\") { \$0 + String(format: \"%02x\", \$1) }
}
static let info = PackageBuild(
isDirty: $isDirty,
timeStamp: Date(timeIntervalSince1970: $date),
timeZone: TimeZone(secondsFromGMT: $timeZone) ?? TimeZone.current,
count: $count,
tag: $tag,
countSinceTag: $countSinceTag,
branch: $branch,
digest: $digestS)
}" >"$2"
Binary file removed Binaries/PackageBuildInfo.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ let package = Package(
capability: .buildTool(),
dependencies: ["PackageBuildInfo"]
),
.binaryTarget(name: "PackageBuildInfo", path: "Binaries/PackageBuildInfo.zip"),
.binaryTarget(name: "PackageBuildInfo", path: "Binaries/PackageBuildInfo.artifactbundle"),
]
)

0 comments on commit 3533e83

Please sign in to comment.