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,
"tabWidth": 2,
"tabWidth": 4,
"useTabs": false,
"semi": false,
"singleQuote": true,

View file

@ -1,3 +1,3 @@
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",
"build": "ncc build src/main.ts --out dist/main --minify && ncc build src/post.ts --out dist/post --minify",
"test": "jest",
"all": "npm run build && npm test"
"all": "npm run format && npm run build && npm test"
},
"repository": {
"type": "git",

View file

@ -1,31 +1,33 @@
import * as exec from "@actions/exec";
import * as exec from '@actions/exec'
export async function execute(executable: string, root: string, argv: string[]): Promise<BuildResult> {
let publishing = false;
let buildScanUrl: string | undefined;
export async function execute(
executable: string,
root: string,
argv: string[]
): Promise<BuildResult> {
let publishing = false
let buildScanUrl: string | undefined
const status: number = await exec.exec(executable, argv, {
cwd: root,
ignoreReturnCode: true,
listeners: {
stdline: (line: string) => {
if (line.startsWith("Publishing build scan...")) {
publishing = true;
if (line.startsWith('Publishing build scan...')) {
publishing = true
}
if (publishing && line.length == 0) {
publishing = false
}
if (publishing && line.startsWith("http")) {
buildScanUrl = line.trim();
if (publishing && line.startsWith('http')) {
buildScanUrl = line.trim()
publishing = false
}
}
}
});
})
return new BuildResultImpl(status, buildScanUrl);
return new BuildResultImpl(status, buildScanUrl)
}
export interface BuildResult {
@ -34,9 +36,5 @@ export interface BuildResult {
}
class BuildResultImpl implements BuildResult {
constructor(
readonly status: number,
readonly buildScanUrl?: string
) {
}
constructor(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 {
return IS_WINDOWS ? "gradlew.bat" : "gradlew";
return IS_WINDOWS ? 'gradlew.bat' : 'gradlew'
}
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 path from "path";
import {parseArgsStringToArgv} from "string-argv";
import * as execution from "./execution";
import * as gradlew from "./gradlew";
import * as provision from "./provision";
import * as core from '@actions/core'
import * as path from 'path'
import {parseArgsStringToArgv} from 'string-argv'
import * as execution from './execution'
import * as gradlew from './gradlew'
import * as provision from './provision'
// Invoked by GitHub Actions
export async function run() {
try {
const baseDirectory = process.env[`GITHUB_WORKSPACE`] || "";
const baseDirectory = process.env[`GITHUB_WORKSPACE`] || ''
let result = await execution.execute(
await resolveGradleExecutable(baseDirectory),
resolveBuildRootDirectory(baseDirectory),
parseCommandLineArguments()
);
)
if (result.buildScanUrl) {
core.setOutput("build-scan-url", result.buildScanUrl);
core.setOutput('build-scan-url', result.buildScanUrl)
}
if (result.status != 0) {
core.setFailed(`Gradle process exited with status ${result.status}`)
}
} catch (error) {
core.setFailed(error.message);
core.setFailed(error.message)
}
}
run();
run()
async function resolveGradleExecutable(baseDirectory: string): Promise<string> {
const gradleVersion = inputOrNull("gradle-version");
if (gradleVersion != null && gradleVersion != "wrapper") {
const gradleVersion = inputOrNull('gradle-version')
if (gradleVersion != null && gradleVersion != 'wrapper') {
return path.resolve(await provision.gradleVersion(gradleVersion))
}
const gradleExecutable = inputOrNull("gradle-executable");
const gradleExecutable = inputOrNull('gradle-executable')
if (gradleExecutable != null) {
return path.resolve(baseDirectory, gradleExecutable)
}
const wrapperDirectory = inputOrNull("wrapper-directory");
const executableDirectory = wrapperDirectory != null
? path.join(baseDirectory, wrapperDirectory)
: baseDirectory;
const wrapperDirectory = inputOrNull('wrapper-directory')
const executableDirectory =
wrapperDirectory != null
? path.join(baseDirectory, wrapperDirectory)
: baseDirectory
return path.resolve(executableDirectory, gradlew.wrapperFilename());
return path.resolve(executableDirectory, gradlew.wrapperFilename())
}
function resolveBuildRootDirectory(baseDirectory: string): string {
let buildRootDirectory = inputOrNull("build-root-directory");
let buildRootDirectory = inputOrNull('build-root-directory')
return buildRootDirectory == null
? path.resolve(baseDirectory)
: path.resolve(baseDirectory, buildRootDirectory);
: path.resolve(baseDirectory, buildRootDirectory)
}
function parseCommandLineArguments(): string[] {
const input = inputOrNull("arguments");
const input = inputOrNull('arguments')
return input == null ? [] : parseArgsStringToArgv(input)
}
function inputOrNull(name: string): string | null {
const inputString = core.getInput(name);
const inputString = core.getInput(name)
if (inputString.length == 0) {
return null;
return null
}
return inputString
}

View file

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