dircat 1.0.1

High-performance Rust utility that concatenates and displays directory contents, similar to the C++ DirCat.
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
name: Release

on:
  push:
    tags:
      - 'v*.*.*'
  workflow_dispatch:

permissions:
  contents: write

env:
  # Prevent Cargo from stripping binaries automatically.
  # We need unstripped binaries for the "Full" artifacts (debug symbols/PDBs).
  CARGO_PROFILE_RELEASE_STRIP: false
  # Map secret existence to env vars to avoid context issues in 'if' conditions
  HAS_GPG_SECRET: ${{ secrets.GPG_PRIVATE_KEY != '' }}
  HAS_PUBLIC_KEY: ${{ vars.GPG_PUBLIC_KEY != '' }}

jobs:
  # ===========================================================================
  # JOB 1: Build Matrix (Linux, Windows, macOS Single-Arch)
  # ===========================================================================
  build-matrix:
    name: Build ${{ matrix.target }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          # --- Linux (GNU) ---
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            use_cross: false
          - os: ubuntu-latest
            target: aarch64-unknown-linux-gnu
            use_cross: true
          - os: ubuntu-latest
            target: armv7-unknown-linux-gnueabihf
            use_cross: true

          # --- Linux (MUSL) ---
          - os: ubuntu-latest
            target: x86_64-unknown-linux-musl
            use_cross: true
          - os: ubuntu-latest
            target: aarch64-unknown-linux-musl
            use_cross: true
          - os: ubuntu-latest
            target: armv7-unknown-linux-musleabihf
            use_cross: true

          # --- macOS ---
          - os: macos-latest
            target: x86_64-apple-darwin
            use_cross: false
          - os: macos-14 # Apple Silicon runner
            target: aarch64-apple-darwin
            use_cross: false

          # --- Windows ---
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            use_cross: false
          - os: windows-latest
            target: aarch64-pc-windows-msvc
            use_cross: false

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Install Rust toolchain
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          toolchain: stable
          target: ${{ matrix.target }}
          cache: 'cargo'

      # --- Auto-detect Binary Name ---
      - name: Get Binary Name
        id: bin_name
        shell: bash
        run: |
          # Parses Cargo.toml metadata to find the name of the first "bin" target.
          # This handles cases where package.name != binary.name
          BIN_NAME=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].targets[] | select(.kind[] | contains("bin")) | .name' | head -n 1)
          if [ -z "$BIN_NAME" ]; then
            echo "Error: Could not determine binary name from Cargo.toml"
            exit 1
          fi
          echo "Detected binary: $BIN_NAME"
          echo "BINARY_NAME=$BIN_NAME" >> $GITHUB_ENV

      - name: Install Cross
        if: matrix.use_cross
        uses: taiki-e/install-action@v2
        with:
          tool: cross

      - name: Build Binary
        shell: bash
        run: |
          CMD="cargo"
          if [[ "${{ matrix.use_cross }}" == "true" ]]; then CMD="cross"; fi
          $CMD build --release --target ${{ matrix.target }} --verbose

      # Only import GPG if the secret is actually set.
      - name: Import GPG Key
        if: env.HAS_GPG_SECRET == 'true'
        uses: crazy-max/ghaction-import-gpg@v6
        with:
          gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }}
          passphrase: ${{ secrets.GPG_PASSPHRASE }}

      - name: Package and Sign
        shell: bash
        env:
          TARGET: ${{ matrix.target }}
          REF_NAME: ${{ github.ref_name }}
          HAS_GPG: ${{ secrets.GPG_PRIVATE_KEY != '' }}
        run: |
          # --- Setup Variables ---
          # Ensure version always has 'v' prefix for folder naming (e.g., v1.0.0).
          CLEAN_VERSION="${REF_NAME#v}"
          VERSION="v${CLEAN_VERSION}"
          
          # Normalize binary name (Rust replaces hyphens with underscores).
          BIN_FILE="${BINARY_NAME//-/_}"
          
          # Determine extensions based on OS.
          BIN_EXT=""
          ARCHIVE_EXT="tar.gz"
          if [[ "$TARGET" == *"windows"* ]]; then
            BIN_EXT=".exe"
            ARCHIVE_EXT="zip"
          elif [[ "$TARGET" == *"apple"* ]]; then
            ARCHIVE_EXT="zip"
          fi

          # Locate the built binary.
          BUILD_DIR="target/$TARGET/release"
          BIN_PATH="$BUILD_DIR/$BIN_FILE$BIN_EXT"

          if [[ ! -f "$BIN_PATH" ]]; then
            echo "Error: Binary not found at $BIN_PATH"
            exit 1
          fi

          # --- Prepare Directories (binstall compliance) ---
          # Structure: {name}-{target}-{version}/
          DIR_NAME="${BINARY_NAME}-${TARGET}-${VERSION}"
          FULL_DIR_NAME="${BINARY_NAME}-${TARGET}-${VERSION}-full"
          
          mkdir -p "$DIR_NAME" "$FULL_DIR_NAME"

          # --- Populate "Full" Directory (Unstripped + PDB) ---
          cp "$BIN_PATH" "$FULL_DIR_NAME/$BINARY_NAME$BIN_EXT"
          cp LICENSE README.md "$FULL_DIR_NAME/"
          # On Windows, include the PDB debug symbols.
          if [[ "$TARGET" == *"windows"* ]]; then
            cp "$BUILD_DIR/$BIN_FILE.pdb" "$FULL_DIR_NAME/$BINARY_NAME.pdb" || true
          fi

          # --- Populate "Standard" Directory (Stripped) ---
          cp "$BIN_PATH" "$DIR_NAME/$BINARY_NAME$BIN_EXT"
          cp LICENSE README.md "$DIR_NAME/"
          
          # Manually strip the binary for the standard release to reduce size.
          if [[ "$TARGET" != *"windows"* ]]; then
            if [[ "$TARGET" == *"apple"* ]]; then
              strip -x "$DIR_NAME/$BINARY_NAME$BIN_EXT" || true
            else
              strip "$DIR_NAME/$BINARY_NAME$BIN_EXT" || true
            fi
          fi

          # --- Create Archives ---
          FULL_ARCHIVE="${FULL_DIR_NAME}.${ARCHIVE_EXT}"
          STD_ARCHIVE="${DIR_NAME}.${ARCHIVE_EXT}"

          if [[ "$ARCHIVE_EXT" == "zip" ]]; then
            if command -v zip &> /dev/null; then
              zip -r "$FULL_ARCHIVE" "$FULL_DIR_NAME"
              zip -r "$STD_ARCHIVE" "$DIR_NAME"
            elif command -v 7z &> /dev/null; then
              7z a "$FULL_ARCHIVE" "$FULL_DIR_NAME"
              7z a "$STD_ARCHIVE" "$DIR_NAME"
            else
              echo "Error: Neither 'zip' nor '7z' found."
              exit 1
            fi
          else
            tar -czf "$FULL_ARCHIVE" "$FULL_DIR_NAME"
            tar -czf "$STD_ARCHIVE" "$DIR_NAME"
          fi

          # --- Sign Archives (Optional) ---
          if [[ "$HAS_GPG" == "true" ]]; then
            gpg --batch --yes --detach-sign --armor --output "${FULL_ARCHIVE}.sig" "$FULL_ARCHIVE"
            gpg --batch --yes --detach-sign --armor --output "${STD_ARCHIVE}.sig" "$STD_ARCHIVE"
          fi

          # --- Organize for Upload ---
          mkdir dist
          mv "${FULL_ARCHIVE}" "${STD_ARCHIVE}" dist/
          if [[ "$HAS_GPG" == "true" ]]; then
            mv "${FULL_ARCHIVE}.sig" "${STD_ARCHIVE}.sig" dist/
          fi

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

  # ===========================================================================
  # JOB 2: Build Universal macOS Binary
  # ===========================================================================
  build-mac-universal:
    name: Build universal-apple-darwin
    needs: build-matrix
    runs-on: macos-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      # We need Rust installed here just to run 'cargo metadata' to get the name.
      - name: Install Rust toolchain
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          toolchain: stable
          cache: 'cargo'

      # --- Auto-detect Binary Name ---
      - name: Get Binary Name
        id: bin_name
        shell: bash
        run: |
          BIN_NAME=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].targets[] | select(.kind[] | contains("bin")) | .name' | head -n 1)
          echo "Detected binary: $BIN_NAME"
          echo "BINARY_NAME=$BIN_NAME" >> $GITHUB_ENV

      - name: Download x86_64 Artifacts
        uses: actions/download-artifact@v4
        with:
          name: artifacts-x86_64-apple-darwin
          path: artifacts-x86

      - name: Download arm64 Artifacts
        uses: actions/download-artifact@v4
        with:
          name: artifacts-aarch64-apple-darwin
          path: artifacts-arm

      - name: Create Universal Binary (Lipo)
        shell: bash
        run: |
          # Extract the "Full" (unstripped) zips to get the raw binaries.
          unzip -o artifacts-x86/*x86_64-apple-darwin*-full.zip -d x86_extract
          unzip -o artifacts-arm/*aarch64-apple-darwin*-full.zip -d arm_extract
          
          # Find the binary files (ignoring folder structure).
          BIN_X86=$(find x86_extract -type f -name "${BINARY_NAME}" | head -n 1)
          BIN_ARM=$(find arm_extract -type f -name "${BINARY_NAME}" | head -n 1)

          if [[ -z "$BIN_X86" || -z "$BIN_ARM" ]]; then
            echo "Error: Could not locate extracted binaries for lipo."
            exit 1
          fi

          # Merge architectures.
          mkdir -p output
          lipo -create -output output/${BINARY_NAME} "$BIN_X86" "$BIN_ARM"

      - name: Import GPG Key
        if: env.HAS_GPG_SECRET == 'true'
        uses: crazy-max/ghaction-import-gpg@v6
        with:
          gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }}
          passphrase: ${{ secrets.GPG_PASSPHRASE }}

      - name: Package and Sign
        shell: bash
        env:
          HAS_GPG: ${{ secrets.GPG_PRIVATE_KEY != '' }}
          REF_NAME: ${{ github.ref_name }}
        run: |
          CLEAN_VERSION="${REF_NAME#v}"
          VERSION="v${CLEAN_VERSION}"
          
          TARGET="universal-apple-darwin"
          BIN_PATH="output/${BINARY_NAME}"
          
          # --- Prepare Directories ---
          DIR_NAME="${BINARY_NAME}-${TARGET}-${VERSION}"
          FULL_DIR_NAME="${BINARY_NAME}-${TARGET}-${VERSION}-full"
          
          mkdir -p "$DIR_NAME" "$FULL_DIR_NAME"

          # Full (Unstripped)
          cp "$BIN_PATH" "$FULL_DIR_NAME/$BINARY_NAME"
          cp LICENSE README.md "$FULL_DIR_NAME/"
          
          # Standard (Stripped)
          cp "$BIN_PATH" "$DIR_NAME/$BINARY_NAME"
          cp LICENSE README.md "$DIR_NAME/"
          strip -x "$DIR_NAME/$BINARY_NAME"

          # --- Create Archives ---
          FULL_ARCHIVE="${FULL_DIR_NAME}.zip"
          STD_ARCHIVE="${DIR_NAME}.zip"
          
          zip -r "$FULL_ARCHIVE" "$FULL_DIR_NAME"
          zip -r "$STD_ARCHIVE" "$DIR_NAME"

          # --- Sign Archives ---
          if [[ "$HAS_GPG" == "true" ]]; then
            gpg --batch --yes --detach-sign --armor --output "${FULL_ARCHIVE}.sig" "$FULL_ARCHIVE"
            gpg --batch --yes --detach-sign --armor --output "${STD_ARCHIVE}.sig" "$STD_ARCHIVE"
          fi

          # --- Organize for Upload ---
          mkdir dist
          mv "${FULL_ARCHIVE}" "${STD_ARCHIVE}" dist/
          if [[ "$HAS_GPG" == "true" ]]; then
             mv "${FULL_ARCHIVE}.sig" "${STD_ARCHIVE}.sig" dist/
          fi

      - name: Upload Artifacts
        uses: actions/upload-artifact@v4
        with:
          name: artifacts-universal
          path: dist/*

  # ===========================================================================
  # JOB 3: Create Release
  # ===========================================================================
  create-release:
    name: Create GitHub Release
    needs: [build-matrix, build-mac-universal]
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          # Fetch full history so we can find the previous tag for comparison links
          fetch-depth: 0

      - name: Download All Artifacts
        uses: actions/download-artifact@v4
        with:
          path: release-assets
          merge-multiple: true

      - name: Include Public Key
        # Only run if the variable is set in GitHub Settings
        if: env.HAS_PUBLIC_KEY == 'true'
        run: |
          # Write the variable content to a file in the assets folder
          echo "${{ vars.GPG_PUBLIC_KEY }}" > release-assets/public.key

      - name: Install parse-changelog
        uses: taiki-e/install-action@v2
        with:
          tool: parse-changelog

      - name: Generate Release Notes
        id: notes
        shell: bash
        run: |
          # Try to parse the changelog. 
          # 2>/dev/null suppresses error messages if not found.
          if parse-changelog CHANGELOG.md "${{ github.ref_name }}" > release_notes.md 2>/dev/null; then
            echo "✅ Found entry in CHANGELOG.md"
            
            # --- Append Compare Link (Only if using custom changelog) ---
            CURRENT_TAG="${{ github.ref_name }}"
            PREV_TAG=$(git describe --abbrev=0 --tags "${CURRENT_TAG}^" 2>/dev/null || true)
            
            if [ -n "$PREV_TAG" ]; then
              REPO_URL="${{ github.server_url }}/${{ github.repository }}"
              echo -e "\n\n---" >> release_notes.md
              echo "🆚 [Compare changes](${REPO_URL}/compare/${PREV_TAG}...${CURRENT_TAG})" >> release_notes.md
            fi
            
            # Set output to true
            echo "use_changelog=true" >> $GITHUB_OUTPUT
          else
            echo "⚠️ No entry found in CHANGELOG.md for ${{ github.ref_name }}"
            echo "Falling back to GitHub auto-generated notes."
            
            # Set output to false
            echo "use_changelog=false" >> $GITHUB_OUTPUT
          fi

      - name: Create Release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ github.ref_name }}
          # If use_changelog is true, use the file. Otherwise, leave empty.
          body_path: ${{ steps.notes.outputs.use_changelog == 'true' && 'release_notes.md' || '' }}
          # If use_changelog is NOT true, generate notes automatically.
          generate_release_notes: ${{ steps.notes.outputs.use_changelog != 'true' }}
          files: release-assets/*
          draft: true
          prerelease: false
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}