name: Build app
on:
  workflow_dispatch:
    inputs:
      buildAndroid:
        description: Build for Android
        required: true
        default: true
        type: boolean
      buildWindowsX64:
        description: Build for Windows x64
        required: true
        default: false
        type: boolean
      buildLinuxX64:
        description: Build for Linux x64
        required: true
        default: false
        type: boolean

jobs:
  analyze:
    name: Linting
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: subosito/flutter-action@v2
        with:
          flutter-version: 3.22.1
      - name: Disabling flutter analytics
        run: flutter config --no-analytics
      - name: Analyzing project code
        run: flutter analyze --no-fatal-infos
  build-android:
    name: Building for Android
    if: ${{ github.event.inputs.buildAndroid == 'true' }}
    runs-on: ubuntu-latest
    needs: analyze
    steps:
      - uses: actions/checkout@v4
      - uses: subosito/flutter-action@v2
        with:
          flutter-version: 3.22.1
      - uses: actions/setup-java@v4
        with:
          distribution: "adopt"
          java-version: "17"
      - name: Copy keystore file
        run: |
          echo $'storePassword=${{ secrets.ANDROID_KEYSTORE_PASSPHRASE }}\nkeyPassword=${{ secrets.ANDROID_KEYSTORE_PASSPHRASE }}\nkeyAlias=upload\nstoreFile=upload-keystore.jks\n' > ./android/key.properties
          echo "${{ secrets.ANDROID_KEYSTORE }}" > ./android/app/upload-keystore.jks.asc
          gpg -d --passphrase "${{ secrets.ANDROID_KEYSTORE_PASSPHRASE }}" --batch ./android/app/upload-keystore.jks.asc > ./android/app/upload-keystore.jks
      - name: Get flutter version
        id: get_flutter_version
        uses: its404/get-flutter-version@v1.0.0
      - name: Disabling flutter analytics
        run: flutter config --no-analytics
      - name: Running build
        id: compile
        run: flutter build apk --obfuscate --split-debug-info=build/debugAndroid
      - name: Preparing files
        run: |
          cp build/app/outputs/flutter-apk/app-release.apk build/app/outputs/flutter-apk/ollama-android-v${{ steps.get_flutter_version.outputs.version_number }}.apk
      - name: Uploading APK
        uses: actions/upload-artifact@v4
        with:
          name: ollama-android
          path: |
            build/app/outputs/flutter-apk/ollama-android-v${{ steps.get_flutter_version.outputs.version_number }}.apk
  build-windows-x64:
    name: Building for Windows x64
    if: ${{ github.event.inputs.buildWindowsX64 == 'true' }}
    runs-on: windows-latest
    needs: analyze
    steps:
      - uses: actions/checkout@v4
      - uses: subosito/flutter-action@v2
        with:
          flutter-version: 3.22.1
      - name: Get flutter version
        id: get_flutter_version
        uses: its404/get-flutter-version@v1.0.0
      - name: Disabling flutter analytics
        run: flutter config --no-analytics
      - name: Running build
        id: compile
        run: flutter build windows --obfuscate --split-debug-info=build\debugWindows
      - name: Running installer build
        uses: Minionguyjpro/Inno-Setup-Action@v1.2.2
        with:
          path: .\windows_installer\ollama.iss
          options: /O+ /dAppVersion=${{ steps.get_flutter_version.outputs.version_number }}
      - name: Uploading installer
        uses: actions/upload-artifact@v4
        with:
          name: ollama-windows-x64
          path: build\windows\x64\runner\ollama-windows-x64-v${{ steps.get_flutter_version.outputs.version_number }}.exe
  build-linux-x64:
    name: Building for Linux x64
    if: ${{ github.event.inputs.buildLinuxX64 == 'true' }}
    runs-on: ubuntu-latest
    needs: analyze
    steps:
      - uses: actions/checkout@v4
      - uses: subosito/flutter-action@v2
        with:
          flutter-version: 3.22.1
      - name: Get flutter version
        id: get_flutter_version
        uses: its404/get-flutter-version@v1.0.0
      - name: Disabling flutter analytics
        run: flutter config --no-analytics
      - name: Installing linux dependencies
        run: |
          sudo apt-get install ninja-build
          sudo apt-get install build-essential libgtk-3-dev
      - name: Running build
        id: compile
        run: flutter build linux --obfuscate --split-debug-info=build/debugLinux
      - name: Creating archive
        run: |
          cd build/linux/x64/release/bundle
          tar -czf ollama-linux-x64-v${{ steps.get_flutter_version.outputs.version_number }}.tar.gz *
      - name: Uploading archive
        uses: actions/upload-artifact@v4
        with:
          name: ollama-linux-x64
          path: build/linux/x64/release/bundle/ollama-linux-x64-v${{ steps.get_flutter_version.outputs.version_number }}.tar.gz
  bundle:
    name: Creating bundle
    runs-on: ubuntu-latest
    if: ${{ always() }}
    needs: [build-android, build-windows-x64, build-linux-x64]
    steps:
      - name: Adding builds
        uses: actions/download-artifact@v4
        with:
          merge-multiple: true
      - name: Generating timestamp
        run: echo "timestamp=$EPOCHSECONDS"$'\n\norigin=${{ github.repository }}\nhost=${{ github.server_url }}\nowner=${{ github.repository_owner }}\n\nworkflow=${{ github.workflow }}\nrun_id=${{ github.run_id }}\nrun_number=${{ github.run_number }}' > manifest.yaml
      - name: Bundling files
        uses: actions/upload-artifact@v4
        with:
          name: ollama
          path: ./