Skip to content

Commit ef8bd0a

Browse files
committed
Use AssemblyInfoFileUpdater in UpdateAssemblyInfo
1 parent a1ff7df commit ef8bd0a

File tree

3 files changed

+30
-31
lines changed

3 files changed

+30
-31
lines changed

src/GitVersionCore/VersionAssemblyInfoResources/AssemblyInfoFileUpdater.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace GitVersion
88
using System.Text.RegularExpressions;
99
using GitVersion.VersionAssemblyInfoResources;
1010

11-
class AssemblyInfoFileUpdater : IDisposable
11+
public class AssemblyInfoFileUpdater : IDisposable
1212
{
1313
readonly List<Action> restoreBackupTasks = new List<Action>();
1414
readonly List<Action> cleanupBackupTasks = new List<Action>();

src/GitVersionTask/AssemblyInfoBuilder/UpdateAssemblyInfo.cs

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
namespace GitVersionTask
22
{
33
using System;
4+
using System.Collections.Generic;
45
using System.ComponentModel;
56
using System.IO;
6-
using System.Text;
7-
87
using GitVersion;
98
using GitVersion.Helpers;
10-
119
using Microsoft.Build.Framework;
1210

13-
// TODO: Consolidate this with GitVersion.AssemblyInfoFileUpdate in GitVersionExe. @asbjornu
1411
public class UpdateAssemblyInfo : GitVersionTaskBase
1512
{
1613
TaskLogger logger;
@@ -37,7 +34,7 @@ public UpdateAssemblyInfo()
3734
public ITaskItem[] CompileFiles { get; set; }
3835

3936
[Required]
40-
public string RootNamespace { get; set; }
37+
public string Language { get; set; }
4138

4239
[Output]
4340
public string AssemblyInfoTempFilePath { get; set; }
@@ -78,45 +75,47 @@ void InnerExecute()
7875
{
7976
return;
8077
}
78+
8179
CreateTempAssemblyInfo(versionVariables);
8280
}
8381

8482
void CreateTempAssemblyInfo(VersionVariables versionVariables)
8583
{
86-
var assemblyInfoBuilder = AssemblyInfoBuilder.GetAssemblyInfoBuilder(CompileFiles);
84+
var fileExtension = GetFileExtension();
85+
var assemblyInfoFileName = $"GitVersionTaskAssemblyInfo.g.{fileExtension}";
8786

8887
if (IntermediateOutputPath == null)
8988
{
90-
var tempFileName = string.Format("AssemblyInfo_{0}_{1}.g.{2}", Path.GetFileNameWithoutExtension(ProjectFile), Path.GetRandomFileName(), assemblyInfoBuilder.AssemblyInfoExtension);
91-
AssemblyInfoTempFilePath = Path.Combine(TempFileTracker.TempPath, tempFileName);
92-
}
93-
else
94-
{
95-
AssemblyInfoTempFilePath = Path.Combine(IntermediateOutputPath, string.Format("GitVersionTaskAssemblyInfo.g.{0}", assemblyInfoBuilder.AssemblyInfoExtension));
89+
assemblyInfoFileName = $"AssemblyInfo_{Path.GetFileNameWithoutExtension(ProjectFile)}_{Path.GetRandomFileName()}.g.{fileExtension}";
9690
}
9791

98-
var assemblyInfo = assemblyInfoBuilder.GetAssemblyInfoText(versionVariables, RootNamespace).Trim();
99-
var encoding = EncodingHelper.DetectEncoding(AssemblyInfoTempFilePath) ?? Encoding.UTF8;
92+
var workingDirectory = IntermediateOutputPath ?? TempFileTracker.TempPath;
10093

101-
// We need to try to read the existing text first if the file exists and see if it's the same
102-
// This is to avoid writing when there's no differences and causing a rebuild
103-
try
94+
AssemblyInfoTempFilePath = Path.Combine(workingDirectory, assemblyInfoFileName);
95+
96+
using (var assemblyInfoFileUpdater = new AssemblyInfoFileUpdater(assemblyInfoFileName, workingDirectory, versionVariables, new FileSystem(), true))
10497
{
105-
if (File.Exists(AssemblyInfoTempFilePath))
106-
{
107-
var content = File.ReadAllText(AssemblyInfoTempFilePath, encoding).Trim();
108-
if (string.Equals(assemblyInfo, content, StringComparison.Ordinal))
109-
{
110-
return; // nothign to do as the file matches what we'd create
111-
}
112-
}
98+
assemblyInfoFileUpdater.Update();
99+
assemblyInfoFileUpdater.DoNotRestoreAssemblyInfo();
113100
}
114-
catch (Exception)
101+
}
102+
103+
string GetFileExtension()
104+
{
105+
switch(Language)
115106
{
116-
// Something happened reading the file, try to overwrite anyway
117-
}
107+
case "C#":
108+
return "cs";
118109

119-
File.WriteAllText(AssemblyInfoTempFilePath, assemblyInfo, encoding);
110+
case "F#":
111+
return "fs";
112+
113+
case "VB":
114+
return "vb";
115+
116+
default:
117+
throw new Exception($"Unknown language detected: '{Language}'");
118+
}
120119
}
121120
}
122121
}

src/GitVersionTask/NugetAssets/build/GitVersionTask.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
NoFetch="$(GitVersion_NoFetchEnabled)"
5656
ProjectFile="$(MSBuildProjectFullPath)"
5757
IntermediateOutputPath="$(IntermediateOutputPath)"
58-
RootNamespace="$(RootNamespace)"
58+
Language="$(Language)"
5959
CompileFiles ="@(Compile)">
6060
<Output
6161
TaskParameter="AssemblyInfoTempFilePath"

0 commit comments

Comments
 (0)