|
| 1 | +# Code Generation |
| 2 | + |
| 3 | +The Brownie CLI generates native types from your TypeScript store definitions. |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +```bash |
| 8 | +brownie codegen # Generate for all configured platforms |
| 9 | +brownie codegen -p swift # Generate Swift only |
| 10 | +brownie codegen --platform kotlin # Generate Kotlin only (coming soon) |
| 11 | +brownie --help # Show help |
| 12 | +brownie --version # Show version |
| 13 | +``` |
| 14 | + |
| 15 | +## Configuration |
| 16 | + |
| 17 | +Add to your app's `package.json`: |
| 18 | + |
| 19 | +```json |
| 20 | +{ |
| 21 | + "brownie": { |
| 22 | + "swift": "./ios/Generated/", |
| 23 | + "kotlin": "./android/app/src/main/java/com/example/", |
| 24 | + "kotlinPackageName": "com.example" |
| 25 | + } |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +| Field | Required | Description | |
| 30 | +| ------------------- | -------- | ---------------------------------------------------------- | |
| 31 | +| `swift` | No\* | Output directory for Swift files | |
| 32 | +| `kotlin` | No\* | Output directory for Kotlin files | |
| 33 | +| `kotlinPackageName` | No | Kotlin package name (extracted from path if not specified) | |
| 34 | + |
| 35 | +\*At least one of `swift` or `kotlin` is required. |
| 36 | + |
| 37 | +## Generated Output |
| 38 | + |
| 39 | +For this TypeScript definition: |
| 40 | + |
| 41 | +```ts |
| 42 | +interface BrownfieldStore extends BrownieStore { |
| 43 | + counter: number; |
| 44 | + user: { name: string }; |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +### Swift Output |
| 49 | + |
| 50 | +```swift |
| 51 | +import Brownie |
| 52 | + |
| 53 | +struct BrownfieldStore: Codable { |
| 54 | + var counter: Double |
| 55 | + var user: User |
| 56 | +} |
| 57 | + |
| 58 | +struct User: Codable { |
| 59 | + var name: String |
| 60 | +} |
| 61 | + |
| 62 | +extension BrownfieldStore: BrownieStoreProtocol { |
| 63 | + public static let storeName = "BrownfieldStore" |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +The generated struct: |
| 68 | + |
| 69 | +- Conforms to `Codable` for JSON serialization |
| 70 | +- Conforms to `BrownieStoreProtocol` with auto-generated `storeName` |
| 71 | +- Uses mutable `var` properties |
| 72 | + |
| 73 | +### Kotlin Output (Coming Soon) |
| 74 | + |
| 75 | +```kotlin |
| 76 | +package com.example |
| 77 | + |
| 78 | +data class BrownfieldStore( |
| 79 | + val counter: Double, |
| 80 | + val user: User |
| 81 | +) |
| 82 | + |
| 83 | +data class User( |
| 84 | + val name: String |
| 85 | +) |
| 86 | +``` |
| 87 | + |
| 88 | +## Auto-Generation Hooks |
| 89 | + |
| 90 | +### iOS (Podfile) |
| 91 | + |
| 92 | +Run codegen before pod install: |
| 93 | + |
| 94 | +```ruby |
| 95 | +pre_install do |installer| |
| 96 | + system("npx", "brownie", "codegen", "-p", "swift") |
| 97 | +end |
| 98 | +``` |
| 99 | + |
| 100 | +### Android (build.gradle.kts) |
| 101 | + |
| 102 | +Run codegen before build: |
| 103 | + |
| 104 | +```kotlin |
| 105 | +tasks.register("generateBrownfieldStore") { |
| 106 | + exec { commandLine("npx", "brownie", "codegen", "-p", "kotlin") } |
| 107 | +} |
| 108 | +preBuild.dependsOn("generateBrownfieldStore") |
| 109 | +``` |
| 110 | + |
| 111 | +## How It Works |
| 112 | + |
| 113 | +1. CLI recursively finds all `*.brownie.ts` files |
| 114 | +2. Parses `declare module '@callstack/brownie'` blocks using ts-morph |
| 115 | +3. Extracts store names from `BrownieStores` interface |
| 116 | +4. Generates native types using quicktype |
0 commit comments