From f83767c0cf89955b3e5ce5389aa41dd09357c101 Mon Sep 17 00:00:00 2001 From: Ade Hery Shopyan <51158020+adeherysh@users.noreply.github.com> Date: Thu, 4 Apr 2024 17:19:34 +0700 Subject: [PATCH] feat: add input for bun-version-file --- .github/workflows/test.yml | 46 ++++++++++++++++++++++++++-- README.md | 21 +++++++++---- action.yml | 3 ++ src/index.ts | 62 +++++++++++++++++++++++++++++++++----- 4 files changed, 116 insertions(+), 16 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a4896e3..5009221 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -85,6 +85,7 @@ jobs: echo "Expected version to be 1.1.0, got ${{ steps.bun.outputs.version }}" exit 1 fi + setup-bun-from-tool-versions: runs-on: ${{ matrix.os }} strategy: @@ -99,10 +100,51 @@ jobs: steps: - name: Checkout uses: actions/checkout@v4 - - name: Setup package.json + + - name: Setup .tool-versions shell: bash run: | - echo "bun ${{ matrix.content }}" > .tool-versions + echo "${{ matrix.content }}" > .tool-versions + + - name: Setup Bun + uses: ./ + + - name: Run Bun + id: bun + shell: bash + run: | + bun --version + echo "version=$(bun --version)" >> $GITHUB_OUTPUT + + - name: Check version + shell: bash + run: | + if [[ "${{ steps.bun.outputs.version }}" == "1.1.0" ]]; then + echo "Version is 1.1.0" + else + echo "Expected version to be 1.1.0, got ${{ steps.bun.outputs.version }}" + exit 1 + fi + + setup-bun-from-bumrc: + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: + - ubuntu-latest + - macos-latest + - windows-latest + content: + - "1.1.0" + - "latest" + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup .bumrc + shell: bash + run: | + echo "${{ matrix.content }}" > .bumrc - name: Setup Bun uses: ./ diff --git a/README.md b/README.md index 8eefc3f..219c1c4 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,14 @@ Download, install, and setup [Bun](https://bun.sh) in GitHub Actions. bun-version: latest ``` +## Using version file + +```yaml +- uses: oven-sh/setup-bun@v1 + with: + bun-version-file: '.bumrc' +``` + ### Using a custom NPM registry ```yaml @@ -34,12 +42,13 @@ In most cases, you shouldn't need to use the [setup-node](https://github.com/act ## Inputs -| Name | Description | Default | Examples | -| -------------- | -------------------------------------------------- | ----------- | ------------------------------- | -| `bun-version` | The version of Bun to download and install. | `latest` | `canary`, `1.0.0`, `1.0.x` | -| `registry-url` | Registry URL where some private package is stored. | `undefined` | `"https://npm.pkg.github.com/"` | -| `scope` | Scope for private packages. | `undefined` | `"@foo"`, `"@orgname"` | -| `no-cache` | Disable caching of the downloaded executable. | `false` | `true`, `false` | +| Name | Description | Default | Examples | +| ------------------- | ------------------------------------------------------ | ----------- | ------------------------------- | +| `bun-version` | The version of Bun to download and install. | `latest` | `canary`, `1.0.0`, `1.0.x` | +| `bun-version-file` | The version of Bun to download and install from file. | `undefined` | `canary`, `1.0.0`, `1.0.x` | +| `registry-url` | Registry URL where some private package is stored. | `undefined` | `"https://npm.pkg.github.com/"` | +| `scope` | Scope for private packages. | `undefined` | `"@foo"`, `"@orgname"` | +| `no-cache` | Disable caching of the downloaded executable. | `false` | `true`, `false` | ## Outputs diff --git a/action.yml b/action.yml index bfb1def..de0ea63 100644 --- a/action.yml +++ b/action.yml @@ -8,6 +8,9 @@ inputs: bun-version: description: 'The version of Bun to install. (e.g. "latest", "canary", "1.0.0", "1.0.x", )' required: false + bun-version-file: + description: 'The version of Bun to install from file. (e.g. ".bumrc")' + required: false registry-url: required: false description: "The URL of the package registry to use for installing Bun. Set the $BUN_AUTH_TOKEN environment variable to authenticate with the registry." diff --git a/src/index.ts b/src/index.ts index 08f0a62..b66f1c1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,12 +14,12 @@ if (!process.env.RUNNER_TEMP) { process.env.RUNNER_TEMP = tmpdir(); } -function readVersionFromPackageJson(): string | undefined { +function readVersionFromPackageJson(file: string): string | undefined { const cwd = process.env.GITHUB_WORKSPACE; if (!cwd) { return; } - const path = join(cwd, "package.json"); + const path = join(cwd, file); try { if (!existsSync(path)) { return; @@ -32,16 +32,16 @@ function readVersionFromPackageJson(): string | undefined { return version; } catch (error) { const { message } = error as Error; - warning(`Failed to read package.json: ${message}`); + warning(`Failed to read ${file}: ${message}`); } } -function readVersionFromToolVersions(): string | undefined { +function readVersionFromToolVersions(file: string): string | undefined { const cwd = process.env.GITHUB_WORKSPACE; if (!cwd) { return; } - const path = join(cwd, ".tool-versions"); + const path = join(cwd, file); try { if (!existsSync(path)) { return; @@ -52,15 +52,61 @@ function readVersionFromToolVersions(): string | undefined { return match?.groups?.version; } catch (error) { const { message } = error as Error; - warning(`Failed to read .tool-versions: ${message}`); + warning(`Failed to read ${file}: ${message}`); + } +} + +function readVersionFromBumrc(file: string): string | undefined { + const cwd = process.env.GITHUB_WORKSPACE; + if (!cwd) { + return; + } + const path = join(cwd, file); + try { + if (!existsSync(path)) { + return; + } + + const match = readFileSync(path, "utf8"); + + return JSON.parse(match); + } catch (error) { + const { message } = error as Error; + warning(`Failed to read ${file}: ${message}`); + } +} + +function readVersionFromFile(): string | undefined { + const cwd = process.env.GITHUB_WORKSPACE; + if (!cwd) { + return; + } + const file = getInput("bun-version-file"); + const path = join(cwd, file); + try { + if (!existsSync(path)) { + return; + } + + if (file === "package.json") { + return readVersionFromPackageJson(file) + } else if (file === ".tool-versions") { + return readVersionFromToolVersions(file) + } else if (file === ".bumrc") { + return readVersionFromBumrc(file) + } else { + warning(`Not allowed read version from ${file}`); + } + } catch (error) { + const { message } = error as Error; + warning(`Failed to read ${file}: ${message}`); } } runAction({ version: getInput("bun-version") || - readVersionFromPackageJson() || - readVersionFromToolVersions() || + readVersionFromFile() || undefined, customUrl: getInput("bun-download-url") || undefined, registryUrl: getInput("registry-url") || undefined,