Skip to content

armgit / xtool

Public Imported from GitHub
0

Cross-platform Xcode replacement. Build and deploy iOS apps with SwiftPM on Linux, Windows, macOS.

Package xtool as a .app on macOS (#12)

Kabir Oberai51cab9a32e7e
diff --git a/Sources/xtool/XTool.swift b/Sources/xtool/XTool.swift
index 173f4cf..916475d 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 0000000..e2b06cd
--- /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 0000000..35ad085
--- /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 0000000..360ad8d
--- /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/xtool" "$@"
diff --git a/macOS/Support/Base.xcconfig b/macOS/Support/Base.xcconfig
new file mode 100644
index 0000000..a23580f
--- /dev/null
+++ b/macOS/Support/Base.xcconfig
@@ -0,0 +1,9 @@
+GENERATE_INFOPLIST_FILE = YES
+CODE_SIGN_ENTITLEMENTS = Support/XToolMac.entitlements
+CODE_SIGN_STYLE = Automatic
+ENABLE_HARDENED_RUNTIME = YES
+
+PRODUCT_NAME = xtool
+PRODUCT_MODULE_NAME = XToolMac
+
+#include? "Private.xcconfig"
diff --git a/macOS/Support/Debug.xcconfig b/macOS/Support/Debug.xcconfig
new file mode 100644
index 0000000..99abc5a
--- /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 0000000..99abc5a
--- /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 0000000..55d9160
--- /dev/null
+++ b/macOS/Support/XToolMac.entitlements
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+ <key>keychain-access-groups</key>
+ <array>
+ <string>$(AppIdentifierPrefix)sh.xtool.keychain</string>
+ </array>
+</dict>
+</plist>
diff --git a/macOS/XToolMac/XToolMac.swift b/macOS/XToolMac/XToolMac.swift
new file mode 100644
index 0000000..a4a9700
--- /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 0000000..3dfa29f
--- /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/xtool.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<Content: View>: 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 0000000..454f350
--- /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