Skip to content

Commit a220f94

Browse files
committed
Working on a quick start guide
1 parent 4c117fe commit a220f94

4 files changed

Lines changed: 367 additions & 6 deletions

File tree

.taskmaster/tasks/tasks.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -356,15 +356,15 @@
356356
"dependencies": [
357357
3
358358
],
359-
"status": "pending",
359+
"status": "in-progress",
360360
"subtasks": [
361361
{
362362
"id": 1,
363363
"title": "Create SPM installation instructions",
364364
"description": "Write streamlined 2-minute Swift Package Manager installation section with copy-paste code examples",
365365
"dependencies": [],
366366
"details": "Create clear step-by-step SPM installation instructions in the Quick Start tutorial. Include Package.swift dependency code, Xcode integration steps, and import statements. Focus on minimal friction and copy-paste convenience. Target completion in 2 minutes or less.",
367-
"status": "pending",
367+
"status": "done",
368368
"testStrategy": ""
369369
},
370370
{
@@ -375,7 +375,7 @@
375375
"5.1"
376376
],
377377
"details": "Build a practical enum generator example that reads from JSON config and produces Swift enum code. Show the 'aha moment' of dynamic generation vs static code. Include both the JSON input and resulting Swift output. Keep example simple but meaningful, targeting 2-minute completion.",
378-
"status": "pending",
378+
"status": "in-progress",
379379
"testStrategy": ""
380380
},
381381
{
@@ -905,7 +905,7 @@
905905
],
906906
"metadata": {
907907
"created": "2025-08-31T16:26:25.262Z",
908-
"updated": "2025-09-01T13:50:07.437Z",
908+
"updated": "2025-09-01T13:56:52.096Z",
909909
"description": "Tasks for master context"
910910
}
911911
}

