name: Build and Test Scala-CLI Template

runs:
  using: composite
  steps:
    - name: Install CIRCT
      uses: ./.github/workflows/install-circt
    - name: Cache Scala-CLI
      uses: coursier/cache-action@v6
    - name: Setup Scala-CLI
      uses: VirtusLab/scala-cli-setup@v0.2.1
      with:
        jvm: adoptium:1.17
        apps: sbt
    - name: Generate Scala CLI Template
      shell: bash
      run: |
        # Determine the version and insert it into the template
        sbt emitVersion
        VERSION=$(cat version.txt)
        sed "s/@VERSION@/$VERSION/g" .github/workflows/build-scala-cli-template/chisel-template.scala > chisel-template.scala
        # If the version does NOT contain SNAPSHOT, remove line including snapshots repo
        if ! grep -qi 'snapshot' <<< $VERSION; then
          sed -i '1d' chisel-template.scala
        fi
    # Need to publishLocal to test the template
    - name: Publish Local
      shell: bash
      run: sbt "unipublish / publishLocal"
    - name: Test Scala CLI Template
      shell: bash
      run: scala-cli chisel-template.scala
