Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 51 additions & 19 deletions dist/tools/libs/gitversion.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as path from 'node:path';
import * as fs from 'node:fs/promises';
import * as crypto from 'node:crypto';
import { S as SettingsProvider, D as DotnetTool, k as keysOf, A as ArgumentsBuilder, R as RunnerBase } from './tools.mjs';
import 'node:crypto';
import 'node:fs/promises';
import 'node:os';
import 'node:path';
import './semver.mjs';
import { allIndexesOf } from '../lib.mjs';

Expand Down Expand Up @@ -63,9 +63,12 @@ class GitVersionTool extends DotnetTool {
const settings = this.settingsProvider.getExecuteSettings();
const workDir = await this.getRepoDir(settings);
await this.checkShallowClone(settings, workDir);
const args = await this.getExecuteArguments(workDir, settings);
const outputFile = path.join(this.buildAgent.tempDir, `gitversion-${crypto.randomUUID()}.json`);
this.buildAgent.debug(`Writing GitVersion variables to file: ${outputFile}`);
const args = await this.getExecuteArguments(workDir, settings, outputFile);
await this.setDotnetRoot();
return await this.executeTool(args);
const result = await this.executeTool(args);
return { ...result, outputFile };
}
async executeCommand() {
const settings = this.settingsProvider.getCommandSettings();
Expand Down Expand Up @@ -104,8 +107,14 @@ class GitVersionTool extends DotnetTool {
async getRepoDir(settings) {
return await super.getRepoPath(settings.targetPath);
}
async getExecuteArguments(workDir, options) {
const builder = new ArgumentsBuilder().addArgument(workDir).addArgument("/output").addArgument("json").addArgument("/l").addArgument("console");
async getExecuteArguments(workDir, options, outputFile) {
const builder = new ArgumentsBuilder().addArgument(workDir);
if (outputFile) {
builder.addArgument("/output").addArgument("file").addArgument("/outputfile").addArgument(outputFile);
} else {
builder.addArgument("/output").addArgument("json");
}
builder.addArgument("/l").addArgument("console");
const {
disableCache,
disableNormalization,
Expand Down Expand Up @@ -169,6 +178,13 @@ class GitVersionTool extends DotnetTool {
}
}
}
async readGitVersionOutput(outputFile) {
const content = await fs.readFile(outputFile, "utf8");
const output = JSON.parse(content);
await fs.unlink(outputFile).catch(() => {
});
return output;
}
toCamelCase(input) {
return input.replace(/^\w|[A-Z]|\b\w|\s+/g, function(match, index) {
if (+match === 0) return "";
Expand Down Expand Up @@ -203,33 +219,49 @@ class Runner extends RunnerBase {
async execute() {
return this.safeExecute(async () => {
const result = await this.tool.executeJson();
return this.processGitVersionOutput(result);
return await this.processGitVersionOutput(result);
}, "GitVersion executed successfully");
}
async command() {
return this.safeExecute(async () => await this.tool.executeCommand(), "GitVersion executed successfully");
}
processGitVersionOutput(result) {
this.buildAgent.debug("Parsing GitVersion output");
async processGitVersionOutput(result) {
this.buildAgent.debug("Processing GitVersion output");
if (result.code !== 0) {
return result;
}
const stdout = result.stdout;
const gitVersionOutput = this.extractGitVersionOutput(stdout);
let gitVersionOutput = null;
if (result.outputFile) {
this.buildAgent.debug(`Reading GitVersion variables from file: ${result.outputFile}`);
try {
gitVersionOutput = await this.tool.readGitVersionOutput(result.outputFile);
} catch (error) {
return this.handleOutputError(`Failed to read or parse GitVersion variables file: ${this.getErrorMessage(error)}`);
}
} else {
this.buildAgent.debug("Parsing GitVersion output from stdout");
const stdout = result.stdout;
gitVersionOutput = this.extractGitVersionOutput(stdout);
}
if (gitVersionOutput === null) {
const errorMessage = "GitVersion output is not valid JSON, see output details";
this.buildAgent.debug(errorMessage);
this.buildAgent.setFailed(errorMessage, true);
return {
code: -1,
error: new Error(errorMessage)
};
return this.handleOutputError("GitVersion output is not valid JSON, see output details");
}
this.tool.writeGitVersionToAgent(gitVersionOutput);
this.tool.updateBuildNumber();
this.buildAgent.setSucceeded("GitVersion executed successfully", true);
return result;
}
getErrorMessage(error) {
return error instanceof Error ? error.message : String(error);
}
handleOutputError(message) {
this.buildAgent.debug(message);
this.buildAgent.setFailed(message, true);
return {
code: -1,
error: new Error(message)
};
}
/**
* Attempts to extract and parse a JSON object representing `GitVersionOutput` from the given input string.
* The method assumes the last closing curly brace (`}`) in the input belongs to the end of the JSON object,
Expand Down
Loading
Loading