Skip to content

Commit 6f6959e

Browse files
authored
Merge pull request #10 from tonyarnold/proper-version-info
Properly inject version information into the tool from the environment
2 parents 4caf9b5 + aaa83c9 commit 6f6959e

File tree

10 files changed

+253
-74
lines changed

10 files changed

+253
-74
lines changed

.github/workflows/ci.yml

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ jobs:
3131
arch: "x86_64"
3232
triple: "x86_64-unknown-linux-gnu"
3333
comprehensive_test: true
34+
lint_check: true
3435
- name: "Linux ARM64"
3536
runner: ubuntu-24.04-arm
3637
platform: "Linux"
3738
arch: "aarch64"
3839
triple: "aarch64-unknown-linux-gnu"
3940
comprehensive_test: true
41+
lint_check: true
4042

4143
steps:
4244
- name: Checkout code
@@ -99,7 +101,30 @@ jobs:
99101
100102
if [ "${{ matrix.platform }}" = "macOS" ]; then
101103
echo "Building universal macOS binary..."
102-
swift build --configuration release --arch arm64 --arch x86_64 -Xswiftc -Osize
104+
105+
# Build ARM64 binary
106+
echo "Building ARM64 binary..."
107+
swift build --configuration release --arch arm64 -Xswiftc -Osize
108+
109+
# Build x86_64 binary
110+
echo "Building x86_64 binary..."
111+
swift build --configuration release --arch x86_64 -Xswiftc -Osize
112+
113+
# Create output directory
114+
mkdir -p .build/apple/Products/Release
115+
116+
# Combine architectures with lipo
117+
echo "Combining architectures with lipo..."
118+
lipo -create \
119+
.build/arm64-apple-macosx/release/swift-dependency-audit \
120+
.build/x86_64-apple-macosx/release/swift-dependency-audit \
121+
-output .build/apple/Products/Release/swift-dependency-audit
122+
123+
# Verify the universal binary
124+
echo "Verifying universal binary..."
125+
lipo -info .build/apple/Products/Release/swift-dependency-audit
126+
127+
# Strip the final binary
103128
strip -rSTx .build/apple/Products/Release/swift-dependency-audit
104129
else
105130
echo "Building static Linux binary for ${{ matrix.arch }}..."
@@ -138,7 +163,7 @@ jobs:
138163
139164
echo "=== Testing JSON Output ==="
140165
$BINARY_PATH . --output-format json --no-color --exclude-tests > output.json
141-
cat output.json | python3 -m json.tool > /dev/null || (echo "❌ Invalid JSON output" && exit 1)
166+
jq . output.json > /dev/null || (echo "❌ Invalid JSON output" && exit 1)
142167
echo "✅ JSON output is valid"
143168
144169
echo "=== Testing Output Formats ==="
@@ -148,14 +173,7 @@ jobs:
148173
echo "=== Testing Custom Whitelist ==="
149174
$BINARY_PATH . --whitelist "Foundation,SwiftUI,ArgumentParser" --verbose --exclude-tests
150175
151-
echo "=== Testing Plugin Integration ==="
152-
if swift package plugin --list | grep -q "DependencyAuditPlugin"; then
153-
echo "✅ Plugin is properly registered"
154-
else
155-
echo "⚠️ Plugin not found in list, but core functionality works"
156-
fi
157-
158-
- name: Lint and format check (macOS only)
176+
- name: Lint and format check
159177
if: matrix.lint_check
160178
run: |
161179
echo "=== Format Check ==="
@@ -165,16 +183,6 @@ jobs:
165183
else
166184
echo "swift-format not available, skipping format check"
167185
fi
168-
169-
echo "=== Build Warnings Check ==="
170-
swift build --configuration release 2>&1 | tee build.log
171-
if grep -q "warning:" build.log; then
172-
echo "⚠️ Build warnings found:"
173-
grep "warning:" build.log
174-
exit 1
175-
else
176-
echo "✅ No build warnings found"
177-
fi
178186
continue-on-error: true
179187

180188
- name: Upload build artifacts

