This file is the canonical source of project conventions for humans & agents. Read it before making repository changes.
- Swift: 6.3
- Xcode: 26.4
- macOS: 13
Scripts/bootstrapScripts/formatScripts/lint -AP(quick) /Scripts/lint(includes unused code checks)Scripts/build(debug) /Scripts/build '' -c release(release)Scripts/testScripts/package
mainis the trunk- Branch topics from
main - Before committing (to preserve tokens, agents should skip all of the following
steps unless explicitly directed to perform them):
- Add or edit tests for non-trivial changes
- Repeatedly run
Scripts/formatuntil no modifications are made - Repeatedly run
Scripts/lint& fix all violations until no violations are reported
- Commit messages: Follow commit message conventions
- Tag releases as
vX.Y.Z
- Newlines: Unix (i.e.
\n) - Indentation: Tabs (2 characters wide) for all files unless otherwise specified; 2 spaces for YAML; 1 space for Markdown
- Max line length: 120 characters for all files unless otherwise specified (tabs count as 2 characters); 80 for Markdown; unlimited for header, JSON & swiftformat
- Unnecessary trailing whitespace: Remove
- File ends: Single newline
- Quoting: Quote strings only when necessary, preferring the most literal format that works over more interpreted formats; if multiple quote syntaxes are functionally equivalent, prefer the visually lightest, e.g., prefer single quotes over double quotes if they are functionally equivalent
- Text: In documentation, comments, strings, etc.:
- Commas: Use Oxford commas for lists
- Ampersands: Prefer
&toand(omit Oxford comma before&) - Exceptions:
andshould be used inand/or& after a comma that separates distinct clauses (but not in a list) - Quotes: The enclosing quotations marks of a quote at the end of a sentence (iff the whole sentence isn't a quote) should not enclose the terminal punctuation mark of the encompassing sentence
- Iff: Use
iffasif & only if
- Style: GitHub-Flavored Markdown (GFM), ATX headings, backtick-fenced code blocks with language identifier, underscore emphasis, asterisk strong & hyphen bullets
- HTML: Limit to HTML supported by GFM that doesn't have a native GFM equivalent
Unless absolutely necessary for functionality or fixes, or unless violations of standards are discovered, do not:
- reformat
- rename
- reorder
- respace
- reword
- remove comments
- refactor if it worsens the caller interface
Refactoring should:
- Keep clean abstractions
- Inline a utility iff it is single-use
- Replace a utility iff the new version is more correct, performant, and/or simpler than the existing version, in descending order of priority
- Use zsh for scripts (except for shell-specific completion scripts)
- Zsh scripts must be compatible with all zsh versions starting with the version (currently 5.9) bundled with the newest version (currently 13.5.x) of the oldest macOS major version supported by mas (currently 13)
- Use
#!/bin/zshshebang (with-Ndefgkuoptions, unless any changes to the options are absolutely necessary) - Run
. "${0:A:h}/_setup_script"at the start of all development scripts - Prefer concision over verbosity
- If performance is at least almost equivalent or better, prefer in descending
order:
- zsh expansions
- zsh globs
- zsh builtins
- zsh loops
- external commands
- Make variables local & readonly when possible
- Use:
cp -cinstead ofcptrashinstead ofrm
mas is a SwiftPM project that uses Swift Argument Parser to interact with the command-line.
The PrivateFrameworks SwiftPM target exposes the following Apple private
frameworks (via Objective-C headers extracted from the DSC) to deploy App Store
apps:
- CommerceKit: Controllers
- StoreFoundation: Models
Use private frameworks only when public APIs are insufficient.
Newer Apple private frameworks (e.g., AppStoreDaemon & AppleMediaServices) seem to supersede the currently used ones, but the newer ones seem usable only by code with Apple-exclusive entitlements.
Swift source is organized in subfolders of Sources/mas:
- Commands: CLI implementation
- Models: Data types & suppliers
- Utilities: Utilities
Commands follow a consistent structure:
- Commands are nested structs within the
MASmain command - Use
@OptionGroupto compose reusable argument sets from dedicated types that conform toParsableArguments - Implement
func run() async { … }as the main command entry point - Use the static
MAS.printerfor all output to ensure consistent formatting - Call methods on
AppStoreActionenum cases (accessible via theAppStoretypealias) to execute business logic, e.g.,await AppStore.install.apps(…)
- Name most function parameters
- Capitalize acronym & initialism characters consistently (e.g.,
ADAM,API,HTTPRequest,JSON) - Shadow variables if the respective original will no longer be used
- Strongify weak references instead of evaluating them multiple times
- Group computed properties below stored properties
Each subsection contains code preferences in descending order.
Within this section & all subsections, X is a placeholder for any type name.
- Standardized name
- Concise name
- Verbose name
- Concise code, e.g.:
- Optional binding shorthand (e.g.,
if let x { … }, notif let x = x{ … })
- Optional binding shorthand (e.g.,
- Verbose code
- Composition
- Protocol conformance
- Class inheritance
- Inferred type, e.g.:
var a = [X]()var o = X?.nonevar c: X { .init() }f(array: .init())f(dictionary: .init())
- Cast type, e.g.:
var a = [] as [X]var o = nil as X?
- Explicit type, e.g.:
var a: [X] = .init()var o: X? = nilvar c: X { X() }f(array: [])f(dictionary: [:])
- Functional
- Non-functional
- Inlined single-use value
letmultiple-use valuevarmultiple-use value
- Inlined single-use code (unless inlined code is much more complex)
- Computed property
- Function
- Nil-coalescing operator (
??) - Ternary operator
Optional.map(_:)/Optional.flatMap(_:)- Single
guard if/else(noelse if)switch- Multiple
guard if/else if… /elsepreconditionFailure(_:file:line:)- Forced unwrapping (
!suffix) fatalError(_:file:line:)
- Typed throws (
throws(ErrorType)) - Untyped rethrows (
rethrows) - Untyped throws (
throws)
- Framework/library call
- Custom code
- Global
let enumstatic letstructstatic letclassstatic let
- Unaliased infrequent tuple/closure
- Type-aliased frequent tuple/closure
enumstructactorfinal classclass
- Concision:
- Generics:
<T: X> - Optional:
X? - Collection:
[X] - Dictionary:
[X:X]
- Generics:
- Verbosity:
- Generics:
where T: X - Optional:
Optional<X> - Collection:
Array<X> - Dictionary:
Dictionary<X, X>
- Generics:
()for void parameter typeVoidfor void return type
- Trailing closure
- Inline closure
- Shorthand argument names (e.g.,
$0) iff one-line closure - Explicit argument names for multi-line closure
- KeyPath
- Function reference
- Closure
- Memory-safe code (i.e. not
unsafe) unsafecode iff a memory-safe alternative:- Is not available from frameworks/libraries
- Is too difficult to implement properly & performantly
- Add tests for all non-trivial changes (to preserve tokens, agents should not add tests unless explicitly directed to do so)
- Implement in Swift Testing
- Derive test file paths from source file paths:
- replace the
Sources/massource path folder prefix withTests/MASTests - prepend
MASTests+to the source file name - e.g.,
Sources/mas/Commands/X.swift→Tests/MASTests/Commands/MASTests+X.swift
- replace the
- Use force unwrapping in tests where appropriate