Add an Apple Watch app that can control the glasses

This commit is contained in:
James Babcock committed 2026-11-04 02:20:26 -08:00
1 parent c896ce9ac8
commit 2696f3b76c
46 files changed
+3877 -88

No files matched your search

+15
View File
@@ -0,0 +1,15 @@
#!/bin/bash
# Prints the UDID of the first real Apple Watch devicectl can see, if any. A UDID
# works both as a devicectl --device and as an xcodebuild destination id.
set -euo pipefail
output="$(mktemp)"
trap 'rm -f "$output"' EXIT
xcrun devicectl list devices --quiet --json-output "$output" >/dev/null
node -e '
const devices = JSON.parse(require("fs").readFileSync(process.argv[1], "utf8")).result.devices;
// Xcode 27 lists simulators too and moved these fields into `properties`. `reality`
// is absent on a real device until it has connected, so only "simulated" rules one out.
const hardware = device => device.properties?.hardware ?? device.hardwareProperties ?? {};
const watch = devices.map(hardware).find(h => h.platform === "watchOS" && h.reality !== "simulated");
if (watch) console.log(watch.udid);
' "$output"
+69
View File
@@ -0,0 +1,69 @@
// Ties the Apple Watch app (apple-watch/) to the iOS build. WatchConnectivity
// only links the two apps when the phone app's bundle carries the watch app in
// Watch/, so a phone build that wants a working watch must embed it.
//
// Embedding is opt-in (FACECLAW_EMBED_WATCH=1 in build_paths.sh): signing the
// result needs a provisioning profile for the watch app, which can't be made
// for a team with no watch registered, so it would break everyone else's builds.
//
// node scripts/apple-watch-embed.cjs config Write apple-watch/Local.xcconfig.
// node scripts/apple-watch-embed.cjs sync Make platforms/ios match the setting.
const { execFileSync } = require('node:child_process');
const fs = require('node:fs');
const path = require('node:path');
const root = path.resolve(__dirname, '..');
const pbxproj = path.join(root, 'platforms/ios/faceclaw.xcodeproj/project.pbxproj');
// The CLI drops `-sdk iphoneos` from xcodebuild, which would otherwise force
// the watch target onto the iOS SDK, only if this directory exists. Its own watch
// support (the legacy two-target layout) stays inert without a `watchextension`.
const cliWatchMarker = path.join(root, 'App_Resources/iOS/watchapp');
function embedEnabled() {
return process.env.FACECLAW_EMBED_WATCH === '1';
}
/** Xcode's own builds of the watch project read this too, so they sign the same way. */
function writeLocalConfig() {
const version = /FACECLAW_VERSION = "(.*)"/.exec(fs.readFileSync(path.join(root, 'app/version.ts'), 'utf8'))[1];
const lines = [
`FACECLAW_IOS_BUNDLE_ID = ${process.env.IOS_BUNDLE_ID || 'com.faceclaw.app'}`,
// An embedded watch app has to carry the phone app's version.
`MARKETING_VERSION = ${version}`,
];
if (process.env.IOS_TEAM_ID) lines.push(`DEVELOPMENT_TEAM = ${process.env.IOS_TEAM_ID}`);
fs.writeFileSync(path.join(root, 'apple-watch/Local.xcconfig'), `${lines.join('\n')}\n`);
}
function syncEmbed() {
const enabled = embedEnabled();
if (enabled) writeLocalConfig();
// Without the setting there's normally nothing to undo, and no need for Ruby.
const embedded = fs.existsSync(pbxproj) && fs.readFileSync(pbxproj, 'utf8').includes('Embed Watch Content');
if (enabled !== embedded && fs.existsSync(pbxproj)) {
execFileSync('ruby', [path.join(__dirname, 'embed-apple-watch.rb'), enabled ? 'embed' : 'remove'], { cwd: root, stdio: 'inherit' });
}
// Last: a project that still embeds the watch app can't build without this.
if (enabled) {
fs.mkdirSync(cliWatchMarker, { recursive: true });
} else {
fs.rmSync(cliWatchMarker, { recursive: true, force: true });
// Xcode leaves what it copied in earlier builds, which would still embed the old app.
const buildDir = path.join(root, 'platforms/ios/build');
for (const config of fs.existsSync(buildDir) ? fs.readdirSync(buildDir) : []) {
fs.rmSync(path.join(buildDir, config, 'faceclaw.app/Watch'), { recursive: true, force: true });
}
}
}
module.exports = { embedEnabled, syncEmbed, writeLocalConfig };
if (require.main === module) {
const command = process.argv[2];
if (command === 'config') writeLocalConfig();
else if (command === 'sync') syncEmbed();
else {
console.error('usage: apple-watch-embed.cjs config|sync');
process.exit(2);
}
}
+50
View File
@@ -0,0 +1,50 @@
#!/usr/bin/env ruby
# Adds apple-watch/Faceclaw.xcodeproj to the generated iOS project as a subproject
# and embeds its app in the phone app's Watch/ folder, or takes all that back out.
# See apple-watch-embed.cjs, which decides when to run this.
require 'xcodeproj'
PHASE_NAME = 'Embed Watch Content'
WATCH_TARGET = 'Faceclaw'
root = File.expand_path('..', __dir__)
watch_project_path = File.join(root, 'apple-watch/Faceclaw.xcodeproj')
project = Xcodeproj::Project.open(File.join(root, 'platforms/ios/faceclaw.xcodeproj'))
app = project.targets.find { |target| target.name == 'faceclaw' } or abort 'No faceclaw target in the iOS project'
def remove_embed(project, app, watch_project_path)
app.copy_files_build_phases.select { |phase| phase.name == PHASE_NAME }.each do |phase|
phase.files.to_a.each(&:remove_from_project)
phase.remove_from_project
end
app.dependencies.select { |dependency| dependency.display_name == WATCH_TARGET }.each(&:remove_from_project)
reference = project.reference_for_path(watch_project_path)
return unless reference
# Saving fails on any proxy still pointing at the subproject.
project.objects.select { |object| object.isa == 'PBXContainerItemProxy' && object.container_portal == reference.uuid }
.each(&:remove_from_project)
entries = project.root_object.project_references
entries.select { |entry| entry[:project_ref] == reference }.each do |entry|
entry[:product_group].remove_from_project
entries.delete(entry)
end
reference.remove_from_project
end
remove_embed(project, app, watch_project_path)
if ARGV[0] == 'embed'
reference = project.main_group.new_file(watch_project_path)
watch_target = Xcodeproj::Project.open(watch_project_path).targets.find { |target| target.name == WATCH_TARGET }
app.add_dependency(watch_target)
entry = project.root_object.project_references.find { |candidate| candidate[:project_ref] == reference }
product = entry[:product_group].children.find { |child| child.path == "#{WATCH_TARGET}.app" } or abort 'The watch project has no app product'
phase = app.new_copy_files_build_phase(PHASE_NAME)
phase.symbol_dst_subfolder_spec = :products_directory
phase.dst_path = '$(CONTENTS_FOLDER_PATH)/Watch'
phase.add_file_reference(product).settings = { 'ATTRIBUTES' => ['RemoveHeadersOnCopy'] }
end
project.save
@@ -0,0 +1,6 @@
const { syncEmbed } = require('../../scripts/apple-watch-embed.cjs');
// After prepare, because that's when the iOS project and its pods are finished.
module.exports = function (hookArgs) {
if (hookArgs.prepareData.platform === 'ios') syncEmbed();
};
+1
View File
@@ -8,6 +8,7 @@ const path = require('node:path');
const root = path.resolve(__dirname, '..');
const hooks = [
{ source: 'scripts/hooks/faceclaw-kotlin.before-prepare.js', target: 'hooks/before-prepare/faceclaw-kotlin.js' },
{ source: 'scripts/hooks/faceclaw-watch.after-prepare.js', target: 'hooks/after-prepare/faceclaw-watch.js' },
];
function installHooks() {