.github/workflows/release.yml

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,30 @@ jobs:
9696
9797
if [ "${{ matrix.platform }}" = "macOS" ]; then
9898
echo "Building universal macOS binary (version will be detected as: $VERSION)..."
99-
swift build --configuration release --arch arm64 --arch x86_64 -Xswiftc -Osize
99+
100+
# Build ARM64 binary
101+
echo "Building ARM64 binary..."
102+
swift build --configuration release --arch arm64 -Xswiftc -Osize
103+
104+
# Build x86_64 binary
105+
echo "Building x86_64 binary..."
106+
swift build --configuration release --arch x86_64 -Xswiftc -Osize
107+
108+
# Create output directory
109+
mkdir -p .build/apple/Products/Release
110+
111+
# Combine architectures with lipo
112+
echo "Combining architectures with lipo..."
113+
lipo -create \
114+
.build/arm64-apple-macosx/release/swift-dependency-audit \
115+
.build/x86_64-apple-macosx/release/swift-dependency-audit \
116+
-output .build/apple/Products/Release/swift-dependency-audit
117+
118+
# Verify the universal binary
119+
echo "Verifying universal binary..."
120+
lipo -info .build/apple/Products/Release/swift-dependency-audit
121+
122+
# Strip the final binary
100123
strip -rSTx .build/apple/Products/Release/swift-dependency-audit
101124
else
102125
echo "Building static Linux binary for ${{ matrix.arch }} (version will be detected as: $VERSION)..."
@@ -134,7 +157,7 @@ jobs:
134157
135158
echo "=== Testing JSON Output ==="
136159
$BINARY_PATH . --output-format json --no-color --exclude-tests > output.json
137-
cat output.json | python3 -m json.tool > /dev/null || (echo "❌ Invalid JSON output" && exit 1)
160+
jq . output.json > /dev/null || (echo "❌ Invalid JSON output" && exit 1)
138161
echo "✅ JSON output is valid"
139162
140163
echo "=== Testing Output Formats ==="
@@ -144,13 +167,6 @@ jobs:
144167
echo "=== Testing Custom Whitelist ==="
145168
$BINARY_PATH . --whitelist "Foundation,SwiftUI,ArgumentParser" --verbose --exclude-tests
146169
147-
echo "=== Testing Plugin Integration ==="
148-
if swift package plugin --list | grep -q "DependencyAuditPlugin"; then
149-
echo "✅ Plugin is properly registered"
150-
else
151-
echo "⚠️ Plugin not found in list, but core functionality works"
152-
fi
153-
154170
- name: Upload build artifacts
155171
uses: actions/upload-artifact@v4
156172
with:
@@ -290,7 +306,15 @@ jobs:
290306
- name: Generate checksums
291307
run: |
292308
cd release
293-
shasum -a 256 *.tar.gz > checksums.txt
309+
# Use portable checksum command
310+
if command -v shasum >/dev/null 2>&1; then
311+
shasum -a 256 *.tar.gz > checksums.txt
312+
elif command -v sha256sum >/dev/null 2>&1; then
313+
sha256sum *.tar.gz > checksums.txt
314+
else
315+
echo "❌ No checksum tool available"
316+
exit 1
317+
fi
294318
cat checksums.txt
295319
296320
- name: Generate release notes

Makefile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ clean:
3535
# Build for current platform
3636
.PHONY: build
3737
build:
38-
swift build $(SWIFT_BUILD_FLAGS)
38+
VERSION=$(VERSION) swift build $(SWIFT_BUILD_FLAGS)
3939

4040
# Build universal macOS binary
4141
.PHONY: build_macos
4242
build_macos:
4343
@echo "Building universal macOS binary..."
44-
swift build $(SWIFT_BUILD_FLAGS) --arch arm64 --arch x86_64
44+
VERSION=$(VERSION) swift build $(SWIFT_BUILD_FLAGS) --arch arm64 --arch x86_64
4545
strip -rSTx $(MACOS_BUILD_DIR)/$(EXECUTABLE_NAME)
4646
@echo "✅ macOS universal binary built at $(MACOS_BUILD_DIR)/$(EXECUTABLE_NAME)"
4747

