2019-09-10 00:27:23 +07:00
// Load tempDirectory before it gets wiped by tool-cache
import * as core from '@actions/core' ;
import * as exec from '@actions/exec' ;
import * as io from '@actions/io' ;
2022-09-27 19:47:12 +07:00
import * as hc from '@actions/http-client' ;
2019-09-10 00:27:23 +07:00
import { chmodSync } from 'fs' ;
2022-09-29 22:45:25 +07:00
import { readdir } from 'fs/promises' ;
2022-09-27 19:47:12 +07:00
import path from 'path' ;
2022-10-10 19:27:29 +07:00
import os from 'os' ;
2022-09-27 19:47:12 +07:00
import semver from 'semver' ;
2022-10-04 15:22:05 +07:00
import { IS_LINUX , IS_WINDOWS } from './utils' ;
2022-09-27 19:47:12 +07:00
import { QualityOptions } from './setup-dotnet' ;
export interface DotnetVersion {
type : string ;
value : string ;
qualityFlag : boolean ;
}
2019-09-10 00:27:23 +07:00
2022-09-27 19:47:12 +07:00
export class DotnetVersionResolver {
private inputVersion : string ;
private resolvedArgument : DotnetVersion ;
2020-04-05 00:23:59 +07:00
2019-09-10 00:27:23 +07:00
constructor ( version : string ) {
2022-09-27 19:47:12 +07:00
this . inputVersion = version . trim ( ) ;
this . resolvedArgument = { type : '' , value : '' , qualityFlag : false } ;
2020-04-04 23:05:12 +07:00
}
2022-09-27 19:47:12 +07:00
private async resolveVersionInput ( ) : Promise < void > {
if ( ! semver . validRange ( this . inputVersion ) ) {
throw new Error (
` 'dotnet-version' was supplied in invalid format: ${ this . inputVersion } ! Supported syntax: A.B.C, A.B, A.B.x, A, A.x `
) ;
}
if ( semver . valid ( this . inputVersion ) ) {
this . resolvedArgument . type = 'version' ;
this . resolvedArgument . value = this . inputVersion ;
} else {
const [ major , minor ] = this . inputVersion . split ( '.' ) ;
if ( this . isNumericTag ( major ) ) {
this . resolvedArgument . type = 'channel' ;
if ( this . isNumericTag ( minor ) ) {
this . resolvedArgument . value = ` ${ major } . ${ minor } ` ;
} else {
const httpClient = new hc . HttpClient ( 'actions/setup-dotnet' , [ ] , {
allowRetries : true ,
maxRetries : 3
} ) ;
this . resolvedArgument . value = await this . getLatestVersion (
httpClient ,
[ major , minor ]
) ;
}
}
this . resolvedArgument . qualityFlag = + major >= 6 ? true : false ;
2020-04-05 20:37:29 +07:00
}
}
2022-09-27 19:47:12 +07:00
private isNumericTag ( versionTag ) : boolean {
return /^\d+$/ . test ( versionTag ) ;
2019-09-10 00:27:23 +07:00
}
2022-09-27 19:47:12 +07:00
public async createDotNetVersion ( ) : Promise < {
type : string ;
value : string ;
qualityFlag : boolean ;
} > {
await this . resolveVersionInput ( ) ;
if ( ! this . resolvedArgument . type ) {
return this . resolvedArgument ;
2019-09-10 00:27:23 +07:00
}
if ( IS_WINDOWS ) {
2022-09-27 19:47:12 +07:00
this . resolvedArgument . type =
this . resolvedArgument . type === 'channel' ? '-Channel' : '-Version' ;
2019-09-10 00:27:23 +07:00
} else {
2022-09-27 19:47:12 +07:00
this . resolvedArgument . type =
this . resolvedArgument . type === 'channel' ? '--channel' : '--version' ;
}
return this . resolvedArgument ;
}
2020-09-15 23:36:09 +07:00
2022-09-27 19:47:12 +07:00
private async getLatestVersion (
httpClient : hc.HttpClient ,
versionParts : string [ ]
) : Promise < string > {
const response = await httpClient . getJson < any > (
DotnetVersionResolver . DotNetCoreIndexUrl
) ;
const result = response . result || { } ;
let releasesInfo : any [ ] = result [ 'releases-index' ] ;
2019-09-10 00:27:23 +07:00
2022-09-27 19:47:12 +07:00
let releaseInfo = releasesInfo . find ( info = > {
let sdkParts : string [ ] = info [ 'channel-version' ] . split ( '.' ) ;
return sdkParts [ 0 ] === versionParts [ 0 ] ;
} ) ;
2020-09-15 23:36:09 +07:00
2022-09-27 19:47:12 +07:00
if ( ! releaseInfo ) {
throw new Error (
` Could not find info for version ${ versionParts . join ( '.' ) } at ${
DotnetVersionResolver . DotNetCoreIndexUrl
} `
) ;
2019-09-10 00:27:23 +07:00
}
2022-09-27 19:47:12 +07:00
return releaseInfo [ 'channel-version' ] ;
2021-11-23 17:03:56 +07:00
}
2022-09-27 19:47:12 +07:00
static DotNetCoreIndexUrl : string =
2022-10-24 19:21:32 +07:00
'https://dotnetcli.azureedge.net/dotnet/release-metadata/releases-index.json' ;
2022-09-27 19:47:12 +07:00
}
export class DotnetCoreInstaller {
private version : string ;
private quality : QualityOptions ;
2022-10-10 19:27:29 +07:00
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 ) ;
2020-09-15 23:36:09 +07:00
} else {
if ( IS_WINDOWS ) {
2022-10-10 19:27:29 +07:00
process . env [ 'DOTNET_INSTALL_DIR' ] = installationDirectoryWindows ;
2020-09-15 23:36:09 +07:00
} else {
2022-10-10 19:27:29 +07:00
process . env [ 'DOTNET_INSTALL_DIR' ] = IS_LINUX
? installationDirectoryLinux
: installationDirectoryMac ;
2020-09-15 23:36:09 +07:00
}
2019-09-10 00:27:23 +07:00
}
2022-09-27 19:47:12 +07:00
}
2019-09-10 00:27:23 +07:00
2022-09-27 19:47:12 +07:00
constructor ( version : string , quality : QualityOptions ) {
this . version = version ;
this . quality = quality ;
2019-09-10 00:27:23 +07:00
}
2022-10-10 19:27:29 +07:00
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' ] ) ;
}
2022-09-27 19:47:12 +07:00
private setQuality (
dotnetVersion : DotnetVersion ,
scriptArguments : string [ ]
) : void {
const option = IS_WINDOWS ? '-Quality' : '--quality' ;
if ( dotnetVersion . qualityFlag ) {
scriptArguments . push ( option , this . quality ) ;
} else {
core . warning (
` 'dotnet-quality' input can be used only with .NET SDK version in A.B, A.B.x, A and A.x formats where the major tag is higher than 5. You specified: ${ this . version } . 'dotnet-quality' input is ignored. `
) ;
2019-09-10 00:27:23 +07:00
}
2022-09-27 19:47:12 +07:00
}
2019-09-10 00:27:23 +07:00
2022-09-29 22:45:25 +07:00
public async installDotnet ( ) : Promise < string > {
2022-09-27 19:47:12 +07:00
const windowsDefaultOptions = [
'-NoLogo' ,
'-Sta' ,
'-NoProfile' ,
'-NonInteractive' ,
'-ExecutionPolicy' ,
'Unrestricted' ,
'-Command'
] ;
const scriptName = IS_WINDOWS ? 'install-dotnet.ps1' : 'install-dotnet.sh' ;
const escapedScript = path
. join ( __dirname , '..' , 'externals' , scriptName )
. replace ( /'/g , "''" ) ;
let scriptArguments : string [ ] ;
let scriptPath = '' ;
const versionResolver = new DotnetVersionResolver ( this . version ) ;
const dotnetVersion = await versionResolver . createDotNetVersion ( ) ;
2019-11-08 23:15:28 +07:00
2022-09-27 19:47:12 +07:00
if ( IS_WINDOWS ) {
scriptArguments = [ '&' , ` ' ${ escapedScript } ' ` ] ;
2019-09-10 00:27:23 +07:00
2022-09-27 19:47:12 +07:00
if ( dotnetVersion . type ) {
scriptArguments . push ( dotnetVersion . type , dotnetVersion . value ) ;
}
2020-04-05 00:23:59 +07:00
2022-09-27 19:47:12 +07:00
if ( this . quality ) {
this . setQuality ( dotnetVersion , scriptArguments ) ;
}
2020-04-05 00:23:59 +07:00
2022-09-27 19:47:12 +07:00
if ( process . env [ 'https_proxy' ] != null ) {
scriptArguments . push ( ` -ProxyAddress ${ process . env [ 'https_proxy' ] } ` ) ;
}
// This is not currently an option
if ( process . env [ 'no_proxy' ] != null ) {
scriptArguments . push ( ` -ProxyBypassList ${ process . env [ 'no_proxy' ] } ` ) ;
}
2020-04-04 23:05:12 +07:00
2022-09-27 19:47:12 +07:00
scriptPath =
( await io . which ( 'pwsh' , false ) ) || ( await io . which ( 'powershell' , true ) ) ;
2022-09-29 22:45:25 +07:00
scriptArguments = windowsDefaultOptions . concat ( scriptArguments ) ;
2022-09-27 19:47:12 +07:00
} else {
chmodSync ( escapedScript , '777' ) ;
scriptPath = await io . which ( escapedScript , true ) ;
scriptArguments = [ ] ;
2019-09-10 00:27:23 +07:00
2022-09-27 19:47:12 +07:00
if ( dotnetVersion . type ) {
scriptArguments . push ( dotnetVersion . type , dotnetVersion . value ) ;
}
2020-09-24 22:26:00 +07:00
2022-09-27 19:47:12 +07:00
if ( this . quality ) {
this . setQuality ( dotnetVersion , scriptArguments ) ;
2019-11-08 23:15:28 +07:00
}
}
2022-10-04 15:22:05 +07:00
// process.env must be explicitly passed in for DOTNET_INSTALL_DIR to be used
const getExecOutputOptions = {
ignoreReturnCode : true ,
env : process.env as { string : string }
} ;
2023-01-26 16:35:10 +07:00
const { exitCode , stderr } = await exec . getExecOutput (
2022-09-27 19:47:12 +07:00
` " ${ scriptPath } " ` ,
scriptArguments ,
2022-10-04 15:22:05 +07:00
getExecOutputOptions
2022-09-27 19:47:12 +07:00
) ;
if ( exitCode ) {
2023-01-26 16:35:10 +07:00
throw new Error (
` Failed to install dotnet, exit code: ${ exitCode } . ${ stderr } `
) ;
2021-11-23 20:58:49 +07:00
}
2022-09-29 22:45:25 +07:00
2022-10-10 19:27:29 +07:00
return this . outputDotnetVersion ( dotnetVersion . value ) ;
2022-09-29 22:45:25 +07:00
}
2022-10-10 19:27:29 +07:00
private async outputDotnetVersion ( version ) : Promise < string > {
const installationPath = process . env [ 'DOTNET_INSTALL_DIR' ] ! ;
2022-09-29 22:45:25 +07:00
let versionsOnRunner : string [ ] = await readdir (
path . join ( installationPath . replace ( /'/g , '' ) , 'sdk' )
) ;
let installedVersion = semver . maxSatisfying ( versionsOnRunner , version , {
includePrerelease : true
} ) ! ;
return installedVersion ;
2019-09-10 00:27:23 +07:00
}
}