Skip to content

Commit 4d4a70f

Browse files
authored
Add logic for processing DOTNET_INSTALL_DIR environment variable (#332)
1 parent 70c3f4d commit 4d4a70f

9 files changed

+598
-580
lines changed

.gitattributes

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
.licenses/** -diff linguist-generated=true
1+
.licenses/** -diff linguist-generated=true

.prettierrc.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
"trailingComma": "none",
88
"bracketSpacing": false,
99
"arrowParens": "avoid",
10-
"parser": "typescript"
10+
"parser": "typescript",
11+
"endOfLine": "auto"
1112
}

README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,18 @@ Some environment variables may be necessary for your particular case or to impro
195195

196196
| **Env.variable** | **Description** | **Default value** |
197197
| ----------- | ----------- | ----------- |
198-
| DOTNET_INSTALL_DIR |Specifies a directory where .NET SDKs should be installed by the action|*isn't set*|
198+
| DOTNET_INSTALL_DIR |Specifies a directory where .NET SDKs should be installed by the action.|*default value for each OS* |
199199
| DOTNET_NOLOGO |Removes logo and telemetry message from first run of dotnet cli|*false*|
200200
| DOTNET_CLI_TELEMETRY_OPTOUT |Opt-out of telemetry being sent to Microsoft|*false*|
201201
| DOTNET_MULTILEVEL_LOOKUP |Configures whether the global install location is used as a fall-back|*true*|
202202

203+
The default value of the `DOTNET_INSTALL_DIR` environment variable depends on the operation system which is used on a runner:
204+
| **Operation system** | **Default value** |
205+
| ----------- | ----------- |
206+
| **Windows** | `C:\Program Files\dotnet` |
207+
| **Ubuntu** | `/usr/share/dotnet` |
208+
| **macOS** | `/Users/runner/.dotnet` |
209+
203210
**Example usage**:
204211
```yml
205212
build:

__tests__/authutil.test.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ import path from 'path';
55
const fakeSourcesDirForTesting = path.join(
66
__dirname,
77
'runner',
8-
path.join(
9-
Math.random()
10-
.toString(36)
11-
.substring(7)
12-
),
8+
path.join(Math.random().toString(36).substring(7)),
139
's'
1410
);
1511

dist/index.js

+522-503
Large diffs are not rendered by default.

package-lock.json

+20-17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "setup-dotnet",
3-
"version": "3.0.1",
3+
"version": "3.0.2",
44
"private": true,
55
"description": "setup dotnet action",
66
"main": "lib/setup-dotnet.js",
@@ -41,9 +41,9 @@
4141
"husky": "^8.0.1",
4242
"jest": "^27.2.5",
4343
"jest-circus": "^27.2.5",
44-
"prettier": "^1.19.1",
44+
"prettier": "^2.7.1",
4545
"ts-jest": "^27.0.5",
46-
"typescript": "^3.9.7",
46+
"typescript": "^4.8.4",
4747
"wget-improved": "^3.2.1"
4848
},
4949
"jest": {

src/installer.ts

+40-49
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as hc from '@actions/http-client';
66
import {chmodSync} from 'fs';
77
import {readdir} from 'fs/promises';
88
import path from 'path';
9+
import os from 'os';
910
import semver from 'semver';
1011
import {IS_LINUX, IS_WINDOWS} from './utils';
1112
import {QualityOptions} from './setup-dotnet';
@@ -112,40 +113,29 @@ export class DotnetVersionResolver {
112113
export class DotnetCoreInstaller {
113114
private version: string;
114115
private quality: QualityOptions;
115-
private static readonly installationDirectoryWindows = path.join(
116-
process.env['PROGRAMFILES'] + '',
117-
'dotnet'
118-
);
119-
private static readonly installationDirectoryLinux = '/usr/share/dotnet';
120-
private static readonly installationDirectoryMac = path.join(
121-
process.env['HOME'] + '',
122-
'.dotnet'
123-
);
124116

125-
static addToPath() {
126-
if (process.env['DOTNET_INSTALL_DIR']) {
127-
core.addPath(process.env['DOTNET_INSTALL_DIR']);
128-
core.exportVariable('DOTNET_ROOT', process.env['DOTNET_INSTALL_DIR']);
117+
static {
118+
const installationDirectoryWindows = path.join(
119+
process.env['PROGRAMFILES'] + '',
120+
'dotnet'
121+
);
122+
const installationDirectoryLinux = '/usr/share/dotnet';
123+
const installationDirectoryMac = path.join(
124+
process.env['HOME'] + '',
125+
'.dotnet'
126+
);
127+
const dotnetInstallDir: string | undefined =
128+
process.env['DOTNET_INSTALL_DIR'];
129+
if (dotnetInstallDir) {
130+
process.env['DOTNET_INSTALL_DIR'] =
131+
this.convertInstallPathToAbsolute(dotnetInstallDir);
129132
} else {
130133
if (IS_WINDOWS) {
131-
core.addPath(DotnetCoreInstaller.installationDirectoryWindows);
132-
core.exportVariable(
133-
'DOTNET_ROOT',
134-
DotnetCoreInstaller.installationDirectoryWindows
135-
);
136-
} else if (IS_LINUX) {
137-
core.addPath(DotnetCoreInstaller.installationDirectoryLinux);
138-
core.exportVariable(
139-
'DOTNET_ROOT',
140-
DotnetCoreInstaller.installationDirectoryLinux
141-
);
134+
process.env['DOTNET_INSTALL_DIR'] = installationDirectoryWindows;
142135
} else {
143-
// This is the default set in install-dotnet.sh
144-
core.addPath(DotnetCoreInstaller.installationDirectoryMac);
145-
core.exportVariable(
146-
'DOTNET_ROOT',
147-
DotnetCoreInstaller.installationDirectoryMac
148-
);
136+
process.env['DOTNET_INSTALL_DIR'] = IS_LINUX
137+
? installationDirectoryLinux
138+
: installationDirectoryMac;
149139
}
150140
}
151141
}
@@ -155,6 +145,23 @@ export class DotnetCoreInstaller {
155145
this.quality = quality;
156146
}
157147

148+
private static convertInstallPathToAbsolute(installDir: string): string {
149+
let transformedPath;
150+
if (path.isAbsolute(installDir)) {
151+
transformedPath = installDir;
152+
} else {
153+
transformedPath = installDir.startsWith('~')
154+
? path.join(os.homedir(), installDir.slice(1))
155+
: (transformedPath = path.join(process.cwd(), installDir));
156+
}
157+
return path.normalize(transformedPath);
158+
}
159+
160+
static addToPath() {
161+
core.addPath(process.env['DOTNET_INSTALL_DIR']!);
162+
core.exportVariable('DOTNET_ROOT', process.env['DOTNET_INSTALL_DIR']);
163+
}
164+
158165
private setQuality(
159166
dotnetVersion: DotnetVersion,
160167
scriptArguments: string[]
@@ -208,11 +215,6 @@ export class DotnetCoreInstaller {
208215
scriptArguments.push(`-ProxyBypassList ${process.env['no_proxy']}`);
209216
}
210217

211-
if (!process.env['DOTNET_INSTALL_DIR']) {
212-
process.env['DOTNET_INSTALL_DIR'] =
213-
DotnetCoreInstaller.installationDirectoryWindows;
214-
}
215-
216218
scriptPath =
217219
(await io.which('pwsh', false)) || (await io.which('powershell', true));
218220
scriptArguments = windowsDefaultOptions.concat(scriptArguments);
@@ -228,12 +230,6 @@ export class DotnetCoreInstaller {
228230
if (this.quality) {
229231
this.setQuality(dotnetVersion, scriptArguments);
230232
}
231-
232-
if (!process.env['DOTNET_INSTALL_DIR']) {
233-
process.env['DOTNET_INSTALL_DIR'] = IS_LINUX
234-
? DotnetCoreInstaller.installationDirectoryLinux
235-
: DotnetCoreInstaller.installationDirectoryMac;
236-
}
237233
}
238234
// process.env must be explicitly passed in for DOTNET_INSTALL_DIR to be used
239235
const getExecOutputOptions = {
@@ -249,16 +245,11 @@ export class DotnetCoreInstaller {
249245
throw new Error(`Failed to install dotnet ${exitCode}. ${stdout}`);
250246
}
251247

252-
return this.outputDotnetVersion(
253-
dotnetVersion.value,
254-
process.env['DOTNET_INSTALL_DIR']
255-
);
248+
return this.outputDotnetVersion(dotnetVersion.value);
256249
}
257250

258-
private async outputDotnetVersion(
259-
version,
260-
installationPath
261-
): Promise<string> {
251+
private async outputDotnetVersion(version): Promise<string> {
252+
const installationPath = process.env['DOTNET_INSTALL_DIR']!;
262253
let versionsOnRunner: string[] = await readdir(
263254
path.join(installationPath.replace(/'/g, ''), 'sdk')
264255
);

tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
3636

3737
/* Additional Checks */
38+
"useUnknownInCatchVariables": false, /* Type catch clause variables as 'unknown' instead of 'any'. */
3839
// "noUnusedLocals": true, /* Report errors on unused locals. */
3940
// "noUnusedParameters": true, /* Report errors on unused parameters. */
4041
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */

0 commit comments

Comments
 (0)