Skip to content

Commit 90bef79

Browse files
committed
Updates var names and text to move away from only supporting csproj files
1 parent 0fc122d commit 90bef79

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

src/GitVersionCore/VersionConverters/AssemblyInfo/ProjectFileUpdater.cs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,40 +33,41 @@ public ProjectFileUpdater(ILog log, IFileSystem fileSystem)
3333
public void Execute(VersionVariables variables, AssemblyInfoContext context)
3434
{
3535
if (context.EnsureAssemblyInfo)
36-
throw new WarningException($"Configuration setting {nameof(context.EnsureAssemblyInfo)} is not valid when updating .csproj files!");
36+
throw new WarningException($"Configuration setting {nameof(context.EnsureAssemblyInfo)} is not valid when updating project files!");
3737

38-
var csProjFilesToUpdate = GetCsProjFiles(context).ToList();
38+
var projectFilesToUpdate = GetProjectFiles(context).ToList();
3939

4040
var assemblyVersion = variables.AssemblySemVer;
4141
var assemblyInfoVersion = variables.InformationalVersion;
4242
var assemblyFileVersion = variables.AssemblySemFileVer;
4343

44-
foreach (var csProjFile in csProjFilesToUpdate)
44+
foreach (var projectFile in projectFilesToUpdate)
4545
{
46-
var localCsProjFile = csProjFile.FullName;
46+
var localProjectFile = projectFile.FullName;
4747

48-
var originalFileContents = fileSystem.ReadAllText(localCsProjFile);
48+
var originalFileContents = fileSystem.ReadAllText(localProjectFile);
4949
var fileXml = XElement.Parse(originalFileContents);
5050

5151
if (!CanUpdateProjectFile(fileXml))
5252
{
53+
log.Warning($"Unable to update file: {localProjectFile}");
5354
continue;
5455
}
5556

56-
var backupCsProjFile = localCsProjFile + ".bak";
57-
fileSystem.Copy(localCsProjFile, backupCsProjFile, true);
57+
var backupProjectFile = localProjectFile + ".bak";
58+
fileSystem.Copy(localProjectFile, backupProjectFile, true);
5859

5960
restoreBackupTasks.Add(() =>
6061
{
61-
if (fileSystem.Exists(localCsProjFile))
62+
if (fileSystem.Exists(localProjectFile))
6263
{
63-
fileSystem.Delete(localCsProjFile);
64+
fileSystem.Delete(localProjectFile);
6465
}
6566

66-
fileSystem.Move(backupCsProjFile, localCsProjFile);
67+
fileSystem.Move(backupProjectFile, localProjectFile);
6768
});
6869

69-
cleanupBackupTasks.Add(() => fileSystem.Delete(backupCsProjFile));
70+
cleanupBackupTasks.Add(() => fileSystem.Delete(backupProjectFile));
7071

7172
if (!string.IsNullOrWhiteSpace(assemblyVersion))
7273
{
@@ -86,49 +87,49 @@ public void Execute(VersionVariables variables, AssemblyInfoContext context)
8687
var outputXmlString = fileXml.ToString();
8788
if (originalFileContents != outputXmlString)
8889
{
89-
fileSystem.WriteAllText(localCsProjFile, outputXmlString);
90+
fileSystem.WriteAllText(localProjectFile, outputXmlString);
9091
}
9192
}
9293

9394
CommitChanges();
9495
}
9596

96-
internal bool CanUpdateProjectFile(XElement csProjRoot)
97+
internal bool CanUpdateProjectFile(XElement xmlRoot)
9798
{
98-
if (csProjRoot.Name != "Project")
99+
if (xmlRoot.Name != "Project")
99100
{
100-
log.Warning($"Invalid project file specified, root element must be <Project> -- skipping");
101+
log.Warning($"Invalid project file specified, root element must be <Project>.");
101102
return false;
102103
}
103104

104105
var supportedSdk = "Microsoft.NET.Sdk";
105-
var sdkAttribute = csProjRoot.Attribute("Sdk");
106+
var sdkAttribute = xmlRoot.Attribute("Sdk");
106107
if (sdkAttribute == null || sdkAttribute.Value != supportedSdk)
107108
{
108-
log.Warning($"Specified project file Sdk ({sdkAttribute?.Value} is not supported, please ensure the project sdk is {supportedSdk} -- skipping");
109+
log.Warning($"Specified project file Sdk ({sdkAttribute?.Value}) is not supported, please ensure the project sdk is {supportedSdk}.");
109110
return false;
110111
}
111112

112-
var propertyGroups = csProjRoot.Descendants("PropertyGroup").ToList();
113+
var propertyGroups = xmlRoot.Descendants("PropertyGroup").ToList();
113114
if (!propertyGroups.Any())
114115
{
115-
log.Warning("Unable to locate any <PropertyGroup> elements in specified project file. Are you sure it is in a correct format? -- skipping");
116+
log.Warning("Unable to locate any <PropertyGroup> elements in specified project file. Are you sure it is in a correct format?");
116117
return false;
117118
}
118119

119120
var lastGenerateAssemblyInfoElement = propertyGroups.SelectMany(s => s.Elements("GenerateAssemblyInfo")).LastOrDefault();
120121
if (lastGenerateAssemblyInfoElement != null && (bool) lastGenerateAssemblyInfoElement == false)
121122
{
122-
log.Warning($"Project file specifies <GenerateAssemblyInfo>false</GenerateAssemblyInfo>: versions set in this project file will not affect the output artifacts -- skipping");
123+
log.Warning($"Project file specifies <GenerateAssemblyInfo>false</GenerateAssemblyInfo>: versions set in this project file will not affect the output artifacts.");
123124
return false;
124125
}
125126

126127
return true;
127128
}
128129

129-
internal void UpdateProjectVersionElement(XElement csProjRoot, string versionElement, string versionValue)
130+
internal void UpdateProjectVersionElement(XElement xmlRoot, string versionElement, string versionValue)
130131
{
131-
var propertyGroups = csProjRoot.Descendants("PropertyGroup").ToList();
132+
var propertyGroups = xmlRoot.Descendants("PropertyGroup").ToList();
132133

133134
var propertyGroupToModify = propertyGroups.LastOrDefault(l => l.Element(versionElement) != null)
134135
?? propertyGroups.First();
@@ -166,7 +167,7 @@ private void CommitChanges()
166167
restoreBackupTasks.Clear();
167168
}
168169

169-
private IEnumerable<FileInfo> GetCsProjFiles(AssemblyInfoContext context)
170+
private IEnumerable<FileInfo> GetProjectFiles(AssemblyInfoContext context)
170171
{
171172
var workingDirectory = context.WorkingDirectory;
172173
var assemblyInfoFileNames = new HashSet<string>(context.AssemblyInfoFiles);

0 commit comments

Comments
 (0)