From 572fc897bacfbf8017c08ae558d0853cf80ae92e Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Fri, 9 May 2025 18:24:56 +0530 Subject: [PATCH] Package xtool as a .app on macOS --- Sources/xtool/XTool.swift | 4 -- macOS/.gitignore | 2 + macOS/README.md | 11 ++++ macOS/Resources/xtool | 22 ++++++++ macOS/Support/Base.xcconfig | 5 ++ macOS/Support/Debug.xcconfig | 1 + macOS/Support/Release.xcconfig | 1 + macOS/Support/XToolMac.entitlements | 10 ++++ macOS/XToolMac/XToolMac.swift | 24 +++++++++ macOS/XToolMac/XToolMacUI.swift | 79 +++++++++++++++++++++++++++++ macOS/project.yml | 27 ++++++++++ 11 files changed, 182 insertions(+), 4 deletions(-) create mode 100644 macOS/.gitignore create mode 100644 macOS/README.md create mode 100755 macOS/Resources/xtool create mode 100644 macOS/Support/Base.xcconfig create mode 100644 macOS/Support/Debug.xcconfig create mode 100644 macOS/Support/Release.xcconfig create mode 100644 macOS/Support/XToolMac.entitlements create mode 100644 macOS/XToolMac/XToolMac.swift create mode 100644 macOS/XToolMac/XToolMacUI.swift create mode 100644 macOS/project.yml diff --git a/Sources/xtool/XTool.swift b/Sources/xtool/XTool.swift index 173f4cf3..916475d0 100644 --- a/Sources/xtool/XTool.swift +++ b/Sources/xtool/XTool.swift @@ -9,13 +9,9 @@ enum XToolMain { static func main() async throws { prepareDependencies { dependencies in #warning("Improve persistence mechanism") - // for macOS, we can use KeychainStorage but we need to sign with entitlements. // for Windows, we could use dpapi.h or wincred.h. // for Linux, maybe use libsecret? // see https://github.com/atom/node-keytar -// #if os(macOS) -// dependencies.keyValueStorage = KeychainStorage(service: "com.kabiroberai.xtool-keychain.credentials") -// #endif } try await XTool.run() } diff --git a/macOS/.gitignore b/macOS/.gitignore new file mode 100644 index 00000000..e2b06cdb --- /dev/null +++ b/macOS/.gitignore @@ -0,0 +1,2 @@ +/XToolMac.xcodeproj +Private*.xcconfig diff --git a/macOS/README.md b/macOS/README.md new file mode 100644 index 00000000..35ad085b --- /dev/null +++ b/macOS/README.md @@ -0,0 +1,11 @@ +# macOS Packaging + +xtool is packaged as a .app bundle on macOS, so that we can add entitlements and use the keychain. + +Ideally we'd dogfood xtool to package xtool, but it only supports iOS apps right now. Someday, hopefully. + +## Building + +1. Install [XcodeGen](https://github.com/yonaskolb/XcodeGen) +2. Run `xcodegen` inside this (`./macOS`) directory +3. Open the Xcode project and build the `XToolMac` scheme diff --git a/macOS/Resources/xtool b/macOS/Resources/xtool new file mode 100755 index 00000000..879b210b --- /dev/null +++ b/macOS/Resources/xtool @@ -0,0 +1,22 @@ +#!/bin/bash + +# https://github.com/microsoft/vscode/blob/509cc674f24d87a298d56518c318de67bda357cb/resources/darwin/bin/code.sh + +function app_realpath() { + SOURCE="$1" + while [ -h "$SOURCE" ]; do + DIR=$(/usr/bin/dirname "$SOURCE") + SOURCE=$(/usr/bin/readlink "$SOURCE") + [[ "$SOURCE" != /* ]] && SOURCE="$DIR/$SOURCE" + done + SOURCE_DIR="$( cd -P "$( /usr/bin/dirname "$SOURCE" )" >/dev/null 2>&1 && echo "$PWD" )" + echo "${SOURCE_DIR%%${SOURCE_DIR#*.app}}" +} + +APP_PATH="$(app_realpath "${BASH_SOURCE[0]}")" +if [ -z "$APP_PATH" ]; then + echo "Unable to determine app path from symlink : ${BASH_SOURCE[0]}" + exit 1 +fi + +XTL_CLI=1 "$APP_PATH/Contents/MacOS/XToolMac" "$@" diff --git a/macOS/Support/Base.xcconfig b/macOS/Support/Base.xcconfig new file mode 100644 index 00000000..0013da48 --- /dev/null +++ b/macOS/Support/Base.xcconfig @@ -0,0 +1,5 @@ +GENERATE_INFOPLIST_FILE = YES +CODE_SIGN_ENTITLEMENTS = Support/XToolMac.entitlements +CODE_SIGN_STYLE = Automatic + +#include? "Private.xcconfig" diff --git a/macOS/Support/Debug.xcconfig b/macOS/Support/Debug.xcconfig new file mode 100644 index 00000000..99abc5a7 --- /dev/null +++ b/macOS/Support/Debug.xcconfig @@ -0,0 +1 @@ +#include "Base.xcconfig" diff --git a/macOS/Support/Release.xcconfig b/macOS/Support/Release.xcconfig new file mode 100644 index 00000000..99abc5a7 --- /dev/null +++ b/macOS/Support/Release.xcconfig @@ -0,0 +1 @@ +#include "Base.xcconfig" diff --git a/macOS/Support/XToolMac.entitlements b/macOS/Support/XToolMac.entitlements new file mode 100644 index 00000000..55d91602 --- /dev/null +++ b/macOS/Support/XToolMac.entitlements @@ -0,0 +1,10 @@ + + + + + keychain-access-groups + + $(AppIdentifierPrefix)sh.xtool.keychain + + + diff --git a/macOS/XToolMac/XToolMac.swift b/macOS/XToolMac/XToolMac.swift new file mode 100644 index 00000000..a4a97008 --- /dev/null +++ b/macOS/XToolMac/XToolMac.swift @@ -0,0 +1,24 @@ +import Foundation +import Dependencies +import XToolSupport +import XKit + +@main enum XToolMacMain { + static func main() async throws { + // the bin/xtool script invokes us with this env var, allowing us to + // detect command-line invocations vs a UI app launch + if ProcessInfo.processInfo.environment["XTL_CLI"] == "1" { + try await runXTool() + } else { + XToolMacUI.main() + } + } + + private static func runXTool() async throws { + try await withDependencies { + $0.keyValueStorage = KeychainStorage(service: "sh.xtool.keychain.credentials") + } operation: { + try await XTool.run() + } + } +} diff --git a/macOS/XToolMac/XToolMacUI.swift b/macOS/XToolMac/XToolMacUI.swift new file mode 100644 index 00000000..ce88c11f --- /dev/null +++ b/macOS/XToolMac/XToolMacUI.swift @@ -0,0 +1,79 @@ +import SwiftUI + +struct XToolMacUI: App { + var body: some Scene { + WindowGroup { + ContentView() + } + .disableWindowResizingIfPossible() + } +} + +private struct ContentView: View { + private static let command = """ + sudo ln -fs /Applications/XToolMac.app/Contents/Resources/bin/xtool /usr/local/bin/xtool + """ + + var body: some View { + VStack(alignment: .leading, spacing: 16) { + StepView(1) { + Text("Move this app into your `/Applications` folder.") + } + + StepView(2) { + VStack(alignment: .leading, spacing: 16) { + Text("Symlink xtool into a location on your `PATH`.") + + Text(Self.command) + .font(.body.monospaced()) + .textSelection(.enabled) + + Button("Copy Command") { + NSPasteboard.general.clearContents() + NSPasteboard.general.setString(Self.command, forType: .string) + } + } + } + } + .padding() + .padding(.vertical) + .frame(width: 500) + .fixedSize() + .navigationTitle("xtool") + } +} + +private struct StepView: View { + let number: Int + let content: Content + + init(_ number: Int, @ViewBuilder content: () -> Content) { + self.number = number + self.content = content() + } + + var body: some View { + HStack(alignment: .firstTextBaseline) { + Text("\(number)") + .foregroundStyle(.background) + .padding(8) + .background(.secondary, in: .circle) + + content + } + } +} + +extension Scene { + fileprivate func disableWindowResizingIfPossible() -> some Scene { + if #available(macOS 13, *) { + return self.windowResizability(.contentSize) + } else { + return self + } + } +} + +#Preview { + ContentView() +} diff --git a/macOS/project.yml b/macOS/project.yml new file mode 100644 index 00000000..454f350c --- /dev/null +++ b/macOS/project.yml @@ -0,0 +1,27 @@ +name: XToolMac +options: + bundleIdPrefix: sh.xtool +packages: + xtool: + path: .. + group: "" +fileGroups: + - Support +targets: + XToolMac: + type: application + platform: macOS + deploymentTarget: "12.0" + sources: + - path: XToolMac + - path: Resources/xtool + buildPhase: + copyFiles: + destination: resources + subpath: bin + configFiles: + Debug: Support/Debug.xcconfig + Release: Support/Release.xcconfig + dependencies: + - package: xtool + product: XToolSupport