import * as os from 'os' ; import Constants from '../Constants' ; import PowerShellToolRunner from '../Utilities/PowerShellToolRunner' ; import ScriptBuilder from './ScriptBuilder' ; export default class Utils { param azPSVersion static setPSModulePath ( azPSVersion = "" ) { let modulePath = "" ; const runner = process . env . RUNNER_OS || os . type ( ) ; switch ( runner . toLowerCase ( ) ) { case "linux" : modulePath = `/usr/share/${ azPSVersion }:` ; break ; case "windows" : case "windows_nt" : modulePath = `C:\\Modules\\${ azPSVersion };` ; break ; case "macos" : case "darwin" : throw new Error ( `OS not supported` ) ; default : throw new Error ( `Unknown os: ${ runner . toLowerCase ( ) }` ) ; } process . env . PSModulePath = `${ modulePath }${ process . env . PSModulePath }` ; } static async getLatestModule ( moduleName ) { let output = "" ; const options = { listeners : { stdout : ( data ) => { output += data . toString ( ) ; } } } ; await PowerShellToolRunner . init ( ) ; await PowerShellToolRunner . executePowerShellScriptBlock ( new ScriptBuilder ( ) . getLatestModuleScript ( moduleName ) , options ) ; const outputJson = JSON . parse ( output . trim ( ) ) ; if ( ! ( Constants . Success in outputJson ) ) { throw new Error ( outputJson [ Constants . Error ] ) ; } const azLatestVersion = outputJson [ Constants . AzVersion ] ; if ( ! Utils . isValidVersion ( azLatestVersion ) ) { throw new Error ( `Invalid AzPSVersion: ${ azLatestVersion }` ) ; } return azLatestVersion ; } static async checkModuleVersion ( moduleName , version ) { let output = "" ; const options = { listeners : { stdout : ( data ) => { output += data . toString ( ) ; } } } ; if ( ! Utils . isValidVersion ( output . trim ( ) ) ) { return "" ; } await PowerShellToolRunner . init ( ) ; await PowerShellToolRunner . executePowerShellCommand ( new ScriptBuilder ( ) . checkModuleVersionScript ( moduleName , version ) , options ) ; const outputJson = JSON . parse ( output . trim ( ) ) ; if ( ! ( Constants . Success in outputJson ) ) { throw new Error ( outputJson [ Constants . Error ] ) ; } const versionExists = outputJson [ Constants . versionExists ] . toLowerCase ( ) === "true" ; if ( ! ( versionExists ) ) { throw new Error ( "Invalid azPSVersion. Refer https://aka.ms/azure-powershell-release-notes for supported versions." ) ; } } static isValidVersion ( version ) { return ! ! version . match ( Constants . versionPattern ) ; } static async isHostedAgent ( moduleContainerPath ) { const script = `Test-Path (Join-Path "${ moduleContainerPath }" "az_*")` ; let output = "" ; const options = { listeners : { stdout : ( data ) => { output += data . toString ( ) ; } } } ; await PowerShellToolRunner . init ( ) ; await PowerShellToolRunner . executePowerShellCommand ( script , options ) ; return output . trim ( ) . toLowerCase ( ) === "true" ; } static isGhes ( ) { const ghUrl = new URL ( process . env [ 'GITHUB_SERVER_URL' ] || 'https://github.com' ) ; return ghUrl . hostname . toUpperCase ( ) !== 'GITHUB.COM' ; } static async saveAzModule ( version , modulePath ) { const script = `
            $prevProgressPref = $ProgressPreference
            $ProgressPreference = 'SilentlyContinue'
            Save-Module -Path ${ modulePath } -Name Az -RequiredVersion ${ version } -Force -ErrorAction Stop
            $ProgressPreference = $prevProgressPref` ; await PowerShellToolRunner . init ( ) ; const exitCode = await PowerShellToolRunner . executePowerShellScriptBlock ( script ) ; if ( exitCode != 0 ) { throw new Error ( `Download from PSGallery failed for Az ${ version } to ${ modulePath }` ) ; } } }