Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add .tool-versions and .dvmrc support #61

Merged
merged 16 commits into from
Jul 5, 2024
1 change: 1 addition & 0 deletions .dvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.43.1
18 changes: 17 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
deno:
[1.x, "1.33.1", canary, ~1.32, b31cf9fde6ad5398c20370c136695db77df6beeb]
[1.x, "1.33.1", canary, ~1.32, b290fd01f3f5d32f9d010fc719ced0240759c049]

steps:
- uses: actions/checkout@v3
Expand All @@ -38,3 +38,19 @@ jobs:
- name: Lint
if: runner.os == 'Linux' && matrix.deno == 'canary'
run: npm run lint

test-version-file:
runs-on: ubuntu-latest
strategy:
matrix:
deno-version-file: [.dvmrc, .tool-versions]
steps:
- uses: actions/checkout@v3

- name: Setup Deno
uses: ./
with:
deno-version-file: ${{ matrix.deno-version-file }}

- name: Check version
run: deno -V | grep -q "deno 1\.43\.1"
7 changes: 7 additions & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
nodejs 20.5.1
bun 1.1.4
ruby 3.3.0
lua 5.4.4
deno 1.43.1
rust 1.65.0
python 3.11.0
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ Set up your GitHub Actions workflow with a specific version of Deno.

## Usage

### Version from file

```yaml
- uses: denoland/setup-deno@v1
with:
deno-version-file: .dvmrc
```

```yaml
- uses: denoland/setup-deno@v1
with:
deno-version-file: .tool-versions
```

### Latest stable for a major

```yaml
Expand Down
2 changes: 2 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ inputs:
deno-version:
description: The Deno version to install. Can be a semver version of a stable release, "canary" for the latest canary, or the Git hash of a specific canary release.
default: "1.x"
deno-version-file:
description: File containing the Deno version to install such as .dvmrc or .tool-versions.
outputs:
deno-version:
description: "The Deno version that was installed."
Expand Down
14 changes: 12 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
const process = require("process");
const core = require("@actions/core");

const { parseVersionRange, resolveVersion } = require("./src/version.js");
const {
parseVersionRange,
getDenoVersionFromFile,
resolveVersion,
} = require("./src/version.js");
const { install } = require("./src/install.js");

/**
Expand All @@ -15,7 +19,13 @@ function exit(message) {

async function main() {
try {
const range = parseVersionRange(core.getInput("deno-version"));
const denoVersionFile = core.getInput("deno-version-file");
const range = parseVersionRange(
denoVersionFile
? getDenoVersionFromFile(denoVersionFile)
: core.getInput("deno-version"),
);

if (range === null) {
exit("The passed version range is not valid.");
}
Expand Down
31 changes: 31 additions & 0 deletions src/version.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const semver = require("semver");
const { fetch } = require("undici");
const fs = require("fs");

const GIT_HASH_RE = /^[0-9a-fA-F]{40}$/;

Expand Down Expand Up @@ -41,6 +42,35 @@ function parseVersionRange(version) {
return null;
}

/**
* Parses the version from the version file
*
* @param {string} versionFilePath
* @returns {string | undefined}
*/
function getDenoVersionFromFile(versionFilePath) {
if (!fs.existsSync(versionFilePath)) {
throw new Error(
`The specified node version file at: ${versionFilePath} does not exist`,
);
}

const contents = fs.readFileSync(versionFilePath, "utf8");

// .tool-versions typically looks like
// ```
// ruby 2.6.5
// deno 1.43.1
// node 20.0.0
// ```
// This parses the version of Deno from the file
const denoVersionInToolVersions = contents.match(
/^deno\s+v?(?<version>[^\s]+)$/m,
);

return denoVersionInToolVersions?.groups?.version || contents.trim();
}

/**
* @param {VersionRange} range
* @returns {Promise<Version | null>}
Expand Down Expand Up @@ -115,4 +145,5 @@ async function fetchWithRetries(url, maxRetries = 5) {
module.exports = {
parseVersionRange,
resolveVersion,
getDenoVersionFromFile,
};
Loading