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 logic for processing DOTNET_INSTALL_DIR environment variable #332

Merged
merged 5 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.licenses/** -diff linguist-generated=true
.licenses/** -diff linguist-generated=true
3 changes: 2 additions & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"trailingComma": "none",
"bracketSpacing": false,
"arrowParens": "avoid",
"parser": "typescript"
"parser": "typescript",
"endOfLine": "auto"
}
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,18 @@ Some environment variables may be necessary for your particular case or to impro

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

The default value of the `DOTNET_INSTALL_DIR` environment variable depends on the operation system which is used on a runner:
| **Operation system** | **Default value** |
| ----------- | ----------- |
| **Windows** | `C:\Program Files\dotnet` |
| **Ubuntu** | `/usr/share/dotnet` |
| **macOS** | `/Users/runner/.dotnet` |

**Example usage**:
```yml
build:
Expand Down
6 changes: 1 addition & 5 deletions __tests__/authutil.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import path from 'path';
const fakeSourcesDirForTesting = path.join(
__dirname,
'runner',
path.join(
Math.random()
.toString(36)
.substring(7)
),
path.join(Math.random().toString(36).substring(7)),
's'
);

Expand Down
1,025 changes: 522 additions & 503 deletions dist/index.js

Large diffs are not rendered by default.

37 changes: 20 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "setup-dotnet",
"version": "3.0.1",
"version": "3.0.2",
"private": true,
"description": "setup dotnet action",
"main": "lib/setup-dotnet.js",
Expand Down Expand Up @@ -41,9 +41,9 @@
"husky": "^8.0.1",
"jest": "^27.2.5",
"jest-circus": "^27.2.5",
"prettier": "^1.19.1",
"prettier": "^2.7.1",
"ts-jest": "^27.0.5",
"typescript": "^3.9.7",
"typescript": "^4.8.4",
"wget-improved": "^3.2.1"
},
"jest": {
Expand Down
89 changes: 40 additions & 49 deletions src/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as hc from '@actions/http-client';
import {chmodSync} from 'fs';
import {readdir} from 'fs/promises';
import path from 'path';
import os from 'os';
import semver from 'semver';
import {IS_LINUX, IS_WINDOWS} from './utils';
import {QualityOptions} from './setup-dotnet';
Expand Down Expand Up @@ -112,40 +113,29 @@ export class DotnetVersionResolver {
export class DotnetCoreInstaller {
private version: string;
private quality: QualityOptions;
private static readonly installationDirectoryWindows = path.join(
process.env['PROGRAMFILES'] + '',
'dotnet'
);
private static readonly installationDirectoryLinux = '/usr/share/dotnet';
private static readonly installationDirectoryMac = path.join(
process.env['HOME'] + '',
'.dotnet'
);

static addToPath() {
if (process.env['DOTNET_INSTALL_DIR']) {
core.addPath(process.env['DOTNET_INSTALL_DIR']);
core.exportVariable('DOTNET_ROOT', process.env['DOTNET_INSTALL_DIR']);
static {
const installationDirectoryWindows = path.join(
process.env['PROGRAMFILES'] + '',
'dotnet'
);
const installationDirectoryLinux = '/usr/share/dotnet';
const installationDirectoryMac = path.join(
process.env['HOME'] + '',
'.dotnet'
);
const dotnetInstallDir: string | undefined =
process.env['DOTNET_INSTALL_DIR'];
if (dotnetInstallDir) {
process.env['DOTNET_INSTALL_DIR'] =
this.convertInstallPathToAbsolute(dotnetInstallDir);
} else {
if (IS_WINDOWS) {
core.addPath(DotnetCoreInstaller.installationDirectoryWindows);
core.exportVariable(
'DOTNET_ROOT',
DotnetCoreInstaller.installationDirectoryWindows
);
} else if (IS_LINUX) {
core.addPath(DotnetCoreInstaller.installationDirectoryLinux);
core.exportVariable(
'DOTNET_ROOT',
DotnetCoreInstaller.installationDirectoryLinux
);
process.env['DOTNET_INSTALL_DIR'] = installationDirectoryWindows;
} else {
// This is the default set in install-dotnet.sh
core.addPath(DotnetCoreInstaller.installationDirectoryMac);
core.exportVariable(
'DOTNET_ROOT',
DotnetCoreInstaller.installationDirectoryMac
);
process.env['DOTNET_INSTALL_DIR'] = IS_LINUX
? installationDirectoryLinux
: installationDirectoryMac;
}
}
}
Expand All @@ -155,6 +145,23 @@ export class DotnetCoreInstaller {
this.quality = quality;
}

private static convertInstallPathToAbsolute(installDir: string): string {
let transformedPath;
if (path.isAbsolute(installDir)) {
transformedPath = installDir;
} else {
transformedPath = installDir.startsWith('~')
? path.join(os.homedir(), installDir.slice(1))
: (transformedPath = path.join(process.cwd(), installDir));
}
return path.normalize(transformedPath);
}

static addToPath() {
core.addPath(process.env['DOTNET_INSTALL_DIR']!);
core.exportVariable('DOTNET_ROOT', process.env['DOTNET_INSTALL_DIR']);
}

private setQuality(
dotnetVersion: DotnetVersion,
scriptArguments: string[]
Expand Down Expand Up @@ -208,11 +215,6 @@ export class DotnetCoreInstaller {
scriptArguments.push(`-ProxyBypassList ${process.env['no_proxy']}`);
}

if (!process.env['DOTNET_INSTALL_DIR']) {
process.env['DOTNET_INSTALL_DIR'] =
DotnetCoreInstaller.installationDirectoryWindows;
}

scriptPath =
(await io.which('pwsh', false)) || (await io.which('powershell', true));
scriptArguments = windowsDefaultOptions.concat(scriptArguments);
Expand All @@ -228,12 +230,6 @@ export class DotnetCoreInstaller {
if (this.quality) {
this.setQuality(dotnetVersion, scriptArguments);
}

if (!process.env['DOTNET_INSTALL_DIR']) {
process.env['DOTNET_INSTALL_DIR'] = IS_LINUX
? DotnetCoreInstaller.installationDirectoryLinux
: DotnetCoreInstaller.installationDirectoryMac;
}
}
// process.env must be explicitly passed in for DOTNET_INSTALL_DIR to be used
const getExecOutputOptions = {
Expand All @@ -249,16 +245,11 @@ export class DotnetCoreInstaller {
throw new Error(`Failed to install dotnet ${exitCode}. ${stdout}`);
}

return this.outputDotnetVersion(
dotnetVersion.value,
process.env['DOTNET_INSTALL_DIR']
);
return this.outputDotnetVersion(dotnetVersion.value);
}

private async outputDotnetVersion(
version,
installationPath
): Promise<string> {
private async outputDotnetVersion(version): Promise<string> {
const installationPath = process.env['DOTNET_INSTALL_DIR']!;
let versionsOnRunner: string[] = await readdir(
path.join(installationPath.replace(/'/g, ''), 'sdk')
);
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */

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