diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..d024910 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,112 @@ +name: Build and Test + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +jobs: + build-native: + strategy: + matrix: + include: + - os: ubuntu-latest + rid: linux-x64 + - os: macos-latest + rid: osx-arm64 + - os: windows-latest + rid: win-x64 + + runs-on: ${{ matrix.os }} + + steps: + - uses: actions/checkout@v4 + with: + submodules: recursive + + - name: Setup Build Tools (Windows) + if: matrix.os == 'windows-latest' + run: choco install make + + - name: Cache Native Build + uses: actions/cache@v3 + with: + path: tree-sitter/dist + key: native-${{ matrix.os }}-${{ hashFiles('tree-sitter/**/*.c', 'tree-sitter/**/*.h', 'tree-sitter/Makefile') }} + restore-keys: | + native-${{ matrix.os }}- + + - name: Build Native Libraries + working-directory: tree-sitter + run: make clean && make all + + - name: Copy Native Libraries + shell: bash + run: | + mkdir -p TypeScriptParser.Native/runtimes/${{ matrix.rid }}/native/ + cp tree-sitter/dist/* TypeScriptParser.Native/runtimes/${{ matrix.rid }}/native/ + + - name: Upload Native Libraries + uses: actions/upload-artifact@v4 + with: + name: native-${{ matrix.rid }} + path: TypeScriptParser.Native/runtimes/${{ matrix.rid }}/native/ + + build-dotnet: + needs: build-native + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - name: Setup .NET + uses: actions/setup-dotnet@v3 + with: + dotnet-version: '9.0.x' + + - name: Download Native Libraries + uses: actions/download-artifact@v4 + with: + pattern: native-* + path: TypeScriptParser.Native/runtimes/ + + - name: Cache NuGet packages + uses: actions/cache@v3 + with: + path: ~/.nuget/packages + key: nuget-${{ runner.os }}-${{ hashFiles('**/*.csproj') }} + restore-keys: | + nuget-${{ runner.os }}- + + - name: Generate Version + id: version + run: | + BASE_VERSION=$(grep '' Directory.Build.props | sed 's/.*\(.*\)<\/Version>.*/\1/') + BUILD_NUMBER=${{ github.run_number }} + VERSION="${BASE_VERSION}.${BUILD_NUMBER}" + echo "version=${VERSION}" >> $GITHUB_OUTPUT + + - name: Restore Dependencies + run: dotnet restore + + - name: Build Projects + run: dotnet build --no-restore --configuration Release -p:Version=${{ steps.version.outputs.version }} + + - name: Run Unit Tests + run: dotnet test TypeScriptParser.Tests --no-build --configuration Release --logger trx --results-directory TestResults/ + + - name: Upload Test Results + uses: actions/upload-artifact@v4 + if: always() + with: + name: test-results + path: TestResults/ + + - name: Upload Build Artifacts + uses: actions/upload-artifact@v4 + with: + name: build-artifacts + path: | + */bin/Release/ + */obj/Release/ \ No newline at end of file diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..058c421 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,128 @@ +name: Release to NuGet + +on: + push: + tags: + - 'v*' + +jobs: + build-native: + strategy: + matrix: + include: + - os: ubuntu-latest + rid: linux-x64 + - os: macos-latest + rid: osx-arm64 + - os: windows-latest + rid: win-x64 + + runs-on: ${{ matrix.os }} + + steps: + - uses: actions/checkout@v4 + with: + submodules: recursive + + - name: Setup Build Tools (Windows) + if: matrix.os == 'windows-latest' + run: choco install make + + - name: Build Native Libraries + working-directory: tree-sitter + run: make clean && make all + + - name: Copy Native Libraries + shell: bash + run: | + mkdir -p TypeScriptParser.Native/runtimes/${{ matrix.rid }}/native/ + cp tree-sitter/dist/* TypeScriptParser.Native/runtimes/${{ matrix.rid }}/native/ + + - name: Upload Native Libraries + uses: actions/upload-artifact@v4 + with: + name: native-${{ matrix.rid }} + path: TypeScriptParser.Native/runtimes/${{ matrix.rid }}/native/ + + release: + needs: build-native + runs-on: ubuntu-latest + environment: production + + steps: + - uses: actions/checkout@v4 + + - name: Setup .NET + uses: actions/setup-dotnet@v3 + with: + dotnet-version: '9.0.x' + + - name: Parse Version from Tag + id: version + run: | + TAG=${GITHUB_REF#refs/tags/v} + echo "version=${TAG}" >> $GITHUB_OUTPUT + + - name: Download Native Libraries + uses: actions/download-artifact@v4 + with: + pattern: native-* + path: TypeScriptParser.Native/runtimes/ + + - name: Cache NuGet packages + uses: actions/cache@v3 + with: + path: ~/.nuget/packages + key: nuget-${{ runner.os }}-${{ hashFiles('**/*.csproj') }} + restore-keys: | + nuget-${{ runner.os }}- + + - name: Build .NET Projects + run: | + dotnet restore + dotnet build --configuration Release -p:Version=${{ steps.version.outputs.version }} + + - name: Pack NuGet Packages + run: | + dotnet pack --no-build --configuration Release --output ./artifacts -p:Version=${{ steps.version.outputs.version }} + + - name: Run Integration Tests + run: | + dotnet test TypeScriptParser.TestPackage --configuration Release --logger trx --results-directory TestResults/ + + - name: Upload Test Results + uses: actions/upload-artifact@v4 + if: always() + with: + name: release-test-results + path: TestResults/ + + - name: Publish to NuGet + env: + NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }} + run: | + dotnet nuget push ./artifacts/*.nupkg --api-key $NUGET_API_KEY --source https://api.nuget.org/v3/index.json --skip-duplicate + + - name: Create GitHub Release + uses: softprops/action-gh-release@v2 + with: + tag_name: ${{ github.ref_name }} + name: Release ${{ steps.version.outputs.version }} + body: | + ## TypeScript Parser v${{ steps.version.outputs.version }} + + ### 🚀 Features + - 跨平台Native库自动构建 + - .NET 9.0 支持 + - NuGet包自动发布 + + ### 📦 NuGet包 + - [TypeScriptParser v${{ steps.version.outputs.version }}](https://www.nuget.org/packages/TypeScriptParser/${{ steps.version.outputs.version }}) + - [TypeScriptParser.Native v${{ steps.version.outputs.version }}](https://www.nuget.org/packages/TypeScriptParser.Native/${{ steps.version.outputs.version }}) + + ### 🔧 安装方式 + ```bash + dotnet add package TypeScriptParser --version ${{ steps.version.outputs.version }} + ``` + draft: false + prerelease: false \ No newline at end of file diff --git a/README.md b/README.md index 7445a28..9a7c31d 100644 --- a/README.md +++ b/README.md @@ -5,25 +5,31 @@ ## 构建步骤 ```bash -# 1. 恢复依赖 -dotnet restore +# 1. 初始化子模块 +git submodule update --init --recursive + +# 2. 构建Native库 +(cd tree-sitter && make clean && make all) -# 2. 构建项目 +# 3. 恢复依赖和构建 +dotnet restore dotnet build -c Release -# 3. 运行测试 +# 4. 运行测试 dotnet test --configuration Release --no-build -# 4. 打包NuGet包 +# 5. 打包NuGet包 dotnet pack -c Release --no-build -o ./artifacts ``` -## 开发构建 +## 🔄 自动化发布 + +- **推送main分支** → 自动构建和测试 +- **创建tag** → 自动发布到NuGet ```bash -# Debug模式构建和测试 -dotnet build -c Debug -dotnet test --configuration Debug --no-build +git tag v1.2.0 +git push origin v1.2.0 ``` ## 项目结构 diff --git a/TypeScriptParser/TypeScriptParser.csproj b/TypeScriptParser/TypeScriptParser.csproj index 7bfe759..12650d2 100644 --- a/TypeScriptParser/TypeScriptParser.csproj +++ b/TypeScriptParser/TypeScriptParser.csproj @@ -10,9 +10,9 @@ $(NoWarn);CS1591 - + - + diff --git a/artifacts/.gitkeep b/artifacts/.gitkeep new file mode 100644 index 0000000..e69de29