@@ -55,13 +55,13 @@ build_linux_native:
5555
ifeq ($(shell uname -m), x86_64)
5656
@echo "Building Linux x86_64 binary natively..."
5757
mkdir -p $(LINUX_X86_64_BUILD_DIR)
58-
swift build $(SWIFT_BUILD_FLAGS) --triple x86_64-unknown-linux-gnu --static-swift-stdlib
58+
VERSION=$(VERSION) swift build $(SWIFT_BUILD_FLAGS) --triple x86_64-unknown-linux-gnu --static-swift-stdlib
5959
strip .build/x86_64-unknown-linux-gnu/release/$(EXECUTABLE_NAME)
6060
@echo "✅ Linux x86_64 binary built natively"
6161
else ifeq ($(shell uname -m), aarch64)
6262
@echo "Building Linux ARM64 binary natively..."
6363
mkdir -p $(LINUX_AARCH64_BUILD_DIR)
64-
swift build $(SWIFT_BUILD_FLAGS) --triple aarch64-unknown-linux-gnu --static-swift-stdlib
64+
VERSION=$(VERSION) swift build $(SWIFT_BUILD_FLAGS) --triple aarch64-unknown-linux-gnu --static-swift-stdlib
6565
strip .build/aarch64-unknown-linux-gnu/release/$(EXECUTABLE_NAME)
6666
@echo "✅ Linux ARM64 binary built natively"
6767
else
@@ -83,7 +83,7 @@ build_linux_x86_64:
8383
-e HOME=/tmp \
8484
swift:6.1 \
8585
bash -c " \
86-
swift build $(SWIFT_BUILD_FLAGS) --triple x86_64-unknown-linux-gnu --static-swift-stdlib && \
86+
VERSION=$(VERSION) swift build $(SWIFT_BUILD_FLAGS) --triple x86_64-unknown-linux-gnu --static-swift-stdlib && \
8787
strip .build/x86_64-unknown-linux-gnu/release/$(EXECUTABLE_NAME) \
8888
"
8989
@echo "✅ Linux x86_64 binary built at $(LINUX_X86_64_BUILD_DIR)/$(EXECUTABLE_NAME)"
@@ -102,7 +102,7 @@ build_linux_aarch64:
102102
echo 'Installing cross-compilation tools...' && \
103103
apt-get update && apt-get install -y gcc-aarch64-linux-gnu && \
104104
echo 'Cross-compiling for ARM64...' && \
105-
swift build $(SWIFT_BUILD_FLAGS) --triple aarch64-unknown-linux-gnu -Xcc -target -Xcc aarch64-unknown-linux-gnu --static-swift-stdlib -Xswiftc -resource-dir -Xswiftc /usr/lib/swift_static && \
105+
VERSION=$(VERSION) swift build $(SWIFT_BUILD_FLAGS) --triple aarch64-unknown-linux-gnu -Xcc -target -Xcc aarch64-unknown-linux-gnu --static-swift-stdlib -Xswiftc -resource-dir -Xswiftc /usr/lib/swift_static && \
106106
aarch64-linux-gnu-strip .build/aarch64-unknown-linux-gnu/release/$(EXECUTABLE_NAME) \
107107
"
108108
@echo "✅ Linux ARM64 binary built at $(LINUX_AARCH64_BUILD_DIR)/$(EXECUTABLE_NAME)"

