hbsx 0.2.9

A easy-to-use file encryption tool with compression support
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
name: Release

on:
  push:
    tags:
      - 'v*'

permissions:
  contents: write

jobs:
  create-release:
    name: Create Release
    runs-on: ubuntu-latest
    outputs:
      upload_url: ${{ steps.create_release.outputs.upload_url }}
      version: ${{ steps.get_version.outputs.version }}
    steps:
      - name: Get version from tag
        id: get_version
        run: echo "version=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT

      - name: Create Release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.ref }}
          release_name: Release ${{ github.ref }}
          draft: false
          prerelease: false

  build:
    name: Build for ${{ matrix.os }} (${{ matrix.target }})
    needs: create-release
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          # Windows
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            artifact_name: hbsx.exe
            asset_name: hbsx-windows-x86_64.exe
          - os: windows-latest
            target: aarch64-pc-windows-msvc
            artifact_name: hbsx.exe
            asset_name: hbsx-windows-aarch64.exe

          # Linux
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            artifact_name: hbsx
            asset_name: hbsx-linux-x86_64
          - os: ubuntu-24.04-arm
            target: aarch64-unknown-linux-gnu
            artifact_name: hbsx
            asset_name: hbsx-linux-aarch64
          - os: ubuntu-latest
            target: x86_64-unknown-linux-musl
            artifact_name: hbsx
            asset_name: hbsx-linux-x86_64-musl

          # macOS
          - os: macos-latest
            target: x86_64-apple-darwin
            artifact_name: hbsx
            asset_name: hbsx-macos-x86_64
          - os: macos-latest
            target: aarch64-apple-darwin
            artifact_name: hbsx
            asset_name: hbsx-macos-aarch64

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

      - name: Update Cargo.toml version
        shell: python
        run: |
          import re
          version = "${{ needs.create-release.outputs.version }}"
          print(f"Updating Cargo.toml to version {version}")
          
          with open('Cargo.toml', 'r', encoding='utf-8') as f:
              content = f.read()
          
          # 更新 version 字段(在 [package] 部分)
          content = re.sub(
              r'^version\s*=\s*"[^"]*"',
              f'version = "{version}"',
              content,
              count=1,
              flags=re.MULTILINE
          )
          
          with open('Cargo.toml', 'w', encoding='utf-8') as f:
              f.write(content)
          
          # 验证更新
          with open('Cargo.toml', 'r', encoding='utf-8') as f:
              for line in f:
                  if line.startswith('version ='):
                      print(f"Updated: {line.strip()}")
                      break

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}

      - name: Install musl tools (Linux musl)
        if: matrix.target == 'x86_64-unknown-linux-musl'
        run: |
          sudo apt-get update
          sudo apt-get install -y musl-tools

      - name: Cache cargo registry
        uses: actions/cache@v4
        with:
          path: ~/.cargo/registry
          key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}

      - name: Cache cargo index
        uses: actions/cache@v4
        with:
          path: ~/.cargo/git
          key: ${{ runner.os }}-cargo-git-${{ hashFiles('**/Cargo.lock') }}

      - name: Cache target directory
        uses: actions/cache@v4
        with:
          path: target
          key: ${{ runner.os }}-${{ matrix.target }}-cargo-target-${{ hashFiles('**/Cargo.lock') }}

      - name: Show build environment
        run: |
          echo "Target: ${{ matrix.target }}"
          echo "OS: ${{ matrix.os }}"
          echo "Architecture: $(uname -m)"
          echo "Rust version:"
          rustc --version
          cargo --version

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

      - name: Strip binary (Linux and macOS)
        if: matrix.os != 'windows-latest'
        run: strip target/${{ matrix.target }}/release/${{ matrix.artifact_name }}

      - name: Compress binary (Linux and macOS)
        if: matrix.os != 'windows-latest'
        run: |
          cd target/${{ matrix.target }}/release
          tar czf ${{ matrix.asset_name }}.tar.gz ${{ matrix.artifact_name }}
          mv ${{ matrix.asset_name }}.tar.gz ../../../

      - name: Compress binary (Windows)
        if: matrix.os == 'windows-latest'
        shell: pwsh
        run: |
          cd target/${{ matrix.target }}/release
          Compress-Archive -Path ${{ matrix.artifact_name }} -DestinationPath ../../../${{ matrix.asset_name }}.zip

      - name: Upload Release Asset (Linux and macOS)
        if: matrix.os != 'windows-latest'
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.create-release.outputs.upload_url }}
          asset_path: ./${{ matrix.asset_name }}.tar.gz
          asset_name: ${{ matrix.asset_name }}.tar.gz
          asset_content_type: application/gzip

      - name: Upload Release Asset (Windows)
        if: matrix.os == 'windows-latest'
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.create-release.outputs.upload_url }}
          asset_path: ./${{ matrix.asset_name }}.zip
          asset_name: ${{ matrix.asset_name }}.zip
          asset_content_type: application/zip

      - name: Generate SHA256 checksums (Linux and macOS)
        if: matrix.os != 'windows-latest'
        run: |
          sha256sum ${{ matrix.asset_name }}.tar.gz > ${{ matrix.asset_name }}.tar.gz.sha256

      - name: Generate SHA256 checksums (Windows)
        if: matrix.os == 'windows-latest'
        shell: pwsh
        run: |
          $hash = (Get-FileHash -Path ${{ matrix.asset_name }}.zip -Algorithm SHA256).Hash.ToLower()
          "$hash  ${{ matrix.asset_name }}.zip" | Out-File -FilePath ${{ matrix.asset_name }}.zip.sha256 -Encoding utf8 -NoNewline

      - name: Upload SHA256 checksum (Linux and macOS)
        if: matrix.os != 'windows-latest'
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.create-release.outputs.upload_url }}
          asset_path: ./${{ matrix.asset_name }}.tar.gz.sha256
          asset_name: ${{ matrix.asset_name }}.tar.gz.sha256
          asset_content_type: text/plain

      - name: Upload SHA256 checksum (Windows)
        if: matrix.os == 'windows-latest'
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.create-release.outputs.upload_url }}
          asset_path: ./${{ matrix.asset_name }}.zip.sha256
          asset_name: ${{ matrix.asset_name }}.zip.sha256
          asset_content_type: text/plain

      # 保存构建产物用于 npm 发布
      - name: Upload artifact for npm publishing
        uses: actions/upload-artifact@v4
        with:
          name: binary-${{ matrix.target }}
          path: target/${{ matrix.target }}/release/${{ matrix.artifact_name }}
          retention-days: 1

  publish-npm:
    name: Publish to npm
    needs: [create-release, build]
    runs-on: ubuntu-latest
    permissions:
      contents: read
      id-token: write
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          registry-url: 'https://registry.npmjs.org'

      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts

      - name: List downloaded artifacts
        run: |
          echo "Downloaded artifacts:"
          ls -R artifacts/

      - name: Copy binaries to platform packages
        run: |
          # 创建 bin 目录
          mkdir -p package/npm/platform-packages/win32-x64/bin
          mkdir -p package/npm/platform-packages/win32-arm64/bin
          mkdir -p package/npm/platform-packages/linux-x64/bin
          mkdir -p package/npm/platform-packages/linux-arm64/bin
          mkdir -p package/npm/platform-packages/darwin-x64/bin
          mkdir -p package/npm/platform-packages/darwin-arm64/bin

          # 复制 Windows 二进制
          cp artifacts/binary-x86_64-pc-windows-msvc/hbsx.exe package/npm/platform-packages/win32-x64/bin/
          cp artifacts/binary-aarch64-pc-windows-msvc/hbsx.exe package/npm/platform-packages/win32-arm64/bin/

          # 复制 Linux 二进制 (使用 musl 版本作为主要版本)
          cp artifacts/binary-x86_64-unknown-linux-musl/hbsx package/npm/platform-packages/linux-x64/bin/
          cp artifacts/binary-aarch64-unknown-linux-gnu/hbsx package/npm/platform-packages/linux-arm64/bin/

          # 复制 macOS 二进制
          cp artifacts/binary-x86_64-apple-darwin/hbsx package/npm/platform-packages/darwin-x64/bin/
          cp artifacts/binary-aarch64-apple-darwin/hbsx package/npm/platform-packages/darwin-arm64/bin/

          # 设置执行权限
          chmod +x package/npm/platform-packages/*/bin/hbsx

      - name: Update package versions
        run: |
          VERSION="${{ needs.create-release.outputs.version }}"
          echo "Updating packages to version $VERSION"
          
          # 使用 Python 脚本更新所有 package.json 文件
          cd package/npm
          
          export VERSION
          python3 << 'EOF'
          import json
          import os
          import sys
          
          version = os.environ.get('VERSION', '')
          if not version:
              print('ERROR: VERSION environment variable is not set')
              sys.exit(1)
          
          print(f'Updating to version {version}')
          
          # 更新主包版本
          with open('package.json', 'r', encoding='utf-8') as f:
              pkg = json.load(f)
          
          pkg['version'] = version
          for dep in pkg.get('optionalDependencies', {}):
              pkg['optionalDependencies'][dep] = version
          
          with open('package.json', 'w', encoding='utf-8') as f:
              json.dump(pkg, f, indent=2, ensure_ascii=False)
              f.write('\n')
          
          print(f'✓ Main package updated to {version}')
          
          # 更新平台包版本
          platforms = ['win32-x64', 'win32-arm64', 'linux-x64', 'linux-arm64', 'darwin-x64', 'darwin-arm64']
          for platform in platforms:
              pkg_path = f'platform-packages/{platform}/package.json'
              with open(pkg_path, 'r', encoding='utf-8') as f:
                  pkg = json.load(f)
              
              pkg['version'] = version
              
              with open(pkg_path, 'w', encoding='utf-8') as f:
                  json.dump(pkg, f, indent=2, ensure_ascii=False)
                  f.write('\n')
              
              print(f'✓ Platform package {platform} updated to {version}')
          
          print(f'✓ All package versions updated to {version}')
          EOF
          
          # 验证版本更新
          echo "Verifying version updates..."
          MAIN_VERSION=$(node -p "require('./package.json').version")
          if [ "$MAIN_VERSION" != "$VERSION" ]; then
            echo "ERROR: Main package version mismatch: $MAIN_VERSION != $VERSION"
            exit 1
          fi
          echo "✓ Version verification passed: $VERSION"

      - name: Publish platform packages
        run: |
          cd package/npm
          
          # 发布平台包
          for platform in win32-x64 win32-arm64 linux-x64 linux-arm64 darwin-x64 darwin-arm64; do
            echo "Publishing @jihuayu/hbsx-$platform..."
            cd platform-packages/$platform
            
            # 检查包是否已存在
            if npm view @jihuayu/hbsx-$platform@${{ needs.create-release.outputs.version }} version 2>/dev/null; then
              echo "⚠️  Package @jihuayu/hbsx-$platform@${{ needs.create-release.outputs.version }} already exists, skipping..."
            else
              npm publish --provenance --access public || { echo "❌ Failed to publish $platform"; exit 1; }
              echo "✓ Published @jihuayu/hbsx-$platform"
            fi
            
            cd ../..
          done
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

      - name: Publish main package
        run: |
          cd package/npm
          
          # 检查主包是否已存在
          if npm view @jihuayu/hbsx@${{ needs.create-release.outputs.version }} version 2>/dev/null; then
            echo "⚠️  Package @jihuayu/hbsx@${{ needs.create-release.outputs.version }} already exists, skipping..."
          else
            echo "Publishing @jihuayu/hbsx..."
            npm publish --provenance --access public || { echo "❌ Failed to publish main package"; exit 1; }
            echo "✓ Published @jihuayu/hbsx"
          fi
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
      - name: Summary
        run: |
          echo "✓ All npm packages published successfully!"
          echo "Version: ${{ needs.create-release.outputs.version }}"
          echo ""
          echo "Install with:"
          echo "  npm install -g @jihuayu/hbsx@${{ needs.create-release.outputs.version }}"

  publish-crates:
    name: Publish to crates.io
    needs: [create-release, build]
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Update Cargo.toml version
        shell: python
        run: |
          import re
          version = "${{ needs.create-release.outputs.version }}"
          print(f"Updating Cargo.toml to version {version}")
          
          with open('Cargo.toml', 'r', encoding='utf-8') as f:
              content = f.read()
          
          # 更新 version 字段
          content = re.sub(
              r'^version\s*=\s*"[^"]*"',
              f'version = "{version}"',
              content,
              count=1,
              flags=re.MULTILINE
          )
          
          with open('Cargo.toml', 'w', encoding='utf-8') as f:
              f.write(content)
          
          print(f"✓ Cargo.toml updated to version {version}")

      - name: Verify package
        run: cargo package --allow-dirty

      - name: Publish to crates.io
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: |
          echo "Publishing hbsx to crates.io..."
          cargo publish --allow-dirty --token "$CARGO_REGISTRY_TOKEN"
          echo "✓ Published to crates.io"

      - name: Summary
        run: |
          echo "✓ Package published to crates.io successfully!"
          echo "Version: ${{ needs.create-release.outputs.version }}"
          echo ""
          echo "Install with:"
          echo "  cargo install hbsx"