mirror of
https://github.com/xtool-org/xtool.git
synced 2026-11-17 04:51:56 +03:00
Package xtool as a .app on macOS
This commit is contained in:
@@ -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()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
/XToolMac.xcodeproj
|
||||
Private*.xcconfig
|
||||
@@ -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
|
||||
Executable
+22
@@ -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" "$@"
|
||||
@@ -0,0 +1,5 @@
|
||||
GENERATE_INFOPLIST_FILE = YES
|
||||
CODE_SIGN_ENTITLEMENTS = Support/XToolMac.entitlements
|
||||
CODE_SIGN_STYLE = Automatic
|
||||
|
||||
#include? "Private.xcconfig"
|
||||
@@ -0,0 +1 @@
|
||||
#include "Base.xcconfig"
|
||||
@@ -0,0 +1 @@
|
||||
#include "Base.xcconfig"
|
||||
@@ -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>
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<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()
|
||||
}
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user