thoth-cli 0.1.87

A terminal scratchpad akin to Heynote
Documentation
name: Release

on:
  workflow_dispatch:
    inputs:
      version:
        description: 'Version to release (e.g., 1.2.3)'
        required: true
      previous_version:
        description: 'Previous version (e.g., 1.2.2)'
        required: true

jobs:
  create_release:
    name: Create Release
    runs-on: ubuntu-latest
    permissions:
      contents: write
    outputs:
      release_id: ${{ steps.create_release.outputs.id }}
      upload_url: ${{ steps.create_release.outputs.upload_url }}
    steps:
      - uses: actions/checkout@v3
      
      - name: Extract Changelog
        id: extract_changelog
        run: |
          CURRENT_VERSION=${{ github.event.inputs.version }}
          PREVIOUS_VERSION=${{ github.event.inputs.previous_version }}
          CHANGELOG_CONTENT=$(awk "/## \[${CURRENT_VERSION}\]/,/## \[${PREVIOUS_VERSION}\]/" CHANGELOG.md | sed '$d')
          echo "CHANGELOG_CONTENT<<EOF" >> $GITHUB_OUTPUT
          echo "$CHANGELOG_CONTENT" >> $GITHUB_OUTPUT
          echo "EOF" >> $GITHUB_OUTPUT

      - name: Create Release
        id: create_release
        uses: softprops/action-gh-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: v${{ github.event.inputs.version }}
          name: v${{ github.event.inputs.version }}
          body: |
            ${{ steps.extract_changelog.outputs.CHANGELOG_CONTENT }}
          draft: false
          prerelease: false
          generate_release_notes: true

  build_and_release:
    name: Build and Release
    needs: create_release
    runs-on: ${{ matrix.os }}
    permissions:
      contents: write
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            artifact_name: thoth
            asset_name: thoth_${{ github.event.inputs.version }}_linux_amd64
          - os: ubuntu-latest
            target: aarch64-unknown-linux-gnu
            artifact_name: thoth
            asset_name: thoth_${{ github.event.inputs.version }}_linux_arm64
          - os: ubuntu-latest
            target: x86_64-unknown-linux-musl
            artifact_name: thoth
            asset_name: thoth_${{ github.event.inputs.version }}_linux-musl_amd64
          - os: ubuntu-latest
            target: aarch64-unknown-linux-musl
            artifact_name: thoth
            asset_name: thoth_${{ github.event.inputs.version }}_linux-musl_arm64
          - os: macos-latest
            target: x86_64-apple-darwin
            artifact_name: thoth
            asset_name: thoth_${{ github.event.inputs.version }}_darwin_amd64
          - os: macos-latest
            target: aarch64-apple-darwin
            artifact_name: thoth
            asset_name: thoth_${{ github.event.inputs.version }}_darwin_arm64

    steps:
    - uses: actions/checkout@v3
    
    - name: Install Rust
      uses: actions-rs/toolchain@v1
      with:
        profile: minimal
        toolchain: stable
        override: true
        target: ${{ matrix.target }}
    
    - name: Build
      uses: actions-rs/cargo@v1
      with:
        use-cross: true
        command: build
        args: --release --target ${{ matrix.target }}
      
    - name: Package
      run: |
        mkdir -p dist
        cp target/${{ matrix.target }}/release/${{ matrix.artifact_name }} dist/
        cd dist
        tar -czvf ${{ matrix.asset_name }}.tar.gz ${{ matrix.artifact_name }}
        
    - name: Calculate SHA256
      run: |
        cd dist
        echo "sha256=$(shasum -a 256 ${{ matrix.asset_name }}.tar.gz | awk '{print $1}')" >> $GITHUB_OUTPUT
      id: calc_sha256

    - name: Create DEB package (Linux only)
      if: runner.os == 'Linux' && !contains(matrix.target, 'musl')
      run: |
        sudo apt-get install -y fakeroot
        mkdir -p dist/deb/DEBIAN dist/deb/usr/bin
        cp target/${{ matrix.target }}/release/${{ matrix.artifact_name }} dist/deb/usr/bin/
        cat << EOF > dist/deb/DEBIAN/control
        Package: thoth
        Version: ${{ github.event.inputs.version }}
        Architecture: ${{ contains(matrix.target, 'aarch64') && 'arm64' || 'amd64' }}
        Maintainer: Thoth Maintainer <maintainer@example.com>
        Description: Thoth application
        EOF
        fakeroot dpkg-deb --build dist/deb dist/${{ matrix.asset_name }}.deb
        
    - name: Create RPM package (Linux only)
      if: runner.os == 'Linux' && !contains(matrix.target, 'musl')
      run: |
        sudo apt-get install -y rpm
        mkdir -p dist/rpm/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
        cp target/${{ matrix.target }}/release/${{ matrix.artifact_name }} dist/rpm/SOURCES/
        cat << EOF > dist/rpm/SPECS/thoth.spec
        Name: thoth
        Version: ${{ github.event.inputs.version }}
        Release: 1
        Summary: Thoth application
        License: MIT
        BuildArch: $(uname -m)
        
        %description
        A terminal scratchpad akin to Heynote
        
        %install
        mkdir -p %{buildroot}/usr/bin
        cp %{_sourcedir}/thoth %{buildroot}/usr/bin/thoth
        
        %files
        /usr/bin/thoth
        
        %define __strip /bin/true
        %define __spec_install_post %{nil}
        EOF
        rpmbuild -bb --define "_topdir $(pwd)/dist/rpm" dist/rpm/SPECS/thoth.spec
        find dist/rpm/RPMS -name '*.rpm' -exec mv {} dist/${{ matrix.asset_name }}.rpm \;
        
    - name: Upload Release Asset (tar.gz)
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ needs.create_release.outputs.upload_url }}
        asset_path: ./dist/${{ matrix.asset_name }}.tar.gz
        asset_name: ${{ matrix.asset_name }}.tar.gz
        asset_content_type: application/gzip

    - name: Upload Release Asset (deb)
      if: runner.os == 'Linux' && !contains(matrix.target, 'musl')
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ needs.create_release.outputs.upload_url }}
        asset_path: ./dist/${{ matrix.asset_name }}.deb
        asset_name: ${{ matrix.asset_name }}.deb
        asset_content_type: application/vnd.debian.binary-package

    - name: Upload Release Asset (rpm)
      if: runner.os == 'Linux' && !contains(matrix.target, 'musl')
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ needs.create_release.outputs.upload_url }}
        asset_path: ./dist/${{ matrix.asset_name }}.rpm
        asset_name: ${{ matrix.asset_name }}.rpm
        asset_content_type: application/x-rpm

  update_homebrew_formula:
    name: Update Homebrew Formula
    needs: [create_release, build_and_release]
    runs-on: ubuntu-latest
    steps:
      - name: Download release assets
        run: |
          curl -LO "https://github.com/jooaf/thoth/releases/download/v${{ github.event.inputs.version }}/thoth_${{ github.event.inputs.version }}_darwin_amd64.tar.gz"
          curl -LO "https://github.com/jooaf/thoth/releases/download/v${{ github.event.inputs.version }}/thoth_${{ github.event.inputs.version }}_darwin_arm64.tar.gz"

      - name: Calculate SHA256
        id: calc_sha256
        run: |
          AMD64_SHA256=$(sha256sum thoth_${{ github.event.inputs.version }}_darwin_amd64.tar.gz | awk '{print $1}')
          ARM64_SHA256=$(sha256sum thoth_${{ github.event.inputs.version }}_darwin_arm64.tar.gz | awk '{print $1}')
          echo "darwin_amd64_sha256=$AMD64_SHA256" >> $GITHUB_OUTPUT
          echo "darwin_arm64_sha256=$ARM64_SHA256" >> $GITHUB_OUTPUT
          echo "Debug: AMD64 SHA256: $AMD64_SHA256"
          echo "Debug: ARM64 SHA256: $ARM64_SHA256"

      - name: Checkout homebrew-thoth
        uses: actions/checkout@v3
        with:
          repository: jooaf/homebrew-thoth
          token: ${{ secrets.HOMEBREW_TAP_TOKEN }}
          path: homebrew-thoth

      - name: Update formula
        env:
          DARWIN_AMD64_SHA256: ${{ steps.calc_sha256.outputs.darwin_amd64_sha256 }}
          DARWIN_ARM64_SHA256: ${{ steps.calc_sha256.outputs.darwin_arm64_sha256 }}
        run: |
          cd homebrew-thoth
          echo "Debug: Using AMD64 SHA256: $DARWIN_AMD64_SHA256"
          echo "Debug: Using ARM64 SHA256: $DARWIN_ARM64_SHA256"
          cat << EOF > thoth.rb
          class Thoth < Formula
            desc "A terminal scratchpad akin to Heynote"
            homepage "https://github.com/jooaf/thoth"
            version "${{ github.event.inputs.version }}"
            
            on_macos do
              if Hardware::CPU.intel?
                url "https://github.com/jooaf/thoth/releases/download/v#{version}/thoth_#{version}_darwin_amd64.tar.gz"
                sha256 "$DARWIN_AMD64_SHA256"
              else
                url "https://github.com/jooaf/thoth/releases/download/v#{version}/thoth_#{version}_darwin_arm64.tar.gz"
                sha256 "$DARWIN_ARM64_SHA256"
              end
            end

            def install
              bin.install "thoth"
            end

            test do
              assert_match "thoth version #{version}", shell_output("#{bin}/thoth --version")
            end
          end
          EOF

      - name: Commit and push changes
        run: |
          cd homebrew-thoth
          git config user.name github-actions
          git config user.email github-actions@github.com
          git add thoth.rb
          git commit -m "Update Thoth to ${{ github.event.inputs.version }}"
          git push
          
  publish_to_cargo:
    name: Publish to Cargo
    needs: create_release
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      
      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true
      
      - name: Update Cargo.lock
        run: |
          cargo update
          git config user.name github-actions
          git config user.email github-actions@github.com
          git add Cargo.lock
          git commit -m "Update Cargo.lock" || echo "No changes to commit"
      
      - name: Verify Cargo Token
        run: |
          if [ -z "${{ secrets.CARGO_REGISTRY_TOKEN }}" ]; then
            echo "CARGO_REGISTRY_TOKEN is not set"
            exit 1
          else
            echo "CARGO_REGISTRY_TOKEN is set"
          fi
      
      - name: Publish to Cargo
        run: |
          cargo publish --token ${{ secrets.CARGO_REGISTRY_TOKEN }} --verbose || {
            echo "Cargo publish failed. Checking package status..."
            cargo package --list
            exit 1
          }
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}