Add Repository Skill
Overview & Prerequisites
This skill guides you to implement/update a token in spark component.
Component Selection
Parameter Handling:
- If the skill is invoked with a parameter (e.g.,
/spark-component-token button): - Use the parameter as the component name
- Verify the folder exists in
Modules/Components/{component-name}/ - If found, use that component and skip the selection step
- If no parameter is provided:
- List all folders in the
Modules/Components/directory - Use AskUserQuestion to let the user select ONE component from the list
- Format: Display folder names as options (e.g., "button", "card", "checkbox")
Once the component is selected, all operations must be performed in Modules/Components/{component-name}/ folder.
Use the files in the template folder as reference to manage the token and the unit tests. See Modules/Components/InputOTP/Sources/Core/Token for a full real-world example.
The template uses
Border,Size,TypographyandAnimationpurely as illustrations of recurring patterns (see Rules below). A real component's token categories are whatever it actually needs — e.g.Colors,Opacity,Layout,Spacing,Icon,Animation, ... Determine the category names and count from the component's design/usage, not from the template names.
The token is exposed publicly through
theme.component.xxx(ComponentToken+XXX.swift) and consumed by the SwiftUI views throughEnvironmentTheme+XXXToken.swift. Theses two files are managed by the skill spark-component-migrate-token.
Rules
The Token file must respect theses rules :
- must be located in Sources/Core/Token and in Tests/UnitTests/Token.
- the root file is named XXXToken.swift and contains a
public struct XXXToken: Equatablewith one property per token category the component actually needs (there is no fixed list or fixed count — could be one category or a dozen), plus apublic init(theme: any Theme)that forwards the theme to every category that needs it. - each token category lives in its own file named XXXToken+CategoryName.swift (e.g.
XXXToken+Opacity.swift), containing apublic extension XXXTokenwith a nestedstruct CategoryName: Equatable. - a category that depends on the theme (illustrated by `
BorderandTypography` in the template): - stores the theme in an internal
let theme: any Theme(no explicit init, the internal memberwiseinit(theme:)is used by the root struct). - exposes its values as public computed properties (
public var radius: CGFloat { ... }) readingself.theme. - exposes the values depending on a state/configuration (intent, isEnabled, isSelected, onError, ...) as public methods (
public func width(isSelected: Bool) -> CGFloat). Theses methods replace the old styling use cases. - implements a manual
static func ==comparing the themes:lhs.theme.equals(rhs.theme). - is assigned in the root
init(theme:). - a category that only exposes fixed constants (no theme-dependent values, illustrated by `
Sizein the template) contains onlypublic letconstants. On the root struct, its property is a stored default value (public let size = Size()), **not** assigned in the rootinit(theme:)` body. - the animation values (durations, curves) are tokens, not constants: they replace the
XXXConstants.Animationenum /XXXConstants.animationDuration. They live in anAnimationcategory (illustrated by `Animation` in the template): - it is a constant category (
public let animation = Animation()on the root struct, no theme). - the file imports
SwiftUIand, since the nested struct shadows the SwiftUI type, refers to it asSwiftUI.Animation. - exposes each duration as a
public let xxxDuration: TimeInterval(used by the UIKit views and by delays such asDispatchQueue.main.asyncAfter). - exposes the matching SwiftUI animation as a public computed property built from the duration (
public var fast: SwiftUI.Animation { .easeInOut(duration: self.fastDuration) }), used by the views (.animation(token.animation.fast, value:),withAnimation(token.animation.fast)). - when migrating, move the animation values from XXXConstants.swift to the token, update every usage (
XXXConstants.animationDuration->self.theme.component.xxx.animation...in the ViewModifiers / views), remove them from the constants (and delete the constants file and its tests if it becomes empty). - every public struct, property and method must be documented with a doc comment (
///). - use
// MARK: - Properties,// MARK: - Methodsand// MARK: - Equatablein the categories. - the root XXXToken.swift file must have a top-level doc comment that:
- starts with "Contains all token values for the XXX component, grouped like the `
Theme`." - lists every category actually present, each with a doc link (e.g. `
XXXToken/CategoryName: description`) - includes a Tip explaining the token is available with
theme.component.xxxand that the categories can be used individually (theme.component.xxx.size) - includes a "Creating a Custom XXX" section with a full SwiftUI usage example using
theme.component.xxx
Workflow
- [ ] Add/Update the code.
- [ ] Add/Update the documentation (the token file and its categories are always public).
- [ ] Run swiftlint ``
$ swiftlint``. - [ ] Run sourcery ``
$ sourcery`` - [ ] Implement the unit tests. Read theses folders to get mocks :
- spark-ios-component-XXX/Sources/Testing
- spark-ios-common/Sources/Testing
- spark-ios-theming/Sources/Testing
Code Conventions
- [ ] File headers follow pattern:
Created by {Firstname Lastname} on DD/MM/YYYY - [ ] Copyright line includes current year for new files (keep the year of existing files)
- [ ] No empty lines contain whitespace or tabs
- [ ] All members accessed with
self. - [ ] Proper MARK comments in place
- [ ] Single
return switchwhen each case returns a value
Test Implementation
- [ ] Tests must be written in Swift Testing framework
- [ ] Imports:
Testing,Foundation,@testable import SparkComponentXXX,@_spi(SI_SPI) import SparkTheming,@_spi(SI_SPI) import SparkThemingTesting
- [ ] Every
@Testmust have a description, written as the function name in a sentence (e.g.@Test("Radius should return radius full")forfunc radiusShouldReturnRadiusFull(),@Test("Value should return expected scale", arguments: [...])for a parameterized test). Never use a bare@Testor@Test(arguments:) - [ ] Function names in lowerCamelCase describing the expectation (e.g.
radiusShouldReturnRadiusFull) - [ ] Use
ThemeGeneratedMock.mocked()for the values andThemeGeneratedMock.mocked2()for the inequality - [ ] For each category:
- one test per computed property comparing with the theme value (fonts compared with
.font) - one parameterized test (
@Test("Description", arguments:)) per method, covering all the combinations of the parameters - a
// MARK: - Equatable Testssection: same theme returns true, different theme returns false - for a constant category: one test per constant
- for the animation category: one test per duration and one test per animation (compared with the expected
SwiftUI.Animation, e.g.#expect(self.token.fast == .easeInOut(duration: 0.1))) - [ ] The root
XXXTokenTestscontainsinitShouldSetAllCategories(each property equals.init(theme: self.theme)or.init()for constants), plus equatable true/false at the root level
When to Ask for Help
If you encounter:
- A token value that doesn't map to any existing theme property
- A use case that mixes styling and business rules (the business rules must not go in the token)
- Component doesn't follow standard patterns
Use AskUserQuestion to clarify with the user before proceeding.