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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace GitVersion
{
public class AssemblyInfo
public class AssemblyInfoData
{
public bool ShouldUpdate;
public bool EnsureAssemblyInfo;
Expand Down
2 changes: 1 addition & 1 deletion src/GitVersionCore/Model/GitVersionOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public GitVersionOptions()
public string ProjectRootDirectory => projectRootDirectory.Value;
public string DynamicGitRepositoryPath => dynamicGitRepositoryPath.Value;

public AssemblyInfo AssemblyInfo { get; } = new AssemblyInfo();
public AssemblyInfoData AssemblyInfo { get; } = new AssemblyInfoData();
public AuthenticationInfo Authentication { get; } = new AuthenticationInfo();
public ConfigInfo ConfigInfo { get; } = new ConfigInfo();
public RepositoryInfo RepositoryInfo { get; } = new RepositoryInfo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ public AssemblyInfoFileUpdater(ILog log, IFileSystem fileSystem)

public void Execute(VersionVariables variables, AssemblyInfoContext context)
{
var assemblyInfoFileNames = new HashSet<string>(context.AssemblyInfoFiles);
var assemblyInfoFiles = GetAssemblyInfoFiles(context).ToList();
log.Info("Updating assembly info files");

var assemblyInfoFiles = GetAssemblyInfoFiles(context.WorkingDirectory, assemblyInfoFileNames, context.EnsureAssemblyInfo).ToList();
log.Info($"Found {assemblyInfoFiles.Count} files");

var assemblyVersion = variables.AssemblySemVer;
Expand Down Expand Up @@ -159,9 +157,13 @@ private string ReplaceOrInsertAfterLastAssemblyAttributeOrAppend(Regex replaceRe
return inputString;
}

private IEnumerable<FileInfo> GetAssemblyInfoFiles(string workingDirectory, ISet<string> assemblyInfoFileNames, bool ensureAssemblyInfo)
private IEnumerable<FileInfo> GetAssemblyInfoFiles(AssemblyInfoContext context)
{
if (assemblyInfoFileNames != null && assemblyInfoFileNames.Any(x => !string.IsNullOrWhiteSpace(x)))
var workingDirectory = context.WorkingDirectory;
var ensureAssemblyInfo = context.EnsureAssemblyInfo;
var assemblyInfoFileNames = new HashSet<string>(context.AssemblyInfoFiles);

if (assemblyInfoFileNames.Any(x => !string.IsNullOrWhiteSpace(x)))
{
foreach (var item in assemblyInfoFileNames)
{
Expand Down
76 changes: 63 additions & 13 deletions src/GitVersionExe.Tests/ArgumentParserTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.IO;
using GitTools.Testing;
using GitVersion;
using GitVersion.Logging;
using GitVersion.Model;
Expand All @@ -20,6 +22,7 @@ public void SetUp()
var sp = ConfigureServices(services =>
{
services.AddSingleton<IArgumentParser, ArgumentParser>();
services.AddSingleton<IGlobbingResolver, GlobbingResolver>();
});
argumentParser = sp.GetService<IArgumentParser>();
}
Expand Down Expand Up @@ -257,19 +260,74 @@ public void CreateMulitpleAssemblyInfoProtected(string command)
[Test]
public void UpdateAssemblyInfoWithFilename()
{
var arguments = argumentParser.ParseArguments("-updateAssemblyInfo CommonAssemblyInfo.cs");
using var repo = new EmptyRepositoryFixture();

var assemblyFile = Path.Combine(repo.RepositoryPath, "CommonAssemblyInfo.cs");
using var file = File.Create(assemblyFile);

var arguments = argumentParser.ParseArguments($"-targetpath {repo.RepositoryPath} -updateAssemblyInfo CommonAssemblyInfo.cs");
arguments.UpdateAssemblyInfo.ShouldBe(true);
arguments.UpdateAssemblyInfoFileName.ShouldContain("CommonAssemblyInfo.cs");
arguments.UpdateAssemblyInfoFileName.Count.ShouldBe(1);
arguments.UpdateAssemblyInfoFileName.ShouldContain(x => Path.GetFileName(x).Equals("CommonAssemblyInfo.cs"));
}

[Test]
public void UpdateAssemblyInfoWithMultipleFilenames()
{
var arguments = argumentParser.ParseArguments("-updateAssemblyInfo CommonAssemblyInfo.cs VersionAssemblyInfo.cs");
using var repo = new EmptyRepositoryFixture();

var assemblyFile1 = Path.Combine(repo.RepositoryPath, "CommonAssemblyInfo.cs");
using var file = File.Create(assemblyFile1);

var assemblyFile2 = Path.Combine(repo.RepositoryPath, "VersionAssemblyInfo.cs");
using var file2 = File.Create(assemblyFile2);

var arguments = argumentParser.ParseArguments($"-targetpath {repo.RepositoryPath} -updateAssemblyInfo CommonAssemblyInfo.cs VersionAssemblyInfo.cs");
arguments.UpdateAssemblyInfo.ShouldBe(true);
arguments.UpdateAssemblyInfoFileName.Count.ShouldBe(2);
arguments.UpdateAssemblyInfoFileName.ShouldContain("CommonAssemblyInfo.cs");
arguments.UpdateAssemblyInfoFileName.ShouldContain("VersionAssemblyInfo.cs");
arguments.UpdateAssemblyInfoFileName.ShouldContain(x => Path.GetFileName(x).Equals("CommonAssemblyInfo.cs"));
arguments.UpdateAssemblyInfoFileName.ShouldContain(x => Path.GetFileName(x).Equals("VersionAssemblyInfo.cs"));
}

[Test]
public void UpdateAssemblyInfoWithMultipleFilenamesMatchingGlobbing()
{
using var repo = new EmptyRepositoryFixture();

var assemblyFile1 = Path.Combine(repo.RepositoryPath, "CommonAssemblyInfo.cs");
using var file = File.Create(assemblyFile1);

var assemblyFile2 = Path.Combine(repo.RepositoryPath, "VersionAssemblyInfo.cs");
using var file2 = File.Create(assemblyFile2);

var subdir = Path.Combine(repo.RepositoryPath, "subdir");
Directory.CreateDirectory(subdir);
var assemblyFile3 = Path.Combine(subdir, "LocalAssemblyInfo.cs");
using var file3 = File.Create(assemblyFile3);

var arguments = argumentParser.ParseArguments($"-targetpath {repo.RepositoryPath} -updateAssemblyInfo **/*AssemblyInfo.cs");
arguments.UpdateAssemblyInfo.ShouldBe(true);
arguments.UpdateAssemblyInfoFileName.Count.ShouldBe(3);
arguments.UpdateAssemblyInfoFileName.ShouldContain(x => Path.GetFileName(x).Equals("CommonAssemblyInfo.cs"));
arguments.UpdateAssemblyInfoFileName.ShouldContain(x => Path.GetFileName(x).Equals("VersionAssemblyInfo.cs"));
arguments.UpdateAssemblyInfoFileName.ShouldContain(x => Path.GetFileName(x).Equals("LocalAssemblyInfo.cs"));
}

[Test]
public void UpdateAssemblyInfoWithRelativeFilename()
{
using var repo = new EmptyRepositoryFixture();

var assemblyFile = Path.Combine(repo.RepositoryPath, "CommonAssemblyInfo.cs");
using var file = File.Create(assemblyFile);

var targetPath = Path.Combine(repo.RepositoryPath, "subdir1", "subdir2");
Directory.CreateDirectory(targetPath);

var arguments = argumentParser.ParseArguments($"-targetpath {targetPath} -updateAssemblyInfo ..\\..\\CommonAssemblyInfo.cs");
arguments.UpdateAssemblyInfo.ShouldBe(true);
arguments.UpdateAssemblyInfoFileName.Count.ShouldBe(1);
arguments.UpdateAssemblyInfoFileName.ShouldContain(x => Path.GetFileName(x).Equals("CommonAssemblyInfo.cs"));
}

[Test]
Expand Down Expand Up @@ -301,14 +359,6 @@ public void OverrideconfigWithInvalidOption(string options)
exception.Message.ShouldContain("Could not parse /overrideconfig option");
}

[Test]
public void UpdateAssemblyInfoWithRelativeFilename()
{
var arguments = argumentParser.ParseArguments("-updateAssemblyInfo ..\\..\\CommonAssemblyInfo.cs");
arguments.UpdateAssemblyInfo.ShouldBe(true);
arguments.UpdateAssemblyInfoFileName.ShouldContain("..\\..\\CommonAssemblyInfo.cs");
}

[Test]
public void EnsureAssemblyInfoTrueWhenFound()
{
Expand Down
45 changes: 33 additions & 12 deletions src/GitVersionExe/ArgumentParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ public class ArgumentParser : IArgumentParser
private readonly IEnvironment environment;
private readonly ICurrentBuildAgent buildAgent;
private readonly IConsole console;
private readonly IGlobbingResolver globbingResolver;

public ArgumentParser(IEnvironment environment, ICurrentBuildAgent buildAgent, IConsole console)
public ArgumentParser(IEnvironment environment, ICurrentBuildAgent buildAgent, IConsole console, IGlobbingResolver globbingResolver)
{
this.environment = environment ?? throw new ArgumentNullException(nameof(environment));
this.console = console ?? throw new ArgumentNullException(nameof(console));
this.globbingResolver = globbingResolver ?? throw new ArgumentNullException(nameof(globbingResolver));
this.buildAgent = buildAgent;
}

Expand Down Expand Up @@ -87,6 +89,8 @@ public Arguments ParseArguments(string[] commandLineArguments)
? System.Environment.CurrentDirectory
: firstArgument;

arguments.TargetPath = arguments.TargetPath.TrimEnd('/', '\\');
arguments.UpdateAssemblyInfoFileName = ResolveFiles(arguments.TargetPath, arguments.UpdateAssemblyInfoFileName).ToHashSet();
arguments.NoFetch = arguments.NoFetch || buildAgent != null && buildAgent.PreventFetch();

return arguments;
Expand Down Expand Up @@ -234,7 +238,6 @@ private void ParseArguments(Arguments arguments, NameValueCollection switchesAnd
if (name.IsSwitch("showConfig"))
{
ParseShowConfig(value, arguments);

return;
}

Expand Down Expand Up @@ -311,6 +314,21 @@ private void ParseArguments(Arguments arguments, NameValueCollection switchesAnd
throw new WarningException(couldNotParseMessage);
}

private void AddAuthentication(Arguments arguments)
{
var username = environment.GetEnvironmentVariable("GITVERSION_REMOTE_USERNAME");
if (!string.IsNullOrWhiteSpace(username))
{
arguments.Authentication.Username = username;
}

var password = environment.GetEnvironmentVariable("GITVERSION_REMOTE_PASSWORD");
if (!string.IsNullOrWhiteSpace(password))
{
arguments.Authentication.Username = password;
}
}

private static void ParseShowConfig(string value, Arguments arguments)
{
if (value.IsTrue())
Expand Down Expand Up @@ -440,7 +458,10 @@ private static void ParseUpdateAssemblyInfo(Arguments arguments, string value, s
else if (!value.IsSwitchArgument())
{
arguments.UpdateAssemblyInfo = true;
arguments.UpdateAssemblyInfoFileName.Add(value);
if (value != null)
{
arguments.UpdateAssemblyInfoFileName.Add(value);
}
}
else
{
Expand All @@ -453,18 +474,18 @@ private static void ParseUpdateAssemblyInfo(Arguments arguments, string value, s
}
}

private void AddAuthentication(Arguments arguments)
private IEnumerable<string> ResolveFiles(string workingDirectory, ISet<string> assemblyInfoFiles)
{
var username = environment.GetEnvironmentVariable("GITVERSION_REMOTE_USERNAME");
if (!string.IsNullOrWhiteSpace(username))
{
arguments.Authentication.Username = username;
}
if (assemblyInfoFiles == null) yield break;

var password = environment.GetEnvironmentVariable("GITVERSION_REMOTE_PASSWORD");
if (!string.IsNullOrWhiteSpace(password))
foreach (var file in assemblyInfoFiles)
{
arguments.Authentication.Username = password;
var paths = globbingResolver.Resolve(workingDirectory, file);

foreach (var path in paths)
{
yield return Path.GetFullPath(Path.Combine(workingDirectory, path));
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/GitVersionExe/Arguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,17 @@ public class Arguments

public GitVersionOptions ToOptions()
{
var workingDirectory = TargetPath.TrimEnd('/', '\\');

return new GitVersionOptions
{
WorkingDirectory = TargetPath.TrimEnd('/', '\\'),
WorkingDirectory = workingDirectory,

AssemblyInfo =
{
ShouldUpdate = UpdateAssemblyInfo,
EnsureAssemblyInfo = EnsureAssemblyInfo,
Files = UpdateAssemblyInfoFileName,
Files = UpdateAssemblyInfoFileName
},

Authentication =
Expand Down
1 change: 1 addition & 0 deletions src/GitVersionExe/GitVersionExe.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="$(PackageVersion_MicrosoftExtensions)" />
<PackageReference Include="Microsoft.Extensions.FileSystemGlobbing" Version="$(PackageVersion_MicrosoftExtensions)" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions src/GitVersionExe/GitVersionExeModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public class GitVersionExeModule : IGitVersionModule
public void RegisterTypes(IServiceCollection services)
{
services.AddSingleton<IArgumentParser, ArgumentParser>();
services.AddSingleton<IGlobbingResolver, GlobbingResolver>();

services.AddSingleton<IHelpWriter, HelpWriter>();
services.AddSingleton<IVersionWriter, VersionWriter>();
services.AddSingleton<IGitVersionExecutor, GitVersionExecutor>();
Expand Down
22 changes: 22 additions & 0 deletions src/GitVersionExe/GlobbingResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Extensions.FileSystemGlobbing;
using Microsoft.Extensions.FileSystemGlobbing.Abstractions;

namespace GitVersion
{
public class GlobbingResolver : IGlobbingResolver
{
private Matcher matcher = new Matcher(StringComparison.OrdinalIgnoreCase);

public IEnumerable<string> Resolve(string workingDirectory, string pattern)
{
matcher.AddInclude(pattern);
return matcher.Execute(GetDirectoryInfoWrapper(workingDirectory)).Files.Select(file => file.Path);
}

protected virtual DirectoryInfoBase GetDirectoryInfoWrapper(string workingDirectory) => new DirectoryInfoWrapper(new DirectoryInfo(workingDirectory));
}
}
9 changes: 9 additions & 0 deletions src/GitVersionExe/IGlobbingResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;

namespace GitVersion
{
public interface IGlobbingResolver
{
public IEnumerable<string> Resolve(string workingDirectory, string pattern);
}
}