|
| 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