public async Task AddDependencyToVersionDetailsAsync( string repo, string branch, DependencyDetail dependency) { XmlDocument versionDetails = await ReadVersionDetailsXmlAsync(repo, null); XmlNode newDependency = versionDetails.CreateElement(VersionFiles.DependencyElementName); SetAttribute(versionDetails, newDependency, VersionFiles.NameAttributeName, dependency.Name); SetAttribute(versionDetails, newDependency, VersionFiles.VersionAttributeName, dependency.Version); // Only add the pinned attribute if the pinned option is set to true if (dependency.Pinned) { SetAttribute(versionDetails, newDependency, VersionFiles.PinnedAttributeName, "True"); } // Only add the coherent parent attribute if it is set if (!string.IsNullOrEmpty(dependency.CoherentParentDependencyName)) { SetAttribute(versionDetails, newDependency, VersionFiles.CoherentParentAttributeName, dependency.CoherentParentDependencyName); } SetElement(versionDetails, newDependency, VersionFiles.UriElementName, dependency.RepoUri); SetElement(versionDetails, newDependency, VersionFiles.ShaElementName, dependency.Commit); XmlNode dependenciesNode = versionDetails.SelectSingleNode($"//{dependency.Type}{VersionFiles.DependenciesElementName}"); if (dependenciesNode == null) { dependenciesNode = versionDetails.CreateElement($"{dependency.Type}{VersionFiles.DependenciesElementName}"); versionDetails.DocumentElement.AppendChild(dependenciesNode); } dependenciesNode.AppendChild(newDependency); // TODO: This should not be done here. This should return some kind of generic file container to the caller, // who will gather up all updates and then call the git client to write the files all at once: // https://github.com/dotnet/arcade/issues/1095. Today this is only called from the Local interface so // it's okay for now. var file = new GitFile(VersionFiles.VersionDetailsXml, versionDetails); await _gitClient.CommitFilesAsync(new List <GitFile> { file }, repo, branch, $"Add {dependency} to " + $"'{VersionFiles.VersionDetailsXml}'"); _logger.LogInformation( $"Dependency '{dependency.Name}' with version '{dependency.Version}' successfully added to " + $"'{VersionFiles.VersionDetailsXml}'"); }
/// <summary> /// Updates existing dependencies in the dependency files /// </summary> /// <param name="dependencies">Dependencies that need updates.</param> /// <param name="remote">Remote instance for gathering eng/common script updates.</param> /// <returns></returns> public async Task UpdateDependenciesAsync(List <DependencyDetail> dependencies, IRemoteFactory remoteFactory) { // Read the current dependency files and grab their locations so that nuget.config can be updated appropriately. // Update the incoming dependencies with locations. IEnumerable <DependencyDetail> oldDependencies = await GetDependenciesAsync(); IRemote barOnlyRemote = await remoteFactory.GetBarOnlyRemoteAsync(_logger); await barOnlyRemote.AddAssetLocationToDependenciesAsync(oldDependencies); await barOnlyRemote.AddAssetLocationToDependenciesAsync(dependencies); var fileContainer = await _fileManager.UpdateDependencyFiles(dependencies, _repo, null, oldDependencies); List <GitFile> filesToUpdate = fileContainer.GetFilesToCommit(); // TODO: This needs to be moved into some consistent handling between local/remote and add/update: // https://github.com/dotnet/arcade/issues/1095 // If we are updating the arcade sdk we need to update the eng/common files as well DependencyDetail arcadeItem = dependencies.FirstOrDefault( i => string.Equals(i.Name, "Microsoft.DotNet.Arcade.Sdk", StringComparison.OrdinalIgnoreCase)); if (arcadeItem != null) { try { IRemote remote = await remoteFactory.GetRemoteAsync(arcadeItem.RepoUri, _logger); List <GitFile> engCommonFiles = await remote.GetCommonScriptFilesAsync(arcadeItem.RepoUri, arcadeItem.Commit); filesToUpdate.AddRange(engCommonFiles); List <GitFile> localEngCommonFiles = await _gitClient.GetFilesAtCommitAsync(null, null, "eng/common"); foreach (GitFile file in localEngCommonFiles) { if (!engCommonFiles.Where(f => f.FilePath == file.FilePath).Any()) { file.Operation = GitFileOperation.Delete; filesToUpdate.Add(file); } } } catch (Exception exc) when (exc.Message == "Not Found") { _logger.LogWarning("Could not update 'eng/common'. Most likely this is a scenario " + "where a packages folder was passed and the commit which generated them is not " + "yet pushed."); } } // Push on local does not commit. await _gitClient.CommitFilesAsync(filesToUpdate, _repo, null, null); }
/// <summary> /// Updates existing dependencies in the dependency files /// </summary> /// <param name="dependencies">Dependencies that need updates.</param> /// <param name="remote">Remote instance for gathering eng/common script updates.</param> /// <returns></returns> public async Task UpdateDependenciesAsync(List <DependencyDetail> dependencies, IRemoteFactory remoteFactory) { // TODO: This should use known updaters, but today the updaters for global.json can only // add, not actually update. This needs a fix. https://github.com/dotnet/arcade/issues/1095 /*List<DependencyDetail> defaultUpdates = new List<DependencyDetail>(, IRemote remote); * foreach (DependencyDetail dependency in dependencies) * { * if (DependencyOperations.TryGetKnownUpdater(dependency.Name, out Delegate function)) * { * await (Task)function.DynamicInvoke(_fileManager, _repo, dependency); * } * else * { * defaultUpdates.Add(dependency); * } * }*/ var fileContainer = await _fileManager.UpdateDependencyFiles(dependencies, _repo, null); List <GitFile> filesToUpdate = fileContainer.GetFilesToCommit(); // TODO: This needs to be moved into some consistent handling between local/remote and add/update: // https://github.com/dotnet/arcade/issues/1095 // If we are updating the arcade sdk we need to update the eng/common files as well DependencyDetail arcadeItem = dependencies.FirstOrDefault( i => string.Equals(i.Name, "Microsoft.DotNet.Arcade.Sdk", StringComparison.OrdinalIgnoreCase)); if (arcadeItem != null) { try { IRemote remote = await remoteFactory.GetRemoteAsync(arcadeItem.RepoUri, _logger); List <GitFile> engCommonFiles = await remote.GetCommonScriptFilesAsync(arcadeItem.RepoUri, arcadeItem.Commit); filesToUpdate.AddRange(engCommonFiles); List <GitFile> localEngCommonFiles = await _gitClient.GetFilesAtCommitAsync(null, null, "eng/common"); foreach (GitFile file in localEngCommonFiles) { if (!engCommonFiles.Where(f => f.FilePath == file.FilePath).Any()) { file.Operation = GitFileOperation.Delete; filesToUpdate.Add(file); } } } catch (Exception exc) when (exc.Message == "Not Found") { _logger.LogWarning("Could not update 'eng/common'. Most likely this is a scenario " + "where a packages folder was passed and the commit which generated them is not " + "yet pushed."); } } // Push on local does not commit. await _gitClient.CommitFilesAsync(filesToUpdate, _repo, null, null); }
/// <summary> /// Updates existing dependencies in the dependency files /// </summary> /// <param name="dependencies">Dependencies that need updates.</param> /// <param name="remote">Remote instance for gathering eng/common script updates.</param> /// <returns></returns> public async Task UpdateDependenciesAsync(List <DependencyDetail> dependencies, IRemoteFactory remoteFactory) { // Read the current dependency files and grab their locations so that nuget.config can be updated appropriately. // Update the incoming dependencies with locations. IEnumerable <DependencyDetail> oldDependencies = await GetDependenciesAsync(); IRemote barOnlyRemote = await remoteFactory.GetBarOnlyRemoteAsync(_logger); await barOnlyRemote.AddAssetLocationToDependenciesAsync(oldDependencies); await barOnlyRemote.AddAssetLocationToDependenciesAsync(dependencies); // If we are updating the arcade sdk we need to update the eng/common files as well DependencyDetail arcadeItem = dependencies.FirstOrDefault( i => string.Equals(i.Name, "Microsoft.DotNet.Arcade.Sdk", StringComparison.OrdinalIgnoreCase)); SemanticVersion targetDotNetVersion = null; IRemote arcadeRemote = null; if (arcadeItem != null) { arcadeRemote = await remoteFactory.GetRemoteAsync(arcadeItem.RepoUri, _logger); targetDotNetVersion = await arcadeRemote.GetToolsDotnetVersionAsync(arcadeItem.RepoUri, arcadeItem.Commit); } var fileContainer = await _fileManager.UpdateDependencyFiles(dependencies, _repo, null, oldDependencies, targetDotNetVersion); List <GitFile> filesToUpdate = fileContainer.GetFilesToCommit(); if (arcadeItem != null) { try { List <GitFile> engCommonFiles = await arcadeRemote.GetCommonScriptFilesAsync(arcadeItem.RepoUri, arcadeItem.Commit); filesToUpdate.AddRange(engCommonFiles); List <GitFile> localEngCommonFiles = await _gitClient.GetFilesAtCommitAsync(null, null, "eng/common"); foreach (GitFile file in localEngCommonFiles) { if (!engCommonFiles.Where(f => f.FilePath == file.FilePath).Any()) { // This is a file in the repo's eng/common folder that isn't present in Arcade at the // requested SHA so delete it during the update. // GitFile instances do not have public setters since we insert/retrieve them from an // In-memory cache during remote updates and we don't want anything to modify the cached, // references, so add a copy with a Delete FileOperation. filesToUpdate.Add(new GitFile( file.FilePath, file.Content, file.ContentEncoding, file.Mode, GitFileOperation.Delete)); } } } catch (Exception exc) when (exc.Message == "Not Found") { _logger.LogWarning("Could not update 'eng/common'. Most likely this is a scenario " + "where a packages folder was passed and the commit which generated them is not " + "yet pushed."); } } // Push on local does not commit. await _gitClient.CommitFilesAsync(filesToUpdate, _repo, null, null); }
public async Task CommitUpdatesAsync( string repoUri, string branch, List <DependencyDetail> itemsToUpdate, string message) { CheckForValidGitClient(); GitFileContentContainer fileContainer = await _fileManager.UpdateDependencyFiles(itemsToUpdate, repoUri, branch); List <GitFile> filesToCommit = new List <GitFile>(); // If we are updating the arcade sdk we need to update the eng/common files and the dotnet versions as well DependencyDetail arcadeItem = itemsToUpdate.FirstOrDefault( i => string.Equals(i.Name, "Microsoft.DotNet.Arcade.Sdk", StringComparison.OrdinalIgnoreCase)); if (arcadeItem != null && repoUri != arcadeItem.RepoUri) { // Files in arcade repository. All Arcade items have a GitHub repo URI by default so we need to change the // URI from we are getting the eng/common files. If in an AzDO context we change the URI to that of // dotnet-arcade in dnceng string arcadeRepoUri = arcadeItem.RepoUri; if (Uri.TryCreate(repoUri, UriKind.Absolute, out Uri parsedUri)) { if (parsedUri.Host == "dev.azure.com" || parsedUri.Host.EndsWith("visualstudio.com")) { arcadeRepoUri = "https://dev.azure.com/dnceng/internal/_git/dotnet-arcade"; } } SemanticVersion arcadeDotnetVersion = await GetToolsDotnetVersionAsync(arcadeRepoUri, arcadeItem.Commit); if (arcadeDotnetVersion != null) { fileContainer.GlobalJson = UpdateDotnetVersionGlobalJson(arcadeDotnetVersion, fileContainer.GlobalJson); } List <GitFile> engCommonFiles = await GetCommonScriptFilesAsync(arcadeRepoUri, arcadeItem.Commit); filesToCommit.AddRange(engCommonFiles); // Files in the target repo string latestCommit = await _gitClient.GetLastCommitShaAsync(repoUri, branch); List <GitFile> targetEngCommonFiles = await GetCommonScriptFilesAsync(repoUri, latestCommit); foreach (GitFile file in targetEngCommonFiles) { if (!engCommonFiles.Where(f => f.FilePath == file.FilePath).Any()) { file.Operation = GitFileOperation.Delete; filesToCommit.Add(file); } } } filesToCommit.AddRange(fileContainer.GetFilesToCommit()); await _gitClient.CommitFilesAsync(filesToCommit, repoUri, branch, message); }