Skip to content

Commit edde936

Browse files
jessedijkstrakt3k
andauthored
feat: add .tool-versions and .dvmrc support (#61)
--------- Signed-off-by: Jesse Dijkstra <[email protected]> Co-authored-by: Yoshiya Hinosawa <[email protected]>
1 parent 041b854 commit edde936

File tree

7 files changed

+84
-3
lines changed

7 files changed

+84
-3
lines changed

.dvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.43.1

.github/workflows/test.yml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
matrix:
1414
os: [ubuntu-latest, windows-latest, macos-latest]
1515
deno:
16-
[1.x, "1.33.1", canary, ~1.32, b31cf9fde6ad5398c20370c136695db77df6beeb]
16+
[1.x, "1.33.1", canary, ~1.32, b290fd01f3f5d32f9d010fc719ced0240759c049]
1717

1818
steps:
1919
- uses: actions/checkout@v3
@@ -38,3 +38,19 @@ jobs:
3838
- name: Lint
3939
if: runner.os == 'Linux' && matrix.deno == 'canary'
4040
run: npm run lint
41+
42+
test-version-file:
43+
runs-on: ubuntu-latest
44+
strategy:
45+
matrix:
46+
deno-version-file: [.dvmrc, .tool-versions]
47+
steps:
48+
- uses: actions/checkout@v3
49+
50+
- name: Setup Deno
51+
uses: ./
52+
with:
53+
deno-version-file: ${{ matrix.deno-version-file }}
54+
55+
- name: Check version
56+
run: deno -V | grep -q "deno 1\.43\.1"

.tool-versions

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
nodejs 20.5.1
2+
bun 1.1.4
3+
ruby 3.3.0
4+
lua 5.4.4
5+
deno 1.43.1
6+
rust 1.65.0
7+
python 3.11.0

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ Set up your GitHub Actions workflow with a specific version of Deno.
44

55
## Usage
66

7+
### Version from file
8+
9+
```yaml
10+
- uses: denoland/setup-deno@v1
11+
with:
12+
deno-version-file: .dvmrc
13+
```
14+
15+
```yaml
16+
- uses: denoland/setup-deno@v1
17+
with:
18+
deno-version-file: .tool-versions
19+
```
20+
721
### Latest stable for a major
822
923
```yaml

action.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ inputs:
88
deno-version:
99
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.
1010
default: "1.x"
11+
deno-version-file:
12+
description: File containing the Deno version to install such as .dvmrc or .tool-versions.
1113
outputs:
1214
deno-version:
1315
description: "The Deno version that was installed."

main.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
const process = require("process");
22
const core = require("@actions/core");
33

4-
const { parseVersionRange, resolveVersion } = require("./src/version.js");
4+
const {
5+
parseVersionRange,
6+
getDenoVersionFromFile,
7+
resolveVersion,
8+
} = require("./src/version.js");
59
const { install } = require("./src/install.js");
610

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

1620
async function main() {
1721
try {
18-
const range = parseVersionRange(core.getInput("deno-version"));
22+
const denoVersionFile = core.getInput("deno-version-file");
23+
const range = parseVersionRange(
24+
denoVersionFile
25+
? getDenoVersionFromFile(denoVersionFile)
26+
: core.getInput("deno-version"),
27+
);
28+
1929
if (range === null) {
2030
exit("The passed version range is not valid.");
2131
}

src/version.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const semver = require("semver");
22
const { fetch } = require("undici");
3+
const fs = require("fs");
34

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

@@ -41,6 +42,35 @@ function parseVersionRange(version) {
4142
return null;
4243
}
4344

45+
/**
46+
* Parses the version from the version file
47+
*
48+
* @param {string} versionFilePath
49+
* @returns {string | undefined}
50+
*/
51+
function getDenoVersionFromFile(versionFilePath) {
52+
if (!fs.existsSync(versionFilePath)) {
53+
throw new Error(
54+
`The specified node version file at: ${versionFilePath} does not exist`,
55+
);
56+
}
57+
58+
const contents = fs.readFileSync(versionFilePath, "utf8");
59+
60+
// .tool-versions typically looks like
61+
// ```
62+
// ruby 2.6.5
63+
// deno 1.43.1
64+
// node 20.0.0
65+
// ```
66+
// This parses the version of Deno from the file
67+
const denoVersionInToolVersions = contents.match(
68+
/^deno\s+v?(?<version>[^\s]+)$/m,
69+
);
70+
71+
return denoVersionInToolVersions?.groups?.version || contents.trim();
72+
}
73+
4474
/**
4575
* @param {VersionRange} range
4676
* @returns {Promise<Version | null>}
@@ -115,4 +145,5 @@ async function fetchWithRetries(url, maxRetries = 5) {
115145
module.exports = {
116146
parseVersionRange,
117147
resolveVersion,
148+
getDenoVersionFromFile,
118149
};

0 commit comments

Comments
 (0)