refactor: vcs detector over branch detector

pull/413/head
Webber 2022-08-29 02:15:23 +02:00
parent ccf0047c1c
commit 4cf1810c70
5 changed files with 29 additions and 20 deletions

View File

@ -5,7 +5,7 @@ import { CommandInterface } from './command/command-interface.ts';
import { configureLogger } from './middleware/logger-verbosity/index.ts'; import { configureLogger } from './middleware/logger-verbosity/index.ts';
import { CommandFactory } from './command/command-factory.ts'; import { CommandFactory } from './command/command-factory.ts';
import { Engine } from './model/engine/engine.ts'; import { Engine } from './model/engine/engine.ts';
import { branchDetection } from './middleware/branch-detection/index.ts'; import { vcsDetection } from './middleware/vcs-detection/index.ts';
export class Cli { export class Cli {
private readonly yargs: YargsInstance; private readonly yargs: YargsInstance;
@ -119,7 +119,7 @@ export class Cli {
.coerce('projectPath', async (arg) => { .coerce('projectPath', async (arg) => {
return arg.replace(/^~/, getHomeDir()).replace(/\/$/, ''); return arg.replace(/^~/, getHomeDir()).replace(/\/$/, '');
}) })
.middleware([engineDetection, branchDetection]) .middleware([engineDetection, vcsDetection])
// Todo - remove these lines with release 3.0.0 // Todo - remove these lines with release 3.0.0
.option('unityVersion', { .option('unityVersion', {

View File

@ -1,13 +0,0 @@
import System from '../../model/system/system.ts';
import { BranchDetector } from './branch-detector.ts';
export const branchDetection = async (argv) => {
const { projectPath } = argv;
const branch = await BranchDetector.getCurrentBranch(projectPath);
// Todo - determine if we ever want to run the cli on a project that has no git repo.
if (!branch) throw new Error('Running GameCI CLI on a project without a git repository is not supported.');
argv.branch = branch;
};

View File

@ -13,6 +13,7 @@ export default class BuildVersionGenerator {
this.currentBranch = currentBranch; this.currentBranch = currentBranch;
} }
// Todo - move more of the get detection logic to the vcs detection class
public async determineBuildVersion(strategy: string, inputVersion: string, allowDirtyBuild: boolean) { public async determineBuildVersion(strategy: string, inputVersion: string, allowDirtyBuild: boolean) {
log.info('Versioning strategy:', strategy); log.info('Versioning strategy:', strategy);
@ -116,8 +117,7 @@ export default class BuildVersionGenerator {
const [major, minor, patch] = `${tag}.${commits}`.split('.'); const [major, minor, patch] = `${tag}.${commits}`.split('.');
const threeDigitVersion = /^\d+$/.test(patch) ? `${major}.${minor}.${patch}` : `${major}.0.${minor}`; const threeDigitVersion = /^\d+$/.test(patch) ? `${major}.${minor}.${patch}` : `${major}.0.${minor}`;
const branch = await this.getCurrentBranch(); log.info(`Found semantic version ${threeDigitVersion} for ${this.currentBranch}@${hash}`);
log.info(`Found semantic version ${threeDigitVersion} for ${branch}@${hash}`);
return `${threeDigitVersion}`; return `${threeDigitVersion}`;
} }

View File

@ -1,13 +1,25 @@
import System from '../../model/system/system.ts'; import System from '../../model/system/system.ts';
export class BranchDetector { export class GitDetector {
public static async getCurrentBranch(projectPath) { private readonly projectPath: string;
constructor(projectPath) {
this.projectPath = projectPath;
}
public async isGitRepository() {
const { status } = await System.shellRun(`git -C '${this.projectPath}' rev-parse 2>/dev/null`);
return status.code === 0;
}
public async getCurrentBranch() {
// GitHub pull request, GitHub non pull request // GitHub pull request, GitHub non pull request
let branchName = this.headRef || this.ref?.slice(11); let branchName = this.headRef || this.ref?.slice(11);
// Local // Local
if (!branchName) { if (!branchName) {
const { status, output } = await System.shellRun('git branch --show-current', { cwd: projectPath }); const { status, output } = await System.shellRun('git branch --show-current', { cwd: this.projectPath });
if (!status.success) throw new Error('did not expect "git branch --show-current"'); if (!status.success) throw new Error('did not expect "git branch --show-current"');
branchName = output; branchName = output;
} }

View File

@ -0,0 +1,10 @@
import { GitDetector } from './git-detector.ts';
export const vcsDetection = async (argv) => {
const { projectPath } = argv;
const gitDetector = new GitDetector(projectPath);
if (await gitDetector.isGitRepository()) {
argv.branch = await gitDetector.getCurrentBranch(projectPath);
}
};