Skip to content

Commit da16f66

Browse files
committed
Merge pull request #142 from JakeGinnivan/AssemblyInfoPatch
Assembly info patch for console
2 parents bee4b87 + a103d64 commit da16f66

20 files changed

+269
-62
lines changed

Build.cmd

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
@echo on
2+
3+
set framework=v4.0.30319
4+
5+
"%~dp0.nuget\nuget.exe" restore
6+
7+
"%SystemDrive%\Windows\Microsoft.NET\Framework\%framework%\MSBuild.exe" "%~dp0GitVersion.sln"
8+
9+
mkdir "%~dp0GitVersion\bin\Intermediate"
10+
cp "%~dp0GitVersion\bin\Debug\GitVersion.exe" "%~dp0GitVersion\bin\Intermediate\GitVersion.exe"
11+
12+
"%~dp0GitVersion\bin\Intermediate\GitVersion.exe" /l console /output buildserver /updateAssemblyInfo /proj "%~dp0GitVersion.sln"

CommonAssemblyInfo.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.

GitVersionCore/ArgumentParser.cs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ static ArgumentParser()
1616

1717
public static Arguments ParseArguments(string commandLineArguments)
1818
{
19-
return ParseArguments(commandLineArguments.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries).ToList());
19+
return ParseArguments(commandLineArguments.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList());
2020
}
2121

2222
public static Arguments ParseArguments(List<string> commandLineArguments)
@@ -59,12 +59,10 @@ public static Arguments ParseArguments(List<string> commandLineArguments)
5959
namedArguments = commandLineArguments.Skip(1).ToList();
6060
}
6161

62-
EnsureArgumentsEvenCount(commandLineArguments, namedArguments);
63-
64-
for (var index = 0; index < namedArguments.Count; index = index+2)
62+
for (var index = 0; index < namedArguments.Count; index = index + 2)
6563
{
6664
var name = namedArguments[index];
67-
var value = namedArguments[index + 1];
65+
var value = namedArguments.Count > index + 1 ? namedArguments[index + 1] : null;
6866

6967
if (IsSwitch("l", name))
7068
{
@@ -120,6 +118,24 @@ public static Arguments ParseArguments(List<string> commandLineArguments)
120118
continue;
121119
}
122120

121+
if (IsSwitch("updateAssemblyInfo", name))
122+
{
123+
if (new[] { "1", "true" }.Contains(value))
124+
{
125+
arguments.UpdateAssemblyInfo = true;
126+
}
127+
else if (new[] { "0", "false" }.Contains(value))
128+
{
129+
arguments.UpdateAssemblyInfo = false;
130+
}
131+
else
132+
{
133+
arguments.UpdateAssemblyInfo = true;
134+
index--;
135+
}
136+
continue;
137+
}
138+
123139
if ((IsSwitch("v", name)) && VersionParts.Contains(value.ToLower()))
124140
{
125141
arguments.VersionPart = value.ToLower();
@@ -128,7 +144,7 @@ public static Arguments ParseArguments(List<string> commandLineArguments)
128144

129145
if (IsSwitch("output", name))
130146
{
131-
var outputType = OutputType.Json;
147+
OutputType outputType;
132148
if (!Enum.TryParse(value, true, out outputType))
133149
{
134150
throw new ErrorException(string.Format("Value '{0}' cannot be parsed as output type, please use 'json' or 'buildserver'", value));
@@ -158,20 +174,11 @@ static bool IsSwitch(string switchName, string value)
158174
return (string.Equals(switchName, value, StringComparison.InvariantCultureIgnoreCase));
159175
}
160176

161-
static void EnsureArgumentsEvenCount(List<string> commandLineArguments, List<string> namedArguments)
162-
{
163-
if (namedArguments.Count.IsOdd())
164-
{
165-
var message = string.Format("Could not parse arguments: '{0}'.", string.Join(" ", commandLineArguments));
166-
throw new ErrorException(message);
167-
}
168-
}
169-
170177
static bool IsHelp(string singleArgument)
171178
{
172-
return (singleArgument == "?") ||
173-
IsSwitch("h", singleArgument) ||
174-
IsSwitch("help", singleArgument) ||
179+
return (singleArgument == "?") ||
180+
IsSwitch("h", singleArgument) ||
181+
IsSwitch("help", singleArgument) ||
175182
IsSwitch("?", singleArgument);
176183
}
177184

GitVersionCore/Arguments.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@ namespace GitVersion
22
{
33
using System;
44

5-
public enum OutputType
6-
{
7-
BuildServer,
8-
9-
Json
10-
}
11-
125
public class Arguments
136
{
147
public Arguments()
@@ -36,5 +29,7 @@ public Arguments()
3629
public string ProjArgs;
3730
public string Exec;
3831
public string ExecArgs;
32+
33+
public bool UpdateAssemblyInfo;
3934
}
4035
}

GitVersionCore/ExtensionMethods.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace GitVersion
22
{
33
using System;
44
using System.Text;
5+
using System.Text.RegularExpressions;
56
using JetBrains.Annotations;
67

78
static class ExtensionMethods
@@ -18,12 +19,14 @@ public static string TrimToFirstLine(this string s)
1819
"\n"
1920
}, StringSplitOptions.None)[0];
2021
}
22+
2123
[StringFormatMethod("format")]
2224
public static void AppendLineFormat(this StringBuilder stringBuilder, string format, params object[] args)
2325
{
2426
stringBuilder.AppendFormat(format, args);
2527
stringBuilder.AppendLine();
2628
}
29+
2730
public static string TrimStart(this string value, string toTrim)
2831
{
2932
if (!value.StartsWith(toTrim))
@@ -34,6 +37,7 @@ public static string TrimStart(this string value, string toTrim)
3437
return value.Substring(startIndex);
3538
}
3639

40+
3741
public static string JsonEncode(this string value)
3842
{
3943
if (value != null)
@@ -49,5 +53,11 @@ public static string JsonEncode(this string value)
4953
}
5054
return null;
5155
}
56+
57+
public static string RegexReplace(this string input, string pattern, string replace, RegexOptions options = RegexOptions.None)
58+
{
59+
return Regex.Replace(input, pattern, replace, options);
60+
}
61+
5262
}
5363
}

