Modern Apps git

← xtool.git

utils: tweaks (#387)

Commit
2f2336cb5fa46a53fa77e6a1dbe5e14a8b3ee82f
Author
Kabir Oberai <[email protected]>
When
2026-10-28 23:22 UTC (1 month ago)
Parents
3e0c11d
## What does this PR do?

Small utils refactors to make subsequent PRs cleaner.

- Minor tweaks to how we determine / handle the xtool build version
- Factor out TemporaryDirectory cache location

## How was it tested?

- [x] Cache directory logic is unchanged
- [x] Checked `xtool --version`

## AI tool usage

How much of this PR was AI-assisted? (check one)

- [ ] **0** - No AI was used to write code
- [x] **1** - I was assisted by AI. I reviewed the finished result.
- [ ] **2** - I set the AI going and left it to it; nobody has read the
result - no review, or AI review only

<!-- If an AI agent is filling this in: declare the level honestly, and
open as a draft if it is #2. Do not lower the declared level to get the
PR reviewed. -->


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **New Features**
* Version information now distinguishes clean builds from builds with
uncommitted changes, displaying a `-dirty` suffix when applicable.
* Nonblocking file-locking operations are now available to external
consumers.
* **Changes**
* Version reporting uses `XTOOL_VERSION` when set; otherwise, it uses
available Git tag or commit information.
* Temporary files now use the tool’s cache directory, honoring
`XTL_TMPDIR` and platform cache settings. On Linux, the location follows
`XDG_CACHE_HOME` when it is an absolute path; otherwise, the user cache
directory is used.
  * `TMPDIR` is no longer used to select the temporary-file location.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

Diff

.gitignore
diff --git a/.gitignore b/.gitignoreindex 0fefab9..6eee1c4 100644--- a/.gitignore+++ b/.gitignore@@ -13,3 +13,4 @@ DerivedData/  /openapi /.tmp+/xtool
Package.swift
diff --git a/Package.swift b/Package.swiftindex 856be61..9d31675 100644--- a/Package.swift+++ b/Package.swift@@ -9,12 +9,14 @@ let xtoolVersion: String? = {         let gitCommit = info.currentCommit         let gitTag = info.currentTag         let gitRef = gitTag ?? "commit \(gitCommit)"-        return "\(gitRef)\(info.hasUncommittedChanges ? " (dirty)" : "")"+        return gitRef     } else {         return nil     } }() +let hasUncommittedChanges = Context.gitInformation?.hasUncommittedChanges ?? false+ let cSettings: [CSetting] = [     .define("_GNU_SOURCE", .when(platforms: [.linux])), ]@@ -90,7 +92,9 @@ let package = Package(             dependencies: [                 .product(name: "OpenSSL", package: "xtool-core")             ],-            cSettings: cSettings + (+            cSettings: cSettings + [+                .define("XTOOL_HAS_UNCOMMITTED_CHANGES", to: hasUncommittedChanges ? "1" : "0"),+            ] + (                 xtoolVersion                     .map { [.define("XTOOL_VERSION", to: "\"\($0)\"")] }                     ?? []
Sources/CXKit/include/version.h
diff --git a/Sources/CXKit/include/version.h b/Sources/CXKit/include/version.hindex f58c6cc..57a4048 100644--- a/Sources/CXKit/include/version.h+++ b/Sources/CXKit/include/version.h@@ -1 +1,4 @@+#include <stdbool.h>+ const char * _Nonnull xtl_version(void);+bool xtl_has_uncommitted_changes(void);
Sources/CXKit/version.c
diff --git a/Sources/CXKit/version.c b/Sources/CXKit/version.cindex 26637a4..d3e7986 100644--- a/Sources/CXKit/version.c+++ b/Sources/CXKit/version.c@@ -1,4 +1,5 @@ #include "version.h"+#include <stddef.h>  const char *xtl_version(void) { #ifdef XTOOL_VERSION@@ -7,3 +8,15 @@ const char *xtl_version(void) {     return "unversioned"; #endif }++bool xtl_has_uncommitted_changes(void) {+#ifndef XTOOL_VERSION+    return true;+#else+#ifdef XTOOL_HAS_UNCOMMITTED_CHANGES+    return XTOOL_HAS_UNCOMMITTED_CHANGES != 0;+#else+    return false;+#endif+#endif+}
Sources/XCLIUtils/CommandVersion.swift
diff --git a/Sources/XCLIUtils/CommandVersion.swift b/Sources/XCLIUtils/CommandVersion.swiftnew file mode 100644index 0000000..bd3e240--- /dev/null+++ b/Sources/XCLIUtils/CommandVersion.swift@@ -0,0 +1,19 @@+public struct CommandVersion: Sendable, Hashable {+    public var version: String+    public var hasUncommittedChanges: Bool++    public init(version: String, hasUncommittedChanges: Bool) {+        self.version = version+        self.hasUncommittedChanges = hasUncommittedChanges+    }++    public static var unversioned: Self {+        .init(version: "unversioned", hasUncommittedChanges: true)+    }+}++extension CommandVersion: CustomStringConvertible {+    public var description: String {+        version + (hasUncommittedChanges ? "-dirty" : "")+    }+}
Sources/XKit/Utilities/KeyValueStorage.swift
diff --git a/Sources/XKit/Utilities/KeyValueStorage.swift b/Sources/XKit/Utilities/KeyValueStorage.swiftindex ff2801e..4e2c5b0 100644--- a/Sources/XKit/Utilities/KeyValueStorage.swift+++ b/Sources/XKit/Utilities/KeyValueStorage.swift@@ -9,6 +9,7 @@ import Foundation import ConcurrencyExtras import Dependencies+import XUtils  public enum KeyValueStorageError: Error {     case stringConversionFailure
Sources/XToolSupport/XTool+Version.swift
diff --git a/Sources/XToolSupport/XTool+Version.swift b/Sources/XToolSupport/XTool+Version.swiftindex 9ad80e2..e5edec0 100644--- a/Sources/XToolSupport/XTool+Version.swift+++ b/Sources/XToolSupport/XTool+Version.swift@@ -1,5 +1,9 @@ import CXKit+import XCLIUtils  extension XTool {-    public static let version = String(cString: xtl_version())+    public static let version = CommandVersion(+        version: String(cString: xtl_version()),+        hasUncommittedChanges: xtl_has_uncommitted_changes(),+    ) }
Sources/XUtils/DirectoryProvider.swift
diff --git a/Sources/XKit/Utilities/DirectoryProvider.swift b/Sources/XUtils/DirectoryProvider.swiftsimilarity index 100%rename from Sources/XKit/Utilities/DirectoryProvider.swiftrename to Sources/XUtils/DirectoryProvider.swift
Sources/XUtils/System+Utils.swift
diff --git a/Sources/XUtils/System+Utils.swift b/Sources/XUtils/System+Utils.swiftindex dd7bb4c..9a278f3 100644--- a/Sources/XUtils/System+Utils.swift+++ b/Sources/XUtils/System+Utils.swift@@ -55,7 +55,7 @@ extension FileDescriptor {         }     } -    enum LockMode {+    public enum LockMode {         case shared         case exclusive @@ -67,7 +67,7 @@ extension FileDescriptor {         }     } -    func tryLock(mode: LockMode) throws -> Bool {+    public func tryLock(mode: LockMode) throws -> Bool {         if flock(rawValue, mode.raw | LOCK_NB) == 0 {             return true         }
Sources/XUtils/TemporaryDirectory.swift
diff --git a/Sources/XUtils/TemporaryDirectory.swift b/Sources/XUtils/TemporaryDirectory.swiftindex fdd3295..144f86b 100644--- a/Sources/XUtils/TemporaryDirectory.swift+++ b/Sources/XUtils/TemporaryDirectory.swift@@ -71,24 +71,7 @@ private struct TemporaryDirectoryRoot {     }      private init() {-        let url: URL-        let env = ProcessInfo.processInfo.environment-        if let tmpdir = env["XTL_TMPDIR"] ?? env["TMPDIR"] {-            url = URL(fileURLWithPath: tmpdir).appendingPathComponent("sh.xtool")-        } else {-            #if os(Linux)-            // On Linux, /tmp is commonly a tmpfs mount while ~/.swiftpm lives on ext4.-            // Foundation's FileManager.copyItem uses sendfile(2) internally, which returns-            // EINVAL when copying between different filesystem types (e.g. tmpfs → ext4).-            // This causes `swift sdk install` to fail on distros like Linux Mint (#181).-            // Using a cache dir on the home filesystem avoids the cross-fs copy entirely.-            let xdgCache = env["XDG_CACHE_HOME"].map { URL(fileURLWithPath: $0) }-                ?? FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent(".cache")-            url = xdgCache.appendingPathComponent("xtool")-            #else-            url = FileManager.default.temporaryDirectory.appendingPathComponent("sh.xtool")-            #endif-        }+        let url = XToolCache.directory          Self.pruneOrphans(in: url) 
Sources/XUtils/XToolCache.swift
diff --git a/Sources/XUtils/XToolCache.swift b/Sources/XUtils/XToolCache.swiftnew file mode 100644index 0000000..3687ad7--- /dev/null+++ b/Sources/XUtils/XToolCache.swift@@ -0,0 +1,22 @@+import Foundation++package enum XToolCache {+    package static var directory: URL {+        let env = ProcessInfo.processInfo.environment+        if let tmpdir = env["XTL_TMPDIR"] ?? env["TMPDIR"] {+            return URL(fileURLWithPath: tmpdir).appendingPathComponent("sh.xtool")+        }+        #if os(Linux)+        // On Linux, /tmp is commonly a tmpfs mount while ~/.swiftpm lives on ext4.+        // Foundation's FileManager.copyItem uses sendfile(2) internally, which returns+        // EINVAL when copying between different filesystem types (e.g. tmpfs → ext4).+        // This causes `swift sdk install` to fail on distros like Linux Mint (#181).+        // Using a cache dir on the home filesystem avoids the cross-fs copy entirely.+        let xdgCache = env["XDG_CACHE_HOME"].map { URL(fileURLWithPath: $0) }+            ?? FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent(".cache")+        return xdgCache.appendingPathComponent("xtool")+        #else+        return FileManager.default.temporaryDirectory.appendingPathComponent("sh.xtool")+        #endif+    }+}