tree2md 0.10.1

Like the tree command, but outputs in Markdown. Optimized for AI agents.
name: Release
on:
  push:
    tags: [ 'v*' ]

permissions:
  contents: write

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    name: Build (${{ matrix.target }})
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            ext: ""
            pack: tar.gz
          - os: macos-latest
            target: aarch64-apple-darwin
            ext: ""
            pack: tar.gz
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            ext: .exe
            pack: zip

    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}
      - uses: Swatinem/rust-cache@v2

      - name: Verify tag == Cargo.toml
        shell: bash
        run: |
          TAG="${GITHUB_REF_NAME}"
          VERSION=$(grep '^version *= *"' Cargo.toml | sed 's/.*"\(.*\)".*/\1/')
          if [ "v$VERSION" != "$TAG" ]; then
            echo "::error::Tag($TAG) != Cargo.toml(v$VERSION)"
            exit 1
          fi
          echo "OK: $TAG matches Cargo.toml"

      - name: Build
        run: cargo build --release --target ${{ matrix.target }}

      - name: Prepare package
        shell: bash
        run: |
          BIN="tree2md${{ matrix.ext }}"
          OUT="tree2md-${{ matrix.target }}"
          mkdir -p dist/$OUT
          cp target/${{ matrix.target }}/release/$BIN dist/$OUT/
          cp README.md dist/$OUT/ 2>/dev/null || true
          cp LICENSE dist/$OUT/ 2>/dev/null || true
          cd dist
          if [ "${{ matrix.pack }}" = "zip" ]; then
            7z a -tzip "$OUT.zip" "$OUT"
            # Use certutil on Windows for SHA256
            certutil -hashfile "$OUT.zip" SHA256 | head -2 | tail -1 | tr -d ' ' > "$OUT.sha256"
            echo "  $OUT.zip" >> "$OUT.sha256"
          else
            tar czf "$OUT.tar.gz" "$OUT"
            shasum -a 256 "$OUT.tar.gz" > "$OUT.sha256"
          fi

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: tree2md-${{ matrix.target }}
          path: dist/tree2md-${{ matrix.target }}.*

  release:
    name: Create GitHub Release
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      
      - uses: actions/download-artifact@v4
        with:
          path: artifacts
      
      # Extract release notes from CHANGELOG.md for this version
      - name: Extract release notes from CHANGELOG.md
        id: notes
        run: |
          TAG="${GITHUB_REF_NAME}"           # e.g. v0.5.0
          VER="${TAG#v}"                     # e.g. 0.5.0
          
          # Extract the section for this version from CHANGELOG.md
          awk -v ver="$VER" '
            $0 ~ "^## \\[" ver "\\]" {p=1; next}
            p && $0 ~ "^## \\[" {exit}
            p && $0 ~ "^\\[Unreleased\\]:" {exit}
            p
          ' CHANGELOG.md > RELEASE_NOTES.md
          
          # Add header with version and date
          echo "## tree2md ${TAG}" > RELEASE_NOTES_FINAL.md
          echo "" >> RELEASE_NOTES_FINAL.md
          cat RELEASE_NOTES.md >> RELEASE_NOTES_FINAL.md
          
          echo "---- RELEASE NOTES ----"
          cat RELEASE_NOTES_FINAL.md
      
      - name: Create release
        uses: softprops/action-gh-release@v2
        with:
          name: "tree2md ${{ github.ref_name }}"
          files: artifacts/**/tree2md-*
          body_path: RELEASE_NOTES_FINAL.md
          generate_release_notes: true
          append_body: true
          draft: false
          prerelease: false

  publish:
    name: Publish to crates.io
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with: { fetch-depth: 0 }
      - uses: dtolnay/rust-toolchain@stable

      - name: Ensure tag == Cargo.toml (strict)
        run: |
          TAG="${GITHUB_REF_NAME}"
          VERSION=$(grep '^version *= *"' Cargo.toml | sed 's/.*"\(.*\)".*/\1/')
          [ "v$VERSION" = "$TAG" ] || (echo "::error::Tag($TAG) != v$VERSION"; exit 1)
          echo "OK: $TAG == v$VERSION"

      - name: Check not already published
        run: |
          VERSION=$(grep '^version *= *"' Cargo.toml | sed 's/.*"\(.*\)".*/\1/')
          # Simple check - fails if version already exists, passes if unpublished (404) or not found
          cargo search tree2md --limit 1 | grep -q "tree2md = \"$VERSION\"" && \
            (echo "::error::Version $VERSION already on crates.io"; exit 1) || true

      - name: Publish
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: cargo publish --token "$CARGO_REGISTRY_TOKEN" --allow-dirty