From 4cf1810c703002f04565cf0e3a64572a59b24d18 Mon Sep 17 00:00:00 2001 From: Webber Date: Mon, 29 Aug 2022 02:15:23 +0200 Subject: [PATCH] refactor: vcs detector over branch detector --- src/cli.ts | 4 ++-- src/middleware/branch-detection/index.ts | 13 ------------- .../build-version-generator.ts | 4 ++-- .../git-detector.ts} | 18 +++++++++++++++--- src/middleware/vcs-detection/index.ts | 10 ++++++++++ 5 files changed, 29 insertions(+), 20 deletions(-) delete mode 100644 src/middleware/branch-detection/index.ts rename src/middleware/{branch-detection/branch-detector.ts => vcs-detection/git-detector.ts} (65%) create mode 100644 src/middleware/vcs-detection/index.ts diff --git a/src/cli.ts b/src/cli.ts index 30a34e95..f13becc3 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -5,7 +5,7 @@ import { CommandInterface } from './command/command-interface.ts'; import { configureLogger } from './middleware/logger-verbosity/index.ts'; import { CommandFactory } from './command/command-factory.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 { private readonly yargs: YargsInstance; @@ -119,7 +119,7 @@ export class Cli { .coerce('projectPath', async (arg) => { return arg.replace(/^~/, getHomeDir()).replace(/\/$/, ''); }) - .middleware([engineDetection, branchDetection]) + .middleware([engineDetection, vcsDetection]) // Todo - remove these lines with release 3.0.0 .option('unityVersion', { diff --git a/src/middleware/branch-detection/index.ts b/src/middleware/branch-detection/index.ts deleted file mode 100644 index c90e3adf..00000000 --- a/src/middleware/branch-detection/index.ts +++ /dev/null @@ -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; -}; diff --git a/src/middleware/build-versioning/build-version-generator.ts b/src/middleware/build-versioning/build-version-generator.ts index 8be221b3..002e93c4 100644 --- a/src/middleware/build-versioning/build-version-generator.ts +++ b/src/middleware/build-versioning/build-version-generator.ts @@ -13,6 +13,7 @@ export default class BuildVersionGenerator { 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) { log.info('Versioning strategy:', strategy); @@ -116,8 +117,7 @@ export default class BuildVersionGenerator { const [major, minor, patch] = `${tag}.${commits}`.split('.'); const threeDigitVersion = /^\d+$/.test(patch) ? `${major}.${minor}.${patch}` : `${major}.0.${minor}`; - const branch = await this.getCurrentBranch(); - log.info(`Found semantic version ${threeDigitVersion} for ${branch}@${hash}`); + log.info(`Found semantic version ${threeDigitVersion} for ${this.currentBranch}@${hash}`); return `${threeDigitVersion}`; } diff --git a/src/middleware/branch-detection/branch-detector.ts b/src/middleware/vcs-detection/git-detector.ts similarity index 65% rename from src/middleware/branch-detection/branch-detector.ts rename to src/middleware/vcs-detection/git-detector.ts index 497dfea6..3f3f19b0 100644 --- a/src/middleware/branch-detection/branch-detector.ts +++ b/src/middleware/vcs-detection/git-detector.ts @@ -1,13 +1,25 @@ import System from '../../model/system/system.ts'; -export class BranchDetector { - public static async getCurrentBranch(projectPath) { +export class GitDetector { + 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 let branchName = this.headRef || this.ref?.slice(11); // Local 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"'); branchName = output; } diff --git a/src/middleware/vcs-detection/index.ts b/src/middleware/vcs-detection/index.ts new file mode 100644 index 00000000..e7aba2b0 --- /dev/null +++ b/src/middleware/vcs-detection/index.ts @@ -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); + } +};