GitVersionCore/GitHubFlow/GitHubFlowVersionFinder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public class GitHubFlowVersionFinder
55
public SemanticVersion FindVersion(GitVersionContext context)
66
{
77
var repositoryDirectory = context.Repository.Info.WorkingDirectory;
8-
var lastTaggedReleaseFinder = new LastTaggedReleaseFinder(context.Repository, repositoryDirectory);
8+
var lastTaggedReleaseFinder = new LastTaggedReleaseFinder(context.Repository);
99
return new BuildNumberCalculator(new NextSemverCalculator(new NextVersionTxtFileFinder(repositoryDirectory),
1010
lastTaggedReleaseFinder), lastTaggedReleaseFinder, context.Repository).GetBuildNumber(context);
1111
}

GitVersionCore/GitHubFlow/LastTaggedReleaseFinder.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
namespace GitVersion
22
{
33
using System;
4-
using System.IO;
54
using System.Linq;
65
using LibGit2Sharp;
76

87
public class LastTaggedReleaseFinder
98
{
10-
string workingDirectory;
119
Lazy<VersionTaggedCommit> lastTaggedRelease;
1210

13-
public LastTaggedReleaseFinder(IRepository gitRepo, string workingDirectory)
11+
public LastTaggedReleaseFinder(IRepository gitRepo)
1412
{
15-
this.workingDirectory = workingDirectory;
1613
lastTaggedRelease = new Lazy<VersionTaggedCommit>(() => GetVersion(gitRepo));
1714
}
1815

@@ -42,11 +39,6 @@ VersionTaggedCommit GetVersion(IRepository gitRepo)
4239
if (lastTaggedCommit != null)
4340
return tags.Last(a => a.Commit.Sha == lastTaggedCommit.Sha);
4441

45-
// Create a next version txt as 0.1.0
46-
var filePath = Path.Combine(workingDirectory, "NextVersion.txt");
47-
if (!File.Exists(filePath))
48-
File.WriteAllText(filePath, "0.1.0");
49-
5042
var commit = branch.Commits.Last();
5143
return new VersionTaggedCommit(commit, new SemanticVersion());
5244
}

GitVersionCore/GitVersionCore.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@
5252
</Reference>
5353
</ItemGroup>
5454
<ItemGroup>
55-
<Compile Include="..\CommonAssemblyInfo.cs">
56-
<Link>CommonAssemblyInfo.cs</Link>
57-
</Compile>
5855
<Compile Include="ArgumentParser.cs" />
5956
<Compile Include="Arguments.cs" />
6057
<Compile Include="BuildServers\AppVeyor.cs" />
@@ -96,8 +93,10 @@
9693
<Compile Include="MissingBranchException.cs" />
9794
<Compile Include="OutputFormatters\BuildOutputFormatter.cs" />
9895
<Compile Include="OutputFormatters\JsonOutputFormatter.cs" />
96+
<Compile Include="OutputType.cs" />
9997
<Compile Include="OutputVariables\CiFeedFormatter.cs" />
10098
<Compile Include="OutputVariables\VariableProvider.cs" />
99+
<Compile Include="Properties\AssemblyInfo.cs" />
101100
<Compile Include="ReleaseDate.cs" />
102101
<Compile Include="ReleaseDateFinder.cs" />
103102
<Compile Include="RepositoryLoader.cs" />
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<ProjectConfiguration>
2+
<CopyReferencedAssembliesToWorkspace>false</CopyReferencedAssembliesToWorkspace>
3+
<ConsiderInconclusiveTestsAsPassing>false</ConsiderInconclusiveTestsAsPassing>
4+
<PreloadReferencedAssemblies>false</PreloadReferencedAssemblies>
5+
<AllowDynamicCodeContractChecking>true</AllowDynamicCodeContractChecking>
6+
<AllowStaticCodeContractChecking>false</AllowStaticCodeContractChecking>
7+
<AllowCodeAnalysis>false</AllowCodeAnalysis>
8+
<IgnoreThisComponentCompletely>false</IgnoreThisComponentCompletely>
9+
<RunPreBuildEvents>false</RunPreBuildEvents>
10+
<RunPostBuildEvents>false</RunPostBuildEvents>
11+
<PreviouslyBuiltSuccessfully>true</PreviouslyBuiltSuccessfully>
12+
<InstrumentAssembly>true</InstrumentAssembly>
13+
<PreventSigningOfAssembly>false</PreventSigningOfAssembly>
14+
<AnalyseExecutionTimes>true</AnalyseExecutionTimes>
15+
<DetectStackOverflow>true</DetectStackOverflow>
16+
<IncludeStaticReferencesInWorkspace>true</IncludeStaticReferencesInWorkspace>
17+
<DefaultTestTimeout>60000</DefaultTestTimeout>
18+
<UseBuildConfiguration />
19+
<UseBuildPlatform />
20+
<ProxyProcessPath />
21+
<UseCPUArchitecture>AutoDetect</UseCPUArchitecture>
22+
<MSTestThreadApartmentState>STA</MSTestThreadApartmentState>
23+
<BuildProcessArchitecture>x86</BuildProcessArchitecture>
24+
</ProjectConfiguration>

GitVersionCore/OutputType.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace GitVersion
2+
{
3+
public enum OutputType
4+
{
5+
BuildServer,
6+
7+
Json
8+
}
9+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
4+
[assembly: AssemblyTitle("GitVersionCore")]
5+
[assembly: AssemblyDescription("")]
6+
[assembly: AssemblyConfiguration("")]
7+
[assembly: AssemblyProduct("GitVersion")]
8+
9+
[assembly: AssemblyVersion("1.0.0.0")]
10+
[assembly: AssemblyFileVersion("1.0.0.0")]
11+
12+
[assembly: InternalsVisibleTo("Tests")]
13+
[assembly: InternalsVisibleTo("GitVersion")]
14+
[assembly: InternalsVisibleTo("AcceptanceTests")]
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
namespace GitVersion
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
7+
internal class AssemblyInfoFileUpdate : IDisposable
8+
{
9+
private readonly List<Action> _restoreBackupTasks = new List<Action>();
10+
private readonly List<Action> _cleanupBackupTasks = new List<Action>();
11+
12+
public AssemblyInfoFileUpdate(Arguments args, string workingDirectory, Dictionary<string, string> variables)
13+
{
14+
if (!args.UpdateAssemblyInfo) return;
15+
16+
if (args.Output != OutputType.Json)
17+
Console.WriteLine("Updating assembly info files");
18+
19+
var assemblyInfoFiles = Directory.GetFiles(workingDirectory, "AssemblyInfo.cs",
20+
SearchOption.AllDirectories);
21+
22+
foreach (var assemblyInfoFile in assemblyInfoFiles)
23+
{
24+
var backupAssemblyInfo = assemblyInfoFile + ".bak";
25+
var localAssemblyInfo = assemblyInfoFile;
26+
File.Copy(assemblyInfoFile, backupAssemblyInfo, true);
27+
_restoreBackupTasks.Add(() =>
28+
{
29+
if (File.Exists(localAssemblyInfo))
30+
File.Delete(localAssemblyInfo);
31+
File.Move(backupAssemblyInfo, localAssemblyInfo);
32+
});
33+
_cleanupBackupTasks.Add(() => File.Delete(backupAssemblyInfo));
34+
35+
var assemblyVersion = string.Format("{0}.{1}.0.0", variables[VariableProvider.Major], variables[VariableProvider.Minor]);
36+
var assemblyInfoVersion = variables[VariableProvider.InformationalVersion];
37+
var assemblyFileVersion = variables[VariableProvider.AssemblySemVer];
38+
var fileContents = File.ReadAllText(assemblyInfoFile)
39+
.RegexReplace(@"AssemblyVersion\(""\d+.\d+.\d+(.\d+|\*)?""\)", string.Format("AssemblyVersion(\"{0}\")", assemblyVersion))
40+
.RegexReplace(@"AssemblyInformationalVersion\(""\d+.\d+.\d+(.\d+|\*)?""\)", string.Format("AssemblyInformationalVersion(\"{0}\")", assemblyInfoVersion))
41+
.RegexReplace(@"AssemblyFileVersion\(""\d+.\d+.\d+(.\d+|\*)?""\)", string.Format("AssemblyFileVersion(\"{0}\")", assemblyFileVersion));
42+
43+
File.WriteAllText(assemblyInfoFile, fileContents);
44+
}
45+
}
46+
47+
public void Dispose()
48+
{
49+
foreach (var restoreBackup in _restoreBackupTasks)
50+
{
51+
restoreBackup();
52+
}
53+
54+
_cleanupBackupTasks.Clear();
55+
_restoreBackupTasks.Clear();
56+
}
57+
58+
public void DoNotRestoreAssemblyInfo()
59+
{
60+
foreach (var cleanupBackupTask in _cleanupBackupTasks)
61+
{
62+
cleanupBackupTask();
63+
}
64+
_cleanupBackupTasks.Clear();
65+
_restoreBackupTasks.Clear();
66+
}
67+
}
68+
}

GitVersionExe/GitVersionExe.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@
5656
<Compile Include="GitPreparer.cs" />
5757
<Compile Include="HelpWriter.cs" />
5858
<Compile Include="ProcessHelper.cs" />
59-
<Compile Include="..\CommonAssemblyInfo.cs" />
6059
<Compile Include="Program.cs" />
60+
<Compile Include="Properties\AssemblyInfo.cs" />
61+
<Compile Include="AssemblyInfoFileUpdate.cs" />
6162
</ItemGroup>
6263
<ItemGroup>
6364
<None Include="GemAssets\gitversion" />
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<ProjectConfiguration>
2+
<CopyReferencedAssembliesToWorkspace>false</CopyReferencedAssembliesToWorkspace>
3+
<ConsiderInconclusiveTestsAsPassing>false</ConsiderInconclusiveTestsAsPassing>
4+
<PreloadReferencedAssemblies>false</PreloadReferencedAssemblies>
5+
<AllowDynamicCodeContractChecking>true</AllowDynamicCodeContractChecking>
6+
<AllowStaticCodeContractChecking>false</AllowStaticCodeContractChecking>
7+
<AllowCodeAnalysis>false</AllowCodeAnalysis>
8+
<IgnoreThisComponentCompletely>false</IgnoreThisComponentCompletely>
9+
<RunPreBuildEvents>false</RunPreBuildEvents>
10+
<RunPostBuildEvents>false</RunPostBuildEvents>
11+
<PreviouslyBuiltSuccessfully>true</PreviouslyBuiltSuccessfully>
12+
<InstrumentAssembly>true</InstrumentAssembly>
13+
<PreventSigningOfAssembly>false</PreventSigningOfAssembly>
14+
<AnalyseExecutionTimes>true</AnalyseExecutionTimes>
15+
<DetectStackOverflow>true</DetectStackOverflow>
16+
<IncludeStaticReferencesInWorkspace>true</IncludeStaticReferencesInWorkspace>
17+
<DefaultTestTimeout>60000</DefaultTestTimeout>
18+
<UseBuildConfiguration />
19+
<UseBuildPlatform />
20+
<ProxyProcessPath />
21+
<UseCPUArchitecture>AutoDetect</UseCPUArchitecture>
22+
<MSTestThreadApartmentState>STA</MSTestThreadApartmentState>
23+
<BuildProcessArchitecture>x86</BuildProcessArchitecture>
24+
</ProjectConfiguration>

0 commit comments

Comments
 (0)