cooklang 0.18.6

Cooklang parser with opt-in extensions
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
name: Release

on:
  workflow_dispatch:
    inputs:
      version:
        description: 'Release version (e.g., 0.17.12)'
        required: true
        type: string
      dry_run:
        description: 'Dry run (skip release creation and publishing)'
        required: false
        default: false
        type: boolean

env:
  CARGO_TERM_COLOR: always

jobs:
  # Prepare release - set version outputs
  prepare-release:
    name: Prepare Release
    runs-on: ubuntu-latest
    permissions:
      contents: write
    outputs:
      version: ${{ steps.set-version.outputs.version }}
      version_tag: ${{ steps.set-version.outputs.version_tag }}
    steps:
      - name: Checkout sources
        uses: actions/checkout@v4
        with:
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Set version outputs
        id: set-version
        run: |
          VERSION="${{ github.event.inputs.version }}"
          echo "version=${VERSION}" >> $GITHUB_OUTPUT
          echo "version_tag=v${VERSION}" >> $GITHUB_OUTPUT

  # Build iOS artifacts on macOS
  build-ios:
    name: Build iOS
    needs: prepare-release
    runs-on: macos-latest
    steps:
      - name: Checkout sources
        uses: actions/checkout@v4
        with:
          ref: main

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: aarch64-apple-ios,aarch64-apple-ios-sim

      - name: Cache Rust dependencies
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            target/
          key: ${{ runner.os }}-ios-cargo-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: ${{ runner.os }}-ios-cargo-

      - name: Build iOS targets
        run: |
          targets=("aarch64-apple-ios" "aarch64-apple-ios-sim")
          for target in "${targets[@]}"; do
            echo "Building for $target..."
            cargo build --release --target $target -p cooklang-bindings
          done

      - name: Generate Swift bindings
        working-directory: bindings
        run: |
          mkdir -p out
          cargo run --features="uniffi/cli" --bin uniffi-bindgen generate \
            --config uniffi.toml \
            --library ../target/aarch64-apple-ios/release/libcooklang_bindings.a \
            --language swift \
            --out-dir out

      - name: Create XCFramework
        working-directory: bindings
        run: |
          NAME="CooklangParser"
          LIBRARY_NAME="libcooklang_bindings.a"
          FRAMEWORK_LIBRARY_NAME="${NAME}FFI"
          FRAMEWORK_NAME="$FRAMEWORK_LIBRARY_NAME.framework"
          XC_FRAMEWORK_NAME="$FRAMEWORK_LIBRARY_NAME.xcframework"
          HEADER_NAME="${NAME}FFI.h"
          OUT_PATH="out"
          MIN_IOS_VERSION="16.0"
          BUNDLE_IDENTIFIER="org.cooklang.$NAME"

          # Target paths
          AARCH64_APPLE_IOS_PATH="../target/aarch64-apple-ios/release"
          AARCH64_APPLE_IOS_SIM_PATH="../target/aarch64-apple-ios-sim/release"

          # Create framework template
          rm -rf $OUT_PATH/$FRAMEWORK_NAME
          mkdir -p $OUT_PATH/$FRAMEWORK_NAME/Headers
          mkdir -p $OUT_PATH/$FRAMEWORK_NAME/Modules
          cp $OUT_PATH/$HEADER_NAME $OUT_PATH/$FRAMEWORK_NAME/Headers

          cat > $OUT_PATH/$FRAMEWORK_NAME/Modules/module.modulemap << EOF
          framework module $FRAMEWORK_LIBRARY_NAME {
            umbrella header "$HEADER_NAME"

            export *
            module * { export * }
          }
          EOF

          cat > $OUT_PATH/$FRAMEWORK_NAME/Info.plist << EOF
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
          <plist version="1.0">
          <dict>
            <key>CFBundleDevelopmentRegion</key>
            <string>en</string>
            <key>CFBundleExecutable</key>
            <string>$FRAMEWORK_LIBRARY_NAME</string>
            <key>CFBundleIdentifier</key>
            <string>$BUNDLE_IDENTIFIER</string>
            <key>CFBundleInfoDictionaryVersion</key>
            <string>6.0</string>
            <key>CFBundleName</key>
            <string>$FRAMEWORK_LIBRARY_NAME</string>
            <key>CFBundlePackageType</key>
            <string>FMWK</string>
            <key>CFBundleShortVersionString</key>
            <string>1.0</string>
            <key>CFBundleVersion</key>
            <string>1.0</string>
            <key>NSPrincipalClass</key>
            <string></string>
            <key>MinimumOSVersion</key>
            <string>$MIN_IOS_VERSION</string>
          </dict>
          </plist>
          EOF

          # Prepare frameworks for each platform
          rm -rf $OUT_PATH/frameworks
          mkdir -p $OUT_PATH/frameworks/sim
          mkdir -p $OUT_PATH/frameworks/ios
          cp -r $OUT_PATH/$FRAMEWORK_NAME $OUT_PATH/frameworks/sim/
          cp -r $OUT_PATH/$FRAMEWORK_NAME $OUT_PATH/frameworks/ios/
          cp $AARCH64_APPLE_IOS_SIM_PATH/$LIBRARY_NAME $OUT_PATH/frameworks/sim/$FRAMEWORK_NAME/$FRAMEWORK_LIBRARY_NAME
          cp $AARCH64_APPLE_IOS_PATH/$LIBRARY_NAME $OUT_PATH/frameworks/ios/$FRAMEWORK_NAME/$FRAMEWORK_LIBRARY_NAME

          # Create xcframework
          echo "Creating xcframework..."
          rm -rf $OUT_PATH/$XC_FRAMEWORK_NAME
          xcodebuild -create-xcframework \
              -framework $OUT_PATH/frameworks/sim/$FRAMEWORK_NAME \
              -framework $OUT_PATH/frameworks/ios/$FRAMEWORK_NAME \
              -output $OUT_PATH/$XC_FRAMEWORK_NAME

      - name: Package iOS artifacts
        run: |
          cd bindings/out
          zip -r ../../CooklangParserFFI.xcframework.zip CooklangParserFFI.xcframework

      - name: Upload iOS artifacts
        uses: actions/upload-artifact@v4
        with:
          name: ios-artifacts
          path: CooklangParserFFI.xcframework.zip

      - name: Upload Swift wrapper
        uses: actions/upload-artifact@v4
        with:
          name: swift-wrapper
          path: bindings/out/CooklangParser.swift

  # Build Android artifacts on Linux
  build-android:
    name: Build Android
    needs: prepare-release
    runs-on: ubuntu-latest
    steps:
      - name: Checkout sources
        uses: actions/checkout@v4
        with:
          ref: main

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: aarch64-linux-android,armv7-linux-androideabi,x86_64-linux-android

      - name: Cache Rust dependencies
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            target/
          key: ${{ runner.os }}-android-cargo-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: ${{ runner.os }}-android-cargo-

      - name: Setup Android NDK
        uses: android-actions/setup-android@v3

      - name: Install NDK
        run: |
          sdkmanager --install "ndk;26.1.10909125"
          echo "ANDROID_NDK_HOME=$ANDROID_HOME/ndk/26.1.10909125" >> $GITHUB_ENV

      - name: Install cargo-ndk
        run: command -v cargo-ndk || cargo install cargo-ndk --locked

      - name: Build uniffi-bindgen for host
        working-directory: bindings
        run: cargo build --features="uniffi/cli" --bin uniffi-bindgen --release

      - name: Build Android targets
        working-directory: bindings
        run: |
          cargo ndk --target aarch64-linux-android --platform 21 build --release
          cargo ndk --target armv7-linux-androideabi --platform 21 build --release
          cargo ndk --target x86_64-linux-android --platform 21 build --release

      - name: Build host library for bindings generation
        working-directory: bindings
        run: cargo build --lib

      - name: Generate Kotlin bindings
        working-directory: bindings
        run: |
          rm -rf out/kotlin
          mkdir -p out/kotlin
          # Use debug host library (not stripped) so uniffi-bindgen can read metadata symbols.
          # The release profile has strip=true which removes symbols uniffi-bindgen needs.
          ../target/release/uniffi-bindgen generate \
            --config uniffi.toml \
            --library ../target/debug/libcooklang_bindings.so \
            --language kotlin \
            --out-dir out/kotlin

          # Verify bindings were generated
          KT_COUNT=$(find out/kotlin -name "*.kt" | wc -l)
          if [ "$KT_COUNT" -eq 0 ]; then
            echo "ERROR: No Kotlin bindings were generated!"
            exit 1
          fi
          echo "Generated $KT_COUNT Kotlin binding file(s):"
          find out/kotlin -name "*.kt" -exec ls -la {} \;

      - name: Organize JNI libraries
        run: |
          mkdir -p target/android/jniLibs/{arm64-v8a,armeabi-v7a,x86_64}
          cp target/aarch64-linux-android/release/libcooklang_bindings.so target/android/jniLibs/arm64-v8a/
          cp target/armv7-linux-androideabi/release/libcooklang_bindings.so target/android/jniLibs/armeabi-v7a/
          cp target/x86_64-linux-android/release/libcooklang_bindings.so target/android/jniLibs/x86_64/

      - name: Create Android library module
        run: |
          mkdir -p target/android/parser/src/main/kotlin
          mkdir -p target/android/parser/src/main/jniLibs

          # Copy JNI libs
          cp -R target/android/jniLibs/* target/android/parser/src/main/jniLibs/

          # Copy Kotlin bindings (preserve directory structure)
          cp -r bindings/out/kotlin/* target/android/parser/src/main/kotlin/

          # Verify Kotlin bindings were copied
          KT_COUNT=$(find target/android/parser/src/main/kotlin -name "*.kt" | wc -l)
          if [ "$KT_COUNT" -eq 0 ]; then
            echo "ERROR: No Kotlin bindings found in Android module!"
            exit 1
          fi
          echo "Kotlin bindings in Android module:"
          find target/android/parser/src/main/kotlin -name "*.kt" -exec ls -la {} \;

          # Create build.gradle.kts
          cat > target/android/parser/build.gradle.kts << 'EOF'
          plugins {
              id("com.android.library")
              id("org.jetbrains.kotlin.android")
          }

          android {
              namespace = "org.cooklang.parser"
              compileSdk = 34

              defaultConfig {
                  minSdk = 26
                  testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
                  consumerProguardFiles("consumer-rules.pro")
              }

              buildTypes {
                  release {
                      isMinifyEnabled = false
                      proguardFiles(
                          getDefaultProguardFile("proguard-android-optimize.txt"),
                          "proguard-rules.pro"
                      )
                  }
              }
              compileOptions {
                  sourceCompatibility = JavaVersion.VERSION_17
                  targetCompatibility = JavaVersion.VERSION_17
              }
              kotlinOptions {
                  jvmTarget = "17"
              }
              sourceSets {
                  getByName("main") {
                      jniLibs.srcDirs("src/main/jniLibs")
                  }
              }
          }

          dependencies {
              implementation("net.java.dev.jna:jna:5.14.0@aar")
              implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
              implementation("androidx.core:core-ktx:1.9.0")
          }
          EOF

          # Create AndroidManifest.xml
          cat > target/android/parser/src/main/AndroidManifest.xml << 'EOF'
          <?xml version="1.0" encoding="utf-8"?>
          <manifest xmlns:android="http://schemas.android.com/apk/res/android">
          </manifest>
          EOF

          # Create proguard rules
          cat > target/android/parser/proguard-rules.pro << 'EOF'
          -keep class uniffi.** { *; }
          -keep class org.cooklang.** { *; }
          -keep class com.sun.jna.** { *; }
          -keepclassmembers class * extends com.sun.jna.** { public *; }
          EOF

          cat > target/android/parser/consumer-rules.pro << 'EOF'
          -keep class uniffi.** { *; }
          -keep class org.cooklang.** { *; }
          EOF

      - name: Package Android artifacts
        run: |
          cd target/android
          zip -r ../../cooklang-parser-android.zip parser jniLibs

      - name: Upload Android artifacts
        uses: actions/upload-artifact@v4
        with:
          name: android-artifacts
          path: cooklang-parser-android.zip

  # Update Package.swift with correct checksum
  update-package-swift:
    name: Update Package.swift
    needs: [prepare-release, build-ios]
    runs-on: macos-latest
    permissions:
      contents: write
    steps:
      - name: Checkout sources
        uses: actions/checkout@v4
        with:
          ref: main
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Download iOS artifacts
        uses: actions/download-artifact@v4
        with:
          name: ios-artifacts

      - name: Download Swift wrapper
        uses: actions/download-artifact@v4
        with:
          name: swift-wrapper

      - name: Compute checksum
        id: checksum
        run: |
          CHECKSUM=$(swift package compute-checksum CooklangParserFFI.xcframework.zip)
          echo "checksum=${CHECKSUM}" >> $GITHUB_OUTPUT
          echo "Computed checksum: ${CHECKSUM}"

      - name: Update Package.swift
        run: |
          VERSION="${{ needs.prepare-release.outputs.version_tag }}"
          CHECKSUM="${{ steps.checksum.outputs.checksum }}"

          sed -i '' "s|url: \"https://github.com/cooklang/cooklang-rs/releases/download/[^\"]*\"|url: \"https://github.com/cooklang/cooklang-rs/releases/download/${VERSION}/CooklangParserFFI.xcframework.zip\"|" Package.swift
          sed -i '' "s|checksum: \"[^\"]*\"|checksum: \"${CHECKSUM}\"|" Package.swift

          echo "Updated Package.swift:"
          grep -A2 "binaryTarget" Package.swift | head -5

      - name: Update Swift wrapper
        run: |
          cp CooklangParser.swift swift/Sources/CooklangParser/CooklangParser.swift

      - name: Commit Package.swift and Swift wrapper
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"

          git add Package.swift swift/Sources/CooklangParser/CooklangParser.swift
          git commit -m "Update Package.swift for ${{ needs.prepare-release.outputs.version_tag }}" || echo "No changes to commit"
          git push origin HEAD:main

  # Create GitHub Release
  create-release:
    name: Create Release
    needs: [prepare-release, build-ios, build-android, update-package-swift]
    runs-on: ubuntu-latest
    if: ${{ github.event.inputs.dry_run != 'true' }}
    permissions:
      contents: write
    steps:
      - name: Checkout sources
        uses: actions/checkout@v4
        with:
          ref: main
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Download iOS artifacts
        uses: actions/download-artifact@v4
        with:
          name: ios-artifacts

      - name: Download Android artifacts
        uses: actions/download-artifact@v4
        with:
          name: android-artifacts

      - name: Create and push tag
        run: |
          VERSION_TAG="${{ needs.prepare-release.outputs.version_tag }}"

          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"

          # Create tag at current commit (which includes updated Package.swift)
          git tag "${VERSION_TAG}"
          git push origin "${VERSION_TAG}"

      - name: Create Release
        uses: softprops/action-gh-release@v1
        with:
          tag_name: ${{ needs.prepare-release.outputs.version_tag }}
          name: Release ${{ needs.prepare-release.outputs.version_tag }}
          draft: false
          prerelease: ${{ contains(github.event.inputs.version, 'alpha') || contains(github.event.inputs.version, 'beta') || contains(github.event.inputs.version, 'rc') }}
          generate_release_notes: true
          files: |
            CooklangParserFFI.xcframework.zip
            cooklang-parser-android.zip
          body: |
            ## Cooklang Parser Release ${{ needs.prepare-release.outputs.version_tag }}

            ### iOS (Swift Package Manager)

            Add to your `Package.swift` or Xcode project:
            ```swift
            .package(url: "https://github.com/cooklang/cooklang-rs.git", from: "${{ needs.prepare-release.outputs.version_tag }}")
            ```

            Or download `CooklangParserFFI.xcframework.zip` for manual integration.

            **Supported architectures:**
            - iOS arm64 (devices)
            - iOS arm64 (simulator, Apple Silicon)

            ### Android (GitHub Packages Maven)

            Add to your `settings.gradle.kts`:
            ```kotlin
            dependencyResolutionManagement {
                repositories {
                    maven {
                        url = uri("https://maven.pkg.github.com/cooklang/cooklang-rs")
                        credentials {
                            username = System.getenv("GITHUB_ACTOR") ?: project.findProperty("gpr.user") as String?
                            password = System.getenv("GITHUB_TOKEN") ?: project.findProperty("gpr.key") as String?
                        }
                    }
                }
            }
            ```

            Add to your `build.gradle.kts`:
            ```kotlin
            implementation("org.cooklang:parser:${{ needs.prepare-release.outputs.version_tag }}")
            ```

            Or download `cooklang-parser-android.zip` for manual integration.

            **Supported architectures:**
            - arm64-v8a
            - armeabi-v7a
            - x86_64

            > **Note:** This replaces the deprecated `cooklang-kotlin` repository.

            ### Rust (crates.io)

            ```toml
            [dependencies]
            cooklang = "${{ needs.prepare-release.outputs.version }}"
            ```

            ### TypeScript/JavaScript (npm)

            ```bash
            npm install @cooklang/cooklang
            ```

            See the README for detailed integration instructions.

  # Publish Android package to GitHub Packages
  publish-android:
    name: Publish Android to GitHub Packages
    needs: [prepare-release, build-android, create-release]
    runs-on: ubuntu-latest
    if: ${{ github.event.inputs.dry_run != 'true' }}
    permissions:
      contents: read
      packages: write
    steps:
      - name: Checkout sources
        uses: actions/checkout@v4
        with:
          ref: main

      - name: Download Android artifacts
        uses: actions/download-artifact@v4
        with:
          name: android-artifacts

      - name: Extract Android artifacts
        run: |
          mkdir -p target/android
          unzip cooklang-parser-android.zip -d target/android/

          # Verify Kotlin sources are present
          KT_COUNT=$(find target/android/parser/src/main/kotlin -name "*.kt" | wc -l)
          if [ "$KT_COUNT" -eq 0 ]; then
            echo "ERROR: No Kotlin sources found in extracted artifacts!"
            echo "Contents of parser directory:"
            find target/android/parser -type f
            exit 1
          fi
          echo "Found $KT_COUNT Kotlin source file(s)"

      - name: Setup JDK
        uses: actions/setup-java@v4
        with:
          distribution: 'temurin'
          java-version: '17'

      - name: Setup Android SDK
        uses: android-actions/setup-android@v3

      - name: Setup Gradle
        uses: gradle/actions/setup-gradle@v3

      - name: Configure Android library for publishing
        run: |
          VERSION="${{ needs.prepare-release.outputs.version }}"
          ANDROID_DIR="target/android/parser"

          # Create settings.gradle.kts with plugin management
          cat > "${ANDROID_DIR}/settings.gradle.kts" << 'EOF'
          pluginManagement {
              repositories {
                  google()
                  mavenCentral()
                  gradlePluginPortal()
              }
          }
          dependencyResolutionManagement {
              repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
              repositories {
                  google()
                  mavenCentral()
              }
          }
          rootProject.name = "parser"
          EOF

          # Create build.gradle.kts with publishing
          cat > "${ANDROID_DIR}/build.gradle.kts" << EOF
          plugins {
              id("com.android.library") version "8.2.0"
              id("org.jetbrains.kotlin.android") version "1.9.22"
              id("maven-publish")
          }

          group = "org.cooklang"
          version = "${VERSION}"

          android {
              namespace = "org.cooklang.parser"
              compileSdk = 34

              defaultConfig {
                  minSdk = 26
                  testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
                  consumerProguardFiles("consumer-rules.pro")

                  aarMetadata {
                      minCompileSdk = 26
                  }
              }

              buildTypes {
                  release {
                      isMinifyEnabled = false
                      proguardFiles(
                          getDefaultProguardFile("proguard-android-optimize.txt"),
                          "proguard-rules.pro"
                      )
                  }
              }
              compileOptions {
                  sourceCompatibility = JavaVersion.VERSION_17
                  targetCompatibility = JavaVersion.VERSION_17
              }
              kotlinOptions {
                  jvmTarget = "17"
              }
              sourceSets {
                  getByName("main") {
                      jniLibs.srcDirs("src/main/jniLibs")
                  }
              }

              publishing {
                  singleVariant("release") {
                      withSourcesJar()
                  }
              }
          }

          dependencies {
              implementation("net.java.dev.jna:jna:5.14.0@aar")
              implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
              implementation("androidx.core:core-ktx:1.9.0")
          }

          publishing {
              publications {
                  register<MavenPublication>("release") {
                      groupId = "org.cooklang"
                      artifactId = "parser"
                      version = "${VERSION}"

                      afterEvaluate {
                          from(components["release"])
                      }

                      pom {
                          name.set("cooklang-parser")
                          description.set("Cooklang Parser Library for Android")
                          url.set("https://github.com/cooklang/cooklang-rs")

                          licenses {
                              license {
                                  name.set("The MIT License (MIT)")
                                  url.set("http://opensource.org/licenses/MIT")
                              }
                          }

                          developers {
                              developer {
                                  id.set("dubadub")
                                  name.set("Alexey Dubovskoy")
                              }
                          }

                          scm {
                              connection.set("scm:git:git://github.com/cooklang/cooklang-rs.git")
                              developerConnection.set("scm:git:ssh://github.com:cooklang/cooklang-rs.git")
                              url.set("https://github.com/cooklang/cooklang-rs")
                          }
                      }
                  }
              }

              repositories {
                  maven {
                      name = "GitHubPackages"
                      url = uri("https://maven.pkg.github.com/cooklang/cooklang-rs")
                      credentials {
                          username = System.getenv("GITHUB_ACTOR")
                          password = System.getenv("GITHUB_TOKEN")
                      }
                  }
              }
          }
          EOF

          # Create gradle.properties
          cat > "${ANDROID_DIR}/gradle.properties" << 'GRADLE_EOF'
          android.useAndroidX=true
          kotlin.code.style=official
          org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
          android.nonTransitiveRClass=true
          GRADLE_EOF

      - name: Publish to GitHub Packages
        working-directory: target/android/parser
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          GITHUB_ACTOR: ${{ github.actor }}
        run: |
          gradle wrapper --gradle-version 8.5
          ./gradlew publish --no-daemon

  # Publish Rust crate to crates.io
  publish-rust:
    name: Publish to crates.io
    needs: [prepare-release, build-ios, build-android]
    runs-on: ubuntu-latest
    if: ${{ github.event.inputs.dry_run != 'true' }}
    permissions:
      contents: read
      id-token: write
    steps:
      - name: Checkout sources
        uses: actions/checkout@v4
        with:
          ref: main

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable

      - name: Authenticate to crates.io
        uses: rust-lang/crates-io-auth-action@v1
        id: auth

      - name: Publish to crates.io
        env:
          CARGO_REGISTRY_TOKEN: ${{ steps.auth.outputs.token }}
        run: cargo publish --no-verify

  # Publish TypeScript package to npm
  publish-typescript:
    name: Publish to npm
    needs: [prepare-release, build-ios, build-android]
    runs-on: ubuntu-latest
    if: ${{ github.event.inputs.dry_run != 'true' }}
    permissions:
      contents: read
      id-token: write
    steps:
      - name: Checkout sources
        uses: actions/checkout@v4
        with:
          ref: main

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          registry-url: 'https://registry.npmjs.org'

      - name: Install wasm-pack
        run: cargo install wasm-pack --locked || true

      - name: Sync package.json version
        working-directory: typescript
        run: npm version "${{ needs.prepare-release.outputs.version }}" --no-git-tag-version --allow-same-version

      - name: Build TypeScript package
        working-directory: typescript
        run: |
          npm ci
          npm run build

      - name: Publish to npm
        working-directory: typescript
        run: npm publish --provenance --access public