diff --git a/.github/workflows/publish-rust-client.yml b/.github/workflows/publish-rust.yml similarity index 63% rename from .github/workflows/publish-rust-client.yml rename to .github/workflows/publish-rust.yml index 546cee7..6d7d461 100644 --- a/.github/workflows/publish-rust-client.yml +++ b/.github/workflows/publish-rust.yml @@ -1,8 +1,15 @@ -name: Publish Rust Client +name: Publish Rust on: workflow_dispatch: inputs: + lib_type: + description: Library Type + required: true + type: choice + options: + - client + - program level: description: Level required: true @@ -34,7 +41,7 @@ on: jobs: test_rust: - name: Test Rust client + name: Test Rust runs-on: ubuntu-latest steps: - name: Git Checkout @@ -43,25 +50,41 @@ jobs: - name: Setup Environment uses: ./.github/actions/setup with: - cargo-cache-key: cargo-rust-client + cargo-cache-key: ${{ inputs.lib_type == 'client' && 'cargo-rust-client' || 'cargo-programs' }} clippy: true rustfmt: true solana: true - - name: Format Rust Client - run: pnpm clients:rust:format + - name: Format + run: | + if [ "${{ inputs.lib_type }}" == "client" ]; then + pnpm clients:rust:format + else + pnpm programs:format + fi - - name: Lint Rust Client - run: pnpm clients:rust:lint + - name: Lint + run: | + if [ "${{ inputs.lib_type }}" == "client" ]; then + pnpm clients:rust:lint + else + pnpm programs:lint + fi - - name: Build Programs + - name: Build Program + if: ${{ inputs.lib_type == 'client' }} run: pnpm programs:build - - name: Test Rust Client - run: pnpm clients:rust:test + - name: Test + run: | + if [ "${{ inputs.lib_type }}" == "client" ]; then + pnpm clients:rust:test + else + pnpm programs:test + fi publish_rust: - name: Publish Rust Client + name: Publish Rust runs-on: ubuntu-latest needs: test_rust permissions: @@ -73,8 +96,8 @@ jobs: - name: Setup Environment uses: ./.github/actions/setup with: - cargo-cache-key: cargo-publish-rust-client - cargo-cache-fallback-key: cargo-rust-client + cargo-cache-key: ${{ inputs.lib_type == 'client' && 'cargo-publish-rust-client' || 'cargo-publish-program' }} + cargo-cache-fallback-key: ${{ inputs.lib_type == 'client' && 'cargo-rust-client' || 'cargo-programs' }} clippy: true rustfmt: true @@ -95,7 +118,7 @@ jobs: git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com" git config --global user.name "github-actions[bot]" - - name: Publish Rust Client + - name: Publish Rust id: publish env: CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }} @@ -112,7 +135,11 @@ jobs: OPTIONS="" fi - pnpm clients:rust:publish $LEVEL $OPTIONS + if [ "${{ inputs.lib_type }}" == "client" ]; then + pnpm clients:rust:publish $LEVEL $OPTIONS + else + pnpm programs:publish $LEVEL $OPTIONS + fi - name: Push Commit and Tag if: github.event.inputs.dry_run != 'true' @@ -122,4 +149,4 @@ jobs: if: github.event.inputs.create_release == 'true' && github.event.inputs.dry_run != 'true' uses: ncipollo/release-action@v1 with: - tag: rust@v${{ steps.publish.outputs.new_version }} + tag: ${{ inputs.lib_type == 'client' && 'rust' || 'program' }}@v${{ steps.publish.outputs.new_version }} diff --git a/package.json b/package.json index e7fd99a..cab06b7 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "programs:clean": "zx ./scripts/program/clean.mjs", "programs:format": "zx ./scripts/program/format.mjs", "programs:lint": "zx ./scripts/program/lint.mjs", + "programs:publish": "zx ./scripts/publish-rust.mjs program", "solana:check": "zx ./scripts/check-solana-version.mjs", "solana:link": "zx ./scripts/link-solana-version.mjs", "generate": "pnpm generate:clients", @@ -16,11 +17,11 @@ "validator:stop": "zx ./scripts/stop-validator.mjs", "clients:js:format": "zx ./scripts/client/format-js.mjs", "clients:js:lint": "zx ./scripts/client/lint-js.mjs", - "clients:js:publish": "zx ./scripts/client/publish-js.mjs", + "clients:js:publish": "zx ./scripts/publish-js.mjs", "clients:js:test": "zx ./scripts/client/test-js.mjs", "clients:rust:format": "zx ./scripts/client/format-rust.mjs", "clients:rust:lint": "zx ./scripts/client/lint-rust.mjs", - "clients:rust:publish": "zx ./scripts/client/publish-rust.mjs", + "clients:rust:publish": "zx ./scripts/publish-rust.mjs client", "clients:rust:test": "zx ./scripts/client/test-rust.mjs", "template:upgrade": "zx ./scripts/upgrade-template.mjs" }, diff --git a/scripts/client/publish-rust.mjs b/scripts/client/publish-rust.mjs deleted file mode 100644 index b7a9fa6..0000000 --- a/scripts/client/publish-rust.mjs +++ /dev/null @@ -1,40 +0,0 @@ -#!/usr/bin/env zx -import 'zx/globals'; -import { cliArguments, getCargo, workingDirectory } from '../utils.mjs'; - -const dryRun = argv['dry-run'] ?? false; -const [level] = cliArguments(); -if (!level) { - throw new Error('A version level — e.g. "path" — must be provided.'); -} - -// Go to the client directory and install the dependencies. -cd(path.join(workingDirectory, 'clients', 'rust')); - -// Publish the new version. -const releaseArgs = dryRun - ? [] - : ['--no-push', '--no-tag', '--no-confirm', '--execute']; -await $`cargo release ${level} ${releaseArgs}`; - -// Stop here if this is a dry run. -if (dryRun) { - process.exit(0); -} - -// Get the new version. -const newVersion = getCargo(path.join('clients', 'rust')).package.version; - -// Expose the new version to CI if needed. -if (process.env.CI) { - await $`echo "new_version=${newVersion}" >> $GITHUB_OUTPUT`; -} - -// Soft reset the last commit so we can create our own commit and tag. -await $`git reset --soft HEAD~1`; - -// Commit the new version. -await $`git commit -am "Publish Rust client v${newVersion}"`; - -// Tag the new version. -await $`git tag -a rust@v${newVersion} -m "Rust client v${newVersion}"`; diff --git a/scripts/client/publish-js.mjs b/scripts/publish-js.mjs similarity index 94% rename from scripts/client/publish-js.mjs rename to scripts/publish-js.mjs index 51df188..f39cdf4 100644 --- a/scripts/client/publish-js.mjs +++ b/scripts/publish-js.mjs @@ -1,6 +1,6 @@ #!/usr/bin/env zx import 'zx/globals'; -import { cliArguments, workingDirectory } from '../utils.mjs'; +import { cliArguments, workingDirectory } from './utils.mjs'; const [level, tag = 'latest'] = cliArguments(); if (!level) { diff --git a/scripts/publish-rust.mjs b/scripts/publish-rust.mjs new file mode 100644 index 0000000..17f1e9a --- /dev/null +++ b/scripts/publish-rust.mjs @@ -0,0 +1,80 @@ +#!/usr/bin/env zx +import 'zx/globals'; +import { getCargo, workingDirectory } from './utils.mjs'; + +class Publisher { + libType; + + constructor() { + const lib = process.argv[3]; + if (!lib) { + throw new Error('A library type must be provided.'); + } + if (lib !== 'client' && lib !== 'program') { + throw new Error('Invalid library type. Allowed values are "client" or "program".'); + } + this.libType = lib; + } + + path() { + if (this.libType === 'client') { + return 'clients/rust'; + } else { + return 'program'; + } + } + + message() { + if (this.libType === 'client') { + return 'Rust client'; + } else { + return 'Program'; + } + } + + tagPrefix() { + if (this.libType === 'client') { + return 'rust'; + } else { + return 'program'; + } + } +} + +const publisher = new Publisher(); +const dryRun = argv['dry-run'] ?? false; +const [level] = process.argv.slice(4); +if (!level) { + throw new Error('A version level — e.g. "path" — must be provided.'); +} + +// Go to the directory and install the dependencies. +cd(path.join(workingDirectory, publisher.path())); + +// Publish the new version. +const releaseArgs = dryRun + ? [] + : ['--no-push', '--no-tag', '--no-confirm', '--execute']; +await $`cargo release ${level} ${releaseArgs}`; + +// Stop here if this is a dry run. +if (dryRun) { + process.exit(0); +} + +// Get the new version. +const newVersion = getCargo(publisher.path()).package.version; + +// Expose the new version to CI if needed. +if (process.env.CI) { + await $`echo "new_version=${newVersion}" >> $GITHUB_OUTPUT`; +} + +// Soft reset the last commit so we can create our own commit and tag. +await $`git reset --soft HEAD~1`; + +// Commit the new version. +await $`git commit -am "Publish ${publisher.message()} v${newVersion}"`; + +// Tag the new version. +await $`git tag -a ${publisher.tagPrefix()}@v${newVersion} -m "${publisher.message()} v${newVersion}"`;