From 931dff7df86a0ee3eb65925e2f6142a45e6ae4a0 Mon Sep 17 00:00:00 2001 From: Webber Date: Thu, 23 Jun 2022 22:22:08 +0200 Subject: [PATCH] chore: load input from user and yaml --- src/dependencies.ts | 43 +++++++++++-------- src/index.ts | 1 + src/model/action.ts | 2 +- .../remote-client/caching.test.ts | 2 +- src/model/cloud-runner/remote-client/index.ts | 2 +- src/model/input-readers/action-yaml.ts | 2 +- src/model/input-readers/git-repo.ts | 2 +- .../input-readers/test-license-reader.ts | 2 +- src/model/input.ts | 2 +- src/model/unity-versioning.ts | 4 +- 10 files changed, 35 insertions(+), 27 deletions(-) diff --git a/src/dependencies.ts b/src/dependencies.ts index 5bf43990..d6245b56 100644 --- a/src/dependencies.ts +++ b/src/dependencies.ts @@ -3,7 +3,7 @@ import * as assert from 'https://deno.land/std@0.144.0/testing/asserts.ts'; import * as aws from 'https://deno.land/x/aws_api/client/mod.ts'; import * as base64 from 'https://deno.land/std@0.145.0/encoding/base64.ts'; import * as compress from 'https://deno.land/x/compress@v0.3.3/mod.ts'; -// import * as core from 'https://deno.land/x/deno_actions_core/mod.ts'; +// import * as core from 'https://deno.land/x/deno_actions_core@0.1.3/mod.ts'; import * as fs from 'https://deno.land/std@0.142.0/node/fs/promises.ts'; import * as fsSync from 'https://deno.land/std@0.142.0/fs/mod.ts'; import * as k8s from 'https://deno.land/x/kubernetes_client/mod.ts'; @@ -17,11 +17,27 @@ import * as yaml from 'https://deno.land/std@0.145.0/encoding/yaml.ts'; import { crypto } from 'https://deno.land/std@0.142.0/crypto/mod.ts'; import { v4 as uuid } from 'https://deno.land/std@0.142.0/uuid/mod.ts'; import * as http from 'https://deno.land/std@0.145.0/node/http.ts'; +import { Command } from 'https://deno.land/x/cmd@v1.2.0/commander/index.ts'; const core = { - setFailed: () => {}, - info: () => {}, - error: () => {}, + info: console.log, + error: (error) => console.error(error, error.stack), + setFailed: (failure) => console.error('setFailed:', failure), + + // Adapted from: https://github.com/actions/toolkit/blob/9b7bcb1567c9b7f134eb3c2d6bbf409a5106a956/packages/core/src/core.ts#L128 + getInput: (name, options) => { + const val: string = Deno.env.get(`INPUT_${name.replace(/ /g, '_').toUpperCase()}`) || ''; + + if (options?.required && !val) { + throw new Error(`Input required and not supplied: ${name}`); + } + + if (options && options.trimWhitespace === false) { + return val; + } + + return val.trim(); + }, }; const exec = () => { @@ -32,26 +48,17 @@ const getUnityChangeSet = () => { throw new Error('getUnityChangeSet is not implemented'); // unity-changeset' }; -const waitUntil = async (function_: () => Promise, options = {}) => { - const { timeout = 10000, interval = 1000 } = options; - if (timeout || interval) { - // TODO - do some timeout stuff here - } - - await function_(); -}; - class Writable { constructor() { throw new Error('Writable is not implemented'); // stream } } -class Command { - constructor() { - throw new Error('Command is not implemented'); // commander-ts - } -} +// class Command { +// constructor() { +// throw new Error('Command is not implemented'); // commander-ts +// } +// } const __filename = path.fromFileUrl(import.meta.url); const __dirname = path.dirname(path.fromFileUrl(import.meta.url)); diff --git a/src/index.ts b/src/index.ts index 63069b96..d34d31b5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -34,6 +34,7 @@ async function runMain() { // Set output await Output.setBuildVersion(buildParameters.buildVersion); } catch (error) { + core.error(error); core.setFailed((error as Error).message); } } diff --git a/src/model/action.ts b/src/model/action.ts index fe2ea21d..8619b96e 100644 --- a/src/model/action.ts +++ b/src/model/action.ts @@ -1,4 +1,4 @@ -import { path, __dirname } from '../dependencies.ts'; +import { path, __dirname, __filename } from '../dependencies.ts'; class Action { static get supportedPlatforms() { diff --git a/src/model/cloud-runner/remote-client/caching.test.ts b/src/model/cloud-runner/remote-client/caching.test.ts index 7add5a7d..70c2ffbc 100644 --- a/src/model/cloud-runner/remote-client/caching.test.ts +++ b/src/model/cloud-runner/remote-client/caching.test.ts @@ -48,7 +48,7 @@ describe('Cloud Runner Caching', () => { await CloudRunnerSystem.Run(`tree ${cacheFolder}`); // Compare validity to original hash - expect(fs.readFileSync(path.resolve(testFolder, 'test.txt'), { encoding: 'utf8' }).toString()).toContain( + expect(Deno.readTextFileSync(path.resolve(testFolder, 'test.txt'), { encoding: 'utf8' }).toString()).toContain( Cli.options.cacheKey, ); fs.rmdirSync(testFolder, { recursive: true }); diff --git a/src/model/cloud-runner/remote-client/index.ts b/src/model/cloud-runner/remote-client/index.ts index 75085123..2b971b78 100644 --- a/src/model/cloud-runner/remote-client/index.ts +++ b/src/model/cloud-runner/remote-client/index.ts @@ -1,4 +1,4 @@ -import { fs, assert, path } from '../../../dependencies.ts'; +import { fsSync as fs, assert, path } from '../../../dependencies.ts'; import CloudRunner from '../cloud-runner.ts'; import { CloudRunnerFolders } from '../services/cloud-runner-folders.ts'; import { Caching } from './caching.ts'; diff --git a/src/model/input-readers/action-yaml.ts b/src/model/input-readers/action-yaml.ts index 67ed5f85..349ea02a 100644 --- a/src/model/input-readers/action-yaml.ts +++ b/src/model/input-readers/action-yaml.ts @@ -7,7 +7,7 @@ export class ActionYamlReader { if (!fs.existsSync(filename)) { filename = path.join(__dirname, `..`, filename); } - this.actionYamlParsed = yaml.parse(fs.readFileSync(filename).toString()); + this.actionYamlParsed = yaml.parse(Deno.readTextFileSync(filename)); } public GetActionYamlValue(key: string) { return this.actionYamlParsed.inputs[key]?.description || 'No description found in action.yml'; diff --git a/src/model/input-readers/git-repo.ts b/src/model/input-readers/git-repo.ts index e0235952..ab4158b3 100644 --- a/src/model/input-readers/git-repo.ts +++ b/src/model/input-readers/git-repo.ts @@ -1,5 +1,5 @@ import { assert } from 'https://deno.land/std@0.142.0/testing/asserts.ts'; -import { fs } from '../../dependencies.ts'; +import { fsSync as fs } from '../../dependencies.ts'; import { CloudRunnerSystem } from '../cloud-runner/services/cloud-runner-system.ts'; import CloudRunnerLogger from '../cloud-runner/services/cloud-runner-logger.ts'; import Input from '../input.ts'; diff --git a/src/model/input-readers/test-license-reader.ts b/src/model/input-readers/test-license-reader.ts index e3526941..f237cef3 100644 --- a/src/model/input-readers/test-license-reader.ts +++ b/src/model/input-readers/test-license-reader.ts @@ -7,5 +7,5 @@ export function ReadLicense() { } const pipelineFile = path.join(__dirname, `.github`, `workflows`, `cloud-runner-k8s-pipeline.yml`); - return fs.existsSync(pipelineFile) ? yaml.parse(fs.readFileSync(pipelineFile, 'utf8')).env.UNITY_LICENSE : ''; + return fs.existsSync(pipelineFile) ? yaml.parse(Deno.readTextFileSync(pipelineFile, 'utf8')).env.UNITY_LICENSE : ''; } diff --git a/src/model/input.ts b/src/model/input.ts index 6aabb3fa..78b11e07 100644 --- a/src/model/input.ts +++ b/src/model/input.ts @@ -1,4 +1,4 @@ -import { fs, path, core } from '../dependencies.ts'; +import { fsSync as fs, path, core } from '../dependencies.ts'; import { Cli } from './cli/cli.ts'; import CloudRunnerQueryOverride from './cloud-runner/services/cloud-runner-query-override.ts'; import Platform from './platform.ts'; diff --git a/src/model/unity-versioning.ts b/src/model/unity-versioning.ts index 1ca92385..ef27cf3b 100644 --- a/src/model/unity-versioning.ts +++ b/src/model/unity-versioning.ts @@ -1,4 +1,4 @@ -import { fs, path } from '../dependencies.ts'; +import { fsSync as fs, path } from '../dependencies.ts'; export default class UnityVersioning { static get versionPattern() { @@ -19,7 +19,7 @@ export default class UnityVersioning { throw new Error(`Project settings file not found at "${filePath}". Have you correctly set the projectPath?`); } - return UnityVersioning.parse(fs.readFileSync(filePath, 'utf8')); + return UnityVersioning.parse(Deno.readTextFileSync(filePath, 'utf8')); } static parse(projectVersionTxt) {