Skip to content

armgit / cra

Public Imported from GitHub
0

The open-source Arc replacement for macOS. Spaces, pinned tabs, split view, Little Windows and Arc's shortcuts on Chromium, with your Arc sidebar imported in one go.

Keep CraDev profiles isolated and refresh bundled engine builds (#13)

Quinn Battersbycb379ba7af08
diff --git a/Sources/Cra/Services/Persistence.swift b/Sources/Cra/Services/Persistence.swift
index 5821fb4..fc93f1e 100644
--- a/Sources/Cra/Services/Persistence.swift
+++ b/Sources/Cra/Services/Persistence.swift
@@ -23,9 +23,12 @@ enum ProfileCleanup {
enum AppPaths {
static let supportDir: URL = {
// CRA_DATA_DIR runs a second, independent Cra (testing next to the installed one).
+ // Finder launches have no shell environment. The dev bundle must still stay
+ // out of the signed application's profile when opened directly.
+ let folder = Bundle.main.bundleIdentifier == "dk.kildahl.cra.dev" ? "CraDev" : "Cra"
let base = ProcessInfo.processInfo.environment["CRA_DATA_DIR"].map { URL(fileURLWithPath: $0, isDirectory: true) }
?? FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0]
- .appendingPathComponent("Cra", isDirectory: true)
+ .appendingPathComponent(folder, isDirectory: true)
try? FileManager.default.createDirectory(at: base, withIntermediateDirectories: true)
return base
}()
diff --git a/Sources/CraCEF/CraCEF.mm b/Sources/CraCEF/CraCEF.mm
index ddf0ef7..1b9c9a2 100644
--- a/Sources/CraCEF/CraCEF.mm
+++ b/Sources/CraCEF/CraCEF.mm
@@ -52,6 +52,8 @@ static std::atomic<int> g_liveBrowsers{0};
static bool g_quitting = false;
static bool g_useChromeStyle = false;
static bool g_useMockKeychain = false;
+/// Resolved by Swift from the chosen profile, including environment-free dev launches.
+static NSString* g_extensionsDirectory = nil;
static BOOL g_chromeHosting = NO;
static NSMutableArray<CraBrowser*>* g_pendingCreation = nil;
static void (^g_relaunchHandler)(NSArray<NSString*>*) = nil;
@@ -454,10 +456,9 @@ public:
command_line->AppendSwitch("no-first-run");
// Unpacked Chrome extensions (Chrome-style browsers only): one folder per extension.
if (process_type.empty()) {
- // Same folder as ExtensionStore (AppPaths.supportDir/Extensions).
- NSString* dataDir = NSProcessInfo.processInfo.environment[@"CRA_DATA_DIR"]
- ?: [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/Cra"];
- NSString* dir = [dataDir stringByAppendingPathComponent:@"Extensions"];
+ // Use the same resolved data root as AppPaths/ExtensionStore, never an
+ // independent production fallback when CraDev is opened from Finder.
+ NSString* dir = g_extensionsDirectory;
NSMutableArray<NSString*>* paths = [NSMutableArray array];
for (NSString* name in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dir error:nil]) {
NSString* p = [dir stringByAppendingPathComponent:name];
@@ -1958,6 +1959,7 @@ static void MarkProfileExitedCleanly(NSString* profileDir) {
if (g_libraryLoader) return YES;
g_useChromeStyle = useChromeStyle;
g_useMockKeychain = useMockKeychain;
+ g_extensionsDirectory = [[cachePath stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Extensions"];
g_libraryLoader = new CefScopedLibraryLoader();
if (!g_libraryLoader->LoadInMain()) {
NSLog(@"CraCEF: failed to load Chromium Embedded Framework");
diff --git a/docs/development.md b/docs/development.md
index 1d4f08d..8defc0a 100644
--- a/docs/development.md
+++ b/docs/development.md
@@ -39,6 +39,20 @@ it to start over.
## Running a test instance
+`scripts/dev.sh` builds a separate `CraDev.app`, including Sparkle, without a production
+update feed or URL registrations. The script runs it on `build/dev-data` (set `CRA_DATA_DIR`
+for another profile); opened directly from Finder, CraDev uses `~/Library/Application
+Support/CraDev`, never the installed Cra's profile. Ad-hoc development builds use a mock
+encryption key for fresh profiles; use test data and do not copy a daily browser profile
+or real credentials into them.
+
+The dev bundle records the CEF download source and Sparkle version. Changing either
+pin refreshes the bundled frameworks, including an engine rebuild with the same CEF
+version but a new release suffix. The watcher notices changes to both fetch scripts.
+Compilation happens before the running dev instance stops; bundle replacement happens
+only after it has quit. Quit any separately launched CraDev before starting another dev
+script, so it cannot overwrite a framework still in use.
+
Cra is single-instance and the installed copy receives every `cra://` URL, so test a
build next to your daily Cra with its own data folder and a command file:
diff --git a/scripts/dev.sh b/scripts/dev.sh
index 065a3fe..30599dd 100755
--- a/scripts/dev.sh
+++ b/scripts/dev.sh
@@ -6,7 +6,7 @@
# CRA_DATA_DIR=<dir> scripts/dev.sh use another profile (default build/dev-data)
# CraDev is its own app (executable name, bundle id, profile), so it runs next to the
# installed Cra, `killall Cra` elsewhere leaves it alone, and it never claims cra:// or
-# http links. CEF is copied and signed once; each rebuild only swaps the Cra binaries,
+# http links. CEF is copied and signed when its pinned source changes; ordinary rebuilds swap the Cra binaries,
# in a separate SwiftPM scratch dir so parallel `scripts/build.sh` runs don't collide.
set -euo pipefail
cd "$(dirname "$0")/.."
@@ -23,20 +23,27 @@ PID=""
HELPERS=("|" " (Alerts)|.alerts" " (GPU)|.gpu" " (Plugin)|.plugin" " (Renderer)|.renderer")
bootstrap() {
- echo "Creating $APP (one time)..."
- rm -rf "$APP"
- mkdir -p "$APP/Contents/MacOS" "$APP/Contents/Resources" "$APP/Contents/Frameworks"
+ echo "Creating $APP with the current CEF and Sparkle builds..."
+ rm -rf "$APP" || return 1
+ mkdir -p "$APP/Contents/MacOS" "$APP/Contents/Resources" "$APP/Contents/Frameworks" || return 1
sed -e "s/__VERSION__/0.0.0-dev/" -e "s/__BUILD__/0/" resources/Info.plist > "$APP/Contents/Info.plist"
$PB -c "Set :CFBundleExecutable $NAME" -c "Set :CFBundleIdentifier dk.kildahl.cra.dev" \
-c "Set :CFBundleName $NAME" -c "Set :CFBundleDisplayName $NAME" \
-c "Delete :CFBundleURLTypes" -c "Delete :CFBundleDocumentTypes" \
-c "Delete :UTExportedTypeDeclarations" "$APP/Contents/Info.plist"
+ $PB -c "Delete :SUFeedURL" "$APP/Contents/Info.plist"
echo -n "APPL????" > "$APP/Contents/PkgInfo"
[ -f resources/AppIcon.icns ] && cp resources/AppIcon.icns "$APP/Contents/Resources/AppIcon.icns"
local fw="$APP/Contents/Frameworks/Chromium Embedded Framework.framework"
- cp -R "$ROOT/vendor/cef/Release/Chromium Embedded Framework.framework" "$APP/Contents/Frameworks/"
- find "$fw/Libraries" -name '*.dylib' -exec codesign --force --sign - {} \; 2>/dev/null
- codesign --force --sign - "$fw"
+ cp -R "$ROOT/vendor/cef/Release/Chromium Embedded Framework.framework" "$APP/Contents/Frameworks/" || return 1
+ find "$fw/Libraries" -name '*.dylib' -exec codesign --force --sign - {} \; 2>/dev/null || return 1
+ codesign --force --sign - "$fw" || return 1
+ ditto "$ROOT/vendor/sparkle/Sparkle.framework" "$APP/Contents/Frameworks/Sparkle.framework" || return 1
+ local sparkle="$APP/Contents/Frameworks/Sparkle.framework"
+ rm -rf "$sparkle/Versions/B/XPCServices" "$sparkle/XPCServices"
+ codesign --force --sign - "$sparkle/Versions/B/Autoupdate" || return 1
+ codesign --force --sign - "$sparkle/Versions/B/Updater.app" || return 1
+ codesign --force --sign - "$sparkle" || return 1
for entry in "${HELPERS[@]}"; do
local ns="${entry%%|*}" ps="${entry#*|}"
local helper="$APP/Contents/Frameworks/$NAME Helper$ns.app"
@@ -46,27 +53,52 @@ bootstrap() {
resources/Helper-Info.plist > "$helper/Contents/Info.plist"
echo -n "APPL????" > "$helper/Contents/PkgInfo"
done
+ cp "$ROOT/vendor/cef/.cra-source" "$APP/Contents/Resources/cef-source" || return 1
+ cp "$ROOT/vendor/sparkle/VERSION" "$APP/Contents/Resources/sparkle-version" || return 1
}
-# Builds and swaps changed binaries into the bundle. Returns non-zero on a compile error.
+# A separately launched CraDev (or a second watch process) must be quit before its
+# bundle is changed. Match helper names too: they may briefly outlive the main app.
+dev_app_running() {
+ ps -axo comm= | awk -F/ -v name="$NAME" \
+ '$NF == name || index($NF, name " Helper") == 1 { found = 1 } END { exit !found }'
+}
+
+# Compile while the current app is usable, then stop it before changing its bundle.
build() {
mkdir -p "$SCRATCH"
touch "$STAMP"
+ scripts/fetch-cef.sh >/dev/null || return 1
+ scripts/fetch-sparkle.sh >/dev/null || return 1
swift build -c debug --scratch-path "$SCRATCH" || return 1
local bin
- bin="$(swift build -c debug --scratch-path "$SCRATCH" --show-bin-path)"
- [ -d "$APP/Contents/Frameworks/$NAME Helper.app" ] || bootstrap
+ bin="$(swift build -c debug --scratch-path "$SCRATCH" --show-bin-path)" || return 1
+ stop
+ for _ in $(seq 20); do dev_app_running || break; sleep 0.1; done
+ if dev_app_running; then
+ echo "Quit the separately running CraDev before rebuilding its app bundle." >&2
+ return 1
+ fi
+ if [ ! -d "$APP/Contents/Frameworks/$NAME Helper.app" ] || \
+ [ ! -f "$APP/Contents/Frameworks/Chromium Embedded Framework.framework/Chromium Embedded Framework" ] || \
+ [ ! -d "$APP/Contents/Frameworks/Sparkle.framework" ] || \
+ ! cmp -s "$ROOT/vendor/cef/.cra-source" "$APP/Contents/Resources/cef-source" || \
+ ! cmp -s "$ROOT/vendor/sparkle/VERSION" "$APP/Contents/Resources/sparkle-version"; then
+ bootstrap || return 1
+ fi
for entry in "${HELPERS[@]}"; do
local ns="${entry%%|*}"
local helper="$APP/Contents/Frameworks/$NAME Helper$ns.app"
if ! cmp -s "$bin/CraHelper" "$helper/Contents/MacOS/$NAME Helper$ns"; then
- cp "$bin/CraHelper" "$helper/Contents/MacOS/$NAME Helper$ns"
- codesign --force --sign - --entitlements resources/CraHelper.entitlements "$helper"
+ cp "$bin/CraHelper" "$helper/Contents/MacOS/$NAME Helper$ns" || return 1
+ codesign --force --sign - --entitlements resources/CraHelper.entitlements "$helper" || return 1
fi
done
- cp "$bin/Cra" "$APP/Contents/MacOS/$NAME"
+ cp "$bin/Cra" "$APP/Contents/MacOS/$NAME" || return 1
for d in web brand blocklist browsers app-icons; do
- [ -d "resources/$d" ] && rsync -a --delete "resources/$d/" "$APP/Contents/Resources/$d/"
+ if [ -d "resources/$d" ]; then
+ rsync -a --delete "resources/$d/" "$APP/Contents/Resources/$d/" || return 1
+ fi
done
codesign --force --sign - --entitlements resources/Cra.entitlements "$APP"
}
@@ -89,17 +121,25 @@ launch() {
}
changed() {
+ [ scripts/fetch-cef.sh -nt "$STAMP" ] || [ scripts/fetch-sparkle.sh -nt "$STAMP" ] || \
[ -n "$(find Sources resources Package.swift -newer "$STAMP" -type f \
\( -name '*.swift' -o -name '*.m' -o -name '*.mm' -o -name '*.h' -o -path 'resources/*' \) \
-print -quit 2>/dev/null)" ]
}
-scripts/fetch-cef.sh >/dev/null
trap 'stop; exit 0' INT TERM
trap stop EXIT
-build || { [ "${1:-}" = "--once" ] && exit 1; echo "✗ Build failed; fix it and save to retry."; }
-[ -x "$APP/Contents/MacOS/$NAME" ] && launch
+if dev_app_running; then
+ echo "Quit the separately running CraDev before rebuilding its app bundle." >&2
+ exit 1
+fi
+if build; then
+ launch
+else
+ [ "${1:-}" = "--once" ] && exit 1
+ echo "✗ Build failed; fix it and save to retry."
+fi
if [ "${1:-}" = "--once" ]; then wait "$PID"; exit 0; fi
echo "Watching Sources/ and resources/ (Ctrl-C to quit)"
@@ -110,10 +150,9 @@ while true; do
echo "↻ Change detected, rebuilding..."
start=$(date +%s)
if build; then
- stop
launch
echo "✓ Relaunched in $(( $(date +%s) - start ))s"
else
- echo "✗ Build failed; $NAME keeps running the last good build."
+ echo "✗ Rebuild failed; fix the error and save to retry."
fi
done