public void Execute(VersionVariables variables, GitVersionInfoContext context) { var fileName = context.FileName; var directory = context.WorkingDirectory; var filePath = Path.Combine(directory, fileName); string originalFileContents = null; if (File.Exists(filePath)) { originalFileContents = fileSystem.ReadAllText(filePath); } var fileExtension = Path.GetExtension(filePath); var template = templateManager.GetTemplateFor(fileExtension); var addFormat = templateManager.GetAddFormatFor(fileExtension); var indentation = GetIndentation(fileExtension); var members = string.Join(System.Environment.NewLine, variables.Select(v => string.Format(indentation + addFormat, v.Key, v.Value))); var fileContents = string.Format(template, members); if (fileContents != originalFileContents) { fileSystem.WriteAllText(filePath, fileContents); } }
public override void WriteIntegration(Action <string?> writer, VersionVariables variables, bool updateBuildNumber = true) { if (this.file is null) { return; } base.WriteIntegration(writer, variables, updateBuildNumber); writer($"Outputting variables to '{this.file}' ... "); writer("To import the file into your build environment, add the following line to your build step:"); writer($" - source {this.file}"); writer(""); writer("To reuse the file across build steps, add the file as a build artifact:"); writer(" artifacts:"); writer($" - {this.file}"); var exports = variables .Select(variable => $"export GITVERSION_{variable.Key.ToUpperInvariant()}={variable.Value}") .ToList(); File.WriteAllLines(this.file, exports); }
public void Generate() { var filePath = Path.Combine(directory, fileName); string originalFileContents = null; if (File.Exists(filePath)) { originalFileContents = fileSystem.ReadAllText(filePath); } var fileExtension = Path.GetExtension(filePath); var template = templateManager.GetTemplateFor(fileExtension); var addFormat = templateManager.GetAddFormatFor(fileExtension); var members = string.Join(System.Environment.NewLine, variables.Select(v => string.Format(" " + addFormat, v.Key, v.Value))); var fileContents = string.Format(template, members); if (fileContents != originalFileContents) { fileSystem.WriteAllText(filePath, fileContents); } }
static KeyValuePair<string, string>[] GetEnvironmentalVariables(VersionVariables variables) { return variables .Select(v => new KeyValuePair<string, string>("GitVersion_" + v.Key, v.Value)) .ToArray(); }