Package.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ let package = Package(
3333
targets: [
3434
.target(
3535
name: "SwiftDependencyAuditLib",
36+
plugins: ["VersionPlugin"]
3637
),
3738
.executableTarget(
3839
name: "SwiftDependencyAudit",
@@ -41,6 +42,12 @@ let package = Package(
4142
"SwiftDependencyAuditLib"
4243
]
4344
),
45+
.executableTarget(
46+
name: "VersionGenerator",
47+
dependencies: [
48+
.product(name: "ArgumentParser", package: "swift-argument-parser")
49+
]
50+
),
4451
.testTarget(
4552
name: "SwiftDependencyAuditTests",
4653
dependencies: ["SwiftDependencyAuditLib"]
@@ -50,6 +57,11 @@ let package = Package(
5057
capability: .buildTool(),
5158
dependencies: swiftDependencyAuditPluginDependencies
5259
),
60+
.plugin(
61+
name: "VersionPlugin",
62+
capability: .buildTool(),
63+
dependencies: ["VersionGenerator"]
64+
),
5365
]
5466
)
5567

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import Foundation
2+
import PackagePlugin
3+
4+
@main
5+
struct VersionPlugin: BuildToolPlugin {
6+
func createBuildCommands(context: PluginContext, target: Target) async throws -> [Command] {
7+
let versionGenerator = try context.tool(named: "VersionGenerator")
8+
let versionFileURL = context.pluginWorkDirectoryURL.appending(path: "Version.swift")
9+
10+
return [
11+
.buildCommand(
12+
displayName: "Generate version information into \(versionFileURL.lastPathComponent)",
13+
executable: versionGenerator.url,
14+
arguments: [versionFileURL.path(percentEncoded: false)],
15+
environment: ProcessInfo.processInfo.environment,
16+
inputFiles: [],
17+
outputFiles: [versionFileURL]
18+
)
19+
]
20+
}
21+
}

Scripts/spm-artifact-bundle.sh

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@ log_error() {
3636
echo -e "${RED}$*${NC}" >&2
3737
}
3838

39+
# Utility functions
40+
calculate_checksum() {
41+
local file="$1"
42+
43+
if command -v shasum >/dev/null 2>&1; then
44+
# macOS/BSD systems
45+
shasum -a 256 "$file" | cut -d' ' -f1
46+
elif command -v sha256sum >/dev/null 2>&1; then
47+
# Linux/GNU systems
48+
sha256sum "$file" | cut -d' ' -f1
49+
else
50+
log_error "No checksum tool available (shasum or sha256sum)"
51+
return 1
52+
fi
53+
}
54+
3955
# Validation functions
4056
validate_version() {
4157
local version="$1"
@@ -257,7 +273,7 @@ main() {
257273
fi
258274

259275
# Calculate checksum
260-
local checksum=$(shasum -a 256 "$zip_name" | cut -d' ' -f1)
276+
local checksum=$(calculate_checksum "$zip_name")
261277
local zip_size=$(du -h "$zip_name" | cut -f1)
262278

263279
# Save checksum to file for CI

Scripts/update-artifact-bundle.sh

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@ log_error() {
3636
echo -e "${RED}$*${NC}" >&2
3737
}
3838

39+
# Utility functions
40+
calculate_checksum() {
41+
local file="$1"
42+
43+
if command -v shasum >/dev/null 2>&1; then
44+
# macOS/BSD systems
45+
shasum -a 256 "$file" | cut -d' ' -f1
46+
elif command -v sha256sum >/dev/null 2>&1; then
47+
# Linux/GNU systems
48+
sha256sum "$file" | cut -d' ' -f1
49+
else
50+
log_error "No checksum tool available (shasum or sha256sum)"
51+
return 1
52+
fi
53+
}
54+
3955
# Validation functions
4056
validate_version() {
4157
local version="$1"
@@ -102,7 +118,7 @@ main() {
102118

103119
# Calculate and validate checksum
104120
log_info "Calculating SHA256 checksum for $ARTIFACT_BUNDLE..."
105-
local readonly checksum="$(shasum -a 256 "$ARTIFACT_BUNDLE" | cut -d " " -f1)"
121+
local readonly checksum="$(calculate_checksum "$ARTIFACT_BUNDLE")"
106122

107123
if ! validate_checksum "$checksum"; then
108124
exit 1

Sources/SwiftDependencyAudit/DependentImportScanner.swift

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,44 +10,10 @@ import Foundation
1010

1111
/// Get the version string, trying multiple sources
1212
private func getVersion() -> String {
13-
// First try to get version from git if available (for development)
14-
if let gitVersion = getGitVersion() {
15-
return gitVersion
16-
}
17-
18-
// Fallback to embedded version
19-
#if DEBUG
20-
return "dev"
21-
#else
22-
return "1.0.0" // Fallback for release builds
23-
#endif
13+
// Always use the build-time generated version from the plugin
14+
return SwiftDependencyAuditLib.VERSION
2415
}
2516

26-
/// Attempt to get version from git (for development builds)
27-
private func getGitVersion() -> String? {
28-
let process = Process()
29-
process.executableURL = URL(fileURLWithPath: "/usr/bin/git")
30-
process.arguments = ["describe", "--tags", "--always"]
31-
32-
let pipe = Pipe()
33-
process.standardOutput = pipe
34-
process.standardError = Pipe() // Suppress error output
35-
36-
do {
37-
try process.run()
38-
process.waitUntilExit()
39-
40-
guard process.terminationStatus == 0 else { return nil }
41-
42-
let data = pipe.fileHandleForReading.readDataToEndOfFile()
43-
let output = String(data: data, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines)
44-
45-
// Remove 'v' prefix if present
46-
return output?.hasPrefix("v") == true ? String(output!.dropFirst()) : output
47-
} catch {
48-
return nil
49-
}
50-
}
5117

5218
public enum OutputFormat: String, CaseIterable, ExpressibleByArgument {
5319
case terminal = "terminal"

0 commit comments

Comments
 (0)