This commit is contained in:
Paul Merlin 2020-06-13 13:44:30 +02:00
parent b2c379621c
commit a31de8476d
8 changed files with 148 additions and 159 deletions

View File

@ -1,6 +1,6 @@
{ {
"printWidth": 80, "printWidth": 80,
"tabWidth": 2, "tabWidth": 4,
"useTabs": false, "useTabs": false,
"semi": false, "semi": false,
"singleQuote": true, "singleQuote": true,

View File

@ -1,3 +1,3 @@
describe('TODO - Add a test suite', () => { describe('TODO - Add a test suite', () => {
it('TODO - Add a test', async () => {}); it('TODO - Add a test', async () => {})
}); })

View File

@ -9,7 +9,7 @@
"lint": "eslint src/**/*.ts", "lint": "eslint src/**/*.ts",
"build": "ncc build src/main.ts --out dist/main --minify && ncc build src/post.ts --out dist/post --minify", "build": "ncc build src/main.ts --out dist/main --minify && ncc build src/post.ts --out dist/post --minify",
"test": "jest", "test": "jest",
"all": "npm run build && npm test" "all": "npm run format && npm run build && npm test"
}, },
"repository": { "repository": {
"type": "git", "type": "git",

View File

@ -1,31 +1,33 @@
import * as exec from "@actions/exec"; import * as exec from '@actions/exec'
export async function execute(
export async function execute(executable: string, root: string, argv: string[]): Promise<BuildResult> { executable: string,
root: string,
let publishing = false; argv: string[]
let buildScanUrl: string | undefined; ): Promise<BuildResult> {
let publishing = false
let buildScanUrl: string | undefined
const status: number = await exec.exec(executable, argv, { const status: number = await exec.exec(executable, argv, {
cwd: root, cwd: root,
ignoreReturnCode: true, ignoreReturnCode: true,
listeners: { listeners: {
stdline: (line: string) => { stdline: (line: string) => {
if (line.startsWith("Publishing build scan...")) { if (line.startsWith('Publishing build scan...')) {
publishing = true; publishing = true
} }
if (publishing && line.length == 0) { if (publishing && line.length == 0) {
publishing = false publishing = false
} }
if (publishing && line.startsWith("http")) { if (publishing && line.startsWith('http')) {
buildScanUrl = line.trim(); buildScanUrl = line.trim()
publishing = false publishing = false
} }
} }
} }
}); })
return new BuildResultImpl(status, buildScanUrl); return new BuildResultImpl(status, buildScanUrl)
} }
export interface BuildResult { export interface BuildResult {
@ -34,9 +36,5 @@ export interface BuildResult {
} }
class BuildResultImpl implements BuildResult { class BuildResultImpl implements BuildResult {
constructor( constructor(readonly status: number, readonly buildScanUrl?: string) {}
readonly status: number,
readonly buildScanUrl?: string
) {
}
} }

View File

@ -1,11 +1,9 @@
const IS_WINDOWS = process.platform === "win32"; const IS_WINDOWS = process.platform === 'win32'
export function wrapperFilename(): string { export function wrapperFilename(): string {
return IS_WINDOWS ? "gradlew.bat" : "gradlew"; return IS_WINDOWS ? 'gradlew.bat' : 'gradlew'
} }
export function installScriptFilename(): string { export function installScriptFilename(): string {
return IS_WINDOWS ? "gradle.bat" : "gradle"; return IS_WINDOWS ? 'gradle.bat' : 'gradle'
} }

View File

@ -1,80 +1,72 @@
import * as core from "@actions/core"; import * as core from '@actions/core'
import * as path from "path"; import * as path from 'path'
import {parseArgsStringToArgv} from "string-argv"; import {parseArgsStringToArgv} from 'string-argv'
import * as execution from "./execution";
import * as gradlew from "./gradlew";
import * as provision from "./provision";
import * as execution from './execution'
import * as gradlew from './gradlew'
import * as provision from './provision'
// Invoked by GitHub Actions // Invoked by GitHub Actions
export async function run() { export async function run() {
try { try {
const baseDirectory = process.env[`GITHUB_WORKSPACE`] || ''
const baseDirectory = process.env[`GITHUB_WORKSPACE`] || "";
let result = await execution.execute( let result = await execution.execute(
await resolveGradleExecutable(baseDirectory), await resolveGradleExecutable(baseDirectory),
resolveBuildRootDirectory(baseDirectory), resolveBuildRootDirectory(baseDirectory),
parseCommandLineArguments() parseCommandLineArguments()
); )
if (result.buildScanUrl) { if (result.buildScanUrl) {
core.setOutput("build-scan-url", result.buildScanUrl); core.setOutput('build-scan-url', result.buildScanUrl)
} }
if (result.status != 0) { if (result.status != 0) {
core.setFailed(`Gradle process exited with status ${result.status}`) core.setFailed(`Gradle process exited with status ${result.status}`)
} }
} catch (error) { } catch (error) {
core.setFailed(error.message); core.setFailed(error.message)
} }
} }
run()
run();
async function resolveGradleExecutable(baseDirectory: string): Promise<string> { async function resolveGradleExecutable(baseDirectory: string): Promise<string> {
const gradleVersion = inputOrNull('gradle-version')
const gradleVersion = inputOrNull("gradle-version"); if (gradleVersion != null && gradleVersion != 'wrapper') {
if (gradleVersion != null && gradleVersion != "wrapper") {
return path.resolve(await provision.gradleVersion(gradleVersion)) return path.resolve(await provision.gradleVersion(gradleVersion))
} }
const gradleExecutable = inputOrNull("gradle-executable"); const gradleExecutable = inputOrNull('gradle-executable')
if (gradleExecutable != null) { if (gradleExecutable != null) {
return path.resolve(baseDirectory, gradleExecutable) return path.resolve(baseDirectory, gradleExecutable)
} }
const wrapperDirectory = inputOrNull("wrapper-directory"); const wrapperDirectory = inputOrNull('wrapper-directory')
const executableDirectory = wrapperDirectory != null const executableDirectory =
? path.join(baseDirectory, wrapperDirectory) wrapperDirectory != null
: baseDirectory; ? path.join(baseDirectory, wrapperDirectory)
: baseDirectory
return path.resolve(executableDirectory, gradlew.wrapperFilename()); return path.resolve(executableDirectory, gradlew.wrapperFilename())
} }
function resolveBuildRootDirectory(baseDirectory: string): string { function resolveBuildRootDirectory(baseDirectory: string): string {
let buildRootDirectory = inputOrNull("build-root-directory"); let buildRootDirectory = inputOrNull('build-root-directory')
return buildRootDirectory == null return buildRootDirectory == null
? path.resolve(baseDirectory) ? path.resolve(baseDirectory)
: path.resolve(baseDirectory, buildRootDirectory); : path.resolve(baseDirectory, buildRootDirectory)
} }
function parseCommandLineArguments(): string[] { function parseCommandLineArguments(): string[] {
const input = inputOrNull("arguments"); const input = inputOrNull('arguments')
return input == null ? [] : parseArgsStringToArgv(input) return input == null ? [] : parseArgsStringToArgv(input)
} }
function inputOrNull(name: string): string | null { function inputOrNull(name: string): string | null {
const inputString = core.getInput(name); const inputString = core.getInput(name)
if (inputString.length == 0) { if (inputString.length == 0) {
return null; return null
} }
return inputString return inputString
} }

View File

@ -1,8 +1,8 @@
import * as core from "@actions/core"; import * as core from '@actions/core'
// Invoked by GitHub Actions // Invoked by GitHub Actions
export async function run() { export async function run() {
core.info("POST Gradle Command Action") core.info('POST Gradle Command Action')
} }
run(); run()

View File

@ -1,163 +1,164 @@
import * as fs from "fs"; import * as fs from 'fs'
import * as path from "path"; import * as path from 'path'
import * as httpm from 'typed-rest-client/HttpClient'; import * as httpm from 'typed-rest-client/HttpClient'
import * as unzip from "unzipper" import * as unzip from 'unzipper'
import * as core from "@actions/core"; import * as core from '@actions/core'
import * as io from '@actions/io'; import * as io from '@actions/io'
import * as toolCache from "@actions/tool-cache"; import * as toolCache from '@actions/tool-cache'
import * as gradlew from "./gradlew"; import * as gradlew from './gradlew'
const httpc = new httpm.HttpClient("eskatos/gradle-command-action");
const gradleVersionsBaseUrl = "https://services.gradle.org/versions";
const httpc = new httpm.HttpClient('eskatos/gradle-command-action')
const gradleVersionsBaseUrl = 'https://services.gradle.org/versions'
/** /**
* @return Gradle executable path * @return Gradle executable path
*/ */
export async function gradleVersion(gradleVersion: string): Promise<string> { export async function gradleVersion(gradleVersion: string): Promise<string> {
switch (gradleVersion) { switch (gradleVersion) {
case "current": case 'current':
return gradleCurrent(); return gradleCurrent()
case "rc": case 'rc':
return gradleReleaseCandidate(); return gradleReleaseCandidate()
case "nightly": case 'nightly':
return gradleNightly(); return gradleNightly()
case "release-nightly": case 'release-nightly':
return gradleReleaseNightly(); return gradleReleaseNightly()
default: default:
return gradle(gradleVersion); return gradle(gradleVersion)
} }
} }
async function gradleCurrent(): Promise<string> { async function gradleCurrent(): Promise<string> {
const json = await gradleVersionDeclaration(`${gradleVersionsBaseUrl}/current`); const json = await gradleVersionDeclaration(
return provisionGradle(json.version, json.downloadUrl); `${gradleVersionsBaseUrl}/current`
)
return provisionGradle(json.version, json.downloadUrl)
} }
async function gradleReleaseCandidate(): Promise<string> { async function gradleReleaseCandidate(): Promise<string> {
const json = await gradleVersionDeclaration(`${gradleVersionsBaseUrl}/release-candidate`); const json = await gradleVersionDeclaration(
`${gradleVersionsBaseUrl}/release-candidate`
)
if (json) { if (json) {
return provisionGradle(json.version, json.downloadUrl); return provisionGradle(json.version, json.downloadUrl)
} }
return gradleCurrent(); return gradleCurrent()
} }
async function gradleNightly(): Promise<string> { async function gradleNightly(): Promise<string> {
const json = await gradleVersionDeclaration(`${gradleVersionsBaseUrl}/nightly`); const json = await gradleVersionDeclaration(
return provisionGradle(json.version, json.downloadUrl); `${gradleVersionsBaseUrl}/nightly`
)
return provisionGradle(json.version, json.downloadUrl)
} }
async function gradleReleaseNightly(): Promise<string> { async function gradleReleaseNightly(): Promise<string> {
const json = await gradleVersionDeclaration(`${gradleVersionsBaseUrl}/release-nightly`); const json = await gradleVersionDeclaration(
return provisionGradle(json.version, json.downloadUrl); `${gradleVersionsBaseUrl}/release-nightly`
)
return provisionGradle(json.version, json.downloadUrl)
} }
async function gradle(version: string): Promise<string> { async function gradle(version: string): Promise<string> {
const declaration = await findGradleVersionDeclaration(version); const declaration = await findGradleVersionDeclaration(version)
if (!declaration) { if (!declaration) {
throw new Error(`Gradle version ${version} does not exists`); throw new Error(`Gradle version ${version} does not exists`)
} }
return provisionGradle(declaration.version, declaration.downloadUrl); return provisionGradle(declaration.version, declaration.downloadUrl)
} }
async function gradleVersionDeclaration(url: string): Promise<any | undefined> { async function gradleVersionDeclaration(url: string): Promise<any | undefined> {
const json: any = await httpGetJson(url); const json: any = await httpGetJson(url)
return (json.version && json.version.length > 0) ? json : undefined return json.version && json.version.length > 0 ? json : undefined
} }
async function findGradleVersionDeclaration(
async function findGradleVersionDeclaration(version: string): Promise<any | undefined> { version: string
const json: any = await httpGetJson(`${gradleVersionsBaseUrl}/all`); ): Promise<any | undefined> {
const json: any = await httpGetJson(`${gradleVersionsBaseUrl}/all`)
const found: any = json.find((entry: any) => { const found: any = json.find((entry: any) => {
return entry.version === version; return entry.version === version
}); })
return found ? found : undefined return found ? found : undefined
} }
async function provisionGradle(version: string, url: string): Promise<string> { async function provisionGradle(version: string, url: string): Promise<string> {
const cachedInstall: string = toolCache.find('gradle', version)
const cachedInstall: string = toolCache.find("gradle", version);
if (cachedInstall.length > 0) { if (cachedInstall.length > 0) {
const cachedExecutable = executableFrom(cachedInstall); const cachedExecutable = executableFrom(cachedInstall)
core.info(`Provisioned Gradle executable ${cachedExecutable}`); core.info(`Provisioned Gradle executable ${cachedExecutable}`)
return cachedExecutable; return cachedExecutable
} }
const home = process.env["HOME"] || ""; const home = process.env['HOME'] || ''
const tmpdir = path.join(home, "gradle-provision-tmpdir"); const tmpdir = path.join(home, 'gradle-provision-tmpdir')
const downloadsDir = path.join(tmpdir, "downloads"); const downloadsDir = path.join(tmpdir, 'downloads')
const installsDir = path.join(tmpdir, "installs"); const installsDir = path.join(tmpdir, 'installs')
await io.mkdirP(downloadsDir); await io.mkdirP(downloadsDir)
await io.mkdirP(installsDir); await io.mkdirP(installsDir)
core.info(`Downloading ${url}`); core.info(`Downloading ${url}`)
const downloadPath = path.join(downloadsDir, `gradle-${version}-bin.zip`); const downloadPath = path.join(downloadsDir, `gradle-${version}-bin.zip`)
await httpDownload(url, downloadPath); await httpDownload(url, downloadPath)
core.info(`Downloaded at ${downloadPath}, size ${fs.statSync(downloadPath).size}`); core.info(
`Downloaded at ${downloadPath}, size ${fs.statSync(downloadPath).size}`
)
await extractZip(downloadPath, installsDir); await extractZip(downloadPath, installsDir)
const installDir = path.join(installsDir, `gradle-${version}`); const installDir = path.join(installsDir, `gradle-${version}`)
core.info(`Extracted in ${installDir}`); core.info(`Extracted in ${installDir}`)
const executable = executableFrom(installDir); const executable = executableFrom(installDir)
fs.chmodSync(executable, "755"); fs.chmodSync(executable, '755')
core.info(`Provisioned Gradle executable ${executable}`); core.info(`Provisioned Gradle executable ${executable}`)
toolCache.cacheDir(installDir, "gradle", version); toolCache.cacheDir(installDir, 'gradle', version)
return executable; return executable
} }
function executableFrom(installDir: string): string { function executableFrom(installDir: string): string {
return path.join(installDir, "bin", `${gradlew.installScriptFilename()}`); return path.join(installDir, 'bin', `${gradlew.installScriptFilename()}`)
} }
async function httpGetJson(url: string): Promise<any> { async function httpGetJson(url: string): Promise<any> {
const response = await httpc.get(url); const response = await httpc.get(url)
const body = await response.readBody(); const body = await response.readBody()
return JSON.parse(body); return JSON.parse(body)
} }
async function httpDownload(url: string, path: string): Promise<void> { async function httpDownload(url: string, path: string): Promise<void> {
return new Promise<void>(function (resolve, reject) { return new Promise<void>(function (resolve, reject) {
const writeStream = fs.createWriteStream(path); const writeStream = fs.createWriteStream(path)
httpc.get(url).then(response => { httpc
response.message.pipe(writeStream) .get(url)
.on("close", () => { .then(response => {
resolve(); response.message
}) .pipe(writeStream)
.on("error", err => { .on('close', () => {
reject(err) resolve()
}); })
}).catch(reason => { .on('error', err => {
reject(reason); reject(err)
}); })
}); })
.catch(reason => {
reject(reason)
})
})
} }
async function extractZip(zip: string, destination: string): Promise<void> { async function extractZip(zip: string, destination: string): Promise<void> {
return new Promise<void>(function (resolve, reject) { return new Promise<void>(function (resolve, reject) {
fs.createReadStream(zip) fs.createReadStream(zip)
.pipe(unzip.Extract({"path": destination})) .pipe(unzip.Extract({path: destination}))
.on("close", () => { .on('close', () => {
resolve(); resolve()
}) })
.on("error", err => { .on('error', err => {
reject(err) reject(err)
}); })
}); })
} }