diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..26d7deb --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,33 @@ +name: CI + +on: + push: + branches: [main] + pull_request: + +permissions: + contents: read + +jobs: + build-and-check: + runs-on: macos-15 + steps: + - uses: actions/checkout@v4 + + - name: Lint build script + run: | + bash -n build.sh + command -v shellcheck >/dev/null 2>&1 || brew install shellcheck + shellcheck -S error build.sh + + - name: Validate Info.plist + run: plutil -lint Info.plist + + - name: Typecheck make_icon.swift + run: swiftc -typecheck -swift-version 5 -warn-concurrency make_icon.swift + + - name: Build and verify app bundle + run: | + ./build.sh --build-only + codesign --verify --deep --strict build/TwoUp.app + test -s build/TwoUp.app/Contents/Resources/TwoUp.icns diff --git a/.gitignore b/.gitignore index 0197728..761d660 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ build/ +.DS_Store .impeccable/ .claude/ diff --git a/Info.plist b/Info.plist index 76f3f0c..9e34f6e 100644 --- a/Info.plist +++ b/Info.plist @@ -2,6 +2,7 @@ + CFBundleInfoDictionaryVersion6.0 CFBundleNameTwoUp CFBundleDisplayNameTwoUp CFBundleIdentifiercom.mclemore.twoup @@ -10,9 +11,16 @@ CFBundlePackageTypeAPPL CFBundleShortVersionString1.0 CFBundleVersion1 + LSApplicationCategoryTypepublic.app-category.news LSMinimumSystemVersion13.0 NSHighResolutionCapable + NSHumanReadableCopyrightCopyright © 2026 Will McLemore NSPrincipalClassNSApplication NSAppleEventsUsageDescriptionTwoUp reads the article in your browser's current tab. + NSAppTransportSecurity + + NSAllowsArbitraryLoadsInWebContent + + diff --git a/Resources/TwoUp.icns b/Resources/TwoUp.icns deleted file mode 100644 index 0008ea8..0000000 Binary files a/Resources/TwoUp.icns and /dev/null differ diff --git a/build.sh b/build.sh index 7e3c1f7..a2769c4 100755 --- a/build.sh +++ b/build.sh @@ -1,29 +1,65 @@ -#!/bin/zsh -# Builds TwoUp.app and installs it to ~/Applications. +#!/usr/bin/env bash +# Builds TwoUp.app and (unless --build-only is passed) installs it to ~/Applications. set -euo pipefail -cd "$(dirname "$0")" -APP=build/TwoUp.app -rm -rf build && mkdir -p "$APP/Contents/MacOS" "$APP/Contents/Resources" - -if [[ ! -f Resources/TwoUp.icns ]]; then - swiftc -O make_icon.swift -o build/make_icon - ./build/make_icon build/icon.png - mkdir -p build/TwoUp.iconset - for s in 16 32 128 256 512; do - sips -z $s $s build/icon.png --out build/TwoUp.iconset/icon_${s}x${s}.png >/dev/null - sips -z $((s*2)) $((s*2)) build/icon.png --out build/TwoUp.iconset/icon_${s}x${s}@2x.png >/dev/null - done - iconutil -c icns build/TwoUp.iconset -o Resources/TwoUp.icns + +BUILD_ONLY=false +while [[ $# -gt 0 ]]; do + case "$1" in + --build-only) + BUILD_ONLY=true + shift + ;; + -h|--help) + echo "Usage: ./build.sh [--build-only]" + exit 0 + ;; + *) + echo "Unknown option: $1" >&2 + echo "Usage: ./build.sh [--build-only]" >&2 + exit 1 + ;; + esac +done + +cd "$(dirname "${BASH_SOURCE[0]}")" + +SIGN_IDENTITY="${SIGN_IDENTITY:--}" +APP="build/TwoUp.app" + +plutil -lint Info.plist >/dev/null + +rm -rf build/TwoUp.app build/TwoUp.icns build/TwoUp.iconset +mkdir -p "$APP/Contents/MacOS" "$APP/Contents/Resources" + +swift make_icon.swift build/TwoUp.icns + +SWIFT_SOURCES=(main.swift) +if [[ -f Core.swift ]]; then + SWIFT_SOURCES=(Core.swift main.swift) fi -swiftc -O -swift-version 5 main.swift -o "$APP/Contents/MacOS/TwoUp" -framework Cocoa -framework WebKit +swiftc -O -swift-version 5 "${SWIFT_SOURCES[@]}" -o "$APP/Contents/MacOS/TwoUp" -framework Cocoa -framework WebKit + cp Info.plist "$APP/Contents/Info.plist" cp Resources/* "$APP/Contents/Resources/" -codesign --force --sign - "$APP" +cp build/TwoUp.icns "$APP/Contents/Resources/TwoUp.icns" + +codesign --force --deep --sign "$SIGN_IDENTITY" "$APP" +codesign --verify --deep --strict "$APP" + +if [[ "$BUILD_ONLY" == "true" ]]; then + echo "Built $APP" + exit 0 +fi +mkdir -p "$HOME/Applications" DEST="$HOME/Applications/TwoUp.app" if [[ -d "$DEST" ]]; then - if command -v trash >/dev/null; then trash "$DEST"; else rm -rf "$DEST"; fi + if command -v trash >/dev/null 2>&1; then + trash "$DEST" + else + rm -rf "$DEST" + fi fi cp -R "$APP" "$DEST" echo "Installed $DEST" diff --git a/docs/screenshot.png b/docs/screenshot.png index 150472d..b2cc724 100644 Binary files a/docs/screenshot.png and b/docs/screenshot.png differ diff --git a/make_icon.swift b/make_icon.swift index 1a32981..a1f4ef5 100644 --- a/make_icon.swift +++ b/make_icon.swift @@ -1,23 +1,143 @@ import Cocoa + // Draws the app icon: an open two-page spread on a dark tile. -let size: CGFloat = 1024 -let img = NSImage(size: NSSize(width: size, height: size)) -img.lockFocus() -NSColor(calibratedRed: 0.13, green: 0.12, blue: 0.11, alpha: 1).setFill() -NSBezierPath(roundedRect: NSRect(x: 100, y: 100, width: 824, height: 824), xRadius: 185, yRadius: 185).fill() -let paper = NSColor(calibratedRed: 0.98, green: 0.96, blue: 0.91, alpha: 1) -let ink = NSColor(calibratedRed: 0.55, green: 0.52, blue: 0.46, alpha: 1) -for x in [222.0, 522.0] { - paper.setFill() - NSBezierPath(roundedRect: NSRect(x: x, y: 250, width: 280, height: 524), xRadius: 14, yRadius: 14).fill() - ink.setFill() - var y = 690.0 - while y > 300 { - let w = (y < 340 && x > 500) ? 120.0 : 216.0 - NSBezierPath(roundedRect: NSRect(x: x + 32, y: y, width: w, height: 14), xRadius: 7, yRadius: 7).fill() - y -= 38 +enum IconError: LocalizedError { + case bitmapCreationFailed(Int) + case graphicsContextFailed(Int) + case pngEncodingFailed(Int) + case iconutilFailed(Int32, String) + + var errorDescription: String? { + switch self { + case .bitmapCreationFailed(let px): + return "Failed to allocate \(px)x\(px) bitmap image rep." + case .graphicsContextFailed(let px): + return "Failed to create graphics context for \(px)x\(px) icon." + case .pngEncodingFailed(let px): + return "Failed to encode \(px)x\(px) icon as PNG." + case .iconutilFailed(let status, let stderr): + return "iconutil failed with exit code \(status)\(stderr.isEmpty ? "" : ": \(stderr)")." + } } } -img.unlockFocus() -let rep = NSBitmapImageRep(data: img.tiffRepresentation!)! -try! rep.representation(using: .png, properties: [:])!.write(to: URL(fileURLWithPath: CommandLine.arguments[1])) + +func renderIconPNG(pixels px: Int) throws -> Data { + guard let rep = NSBitmapImageRep( + bitmapDataPlanes: nil, + pixelsWide: px, + pixelsHigh: px, + bitsPerSample: 8, + samplesPerPixel: 4, + hasAlpha: true, + isPlanar: false, + colorSpaceName: .calibratedRGB, + bytesPerRow: 0, + bitsPerPixel: 0 + ) else { + throw IconError.bitmapCreationFailed(px) + } + + guard let ctx = NSGraphicsContext(bitmapImageRep: rep) else { + throw IconError.graphicsContextFailed(px) + } + + NSGraphicsContext.saveGraphicsState() + NSGraphicsContext.current = ctx + defer { NSGraphicsContext.restoreGraphicsState() } + + ctx.shouldAntialias = true + ctx.imageInterpolation = .high + let scale = CGFloat(px) / 1024.0 + ctx.cgContext.scaleBy(x: scale, y: scale) + + NSColor(calibratedRed: 0.13, green: 0.12, blue: 0.11, alpha: 1).setFill() + NSBezierPath(roundedRect: NSRect(x: 100, y: 100, width: 824, height: 824), xRadius: 185, yRadius: 185).fill() + + let paper = NSColor(calibratedRed: 0.98, green: 0.96, blue: 0.91, alpha: 1) + let ink = NSColor(calibratedRed: 0.55, green: 0.52, blue: 0.46, alpha: 1) + for x in [222.0, 522.0] { + paper.setFill() + NSBezierPath(roundedRect: NSRect(x: x, y: 250, width: 280, height: 524), xRadius: 14, yRadius: 14).fill() + ink.setFill() + var y = 690.0 + while y > 300 { + let w = (y < 340 && x > 500) ? 120.0 : 216.0 + NSBezierPath(roundedRect: NSRect(x: x + 32, y: y, width: w, height: 14), xRadius: 7, yRadius: 7).fill() + y -= 38 + } + } + + guard let pngData = rep.representation(using: .png, properties: [:]) else { + throw IconError.pngEncodingFailed(px) + } + return pngData +} + +func buildICNS(at outputURL: URL) throws { + let fm = FileManager.default + let iconsetURL = fm.temporaryDirectory + .appendingPathComponent("TwoUp-\(ProcessInfo.processInfo.globallyUniqueString).iconset", isDirectory: true) + try fm.createDirectory(at: iconsetURL, withIntermediateDirectories: true) + defer { try? fm.removeItem(at: iconsetURL) } + + let sizes: [(name: String, pixels: Int)] = [ + ("icon_16x16.png", 16), + ("icon_16x16@2x.png", 32), + ("icon_32x32.png", 32), + ("icon_32x32@2x.png", 64), + ("icon_128x128.png", 128), + ("icon_128x128@2x.png", 256), + ("icon_256x256.png", 256), + ("icon_256x256@2x.png", 512), + ("icon_512x512.png", 512), + ("icon_512x512@2x.png", 1024), + ] + + for entry in sizes { + let data = try renderIconPNG(pixels: entry.pixels) + try data.write(to: iconsetURL.appendingPathComponent(entry.name), options: .atomic) + } + + let process = Process() + process.executableURL = URL(fileURLWithPath: "/usr/bin/iconutil") + process.arguments = ["-c", "icns", iconsetURL.path, "-o", outputURL.path] + let stderrPipe = Pipe() + process.standardError = stderrPipe + try process.run() + let stderrData = stderrPipe.fileHandleForReading.readDataToEndOfFile() + process.waitUntilExit() + + if process.terminationStatus != 0 { + let message = String(data: stderrData, encoding: .utf8)? + .trimmingCharacters(in: .whitespacesAndNewlines) ?? "" + throw IconError.iconutilFailed(process.terminationStatus, message) + } +} + +guard CommandLine.arguments.count == 2 else { + fputs("Usage: swift make_icon.swift \n", stderr) + exit(1) +} + +let outputArg = CommandLine.arguments[1] +if outputArg.isEmpty || outputArg == "-h" || outputArg == "--help" { + fputs("Usage: swift make_icon.swift \n", stderr) + exit(outputArg == "-h" || outputArg == "--help" ? 0 : 1) +} + +let outputURL = URL(fileURLWithPath: outputArg) +do { + try FileManager.default.createDirectory( + at: outputURL.deletingLastPathComponent(), + withIntermediateDirectories: true + ) + if outputURL.pathExtension.lowercased() == "png" { + let data = try renderIconPNG(pixels: 1024) + try data.write(to: outputURL, options: .atomic) + } else { + try buildICNS(at: outputURL) + } +} catch { + fputs("make_icon.swift: \(error.localizedDescription)\n", stderr) + exit(1) +}