Sources/SyntaxKit/CodeBlocks/CodeBlockBuilderResult.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//
88
// Permission is hereby granted, free of charge, to any person
99
// obtaining a copy of this software and associated documentation
10-
// files (the Software), to deal in the Software without
10+
// files (the "Software"), to deal in the Software without
1111
// restriction, including without limitation the rights to use,
1212
// copy, modify, merge, publish, distribute, sublicense, and/or
1313
// sell copies of the Software, and to permit persons to whom the
@@ -17,7 +17,7 @@
1717
// The above copyright notice and this permission notice shall be
1818
// included in all copies or substantial portions of the Software.
1919
//
20-
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND,
20+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2121
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
2222
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2323
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
@@ -32,6 +32,8 @@ import Foundation
3232
/// A result builder for creating arrays of ``CodeBlock``s.
3333
@resultBuilder
3434
public enum CodeBlockBuilderResult: Sendable {
35+
36+
3537
/// Builds a block of ``CodeBlock``s.
3638
public static func buildBlock(_ components: CodeBlock...) -> [CodeBlock] {
3739
components
@@ -56,4 +58,13 @@ public enum CodeBlockBuilderResult: Sendable {
5658
public static func buildArray(_ components: [CodeBlock]) -> [CodeBlock] {
5759
components
5860
}
61+
62+
public static func buildBlock(_ components: [CodeBlock]...) -> [CodeBlock] {
63+
components.flatMap{$0}
64+
}
65+
66+
/// Builds an array of ``CodeBlock``s from a `for` loop.
67+
public static func buildArray(_ components: [[CodeBlock]]) -> [CodeBlock] {
68+
components.flatMap{$0}
69+
}
5970
}
Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
# Quick Start Guide
2+
3+
Get up and running with SyntaxKit in under 5 minutes. This tutorial will take you from zero to generating Swift code, showing you the power of dynamic code generation.
4+
5+
## What You'll Build
6+
7+
In this quick start, you'll create a simple enum generator that reads configuration from JSON and produces Swift enum code. You'll see firsthand how SyntaxKit transforms external data into clean, compilable Swift code.
8+
9+
**Time to complete**: 5 minutes
10+
**Prerequisites**: Basic Swift knowledge, Xcode 16.4+
11+
12+
## Step 1: Add SyntaxKit to Your Project (2 minutes)
13+
14+
### Using Swift Package Manager
15+
16+
1. **In Xcode**: Go to File → Add Package Dependencies
17+
2. **Enter URL**: `https://github.com/brightdigit/SyntaxKit.git`
18+
3. **Choose Version**: Use "Up to Next Major Version" starting from 1.0.0
19+
4. **Add to Target**: Select your target and click "Add Package"
20+
21+
### Using Package.swift
22+
23+
Add SyntaxKit to your `Package.swift` file:
24+
25+
```swift
26+
// swift-tools-version: 6.1
27+
import PackageDescription
28+
29+
let package = Package(
30+
name: "YourPackage",
31+
platforms: [
32+
.macOS(.v13), .iOS(.v13), .watchOS(.v6), .tvOS(.v13), .visionOS(.v1)
33+
],
34+
dependencies: [
35+
.package(url: "https://github.com/brightdigit/SyntaxKit.git", from: "1.0.0")
36+
],
37+
targets: [
38+
.target(
39+
name: "YourTarget",
40+
dependencies: ["SyntaxKit"]
41+
)
42+
]
43+
)
44+
```
45+
46+
### Import SyntaxKit
47+
48+
In your Swift file, add the import:
49+
50+
```swift
51+
import SyntaxKit
52+
```
53+
54+
That's it! SyntaxKit is now available in your project.
55+
56+
## Step 2: Create Your First Dynamic Enum (2 minutes)
57+
58+
Let's create an enum generator that reads from JSON configuration. This demonstrates the core power of SyntaxKit: transforming external data into Swift code.
59+
60+
### The JSON Configuration
61+
62+
First, create this JSON configuration:
63+
64+
```json
65+
{
66+
"name": "HTTPStatus",
67+
"cases": [
68+
{"name": "ok", "value": "200"},
69+
{"name": "notFound", "value": "404"},
70+
{"name": "serverError", "value": "500"}
71+
]
72+
}
73+
```
74+
75+
### The Enum Generator
76+
77+
Now create the generator code:
78+
79+
```swift
80+
import SyntaxKit
81+
import Foundation
82+
83+
// Define our configuration structure
84+
struct EnumConfig: Codable {
85+
let name: String
86+
let cases: [EnumCase]
87+
}
88+
89+
struct EnumCase: Codable {
90+
let name: String
91+
let value: String
92+
}
93+
94+
// The magic happens here - generate Swift enum from JSON
95+
func generateEnum(from json: String) -> String {
96+
// Parse JSON configuration
97+
guard let data = json.data(using: .utf8),
98+
let config = try? JSONDecoder().decode(EnumConfig.self, from: data) else {
99+
return "// Invalid JSON configuration"
100+
}
101+
102+
// Create enum using SyntaxKit's declarative DSL
103+
let enumDecl = Enum(config.name, conformsTo: ["Int", "CaseIterable"]) {
104+
for enumCase in config.cases {
105+
Case(enumCase.name, rawValue: enumCase.value)
106+
}
107+
}
108+
109+
// Generate Swift source code
110+
return enumDecl.formatted().description
111+
}
112+
```
113+
114+
### The JSON Input
115+
116+
```swift
117+
let jsonConfig = """
118+
{
119+
"name": "HTTPStatus",
120+
"cases": [
121+
{"name": "ok", "value": "200"},
122+
{"name": "notFound", "value": "404"},
123+
{"name": "serverError", "value": "500"}
124+
]
125+
}
126+
"""
127+
```
128+
129+
## Step 3: See It in Action (1 minute)
130+
131+
Run the generator and see the magic happen:
132+
133+
```swift
134+
// Generate the Swift code
135+
let swiftCode = generateEnum(from: jsonConfig)
136+
137+
// Print the generated Swift enum
138+
print("Generated Swift Code:")
139+
print("=" * 40)
140+
print(swiftCode)
141+
print("=" * 40)
142+
```
143+
144+
**Output**:
145+
```swift
146+
enum HTTPStatus: Int, CaseIterable {
147+
case ok = 200
148+
case notFound = 404
149+
case serverError = 500
150+
}
151+
```
152+
153+
### The "Aha!" Moment
154+
155+
**This is the power of SyntaxKit**: You just transformed JSON configuration into clean, compilable Swift code!
156+
157+
Compare this to manually maintaining enums:
158+
-**Manual approach**: Edit Swift files every time your API changes
159+
-**SyntaxKit approach**: Update JSON config, regenerate automatically
160+
161+
The generated code is identical to hand-written Swift, but now it can be created dynamically from any data source.
162+
163+
## Try It Yourself
164+
165+
### Experiment 1: Add More Cases
166+
Try adding more HTTP status codes to the JSON:
167+
168+
```json
169+
{
170+
"name": "HTTPStatus",
171+
"cases": [
172+
{"name": "ok", "value": "200"},
173+
{"name": "created", "value": "201"},
174+
{"name": "notFound", "value": "404"},
175+
{"name": "serverError", "value": "500"},
176+
{"name": "badGateway", "value": "502"}
177+
]
178+
}
179+
```
180+
181+
### Experiment 2: Create Different Enums
182+
Try generating a different enum entirely:
183+
184+
```json
185+
{
186+
"name": "Priority",
187+
"cases": [
188+
{"name": "low", "value": "1"},
189+
{"name": "medium", "value": "2"},
190+
{"name": "high", "value": "3"},
191+
{"name": "critical", "value": "4"}
192+
]
193+
}
194+
```
195+
196+
### Experiment 3: Playground Fun
197+
Copy all the code above into a Swift Playground and experiment with different configurations. See how quickly you can generate completely different enums!
198+
199+
## What You've Accomplished
200+
201+
In just 5 minutes, you've:
202+
- ✅ Added SyntaxKit to your project
203+
- ✅ Created a dynamic enum generator
204+
- ✅ Transformed JSON into Swift code
205+
- ✅ Seen the power of declarative code generation
206+
207+
**The key insight**: Instead of writing static Swift code, you're now generating it dynamically from external data. This opens up powerful possibilities for API clients, database models, and code generation tools.
208+
209+
## Next Steps
210+
211+
Ready to dive deeper? Here are your options:
212+
213+
### 🎯 **For Macro Development**
214+
<doc:Creating-Macros-with-SyntaxKit> - Build powerful Swift macros with SyntaxKit's clean DSL
215+
216+
### 🏗️ **For Advanced Examples**
217+
- [Enum Generator CLI Tool](https://swiftpackageindex.com/brightdigit/SyntaxKit/documentation) - Complete command-line enum generator
218+
- [Best Practices Guide](https://swiftpackageindex.com/brightdigit/SyntaxKit/documentation) - Patterns for maintainable code generation
219+
220+
### 📚 **For Understanding When to Use SyntaxKit**
221+
<doc:When-to-Use-SyntaxKit> - Decision framework for choosing SyntaxKit vs regular Swift
222+
223+
### 🎮 **For Hands-On Learning**
224+
Download our [Quick Start Playground](https://github.com/brightdigit/SyntaxKit/releases/latest/download/SyntaxKit-QuickStart.playground.zip) - Complete working examples you can run immediately
225+
226+
## Download Playground
227+
228+
Want to experiment right away? Download our Swift Playground with all examples ready to run:
229+
230+
**[📥 Download SyntaxKit Quick Start Playground](https://github.com/brightdigit/SyntaxKit/releases/latest/download/SyntaxKit-QuickStart.playground.zip)**
231+
232+
The playground includes:
233+
- Complete enum generator example
234+
- Multiple JSON configurations to try
235+
- Interactive experiments
236+
- Links to advanced topics
237+
238+
## Summary
239+
240+
SyntaxKit transforms how you approach code generation in Swift. Instead of manually maintaining repetitive code structures, you can generate them dynamically from external data sources.
241+
242+
**Key takeaways**:
243+
- SyntaxKit uses a declarative DSL for clean, readable code generation
244+
- Generated code is identical to hand-written Swift code
245+
- Perfect for macros, API clients, and developer tools
246+
- Built on Apple's SwiftSyntax for reliability and performance
247+
248+
You're now ready to explore the full power of dynamic Swift code generation with SyntaxKit!
249+
250+
## See Also
251+
252+
- ``Enum``
253+
- ``Case``
254+
- ``Struct``
255+
- ``Function``
256+
- ``Class``

0 commit comments

Comments
 (0)