feat: per-command parameters parsing

pull/413/head
Webber 2022-08-12 23:45:43 +02:00
parent cd3128149a
commit 18af761e9f
6 changed files with 43 additions and 11 deletions

View File

@ -1,10 +1,11 @@
import { exec, OutputMode } from 'https://deno.land/x/exec@0.0.5/mod.ts';
import { CommandInterface } from './command-interface.ts';
import { Options } from '../../config/options.ts';
import { Action, Cache, CloudRunner, Docker, ImageTag, Output } from '../../model/index.ts';
import { Action, Cache, CloudRunner, Docker, ImageTag, Input, Output } from '../../model/index.ts';
import PlatformSetup from '../../model/platform-setup.ts';
import { core, process } from '../../dependencies.ts';
import MacBuilder from '../../model/mac-builder.ts';
import Parameters from '../../model/parameters.ts';
export class BuildCommand implements CommandInterface {
public readonly name: string;
@ -13,6 +14,8 @@ export class BuildCommand implements CommandInterface {
this.name = name;
}
public async parseParameters(input: Input, parameters: Parameters) {}
public async execute(options: Options): Promise<boolean> {
try {
log.info('options', options);

View File

@ -1,7 +1,8 @@
import { CommandInterface } from './command-interface.ts';
import { Options } from '../../config/options.ts';
import { CloudRunner, ImageTag, Output } from '../../model/index.ts';
import { CloudRunner, ImageTag, Input, Output } from '../../model/index.ts';
import { core } from '../../dependencies.ts';
import Parameters from '../../model/parameters.ts';
// Todo - Verify this entire flow
export class BuildRemoteCommand implements CommandInterface {
@ -11,6 +12,8 @@ export class BuildRemoteCommand implements CommandInterface {
this.name = name;
}
public async parseParameters(input: Input, parameters: Parameters) {}
public async execute(options: Options): Promise<boolean> {
try {
const { buildParameters } = options;

View File

@ -1,6 +1,9 @@
import { Options } from '../../config/options.ts';
import Parameters from '../../model/parameters.ts';
import { Input } from '../../model/index.ts';
export interface CommandInterface {
name: string;
execute: (options: Options) => Promise<boolean>;
parseParameters: (input: Input, parameters: Parameters) => Promise<Partial<Parameters>>;
}

View File

@ -11,4 +11,6 @@ export class NonExistentCommand implements CommandInterface {
public async execute(options: Options): Promise<boolean> {
throw new Error(`Command ${this.name} does not exist`);
}
public async parseParameters() {}
}

View File

@ -1,20 +1,26 @@
import { CliArguments } from '../core/cli/cli-arguments.ts';
import { EnvVariables } from '../core/env/env-variables.ts';
import { Parameters, Input } from '../model/index.ts';
import { CommandInterface } from '../commands/command/command-interface.ts';
export class Options {
public input: Input;
public parameters: Parameters;
private readonly env: EnvVariables;
private readonly command: CommandInterface;
constructor(env: EnvVariables) {
constructor(command: CommandInterface, env: EnvVariables) {
this.input = null;
this.parameters = null;
this.env = env;
this.command = command;
return this;
}
public async generateParameters(args: CliArguments) {
this.input = new Input(args);
this.parameters = await new Parameters(this.input, this.env).parse();
this.parameters = await new Parameters(this.input, this.env).registerCommand(this.command).parse();
log.debug('Parameters generated.');

View File

@ -10,8 +10,10 @@ import { GitRepoReader } from './input-readers/git-repo.ts';
import { GithubCliReader } from './input-readers/github-cli.ts';
import { Cli } from './cli/cli.ts';
import { EnvVariables } from '../core/env/env-variables.ts';
import { CommandInterface } from '../commands/command/command-interface.ts';
class Parameters {
private command: CommandInterface;
public editorVersion!: string;
public customImage!: string;
public unitySerial!: string;
@ -99,25 +101,24 @@ class Parameters {
// Todo - Don't use process.env directly, that's what the input model class is for.
// ---
let unitySerial = '';
if (!Deno.env.get('UNITY_SERIAL') && this.input.githubInputEnabled) {
if (!this.env.UNITY_SERIAL && this.input.githubInputEnabled) {
// No serial was present, so it is a personal license that we need to convert
if (!Deno.env.get('UNITY_LICENSE')) {
if (!this.env.UNITY_LICENSE) {
throw new Error(`Missing Unity License File and no Serial was found. If this
is a personal license, make sure to follow the activation
steps and set the UNITY_LICENSE GitHub secret or enter a Unity
serial number inside the UNITY_SERIAL GitHub secret.`);
}
unitySerial = this.getSerialFromLicenseFile(Deno.env.get('UNITY_LICENSE'));
unitySerial = this.getSerialFromLicenseFile(this.env.UNITY_LICENSE);
} else {
unitySerial = Deno.env.get('UNITY_SERIAL')!;
unitySerial = this.env.UNITY_SERIAL!;
}
return {
const parameters = {
editorVersion,
customImage: this.input.customImage,
unitySerial,
runnerTempPath: Deno.env.get('RUNNER_TEMP'),
runnerTempPath: this.env.RUNNER_TEMP,
targetPlatform: this.input.targetPlatform,
projectPath: this.input.projectPath,
buildName: this.input.buildName,
@ -168,6 +169,14 @@ class Parameters {
startDependenciesOverride: this.input.startDependenciesOverride,
cacheKey: this.input.cacheKey,
};
const commandParameterOverrides = this.command.parseParameters(this.input, parameters);
// Todo - Maybe return an instance instead
return {
...parameters,
...commandParameterOverrides,
};
}
static parseBuildFile(filename, platform, androidAppBundle) {
@ -194,6 +203,12 @@ class Parameters {
// Slice off the first 4 characters as they are garbage values
return Buffer.from(license.slice(startIndex, endIndex), 'base64').toString('binary').slice(4);
}
registerCommand(command: CommandInterface) {
this.command = command;
return this;
}
}
export default Parameters;