private IEnumerable <FileInfo> GetAssemblyInfoFiles(AssemblyInfoContext context) { 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) { var fullPath = Path.Combine(workingDirectory, item); if (EnsureVersionAssemblyInfoFile(fullPath, ensureAssemblyInfo)) { yield return(new FileInfo(fullPath)); } } } else { foreach (var item in fileSystem.DirectoryEnumerateFiles(workingDirectory, "AssemblyInfo.*", SearchOption.AllDirectories)) { var assemblyInfoFile = new FileInfo(item); if (templateManager.IsSupported(assemblyInfoFile.Extension)) { yield return(assemblyInfoFile); } } } }
private IEnumerable <FileInfo> GetProjectFiles(AssemblyInfoContext context) { var workingDirectory = context.WorkingDirectory; var assemblyInfoFileNames = new HashSet <string>(context.AssemblyInfoFiles); if (assemblyInfoFileNames.Any(x => !string.IsNullOrWhiteSpace(x))) { foreach (var item in assemblyInfoFileNames) { var fullPath = Path.Combine(workingDirectory, item); if (fileSystem.Exists(fullPath)) { yield return(new FileInfo(fullPath)); } else { log.Warning($"Specified file {fullPath} was not found and will not be updated."); } } } else { foreach (var item in fileSystem.DirectoryEnumerateFiles(workingDirectory, "*", SearchOption.AllDirectories).Where(IsSupportedProjectFile)) { var assemblyInfoFile = new FileInfo(item); yield return(assemblyInfoFile); } } }
public void Execute(VersionVariables variables, AssemblyInfoContext context) { var assemblyInfoFiles = GetAssemblyInfoFiles(context).ToList(); log.Info("Updating assembly info files"); log.Info($"Found {assemblyInfoFiles.Count} files"); var assemblyVersion = variables.AssemblySemVer; var assemblyVersionString = !string.IsNullOrWhiteSpace(assemblyVersion) ? $"AssemblyVersion(\"{assemblyVersion}\")" : null; var assemblyInfoVersion = variables.InformationalVersion; var assemblyInfoVersionString = !string.IsNullOrWhiteSpace(assemblyInfoVersion) ? $"AssemblyInformationalVersion(\"{assemblyInfoVersion}\")" : null; var assemblyFileVersion = variables.AssemblySemFileVer; var assemblyFileVersionString = !string.IsNullOrWhiteSpace(assemblyFileVersion) ? $"AssemblyFileVersion(\"{assemblyFileVersion}\")" : null; foreach (var assemblyInfoFile in assemblyInfoFiles) { var localAssemblyInfo = assemblyInfoFile.FullName; var backupAssemblyInfo = localAssemblyInfo + ".bak"; fileSystem.Copy(localAssemblyInfo, backupAssemblyInfo, true); restoreBackupTasks.Add(() => { if (fileSystem.Exists(localAssemblyInfo)) { fileSystem.Delete(localAssemblyInfo); } fileSystem.Move(backupAssemblyInfo, localAssemblyInfo); }); cleanupBackupTasks.Add(() => fileSystem.Delete(backupAssemblyInfo)); var originalFileContents = fileSystem.ReadAllText(localAssemblyInfo); var fileContents = originalFileContents; var appendedAttributes = false; if (!string.IsNullOrWhiteSpace(assemblyVersion)) { fileContents = ReplaceOrInsertAfterLastAssemblyAttributeOrAppend(assemblyVersionRegex, fileContents, assemblyVersionString, assemblyInfoFile.Extension, ref appendedAttributes); } if (!string.IsNullOrWhiteSpace(assemblyFileVersion)) { fileContents = ReplaceOrInsertAfterLastAssemblyAttributeOrAppend(assemblyFileVersionRegex, fileContents, assemblyFileVersionString, assemblyInfoFile.Extension, ref appendedAttributes); } if (!string.IsNullOrWhiteSpace(assemblyInfoVersion)) { fileContents = ReplaceOrInsertAfterLastAssemblyAttributeOrAppend(assemblyInfoVersionRegex, fileContents, assemblyInfoVersionString, assemblyInfoFile.Extension, ref appendedAttributes); } if (appendedAttributes) { // If we appended any attributes, put a new line after them fileContents += NewLine; } if (originalFileContents != fileContents) { fileSystem.WriteAllText(localAssemblyInfo, fileContents); } } CommitChanges(); }
public void Execute(VersionVariables variables, AssemblyInfoContext context) { if (context.EnsureAssemblyInfo) { throw new WarningException($"Configuration setting {nameof(context.EnsureAssemblyInfo)} is not valid when updating project files!"); } var projectFilesToUpdate = GetProjectFiles(context).ToList(); var assemblyVersion = variables.AssemblySemVer; var assemblyInfoVersion = variables.InformationalVersion; var assemblyFileVersion = variables.AssemblySemFileVer; foreach (var projectFile in projectFilesToUpdate) { var localProjectFile = projectFile.FullName; var originalFileContents = fileSystem.ReadAllText(localProjectFile); var fileXml = XElement.Parse(originalFileContents); if (!CanUpdateProjectFile(fileXml)) { log.Warning($"Unable to update file: {localProjectFile}"); continue; } var backupProjectFile = localProjectFile + ".bak"; fileSystem.Copy(localProjectFile, backupProjectFile, true); restoreBackupTasks.Add(() => { if (fileSystem.Exists(localProjectFile)) { fileSystem.Delete(localProjectFile); } fileSystem.Move(backupProjectFile, localProjectFile); }); cleanupBackupTasks.Add(() => fileSystem.Delete(backupProjectFile)); if (!string.IsNullOrWhiteSpace(assemblyVersion)) { UpdateProjectVersionElement(fileXml, AssemblyVersionElement, assemblyVersion); } if (!string.IsNullOrWhiteSpace(assemblyFileVersion)) { UpdateProjectVersionElement(fileXml, FileVersionElement, assemblyFileVersion); } if (!string.IsNullOrWhiteSpace(assemblyInfoVersion)) { UpdateProjectVersionElement(fileXml, InformationalVersionElement, assemblyInfoVersion); } var outputXmlString = fileXml.ToString(); if (originalFileContents != outputXmlString) { fileSystem.WriteAllText(localProjectFile, outputXmlString); } } CommitChanges(); }