2020-08-11 10:20:52 -07:00
|
|
|
import * as core from '@actions/core'
|
2020-08-26 08:51:32 -07:00
|
|
|
import * as tc from '@actions/tool-cache'
|
|
|
|
import * as exec from '@actions/exec'
|
2020-08-11 10:20:52 -07:00
|
|
|
import * as path from 'path'
|
2020-08-26 08:51:32 -07:00
|
|
|
import * as fs from 'fs'
|
|
|
|
import * as os from 'os'
|
2020-08-11 10:20:52 -07:00
|
|
|
|
2021-01-25 15:16:38 -08:00
|
|
|
const CMDLINE_TOOLS_VERSION = '3.0'
|
2020-10-28 20:43:13 -07:00
|
|
|
const COMMANDLINE_TOOLS_VERSION = '6858069'
|
2020-10-28 21:04:42 -07:00
|
|
|
|
2020-08-26 08:51:32 -07:00
|
|
|
const COMMANDLINE_TOOLS_WIN_URL = `https://dl.google.com/android/repository/commandlinetools-win-${COMMANDLINE_TOOLS_VERSION}_latest.zip`
|
|
|
|
const COMMANDLINE_TOOLS_MAC_URL = `https://dl.google.com/android/repository/commandlinetools-mac-${COMMANDLINE_TOOLS_VERSION}_latest.zip`
|
|
|
|
const COMMANDLINE_TOOLS_LIN_URL = `https://dl.google.com/android/repository/commandlinetools-linux-${COMMANDLINE_TOOLS_VERSION}_latest.zip`
|
|
|
|
|
|
|
|
const HOME = os.homedir()
|
|
|
|
const ANDROID_HOME_DIR = path.join(HOME, '.android')
|
|
|
|
const ANDROID_HOME_SDK_DIR = path.join(ANDROID_HOME_DIR, 'sdk')
|
2021-01-25 15:16:38 -08:00
|
|
|
const ANDROID_SDK_ROOT = process.env['ANDROID_SDK_ROOT'] || ANDROID_HOME_SDK_DIR
|
|
|
|
const ANDROID_REPOSITORIES_CFG = path.join(ANDROID_SDK_ROOT, 'repositories.cfg')
|
2020-08-26 08:51:32 -07:00
|
|
|
|
2021-01-25 15:16:38 -08:00
|
|
|
function getSdkManagerPath(cmdToolsVersion: string): string {
|
|
|
|
const toolsBinDir = path.join(
|
|
|
|
ANDROID_SDK_ROOT,
|
|
|
|
'cmdline-tools',
|
|
|
|
cmdToolsVersion,
|
|
|
|
'bin'
|
|
|
|
)
|
|
|
|
if ('win32' === process.platform) {
|
|
|
|
return path.join(toolsBinDir, 'sdkmanager.bat')
|
2020-08-26 08:51:32 -07:00
|
|
|
}
|
2021-01-25 15:16:38 -08:00
|
|
|
return path.join(toolsBinDir, 'sdkmanager')
|
|
|
|
}
|
2020-08-26 08:51:32 -07:00
|
|
|
|
2021-01-25 15:16:38 -08:00
|
|
|
function findPreinstalledSdkManager(): {
|
|
|
|
isFound: boolean
|
|
|
|
isCorrectVersion: boolean
|
|
|
|
exePath: string
|
|
|
|
} {
|
|
|
|
const result = {isFound: false, isCorrectVersion: false, exePath: ''}
|
2020-08-11 10:20:52 -07:00
|
|
|
|
2021-01-25 15:16:38 -08:00
|
|
|
// First try to find the version defined in CMDLINE_TOOLS_VERSION
|
|
|
|
result.exePath = getSdkManagerPath(CMDLINE_TOOLS_VERSION)
|
|
|
|
result.isFound = fs.existsSync(result.exePath)
|
|
|
|
if (result.isFound) {
|
|
|
|
result.isCorrectVersion = true
|
|
|
|
return result
|
2020-08-26 08:51:32 -07:00
|
|
|
}
|
|
|
|
|
2021-01-25 15:16:38 -08:00
|
|
|
// cmdline-tools could have a 'latest' version, but if it was installed 2 years ago
|
|
|
|
// it may not be 'latest' as of today
|
|
|
|
result.exePath = getSdkManagerPath('latest')
|
|
|
|
result.isFound = fs.existsSync(result.exePath)
|
|
|
|
if (result.isFound) {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
result.exePath = ''
|
|
|
|
|
|
|
|
// Find whatever version is available in ANDROID_SDK_ROOT
|
|
|
|
const cmdlineToolsDir = path.join(ANDROID_SDK_ROOT, 'cmdline-tools')
|
|
|
|
const foundVersions: string[] = fs.existsSync(cmdlineToolsDir)
|
|
|
|
? fs.readdirSync(cmdlineToolsDir)
|
|
|
|
: []
|
|
|
|
const foundVersionsFiltered: string[] = foundVersions.filter(
|
|
|
|
obj => '.' !== obj && '..' !== obj
|
2020-08-26 08:51:32 -07:00
|
|
|
)
|
|
|
|
|
2021-01-25 15:16:38 -08:00
|
|
|
// Sort by desc, to get 2.0 first, before 1.0
|
|
|
|
const foundVersionsSorted: string[] = foundVersionsFiltered.sort(
|
|
|
|
(a: string, b: string) => (a > b ? -1 : 1)
|
2020-08-26 08:51:32 -07:00
|
|
|
)
|
|
|
|
|
2021-01-25 15:16:38 -08:00
|
|
|
for (const version of foundVersionsSorted) {
|
|
|
|
result.exePath = getSdkManagerPath(version)
|
|
|
|
result.isFound = fs.existsSync(result.exePath)
|
|
|
|
if (result.isFound) {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result.exePath = ''
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
async function callSdkManager(sdkManager: string, arg: string): Promise<void> {
|
|
|
|
const acceptBuffer = Buffer.from(Array(10).fill('y').join('\n'), 'utf8')
|
|
|
|
await exec.exec(sdkManager, [arg], {
|
|
|
|
input: acceptBuffer
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async function installSdkManager(): Promise<string> {
|
|
|
|
fs.mkdirSync(ANDROID_SDK_ROOT, {recursive: true})
|
|
|
|
|
|
|
|
// touch ~/.android/repositories.cfg
|
|
|
|
fs.closeSync(fs.openSync(ANDROID_REPOSITORIES_CFG, 'w'))
|
|
|
|
|
|
|
|
const sdkManager = findPreinstalledSdkManager()
|
|
|
|
if (!sdkManager.isFound) {
|
|
|
|
let cmdlineToolsURL
|
|
|
|
if (process.platform === 'linux') {
|
|
|
|
cmdlineToolsURL = COMMANDLINE_TOOLS_LIN_URL
|
|
|
|
} else if (process.platform === 'darwin') {
|
|
|
|
cmdlineToolsURL = COMMANDLINE_TOOLS_MAC_URL
|
|
|
|
} else if (process.platform === 'win32') {
|
|
|
|
cmdlineToolsURL = COMMANDLINE_TOOLS_WIN_URL
|
|
|
|
} else {
|
|
|
|
core.error(`Unsupported platform: ${process.platform}`)
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
const cmdlineToolsZip = await tc.downloadTool(cmdlineToolsURL)
|
|
|
|
const cmdlineToolsExtractedLocation = await tc.extractZip(cmdlineToolsZip)
|
|
|
|
|
|
|
|
// Move cmdline-tools to where it would be if it was installed through sdkmanager
|
|
|
|
// Will allow calling sdkmanager without --sdk_root='..' argument
|
|
|
|
const desiredLocation = path.join(
|
|
|
|
ANDROID_SDK_ROOT,
|
|
|
|
'cmdline-tools',
|
|
|
|
CMDLINE_TOOLS_VERSION
|
|
|
|
)
|
|
|
|
fs.mkdirSync(path.dirname(desiredLocation))
|
|
|
|
fs.renameSync(
|
|
|
|
path.join(cmdlineToolsExtractedLocation, 'cmdline-tools'),
|
|
|
|
desiredLocation
|
|
|
|
)
|
|
|
|
|
|
|
|
sdkManager.exePath = getSdkManagerPath(CMDLINE_TOOLS_VERSION)
|
|
|
|
sdkManager.isCorrectVersion = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sdkManager.isCorrectVersion) {
|
|
|
|
await callSdkManager(
|
|
|
|
sdkManager.exePath,
|
|
|
|
`cmdline-tools;${CMDLINE_TOOLS_VERSION}`
|
|
|
|
)
|
|
|
|
sdkManager.exePath = getSdkManagerPath(CMDLINE_TOOLS_VERSION)
|
|
|
|
}
|
|
|
|
return sdkManager.exePath
|
2020-08-26 08:51:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async function run(): Promise<void> {
|
2021-01-25 15:16:38 -08:00
|
|
|
const sdkManager = await installSdkManager()
|
|
|
|
core.debug(`sdkmanager installed to: ${sdkManager}`)
|
|
|
|
await callSdkManager(sdkManager, '--licenses')
|
|
|
|
await callSdkManager(sdkManager, 'tools')
|
|
|
|
await callSdkManager(sdkManager, 'platform-tools')
|
|
|
|
|
2020-10-28 21:04:42 -07:00
|
|
|
core.setOutput('ANDROID_COMMANDLINE_TOOLS_VERSION', COMMANDLINE_TOOLS_VERSION)
|
2020-08-11 10:20:52 -07:00
|
|
|
core.exportVariable('ANDROID_HOME', ANDROID_SDK_ROOT)
|
|
|
|
core.exportVariable('ANDROID_SDK_ROOT', ANDROID_SDK_ROOT)
|
|
|
|
|
2021-01-25 15:16:38 -08:00
|
|
|
core.addPath(path.dirname(sdkManager))
|
2020-08-11 10:20:52 -07:00
|
|
|
core.addPath(path.join(ANDROID_SDK_ROOT, 'platform-tools'))
|
|
|
|
|
|
|
|
core.debug('add matchers')
|
2020-08-26 08:51:32 -07:00
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log(`##[add-matcher]${path.join(__dirname, '..', 'matchers.json')}`)
|
2020-08-11 10:20:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
run()
|