Skip to content

Instantly share code, notes, and snippets.

View drewmccormack's full-sized avatar

Drew McCormack drewmccormack

View GitHub Profile
@drewmccormack
drewmccormack / SerialMacro.swift
Created July 26, 2024 11:49
A macro that can be applied to an async func to generate a peer func (serial_<funcname>) that uses an internal queue to serialize execution. That is, it guarantees that only one copy of the func will run at a time. (Note that this can deadlock if it reenters.)
import SwiftSyntax
import SwiftSyntaxMacros
import SwiftCompilerPlugin
public enum SerialError: Error, CustomStringConvertible {
case invalidInput
case invalidOutput
public var description: String {
switch self {
@drewmccormack
drewmccormack / AsyncSerialQueue.swift
Last active July 24, 2024 17:17
Proof of concept for a serializing queue for async funcs in Swift. It orders the calls, but also ensures each call is executed to completion before the next starts, thus avoiding interleaving races which can arise in multiple calls to an async func.
/// A queue that executes async functions in order, and atomically.
/// That is, each enqueued func executes fully before the next one is started.
struct AsyncSerialQueue {
typealias Block = () async throws -> [Int]
private struct Item {
let block: Block
let continuation: CheckedContinuation<[Int], Error>
}
@drewmccormack
drewmccormack / UpdateLocalizations.swift
Last active September 26, 2023 08:36
Using the AppStoreConnect_Swift_SDK package to update localizations on App Store Connect.
import AppStoreConnect_Swift_SDK
func updateMetadata(forBundleID bundleID: String) async throws {
let configuration = try! APIConfiguration(issuerID: "...", privateKeyID: "...", privateKey: "...")
let provider = APIProvider(configuration: configuration)
// Get app
let appRequest = APIEndpoint.v1
.apps
.get(parameters: .init(filterBundleID: [bundleID], include: [.appInfos, .appStoreVersions]))
@drewmccormack
drewmccormack / Queue.swift
Created September 29, 2022 07:02
A Queue (FIFO) built from an actor.
actor Queue {
private var tasks: [() async -> Void] = []
func enqueue(_ task: @escaping () async -> Void) {
tasks.append(task)
Task { await runNext() }
}
private func runNext() async {
guard !tasks.isEmpty else { return }
@drewmccormack
drewmccormack / LabelUniqueSet.swift
Created August 10, 2021 09:33
Introduces a Set type that stores enums, including those with associated types, and uniques on the label rather than the value.
struct Label: Hashable {
var label: String
}
protocol LabelUniquing {
var label: Label { get }
}
extension LabelUniquing {
//
// Lightweight but powerful state machine.
//
// Usage:
// enum TrafficLight: State {
// case red, orange, green
// }
//
// var trafficLights = StateMachine<TrafficLight>(initialState: .red)
// trafficLights.addRoute(from: .red, to: .green)
@drewmccormack
drewmccormack / MVVMSwiftUI.swift
Created February 25, 2020 18:39
Simple example of MMVM with SwiftUI.
import Foundation
import SwiftUI
import Combine
let dateFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
return dateFormatter
}()
@drewmccormack
drewmccormack / extract-glossaries.py
Created February 19, 2020 14:21
Extracts terms from Apple lg glossary files, combining into a JSON file
#!/usr/bin/env python
import sys, os.path
import os
import json
from xml.dom.minidom import parse
rootdir = os.getcwd()
lgPaths = ["iOS/AuthKitUI.lg", "iOS/CalendarUIKit.lg", "iOS/iCloudDriveSettings.lg", "iOS/MobileNotes.lg", "macOS/CalendarUI.lg", "macOS/Finder_FE.lg", "macOS/Finder_FinderKit.lg", "macOS/iCloudPrefPane.lg", "macOS/Notes.lg", "macOS/Reminders.lg", "macOS/TextEdit.lg"]
languageDirs = ["Dutch", "French"]
Here is some SwiftUI code I have.
------
NavigationView {
List(translations) { translation in
Button(action: {
self.dataSource.selectedTranslationId = translation.id
}) {
TranslationCell(translation: translation)
}
}
@drewmccormack
drewmccormack / SimpleStruct.swift
Created February 2, 2019 16:39
Simple example of multi-threading with mutable struct.
import Foundation
struct Small {
var i: Int
var j: Int
}
var s = Small(i: 1, j: 2)
DispatchQueue.global(qos: .background).async {