From 56b416b7c8a9aa63ff7323c7e9c5aa41f7d7827d Mon Sep 17 00:00:00 2001 From: Premek Vysoky Date: Thu, 1 Sep 2022 14:11:09 +0200 Subject: [PATCH 01/11] Add VmrDependencyInfo --- .../VirtualMonoRepo/VmrOperationBase.cs | 4 +- .../VirtualMonoRepo/SourceMappingParser.cs | 22 ++++-- .../VirtualMonoRepo/VmrDependencyInfo.cs | 39 ++++++++++ .../DarcLib/VirtualMonoRepo/VmrInitializer.cs | 20 +++--- .../DarcLib/VirtualMonoRepo/VmrManagerBase.cs | 71 +++++-------------- .../src/DarcLib/VirtualMonoRepo/VmrUpdater.cs | 31 ++++---- 6 files changed, 104 insertions(+), 83 deletions(-) create mode 100644 src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrDependencyInfo.cs diff --git a/src/Microsoft.DotNet.Darc/src/Darc/Operations/VirtualMonoRepo/VmrOperationBase.cs b/src/Microsoft.DotNet.Darc/src/Darc/Operations/VirtualMonoRepo/VmrOperationBase.cs index 5eca0ac81f..5c135c49a3 100644 --- a/src/Microsoft.DotNet.Darc/src/Darc/Operations/VirtualMonoRepo/VmrOperationBase.cs +++ b/src/Microsoft.DotNet.Darc/src/Darc/Operations/VirtualMonoRepo/VmrOperationBase.cs @@ -43,7 +43,7 @@ public override async Task ExecuteAsync() return Constants.ErrorCode; } - var vmrManager = Provider.GetRequiredService(); + TVmrManager vmrManager = Provider.GetRequiredService(); IEnumerable<(SourceMapping Mapping, string? Revision)> reposToSync; @@ -61,7 +61,7 @@ public override async Task ExecuteAsync() SourceMapping ResolveMapping(string repo) { - return vmrManager!.Mappings.FirstOrDefault(m => m.Name.Equals(repo, StringComparison.InvariantCultureIgnoreCase)) + return vmrManager.Mappings.FirstOrDefault(m => m.Name.Equals(repo, StringComparison.InvariantCultureIgnoreCase)) ?? throw new Exception($"No mapping named '{repo}' found"); } diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/SourceMappingParser.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/SourceMappingParser.cs index bfc3a3813e..d329b19769 100644 --- a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/SourceMappingParser.cs +++ b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/SourceMappingParser.cs @@ -23,13 +23,13 @@ public class SourceMappingParser : ISourceMappingParser { public async Task> ParseMappings(string vmrPath) { - var mappingFilePath = Path.Combine(vmrPath, VmrManagerBase.VmrSourcesPath, VmrManagerBase.SourceMappingsFileName); + var mappingFilePath = Path.Combine(vmrPath, VmrDependencyInfo.VmrSourcesPath, VmrDependencyInfo.SourceMappingsFileName); var mappingFile = new FileInfo(mappingFilePath); if (!mappingFile.Exists) { throw new FileNotFoundException( - $"Failed to find {VmrManagerBase.SourceMappingsFileName} file in the VMR directory", + $"Failed to find {VmrDependencyInfo.SourceMappingsFileName} file in the VMR directory", mappingFilePath); } @@ -41,7 +41,7 @@ public async Task> ParseMappings(string vmrPa using var stream = File.Open(mappingFile.FullName, FileMode.Open); var settings = await JsonSerializer.DeserializeAsync(stream, options) - ?? throw new Exception($"Failed to deserialize {VmrManagerBase.SourceMappingsFileName}"); + ?? throw new Exception($"Failed to deserialize {VmrDependencyInfo.SourceMappingsFileName}"); var patchesPath = settings.PatchesPath; if (patchesPath is not null) @@ -56,6 +56,18 @@ public async Task> ParseMappings(string vmrPa private static SourceMapping CreateMapping(SourceMappingSetting defaults, SourceMappingSetting setting, string? patchesPath) { + if (setting.Name is null) + { + throw new InvalidOperationException( + $"Missing `{nameof(SourceMapping.Name).ToLower()}` in {VmrDependencyInfo.SourceMappingsFileName}"); + } + + if (setting.DefaultRemote is null) + { + throw new InvalidOperationException( + $"Missing `{nameof(SourceMapping.DefaultRemote).ToLower()}` in {VmrDependencyInfo.SourceMappingsFileName}"); + } + IEnumerable include = setting.Include ?? Enumerable.Empty(); IEnumerable exclude = setting.Exclude ?? Enumerable.Empty(); @@ -77,9 +89,9 @@ private static SourceMapping CreateMapping(SourceMappingSetting defaults, Source : Array.Empty(); return new SourceMapping( - Name: setting.Name ?? throw new InvalidOperationException($"Missing `{nameof(SourceMapping.Name).ToLower()}` in {VmrManagerBase.SourceMappingsFileName}"), + Name: setting.Name, Version: setting.Version, - DefaultRemote: setting.DefaultRemote ?? throw new InvalidOperationException($"Missing `{nameof(SourceMapping.DefaultRemote).ToLower()}` in {VmrManagerBase.SourceMappingsFileName}"), + DefaultRemote: setting.DefaultRemote, DefaultRef: setting.DefaultRef ?? defaults.DefaultRef ?? "main", Include: include.ToImmutableArray(), Exclude: exclude.ToImmutableArray(), diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrDependencyInfo.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrDependencyInfo.cs new file mode 100644 index 0000000000..00ca50a22a --- /dev/null +++ b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrDependencyInfo.cs @@ -0,0 +1,39 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using System.IO; +using System.Threading.Tasks; +using Microsoft.DotNet.Darc.Models.VirtualMonoRepo; + +#nullable enable +namespace Microsoft.DotNet.DarcLib.VirtualMonoRepo; + +public interface IVmrDependencyInfo +{ + string VmrPath { get; } + + string SourcesPath { get; } + + IReadOnlyCollection Mappings { get; } + + string GetRepoSourcesPath(SourceMapping mapping) => Path.Combine(SourcesPath, mapping.Name); + + Task UpdateDependencyVersion(SourceMapping mapping, string sha, string? version); + + Task<(string Sha, string? Version)?> GetDependencyVersion(SourceMapping mapping); +} + +/// +/// Holds information about versions of individual repositories synchronized in the VMR. +/// Uses the AllRepoVersions.props file as source of truth and propagates changes into the git-info files. +/// +public class VmrDependencyInfo : IVmrDependencyInfo +{ + public const string SourceMappingsFileName = "source-mappings.json"; + public const string VmrSourcesPath = "src"; + public const string GitInfoSourcesPath = "git-info"; + + +} diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrInitializer.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrInitializer.cs index 36cebff8ba..2c729cbb4a 100644 --- a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrInitializer.cs +++ b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrInitializer.cs @@ -2,8 +2,9 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System.Collections.Generic; +using System; using System.IO; +using System.Linq; using System.Threading; using System.Threading.Tasks; using LibGit2Sharp; @@ -23,18 +24,20 @@ public class VmrInitializer : VmrManagerBase, IVmrInitializer Original commit: {remote}/commit/{newSha} """; - + + private readonly IVmrDependencyInfo _dependencyInfo; private readonly ILogger _logger; public VmrInitializer( + IVmrDependencyInfo dependencyInfo, IProcessManager processManager, IRemoteFactory remoteFactory, IVersionDetailsParser versionDetailsParser, ILogger logger, - IVmrManagerConfiguration configuration, - IReadOnlyCollection mappings) - : base(processManager, remoteFactory, versionDetailsParser, logger, mappings, configuration.VmrPath, configuration.TmpPath) + IVmrManagerConfiguration configuration) + : base(dependencyInfo, processManager, remoteFactory, versionDetailsParser, logger, configuration.TmpPath) { + _dependencyInfo = dependencyInfo; _logger = logger; } @@ -44,7 +47,7 @@ public async Task InitializeRepository( bool initializeDependencies, CancellationToken cancellationToken) { - if (File.Exists(GetTagFilePath(mapping))) + if (await _dependencyInfo.GetDependencyVersion(mapping) is not null) { throw new EmptySyncException($"Repository {mapping.Name} already exists"); } @@ -64,7 +67,8 @@ public async Task InitializeRepository( await ApplyPatch(mapping, patchPath, cancellationToken); cancellationToken.ThrowIfCancellationRequested(); - await TagRepo(mapping, commit.Id.Sha); + await _dependencyInfo.UpdateDependencyVersion(mapping, commit.Id.Sha, ); + Commands.Stage(new Repository(_dependencyInfo.VmrPath), VmrDependencyInfo.GitInfoSourcesPath); cancellationToken.ThrowIfCancellationRequested(); await ApplyVmrPatches(mapping, cancellationToken); @@ -89,7 +93,7 @@ private async Task InitializeDependencies(SourceMapping mapping, CancellationTok { foreach (var (dependency, dependencyMapping) in await GetDependencies(mapping, cancellationToken)) { - if (Directory.Exists(GetRepoSourcesPath(dependencyMapping))) + if (Directory.Exists(_dependencyInfo.GetRepoSourcesPath(dependencyMapping))) { _logger.LogDebug("Dependency {repo} has already been initialized", dependencyMapping.Name); continue; diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrManagerBase.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrManagerBase.cs index cb09134426..475efb702c 100644 --- a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrManagerBase.cs +++ b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrManagerBase.cs @@ -18,47 +18,36 @@ #nullable enable namespace Microsoft.DotNet.DarcLib.VirtualMonoRepo; -public abstract class VmrManagerBase +public abstract class VmrManagerBase : IVmrManager { protected const string HEAD = "HEAD"; private const string KeepAttribute = "vmr-preserve"; private const string IgnoreAttribute = "vmr-ignore"; - public const string VmrSourcesPath = "src"; - public const string SourceMappingsFileName = "source-mappings.json"; - - private readonly ILogger _logger; + private readonly IVmrDependencyInfo _dependencyInfo; private readonly IProcessManager _processManager; private readonly IRemoteFactory _remoteFactory; private readonly IVersionDetailsParser _versionDetailsParser; - private readonly string _tagsPath; + private readonly ILogger _logger; + private readonly string _tmpPath; - protected string VmrPath { get; } - - protected string SourcesPath { get; } - - public IReadOnlyCollection Mappings { get; } + public IReadOnlyCollection Mappings => _dependencyInfo.Mappings; protected VmrManagerBase( + IVmrDependencyInfo dependencyInfo, IProcessManager processManager, IRemoteFactory remoteFactory, IVersionDetailsParser versionDetailsParser, ILogger logger, - IReadOnlyCollection mappings, - string vmrPath, string tmpPath) { _logger = logger; + _dependencyInfo = dependencyInfo; _processManager = processManager; _remoteFactory = remoteFactory; _versionDetailsParser = versionDetailsParser; _tmpPath = tmpPath; - VmrPath = vmrPath; - SourcesPath = Path.Combine(vmrPath, VmrSourcesPath); - _tagsPath = Path.Combine(SourcesPath, ".tags"); - - Mappings = mappings; } /// @@ -89,26 +78,6 @@ protected async Task CloneOrPull(SourceMapping mapping) return clonePath; } - /// - /// Notes down the current SHA the given repo is synchronized to into a file inside the VMR. - /// - /// Repository - /// SHA - protected async Task TagRepo(SourceMapping mapping, string commitId) - { - if (!Directory.Exists(_tagsPath)) - { - Directory.CreateDirectory(_tagsPath); - } - - var tagFile = GetTagFilePath(mapping); - await File.WriteAllTextAsync(tagFile, commitId); - - // Stage the tag file - using var repository = new Repository(VmrPath); - Commands.Stage(repository, tagFile); - } - /// /// Creates a patch file (a diff) for given two commits in a repo adhering to the in/exclusion filters of the mapping. /// @@ -179,15 +148,15 @@ protected async Task CreatePatch( protected async Task ApplyPatch(SourceMapping mapping, string patchPath, CancellationToken cancellationToken) { // We have to give git a relative path with forward slashes where to apply the patch - var destPath = GetRepoSourcesPath(mapping) - .Replace(VmrPath, null) + var destPath = _dependencyInfo.GetRepoSourcesPath(mapping) + .Replace(_dependencyInfo.VmrPath, null) .Replace("\\", "/") [1..]; _logger.LogInformation("Applying patch {patchPath} to {path}...", patchPath, destPath); // This will help ignore some CR/LF issues (e.g. files with both endings) - (await _processManager.ExecuteGit(VmrPath, new[] { "config", "apply.ignoreWhitespace", "change" }, cancellationToken: cancellationToken)) + (await _processManager.ExecuteGit(_dependencyInfo.VmrPath, new[] { "config", "apply.ignoreWhitespace", "change" }, cancellationToken: cancellationToken)) .ThrowIfFailed("Failed to set git config whitespace settings"); Directory.CreateDirectory(destPath); @@ -213,7 +182,7 @@ protected async Task ApplyPatch(SourceMapping mapping, string patchPath, Cancell patchPath, }; - var result = await _processManager.ExecuteGit(VmrPath, args, cancellationToken: CancellationToken.None); + var result = await _processManager.ExecuteGit(_dependencyInfo.VmrPath, args, cancellationToken: CancellationToken.None); result.ThrowIfFailed($"Failed to apply the patch for {destPath}"); _logger.LogDebug("{output}", result.ToString()); @@ -222,7 +191,7 @@ protected async Task ApplyPatch(SourceMapping mapping, string patchPath, Cancell // This will end up having the working tree all staged _logger.LogInformation("Resetting the working tree..."); args = new[] { "checkout", destPath }; - result = await _processManager.ExecuteGit(VmrPath, args, cancellationToken: CancellationToken.None); + result = await _processManager.ExecuteGit(_dependencyInfo.VmrPath, args, cancellationToken: CancellationToken.None); result.ThrowIfFailed($"Failed to clean the working tree"); _logger.LogDebug("{output}", result.ToString()); } @@ -264,14 +233,14 @@ protected async Task UpdateGitmodules(CancellationToken cancellationToken) // Matches the 'path = ' setting from the .gitmodules file so that we can prefix it var pathSettingRegex = new Regex(@"(\bpath[ \t]*\=[ \t]*\b)"); - using (var vmrGitmodule = File.Open(Path.Combine(VmrPath, gitmodulesFileName), FileMode.Create)) + using (var vmrGitmodule = File.Open(Path.Combine(_dependencyInfo.VmrPath, gitmodulesFileName), FileMode.Create)) using (var writer = new StreamWriter(vmrGitmodule) { NewLine = "\n" }) { foreach (var mapping in Mappings) { cancellationToken.ThrowIfCancellationRequested(); - var repoGitmodulePath = Path.Combine(GetRepoSourcesPath(mapping), gitmodulesFileName); + var repoGitmodulePath = Path.Combine(_dependencyInfo.GetRepoSourcesPath(mapping), gitmodulesFileName); if (!File.Exists(repoGitmodulePath)) { continue; @@ -289,7 +258,7 @@ protected async Task UpdateGitmodules(CancellationToken cancellationToken) // Add src/[repo]/ prefixes to paths content = pathSettingRegex - .Replace(content, $"$1{VmrSourcesPath}/{mapping.Name}/") + .Replace(content, $"$1{VmrDependencyInfo.VmrSourcesPath}/{mapping.Name}/") .Replace("\r\n", "\n"); await writer.WriteAsync(content); @@ -300,7 +269,7 @@ protected async Task UpdateGitmodules(CancellationToken cancellationToken) } } - (await _processManager.ExecuteGit(VmrPath, new[] { "add", gitmodulesFileName }, cancellationToken)) + (await _processManager.ExecuteGit(_dependencyInfo.VmrPath, new[] { "add", gitmodulesFileName }, cancellationToken)) .ThrowIfFailed("Failed to stage the .gitmodules file!"); } @@ -309,7 +278,7 @@ protected void Commit(string commitMessage, Signature author) _logger.LogInformation("Committing.."); var watch = Stopwatch.StartNew(); - using var repository = new Repository(VmrPath); + using var repository = new Repository(_dependencyInfo.VmrPath); var commit = repository.Commit(commitMessage, author, DotnetBotCommitSignature); _logger.LogInformation("Created {sha} in {duration} seconds", ShortenId(commit.Id.Sha), (int) watch.Elapsed.TotalSeconds); @@ -323,7 +292,7 @@ protected void Commit(string commitMessage, Signature author) CancellationToken cancellationToken) { var versionDetailsPath = Path.Combine( - GetRepoSourcesPath(mapping), + _dependencyInfo.GetRepoSourcesPath(mapping), VersionFiles.VersionDetailsXml.Replace('/', Path.DirectorySeparatorChar)); var versionDetailsContent = await File.ReadAllTextAsync(versionDetailsPath, cancellationToken); @@ -352,12 +321,8 @@ protected void Commit(string commitMessage, Signature author) protected string GetPatchFilePath(SourceMapping mapping) => Path.Combine(_tmpPath, $"{mapping.Name}.patch"); - protected string GetTagFilePath(SourceMapping mapping) => Path.Combine(_tagsPath, $".{mapping.Name}"); - protected string GetClonePath(SourceMapping mapping) => Path.Combine(_tmpPath, mapping.Name); - protected string GetRepoSourcesPath(SourceMapping mapping) => Path.Combine(SourcesPath, mapping.Name); - /// /// Takes a given commit message template and populates it with given values, URLs and others. /// diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrUpdater.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrUpdater.cs index c501b6330e..dc55c6d282 100644 --- a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrUpdater.cs +++ b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrUpdater.cs @@ -50,19 +50,21 @@ public class VmrUpdater : VmrManagerBase, IVmrUpdater private static readonly Regex GitPatchSummaryLine = new(@"^[\-0-9]+\s+[\-0-9]+\s+(?[^\s]+)$", RegexOptions.Compiled); private readonly ILogger _logger; + private readonly IVmrDependencyInfo _dependencyInfo; private readonly IProcessManager _processManager; private readonly IRemoteFactory _remoteFactory; public VmrUpdater( + IVmrDependencyInfo dependencyInfo, IProcessManager processManager, IRemoteFactory remoteFactory, IVersionDetailsParser versionDetailsParser, ILogger logger, - IVmrManagerConfiguration configuration, - IReadOnlyCollection mappings) - : base(processManager, remoteFactory, versionDetailsParser, logger, mappings, configuration.VmrPath, configuration.TmpPath) + IVmrManagerConfiguration configuration) + : base(dependencyInfo, processManager, remoteFactory, versionDetailsParser, logger, configuration.TmpPath) { _logger = logger; + _dependencyInfo = dependencyInfo; _processManager = processManager; _remoteFactory = remoteFactory; } @@ -300,7 +302,8 @@ private async Task UpdateRepoToRevision( await ApplyPatch(mapping, patchPath, cancellationToken); } - await TagRepo(mapping, toRevision); + await _dependencyInfo.UpdateDependencyVersion(mapping, toRevision, ); + Commands.Stage(new Repository(_dependencyInfo.VmrPath), VmrDependencyInfo.GitInfoSourcesPath); cancellationToken.ThrowIfCancellationRequested(); await ApplyVmrPatches(mapping, cancellationToken); @@ -347,6 +350,8 @@ private async Task RestorePatchedFilesFromRepo(SourceMapping mapping, string ori var localRepo = new LocalGitClient(_processManager.GitExecutable, _logger); localRepo.Checkout(clonePath, originalRevision); + var repoSourcesPath = _dependencyInfo.GetRepoSourcesPath(mapping); + foreach (var patch in mapping.VmrPatches) { _logger.LogDebug("Processing VMR patch `{patch}`..", patch); @@ -359,7 +364,7 @@ private async Task RestorePatchedFilesFromRepo(SourceMapping mapping, string ori : patchedFile; var originalFile = Path.Combine(clonePath, relativePath); - var destination = Path.Combine(SourcesPath, mapping.Name, relativePath); + var destination = Path.Combine(repoSourcesPath, relativePath); _logger.LogDebug("Restoring file `{originalFile}` to `{destination}`..", originalFile, destination); @@ -369,8 +374,8 @@ private async Task RestorePatchedFilesFromRepo(SourceMapping mapping, string ori } // Stage the restored files (all future patches are applied to index directly) - using var repository = new Repository(VmrPath); - Commands.Stage(repository, $"{VmrSourcesPath}/{mapping.Name}"); + using var repository = new Repository(_dependencyInfo.VmrPath); + Commands.Stage(repository, repoSourcesPath); _logger.LogDebug("Files from VMR patches for {mappingName} restored", mapping.Name); } @@ -401,17 +406,13 @@ private async Task> GetFilesInPatch(string repoPath, private async Task GetCurrentVersion(SourceMapping mapping) { - var tagFile = GetTagFilePath(mapping); - string currentSha; - try - { - currentSha = (await File.ReadAllTextAsync(tagFile)).Trim(); - } - catch (FileNotFoundException) + var version = await _dependencyInfo.GetDependencyVersion(mapping); + + if (!version.HasValue) { throw new InvalidOperationException($"Missing tag file for {mapping.Name} - please initialize the individual repo first"); } - return currentSha; + return version.Value.Sha; } } From 929ab3de6b93a994bcda4edb1eefe2a1cec958d7 Mon Sep 17 00:00:00 2001 From: Premek Vysoky Date: Thu, 1 Sep 2022 15:50:34 +0200 Subject: [PATCH 02/11] Plumb package version through Manager --- .../VirtualMonoRepo/IVmrInitializer.cs | 18 ++++++++++++--- .../DarcLib/VirtualMonoRepo/IVmrUpdater.cs | 4 ++++ .../DarcLib/VirtualMonoRepo/VmrInitializer.cs | 7 +++--- .../src/DarcLib/VirtualMonoRepo/VmrUpdater.cs | 22 ++++++++++++------- 4 files changed, 36 insertions(+), 15 deletions(-) diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/IVmrInitializer.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/IVmrInitializer.cs index d186adfeab..14a3e7cbb2 100644 --- a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/IVmrInitializer.cs +++ b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/IVmrInitializer.cs @@ -18,14 +18,20 @@ public interface IVmrInitializer : IVmrManager /// /// Name of a repository mapping /// Revision (commit SHA, branch, tag..) onto which to synchronize, leave empty for HEAD + /// Version of packages, that the SHA we're updating to, produced /// When true, initializes dependencies (from Version.Details.xml) recursively /// Cancellation token - Task InitializeRepository(string mappingName, string? targetRevision, bool initializeDependencies, CancellationToken cancellationToken) + Task InitializeRepository( + string mappingName, + string? targetRevision, + string? packageVersion, + bool initializeDependencies, + CancellationToken cancellationToken) { var mapping = Mappings.FirstOrDefault(m => m.Name == mappingName) ?? throw new Exception($"No repository mapping named `{mappingName}` found!"); - return InitializeRepository(mapping, targetRevision, initializeDependencies, cancellationToken); + return InitializeRepository(mapping, targetRevision, packageVersion, initializeDependencies, cancellationToken); } /// @@ -33,7 +39,13 @@ Task InitializeRepository(string mappingName, string? targetRevision, bool initi /// /// Repository mapping /// Revision (commit SHA, branch, tag..) onto which to synchronize, leave empty for HEAD + /// Version of packages, that the SHA we're updating to, produced /// When true, initializes dependencies (from Version.Details.xml) recursively /// Cancellation token - Task InitializeRepository(SourceMapping mapping, string? targetRevision, bool initializeDependencies, CancellationToken cancellationToken); + Task InitializeRepository( + SourceMapping mapping, + string? targetRevision, + string? packageVersion, + bool initializeDependencies, + CancellationToken cancellationToken); } diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/IVmrUpdater.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/IVmrUpdater.cs index 7448f8d793..3d020de46e 100644 --- a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/IVmrUpdater.cs +++ b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/IVmrUpdater.cs @@ -18,12 +18,14 @@ public interface IVmrUpdater : IVmrManager /// /// Name of a repository mapping /// Revision (commit SHA, branch, tag..) onto which to synchronize, leave empty for HEAD + /// Version of packages, that the SHA we're updating to, produced /// Whether to pull changes commit by commit instead of squashing all updates into one /// When true, updates dependencies (from Version.Details.xml) recursively /// Cancellation token Task UpdateRepository( string mappingName, string? targetRevision, + string? packageVersion, bool noSquash, bool updateDependencies, CancellationToken cancellationToken) @@ -39,12 +41,14 @@ Task UpdateRepository( /// /// Repository mapping /// Revision (commit SHA, branch, tag..) onto which to synchronize, leave empty for HEAD + /// Version of packages, that the SHA we're updating to, produced /// Whether to pull changes commit by commit instead of squashing all updates into one /// When true, updates dependencies (from Version.Details.xml) recursively /// Cancellation token Task UpdateRepository( SourceMapping mapping, string? targetRevision, + string? packageVersion, bool noSquash, bool updateDependencies, CancellationToken cancellationToken); diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrInitializer.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrInitializer.cs index 2c729cbb4a..e7104c3596 100644 --- a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrInitializer.cs +++ b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrInitializer.cs @@ -2,9 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System; using System.IO; -using System.Linq; using System.Threading; using System.Threading.Tasks; using LibGit2Sharp; @@ -44,6 +42,7 @@ public VmrInitializer( public async Task InitializeRepository( SourceMapping mapping, string? targetRevision, + string? packageVersion, bool initializeDependencies, CancellationToken cancellationToken) { @@ -67,7 +66,7 @@ public async Task InitializeRepository( await ApplyPatch(mapping, patchPath, cancellationToken); cancellationToken.ThrowIfCancellationRequested(); - await _dependencyInfo.UpdateDependencyVersion(mapping, commit.Id.Sha, ); + await _dependencyInfo.UpdateDependencyVersion(mapping, commit.Id.Sha, packageVersion); Commands.Stage(new Repository(_dependencyInfo.VmrPath), VmrDependencyInfo.GitInfoSourcesPath); cancellationToken.ThrowIfCancellationRequested(); @@ -104,7 +103,7 @@ private async Task InitializeDependencies(SourceMapping mapping, CancellationTok dependency.Commit, dependency.Version); - await InitializeRepository(dependencyMapping, dependency.Commit, true, cancellationToken); + await InitializeRepository(dependencyMapping, dependency.Commit, dependency.Version, true, cancellationToken); } } } diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrUpdater.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrUpdater.cs index dc55c6d282..828f91095f 100644 --- a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrUpdater.cs +++ b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrUpdater.cs @@ -72,18 +72,20 @@ public VmrUpdater( public Task UpdateRepository( SourceMapping mapping, string? targetRevision, + string? packageVersion, bool noSquash, bool updateDependencies, CancellationToken cancellationToken) { return updateDependencies - ? UpdateRepositoryRecursively(mapping, targetRevision, noSquash, cancellationToken) - : UpdateRepository(mapping, targetRevision, noSquash, cancellationToken); + ? UpdateRepositoryRecursively(mapping, targetRevision, packageVersion, noSquash, cancellationToken) + : UpdateRepository(mapping, targetRevision, packageVersion, noSquash, cancellationToken); } private async Task UpdateRepository( SourceMapping mapping, string? targetRevision, + string? packageVersion, bool noSquash, CancellationToken cancellationToken) { @@ -174,6 +176,7 @@ await UpdateRepoToRevision( mapping, currentSha, commitToCopy.Sha, + commitToCopy.Sha == targetCommit.Sha ? packageVersion : null, clonePath, message, commitToCopy.Author, @@ -203,6 +206,7 @@ await UpdateRepoToRevision( mapping, currentSha, targetRevision, + packageVersion, clonePath, message, DotnetBotCommitSignature, @@ -217,13 +221,14 @@ await UpdateRepoToRevision( private async Task UpdateRepositoryRecursively( SourceMapping mapping, string? targetRevision, + string? packageVersion, bool noSquash, CancellationToken cancellationToken) { - var reposToUpdate = new Queue<(SourceMapping mapping, string? targetRevision)>(); - reposToUpdate.Enqueue((mapping, targetRevision)); + var reposToUpdate = new Queue<(SourceMapping mapping, string? targetRevision, string? packageVersion)>(); + reposToUpdate.Enqueue((mapping, targetRevision, packageVersion)); - var updatedDependencies = new HashSet<(SourceMapping mapping, string? targetRevision)>(); + var updatedDependencies = new HashSet<(SourceMapping mapping, string? targetRevision, string? packageVersion)>(); while (reposToUpdate.TryDequeue(out var repoToUpdate)) { @@ -233,7 +238,7 @@ private async Task UpdateRepositoryRecursively( mappingToUpdate.Name, repoToUpdate.targetRevision ?? HEAD); - await UpdateRepository(mappingToUpdate, repoToUpdate.targetRevision, noSquash, cancellationToken); + await UpdateRepository(mappingToUpdate, repoToUpdate.targetRevision, repoToUpdate.packageVersion, noSquash, cancellationToken); updatedDependencies.Add(repoToUpdate); foreach (var (dependency, dependencyMapping) in await GetDependencies(mappingToUpdate, cancellationToken)) @@ -250,7 +255,7 @@ private async Task UpdateRepositoryRecursively( continue; } - reposToUpdate.Enqueue((dependencyMapping, dependency.Commit)); + reposToUpdate.Enqueue((dependencyMapping, dependency.Commit, dependency.Version)); } } @@ -272,6 +277,7 @@ private async Task UpdateRepoToRevision( SourceMapping mapping, string fromRevision, string toRevision, + string? packageVersion, string clonePath, string commitMessage, Signature author, @@ -302,7 +308,7 @@ private async Task UpdateRepoToRevision( await ApplyPatch(mapping, patchPath, cancellationToken); } - await _dependencyInfo.UpdateDependencyVersion(mapping, toRevision, ); + await _dependencyInfo.UpdateDependencyVersion(mapping, toRevision, packageVersion); Commands.Stage(new Repository(_dependencyInfo.VmrPath), VmrDependencyInfo.GitInfoSourcesPath); cancellationToken.ThrowIfCancellationRequested(); From 3f24915826b5279af91191cb88f636610720e30a Mon Sep 17 00:00:00 2001 From: Premek Vysoky Date: Thu, 1 Sep 2022 17:56:39 +0200 Subject: [PATCH 03/11] Implement the DependencyInfo class --- .../VirtualMonoRepo/InitializeOperation.cs | 2 +- .../VirtualMonoRepo/UpdateOperation.cs | 2 +- .../VirtualMonoRepo/AllVersionsPropsFile.cs | 24 +++++-- .../DarcLib/VirtualMonoRepo/IVmrUpdater.cs | 2 +- .../VirtualMonoRepo/SourceMappingParser.cs | 2 +- .../VirtualMonoRepo/VmrDependencyInfo.cs | 62 +++++++++++++++++-- .../DarcLib/VirtualMonoRepo/VmrInitializer.cs | 6 +- .../DarcLib/VirtualMonoRepo/VmrManagerBase.cs | 2 +- .../src/DarcLib/VirtualMonoRepo/VmrUpdater.cs | 12 ++-- 9 files changed, 91 insertions(+), 23 deletions(-) diff --git a/src/Microsoft.DotNet.Darc/src/Darc/Operations/VirtualMonoRepo/InitializeOperation.cs b/src/Microsoft.DotNet.Darc/src/Darc/Operations/VirtualMonoRepo/InitializeOperation.cs index 02d1e420dd..3d002e8834 100644 --- a/src/Microsoft.DotNet.Darc/src/Darc/Operations/VirtualMonoRepo/InitializeOperation.cs +++ b/src/Microsoft.DotNet.Darc/src/Darc/Operations/VirtualMonoRepo/InitializeOperation.cs @@ -25,5 +25,5 @@ protected override async Task ExecuteInternalAsync( bool recursive, CancellationToken cancellationToken) => - await vmrManager.InitializeRepository(mapping, targetRevision, recursive, cancellationToken); + await vmrManager.InitializeRepository(mapping, targetRevision, null, recursive, cancellationToken); } diff --git a/src/Microsoft.DotNet.Darc/src/Darc/Operations/VirtualMonoRepo/UpdateOperation.cs b/src/Microsoft.DotNet.Darc/src/Darc/Operations/VirtualMonoRepo/UpdateOperation.cs index ab73e8f8ca..366520ddcc 100644 --- a/src/Microsoft.DotNet.Darc/src/Darc/Operations/VirtualMonoRepo/UpdateOperation.cs +++ b/src/Microsoft.DotNet.Darc/src/Darc/Operations/VirtualMonoRepo/UpdateOperation.cs @@ -28,5 +28,5 @@ protected override async Task ExecuteInternalAsync( bool recursive, CancellationToken cancellationToken) => - await vmrManager.UpdateRepository(mapping, targetRevision, _options.NoSquash, recursive, cancellationToken); + await vmrManager.UpdateRepository(mapping, targetRevision, null, _options.NoSquash, recursive, cancellationToken); } diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/Models/VirtualMonoRepo/AllVersionsPropsFile.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/Models/VirtualMonoRepo/AllVersionsPropsFile.cs index 6a745f4d86..89e617b79d 100644 --- a/src/Microsoft.DotNet.Darc/src/DarcLib/Models/VirtualMonoRepo/AllVersionsPropsFile.cs +++ b/src/Microsoft.DotNet.Darc/src/DarcLib/Models/VirtualMonoRepo/AllVersionsPropsFile.cs @@ -15,7 +15,7 @@ public interface IAllVersionsPropsFile : IMsBuildPropsFile Dictionary Versions { get; } (string? Sha, string? Version) GetVersion(string repository); - void UpdateVersion(string repository, string sha, string version); + void UpdateVersion(string repository, string? sha, string? version); } /// @@ -46,11 +46,27 @@ public AllVersionsPropsFile(Dictionary versions) return (sha, version); } - public void UpdateVersion(string repository, string sha, string version) + public void UpdateVersion(string repository, string? sha, string? version) { var key = SanitizePropertyName(repository); - Versions[key + ShaPropertyName] = sha; - Versions[key + PackageVersionPropertyName] = version; + + if (sha is not null) + { + Versions[key + ShaPropertyName] = sha; + } + else if (Versions.ContainsKey(key + ShaPropertyName)) + { + Versions.Remove(key + ShaPropertyName); + } + + if (version is not null) + { + Versions[key + ShaPropertyName] = version; + } + else if (Versions.ContainsKey(key + PackageVersionPropertyName)) + { + Versions.Remove(key + PackageVersionPropertyName); + } } public static AllVersionsPropsFile DeserializeFromXml(string path) diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/IVmrUpdater.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/IVmrUpdater.cs index 3d020de46e..48c4790179 100644 --- a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/IVmrUpdater.cs +++ b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/IVmrUpdater.cs @@ -33,7 +33,7 @@ Task UpdateRepository( var mapping = Mappings.FirstOrDefault(m => m.Name == mappingName) ?? throw new Exception($"No repository mapping named `{mappingName}` found!"); - return UpdateRepository(mapping, targetRevision, noSquash, updateDependencies, cancellationToken); + return UpdateRepository(mapping, targetRevision, null, noSquash, updateDependencies, cancellationToken); } /// diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/SourceMappingParser.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/SourceMappingParser.cs index d329b19769..d3e399f77d 100644 --- a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/SourceMappingParser.cs +++ b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/SourceMappingParser.cs @@ -23,7 +23,7 @@ public class SourceMappingParser : ISourceMappingParser { public async Task> ParseMappings(string vmrPath) { - var mappingFilePath = Path.Combine(vmrPath, VmrDependencyInfo.VmrSourcesPath, VmrDependencyInfo.SourceMappingsFileName); + var mappingFilePath = Path.Combine(vmrPath, VmrDependencyInfo.VmrSourcesDir, VmrDependencyInfo.SourceMappingsFileName); var mappingFile = new FileInfo(mappingFilePath); if (!mappingFile.Exists) diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrDependencyInfo.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrDependencyInfo.cs index 00ca50a22a..22f2ae944b 100644 --- a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrDependencyInfo.cs +++ b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrDependencyInfo.cs @@ -2,9 +2,10 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System; using System.Collections.Generic; using System.IO; -using System.Threading.Tasks; +using System.Threading; using Microsoft.DotNet.Darc.Models.VirtualMonoRepo; #nullable enable @@ -20,9 +21,9 @@ public interface IVmrDependencyInfo string GetRepoSourcesPath(SourceMapping mapping) => Path.Combine(SourcesPath, mapping.Name); - Task UpdateDependencyVersion(SourceMapping mapping, string sha, string? version); + void UpdateDependencyVersion(SourceMapping mapping, string sha, string? version); - Task<(string Sha, string? Version)?> GetDependencyVersion(SourceMapping mapping); + (string? Sha, string? Version)? GetDependencyVersion(SourceMapping mapping); } /// @@ -32,8 +33,59 @@ public interface IVmrDependencyInfo public class VmrDependencyInfo : IVmrDependencyInfo { public const string SourceMappingsFileName = "source-mappings.json"; - public const string VmrSourcesPath = "src"; - public const string GitInfoSourcesPath = "git-info"; + public const string VmrSourcesDir = "src"; + public const string GitInfoSourcesDir = "git-info"; + // TODO: https://github.com/dotnet/source-build/issues/2250 + private const string DefaultVersion = "7.0.100"; + private readonly Lazy _repoVersions; + private readonly string _allVersionsFilePath; + + public string VmrPath { get; } + + public string SourcesPath { get; } + + public IReadOnlyCollection Mappings { get; } + + public VmrDependencyInfo( + IVmrManagerConfiguration configuration, + IReadOnlyCollection mappings) + { + VmrPath = configuration.VmrPath; + SourcesPath = Path.Combine(configuration.VmrPath, VmrSourcesDir); + Mappings = mappings; + + _allVersionsFilePath = Path.Combine(VmrPath, GitInfoSourcesDir, AllVersionsPropsFile.FileName); + _repoVersions = new Lazy( + () => AllVersionsPropsFile.DeserializeFromXml(_allVersionsFilePath), + LazyThreadSafetyMode.ExecutionAndPublication); + } + + public (string? Sha, string? Version)? GetDependencyVersion(SourceMapping mapping) + => _repoVersions.Value.GetVersion(mapping.Name); + + public void UpdateDependencyVersion(SourceMapping mapping, string sha, string? version) + { + // TODO: https://github.com/dotnet/source-build/issues/2250 + version ??= DefaultVersion; + + _repoVersions.Value.UpdateVersion(mapping.Name, sha, version); + _repoVersions.Value.SerializeToXml(_allVersionsFilePath); + + var (buildId, releaseLabel) = VersionFiles.DeriveBuildInfo(mapping.Name, version); + + var gitInfo = new GitInfoFile + { + GitCommitHash = sha, + OfficialBuildId = buildId, + PreReleaseVersionLabel = releaseLabel, + IsStable = string.IsNullOrWhiteSpace(releaseLabel), + OutputPackageVersion = version, + }; + + gitInfo.SerializeToXml(GetGitInfoFilePath(mapping)); + } + + private string GetGitInfoFilePath(SourceMapping mapping) => Path.Combine(VmrPath, GitInfoSourcesDir, $"{mapping.Name}.props"); } diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrInitializer.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrInitializer.cs index e7104c3596..100083d775 100644 --- a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrInitializer.cs +++ b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrInitializer.cs @@ -46,7 +46,7 @@ public async Task InitializeRepository( bool initializeDependencies, CancellationToken cancellationToken) { - if (await _dependencyInfo.GetDependencyVersion(mapping) is not null) + if (_dependencyInfo.GetDependencyVersion(mapping) is not null) { throw new EmptySyncException($"Repository {mapping.Name} already exists"); } @@ -66,8 +66,8 @@ public async Task InitializeRepository( await ApplyPatch(mapping, patchPath, cancellationToken); cancellationToken.ThrowIfCancellationRequested(); - await _dependencyInfo.UpdateDependencyVersion(mapping, commit.Id.Sha, packageVersion); - Commands.Stage(new Repository(_dependencyInfo.VmrPath), VmrDependencyInfo.GitInfoSourcesPath); + _dependencyInfo.UpdateDependencyVersion(mapping, commit.Id.Sha, packageVersion); + Commands.Stage(new Repository(_dependencyInfo.VmrPath), VmrDependencyInfo.GitInfoSourcesDir); cancellationToken.ThrowIfCancellationRequested(); await ApplyVmrPatches(mapping, cancellationToken); diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrManagerBase.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrManagerBase.cs index 475efb702c..fe828af5d3 100644 --- a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrManagerBase.cs +++ b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrManagerBase.cs @@ -258,7 +258,7 @@ protected async Task UpdateGitmodules(CancellationToken cancellationToken) // Add src/[repo]/ prefixes to paths content = pathSettingRegex - .Replace(content, $"$1{VmrDependencyInfo.VmrSourcesPath}/{mapping.Name}/") + .Replace(content, $"$1{VmrDependencyInfo.VmrSourcesDir}/{mapping.Name}/") .Replace("\r\n", "\n"); await writer.WriteAsync(content); diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrUpdater.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrUpdater.cs index 828f91095f..8b2e407ba2 100644 --- a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrUpdater.cs +++ b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrUpdater.cs @@ -89,7 +89,7 @@ private async Task UpdateRepository( bool noSquash, CancellationToken cancellationToken) { - var currentSha = await GetCurrentVersion(mapping); + var currentSha = GetCurrentVersion(mapping); if (!await HasRemoteUpdates(mapping, currentSha)) { @@ -248,7 +248,7 @@ private async Task UpdateRepositoryRecursively( continue; } - var dependencySha = await GetCurrentVersion(dependencyMapping); + var dependencySha = GetCurrentVersion(dependencyMapping); if (dependencySha == dependency.Commit) { _logger.LogDebug("Dependency {name} is already at {sha}, skipping..", dependency.Name, dependencySha); @@ -308,8 +308,8 @@ private async Task UpdateRepoToRevision( await ApplyPatch(mapping, patchPath, cancellationToken); } - await _dependencyInfo.UpdateDependencyVersion(mapping, toRevision, packageVersion); - Commands.Stage(new Repository(_dependencyInfo.VmrPath), VmrDependencyInfo.GitInfoSourcesPath); + _dependencyInfo.UpdateDependencyVersion(mapping, toRevision, packageVersion); + Commands.Stage(new Repository(_dependencyInfo.VmrPath), VmrDependencyInfo.GitInfoSourcesDir); cancellationToken.ThrowIfCancellationRequested(); await ApplyVmrPatches(mapping, cancellationToken); @@ -410,9 +410,9 @@ private async Task> GetFilesInPatch(string repoPath, return files; } - private async Task GetCurrentVersion(SourceMapping mapping) + private string GetCurrentVersion(SourceMapping mapping) { - var version = await _dependencyInfo.GetDependencyVersion(mapping); + var version = _dependencyInfo.GetDependencyVersion(mapping); if (!version.HasValue) { From 202c22cdbb0dad4de7102e08e6d8cfb59008e1fe Mon Sep 17 00:00:00 2001 From: Premek Vysoky Date: Tue, 6 Sep 2022 11:37:48 +0200 Subject: [PATCH 04/11] Rename VmrDependencyInfo to VmrDependencyTracker --- .../VirtualMonoRepo/IVmrInitializer.cs | 10 ++--- .../DarcLib/VirtualMonoRepo/IVmrUpdater.cs | 8 ++-- .../VirtualMonoRepo/SourceMappingParser.cs | 10 ++--- ...endencyInfo.cs => VmrDependencyTracker.cs} | 6 +-- .../DarcLib/VirtualMonoRepo/VmrInitializer.cs | 18 ++++---- .../DarcLib/VirtualMonoRepo/VmrManagerBase.cs | 6 +-- .../src/DarcLib/VirtualMonoRepo/VmrUpdater.cs | 42 +++++++++---------- 7 files changed, 50 insertions(+), 50 deletions(-) rename src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/{VmrDependencyInfo.cs => VmrDependencyTracker.cs} (96%) diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/IVmrInitializer.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/IVmrInitializer.cs index 14a3e7cbb2..a606ce19c3 100644 --- a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/IVmrInitializer.cs +++ b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/IVmrInitializer.cs @@ -18,20 +18,20 @@ public interface IVmrInitializer : IVmrManager /// /// Name of a repository mapping /// Revision (commit SHA, branch, tag..) onto which to synchronize, leave empty for HEAD - /// Version of packages, that the SHA we're updating to, produced + /// Version of packages, that the SHA we're updating to, produced /// When true, initializes dependencies (from Version.Details.xml) recursively /// Cancellation token Task InitializeRepository( string mappingName, string? targetRevision, - string? packageVersion, + string? targetVersion, bool initializeDependencies, CancellationToken cancellationToken) { var mapping = Mappings.FirstOrDefault(m => m.Name == mappingName) ?? throw new Exception($"No repository mapping named `{mappingName}` found!"); - return InitializeRepository(mapping, targetRevision, packageVersion, initializeDependencies, cancellationToken); + return InitializeRepository(mapping, targetRevision, targetVersion, initializeDependencies, cancellationToken); } /// @@ -39,13 +39,13 @@ Task InitializeRepository( /// /// Repository mapping /// Revision (commit SHA, branch, tag..) onto which to synchronize, leave empty for HEAD - /// Version of packages, that the SHA we're updating to, produced + /// Version of packages, that the SHA we're updating to, produced /// When true, initializes dependencies (from Version.Details.xml) recursively /// Cancellation token Task InitializeRepository( SourceMapping mapping, string? targetRevision, - string? packageVersion, + string? targetVersion, bool initializeDependencies, CancellationToken cancellationToken); } diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/IVmrUpdater.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/IVmrUpdater.cs index 48c4790179..19a7ed4d14 100644 --- a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/IVmrUpdater.cs +++ b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/IVmrUpdater.cs @@ -18,14 +18,14 @@ public interface IVmrUpdater : IVmrManager /// /// Name of a repository mapping /// Revision (commit SHA, branch, tag..) onto which to synchronize, leave empty for HEAD - /// Version of packages, that the SHA we're updating to, produced + /// Version of packages, that the SHA we're updating to, produced /// Whether to pull changes commit by commit instead of squashing all updates into one /// When true, updates dependencies (from Version.Details.xml) recursively /// Cancellation token Task UpdateRepository( string mappingName, string? targetRevision, - string? packageVersion, + string? targetVersion, bool noSquash, bool updateDependencies, CancellationToken cancellationToken) @@ -41,14 +41,14 @@ Task UpdateRepository( /// /// Repository mapping /// Revision (commit SHA, branch, tag..) onto which to synchronize, leave empty for HEAD - /// Version of packages, that the SHA we're updating to, produced + /// Version of packages, that the SHA we're updating to, produced /// Whether to pull changes commit by commit instead of squashing all updates into one /// When true, updates dependencies (from Version.Details.xml) recursively /// Cancellation token Task UpdateRepository( SourceMapping mapping, string? targetRevision, - string? packageVersion, + string? targetVersion, bool noSquash, bool updateDependencies, CancellationToken cancellationToken); diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/SourceMappingParser.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/SourceMappingParser.cs index d3e399f77d..d09c4e823b 100644 --- a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/SourceMappingParser.cs +++ b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/SourceMappingParser.cs @@ -23,13 +23,13 @@ public class SourceMappingParser : ISourceMappingParser { public async Task> ParseMappings(string vmrPath) { - var mappingFilePath = Path.Combine(vmrPath, VmrDependencyInfo.VmrSourcesDir, VmrDependencyInfo.SourceMappingsFileName); + var mappingFilePath = Path.Combine(vmrPath, VmrDependencyTracker.VmrSourcesDir, VmrDependencyTracker.SourceMappingsFileName); var mappingFile = new FileInfo(mappingFilePath); if (!mappingFile.Exists) { throw new FileNotFoundException( - $"Failed to find {VmrDependencyInfo.SourceMappingsFileName} file in the VMR directory", + $"Failed to find {VmrDependencyTracker.SourceMappingsFileName} file in the VMR directory", mappingFilePath); } @@ -41,7 +41,7 @@ public async Task> ParseMappings(string vmrPa using var stream = File.Open(mappingFile.FullName, FileMode.Open); var settings = await JsonSerializer.DeserializeAsync(stream, options) - ?? throw new Exception($"Failed to deserialize {VmrDependencyInfo.SourceMappingsFileName}"); + ?? throw new Exception($"Failed to deserialize {VmrDependencyTracker.SourceMappingsFileName}"); var patchesPath = settings.PatchesPath; if (patchesPath is not null) @@ -59,13 +59,13 @@ private static SourceMapping CreateMapping(SourceMappingSetting defaults, Source if (setting.Name is null) { throw new InvalidOperationException( - $"Missing `{nameof(SourceMapping.Name).ToLower()}` in {VmrDependencyInfo.SourceMappingsFileName}"); + $"Missing `{nameof(SourceMapping.Name).ToLower()}` in {VmrDependencyTracker.SourceMappingsFileName}"); } if (setting.DefaultRemote is null) { throw new InvalidOperationException( - $"Missing `{nameof(SourceMapping.DefaultRemote).ToLower()}` in {VmrDependencyInfo.SourceMappingsFileName}"); + $"Missing `{nameof(SourceMapping.DefaultRemote).ToLower()}` in {VmrDependencyTracker.SourceMappingsFileName}"); } IEnumerable include = setting.Include ?? Enumerable.Empty(); diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrDependencyInfo.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrDependencyTracker.cs similarity index 96% rename from src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrDependencyInfo.cs rename to src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrDependencyTracker.cs index 22f2ae944b..b5a2b03e62 100644 --- a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrDependencyInfo.cs +++ b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrDependencyTracker.cs @@ -11,7 +11,7 @@ #nullable enable namespace Microsoft.DotNet.DarcLib.VirtualMonoRepo; -public interface IVmrDependencyInfo +public interface IVmrDependencyTracker { string VmrPath { get; } @@ -30,7 +30,7 @@ public interface IVmrDependencyInfo /// Holds information about versions of individual repositories synchronized in the VMR. /// Uses the AllRepoVersions.props file as source of truth and propagates changes into the git-info files. /// -public class VmrDependencyInfo : IVmrDependencyInfo +public class VmrDependencyTracker : IVmrDependencyTracker { public const string SourceMappingsFileName = "source-mappings.json"; public const string VmrSourcesDir = "src"; @@ -48,7 +48,7 @@ public class VmrDependencyInfo : IVmrDependencyInfo public IReadOnlyCollection Mappings { get; } - public VmrDependencyInfo( + public VmrDependencyTracker( IVmrManagerConfiguration configuration, IReadOnlyCollection mappings) { diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrInitializer.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrInitializer.cs index 100083d775..581f96feb0 100644 --- a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrInitializer.cs +++ b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrInitializer.cs @@ -23,30 +23,30 @@ public class VmrInitializer : VmrManagerBase, IVmrInitializer Original commit: {remote}/commit/{newSha} """; - private readonly IVmrDependencyInfo _dependencyInfo; + private readonly IVmrDependencyTracker _dependencyTracker; private readonly ILogger _logger; public VmrInitializer( - IVmrDependencyInfo dependencyInfo, + IVmrDependencyTracker dependencyTracker, IProcessManager processManager, IRemoteFactory remoteFactory, IVersionDetailsParser versionDetailsParser, ILogger logger, IVmrManagerConfiguration configuration) - : base(dependencyInfo, processManager, remoteFactory, versionDetailsParser, logger, configuration.TmpPath) + : base(dependencyTracker, processManager, remoteFactory, versionDetailsParser, logger, configuration.TmpPath) { - _dependencyInfo = dependencyInfo; + _dependencyTracker = dependencyTracker; _logger = logger; } public async Task InitializeRepository( SourceMapping mapping, string? targetRevision, - string? packageVersion, + string? targetVersion, bool initializeDependencies, CancellationToken cancellationToken) { - if (_dependencyInfo.GetDependencyVersion(mapping) is not null) + if (_dependencyTracker.GetDependencyVersion(mapping) is not null) { throw new EmptySyncException($"Repository {mapping.Name} already exists"); } @@ -66,8 +66,8 @@ public async Task InitializeRepository( await ApplyPatch(mapping, patchPath, cancellationToken); cancellationToken.ThrowIfCancellationRequested(); - _dependencyInfo.UpdateDependencyVersion(mapping, commit.Id.Sha, packageVersion); - Commands.Stage(new Repository(_dependencyInfo.VmrPath), VmrDependencyInfo.GitInfoSourcesDir); + _dependencyTracker.UpdateDependencyVersion(mapping, commit.Id.Sha, targetVersion); + Commands.Stage(new Repository(_dependencyTracker.VmrPath), VmrDependencyTracker.GitInfoSourcesDir); cancellationToken.ThrowIfCancellationRequested(); await ApplyVmrPatches(mapping, cancellationToken); @@ -92,7 +92,7 @@ private async Task InitializeDependencies(SourceMapping mapping, CancellationTok { foreach (var (dependency, dependencyMapping) in await GetDependencies(mapping, cancellationToken)) { - if (Directory.Exists(_dependencyInfo.GetRepoSourcesPath(dependencyMapping))) + if (Directory.Exists(_dependencyTracker.GetRepoSourcesPath(dependencyMapping))) { _logger.LogDebug("Dependency {repo} has already been initialized", dependencyMapping.Name); continue; diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrManagerBase.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrManagerBase.cs index fe828af5d3..d837d8ceb8 100644 --- a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrManagerBase.cs +++ b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrManagerBase.cs @@ -24,7 +24,7 @@ public abstract class VmrManagerBase : IVmrManager private const string KeepAttribute = "vmr-preserve"; private const string IgnoreAttribute = "vmr-ignore"; - private readonly IVmrDependencyInfo _dependencyInfo; + private readonly IVmrDependencyTracker _dependencyInfo; private readonly IProcessManager _processManager; private readonly IRemoteFactory _remoteFactory; private readonly IVersionDetailsParser _versionDetailsParser; @@ -35,7 +35,7 @@ public abstract class VmrManagerBase : IVmrManager public IReadOnlyCollection Mappings => _dependencyInfo.Mappings; protected VmrManagerBase( - IVmrDependencyInfo dependencyInfo, + IVmrDependencyTracker dependencyInfo, IProcessManager processManager, IRemoteFactory remoteFactory, IVersionDetailsParser versionDetailsParser, @@ -258,7 +258,7 @@ protected async Task UpdateGitmodules(CancellationToken cancellationToken) // Add src/[repo]/ prefixes to paths content = pathSettingRegex - .Replace(content, $"$1{VmrDependencyInfo.VmrSourcesDir}/{mapping.Name}/") + .Replace(content, $"$1{VmrDependencyTracker.VmrSourcesDir}/{mapping.Name}/") .Replace("\r\n", "\n"); await writer.WriteAsync(content); diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrUpdater.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrUpdater.cs index 8b2e407ba2..9ef5ab6f9a 100644 --- a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrUpdater.cs +++ b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrUpdater.cs @@ -50,21 +50,21 @@ public class VmrUpdater : VmrManagerBase, IVmrUpdater private static readonly Regex GitPatchSummaryLine = new(@"^[\-0-9]+\s+[\-0-9]+\s+(?[^\s]+)$", RegexOptions.Compiled); private readonly ILogger _logger; - private readonly IVmrDependencyInfo _dependencyInfo; + private readonly IVmrDependencyTracker _dependencyTracker; private readonly IProcessManager _processManager; private readonly IRemoteFactory _remoteFactory; public VmrUpdater( - IVmrDependencyInfo dependencyInfo, + IVmrDependencyTracker dependencyTracker, IProcessManager processManager, IRemoteFactory remoteFactory, IVersionDetailsParser versionDetailsParser, ILogger logger, IVmrManagerConfiguration configuration) - : base(dependencyInfo, processManager, remoteFactory, versionDetailsParser, logger, configuration.TmpPath) + : base(dependencyTracker, processManager, remoteFactory, versionDetailsParser, logger, configuration.TmpPath) { _logger = logger; - _dependencyInfo = dependencyInfo; + _dependencyTracker = dependencyTracker; _processManager = processManager; _remoteFactory = remoteFactory; } @@ -72,20 +72,20 @@ public VmrUpdater( public Task UpdateRepository( SourceMapping mapping, string? targetRevision, - string? packageVersion, + string? targetVersion, bool noSquash, bool updateDependencies, CancellationToken cancellationToken) { return updateDependencies - ? UpdateRepositoryRecursively(mapping, targetRevision, packageVersion, noSquash, cancellationToken) - : UpdateRepository(mapping, targetRevision, packageVersion, noSquash, cancellationToken); + ? UpdateRepositoryRecursively(mapping, targetRevision, targetVersion, noSquash, cancellationToken) + : UpdateRepository(mapping, targetRevision, targetVersion, noSquash, cancellationToken); } private async Task UpdateRepository( SourceMapping mapping, string? targetRevision, - string? packageVersion, + string? targetVersion, bool noSquash, CancellationToken cancellationToken) { @@ -176,7 +176,7 @@ await UpdateRepoToRevision( mapping, currentSha, commitToCopy.Sha, - commitToCopy.Sha == targetCommit.Sha ? packageVersion : null, + commitToCopy.Sha == targetCommit.Sha ? targetVersion : null, clonePath, message, commitToCopy.Author, @@ -206,7 +206,7 @@ await UpdateRepoToRevision( mapping, currentSha, targetRevision, - packageVersion, + targetVersion, clonePath, message, DotnetBotCommitSignature, @@ -221,14 +221,14 @@ await UpdateRepoToRevision( private async Task UpdateRepositoryRecursively( SourceMapping mapping, string? targetRevision, - string? packageVersion, + string? targetVersion, bool noSquash, CancellationToken cancellationToken) { - var reposToUpdate = new Queue<(SourceMapping mapping, string? targetRevision, string? packageVersion)>(); - reposToUpdate.Enqueue((mapping, targetRevision, packageVersion)); + var reposToUpdate = new Queue<(SourceMapping mapping, string? targetRevision, string? targetVersion)>(); + reposToUpdate.Enqueue((mapping, targetRevision, targetVersion)); - var updatedDependencies = new HashSet<(SourceMapping mapping, string? targetRevision, string? packageVersion)>(); + var updatedDependencies = new HashSet<(SourceMapping mapping, string? targetRevision, string? targetVersion)>(); while (reposToUpdate.TryDequeue(out var repoToUpdate)) { @@ -238,7 +238,7 @@ private async Task UpdateRepositoryRecursively( mappingToUpdate.Name, repoToUpdate.targetRevision ?? HEAD); - await UpdateRepository(mappingToUpdate, repoToUpdate.targetRevision, repoToUpdate.packageVersion, noSquash, cancellationToken); + await UpdateRepository(mappingToUpdate, repoToUpdate.targetRevision, repoToUpdate.targetVersion, noSquash, cancellationToken); updatedDependencies.Add(repoToUpdate); foreach (var (dependency, dependencyMapping) in await GetDependencies(mappingToUpdate, cancellationToken)) @@ -277,7 +277,7 @@ private async Task UpdateRepoToRevision( SourceMapping mapping, string fromRevision, string toRevision, - string? packageVersion, + string? targetVersion, string clonePath, string commitMessage, Signature author, @@ -308,8 +308,8 @@ private async Task UpdateRepoToRevision( await ApplyPatch(mapping, patchPath, cancellationToken); } - _dependencyInfo.UpdateDependencyVersion(mapping, toRevision, packageVersion); - Commands.Stage(new Repository(_dependencyInfo.VmrPath), VmrDependencyInfo.GitInfoSourcesDir); + _dependencyTracker.UpdateDependencyVersion(mapping, toRevision, targetVersion); + Commands.Stage(new Repository(_dependencyTracker.VmrPath), VmrDependencyTracker.GitInfoSourcesDir); cancellationToken.ThrowIfCancellationRequested(); await ApplyVmrPatches(mapping, cancellationToken); @@ -356,7 +356,7 @@ private async Task RestorePatchedFilesFromRepo(SourceMapping mapping, string ori var localRepo = new LocalGitClient(_processManager.GitExecutable, _logger); localRepo.Checkout(clonePath, originalRevision); - var repoSourcesPath = _dependencyInfo.GetRepoSourcesPath(mapping); + var repoSourcesPath = _dependencyTracker.GetRepoSourcesPath(mapping); foreach (var patch in mapping.VmrPatches) { @@ -380,7 +380,7 @@ private async Task RestorePatchedFilesFromRepo(SourceMapping mapping, string ori } // Stage the restored files (all future patches are applied to index directly) - using var repository = new Repository(_dependencyInfo.VmrPath); + using var repository = new Repository(_dependencyTracker.VmrPath); Commands.Stage(repository, repoSourcesPath); _logger.LogDebug("Files from VMR patches for {mappingName} restored", mapping.Name); @@ -412,7 +412,7 @@ private async Task> GetFilesInPatch(string repoPath, private string GetCurrentVersion(SourceMapping mapping) { - var version = _dependencyInfo.GetDependencyVersion(mapping); + var version = _dependencyTracker.GetDependencyVersion(mapping); if (!version.HasValue) { From c1d807b76150d7b951df425de83b2c64132de55c Mon Sep 17 00:00:00 2001 From: Premek Vysoky Date: Tue, 6 Sep 2022 11:44:44 +0200 Subject: [PATCH 05/11] Resolve nullability issue --- .../src/DarcLib/VirtualMonoRepo/VmrUpdater.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrUpdater.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrUpdater.cs index 9ef5ab6f9a..991a47014c 100644 --- a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrUpdater.cs +++ b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrUpdater.cs @@ -414,9 +414,9 @@ private string GetCurrentVersion(SourceMapping mapping) { var version = _dependencyTracker.GetDependencyVersion(mapping); - if (!version.HasValue) + if (!version.HasValue || version.Value.Sha is null) { - throw new InvalidOperationException($"Missing tag file for {mapping.Name} - please initialize the individual repo first"); + throw new InvalidOperationException($"Repository {mapping.Name} has not been initialized yet"); } return version.Value.Sha; From d2af7bcff055b2a698698b0e7f5f6104331fd7ac Mon Sep 17 00:00:00 2001 From: Premek Vysoky Date: Tue, 6 Sep 2022 13:04:27 +0200 Subject: [PATCH 06/11] Fix DI, bugs --- .../VirtualMonoRepo/AllVersionsPropsFile.cs | 10 +++- .../VirtualMonoRepo/VmrDependencyTracker.cs | 14 ++++- .../DarcLib/VirtualMonoRepo/VmrInitializer.cs | 2 +- .../VirtualMonoRepo/VmrManagerFactory.cs | 59 ------------------- .../VirtualMonoRepo/VmrRegistrations.cs | 14 ++++- 5 files changed, 31 insertions(+), 68 deletions(-) delete mode 100644 src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrManagerFactory.cs diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/Models/VirtualMonoRepo/AllVersionsPropsFile.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/Models/VirtualMonoRepo/AllVersionsPropsFile.cs index 89e617b79d..189317170a 100644 --- a/src/Microsoft.DotNet.Darc/src/DarcLib/Models/VirtualMonoRepo/AllVersionsPropsFile.cs +++ b/src/Microsoft.DotNet.Darc/src/DarcLib/Models/VirtualMonoRepo/AllVersionsPropsFile.cs @@ -14,7 +14,7 @@ public interface IAllVersionsPropsFile : IMsBuildPropsFile { Dictionary Versions { get; } - (string? Sha, string? Version) GetVersion(string repository); + (string? Sha, string? Version)? GetVersion(string repository); void UpdateVersion(string repository, string? sha, string? version); } @@ -36,13 +36,19 @@ public AllVersionsPropsFile(Dictionary versions) Versions = versions; } - public (string? Sha, string? Version) GetVersion(string repository) + public (string? Sha, string? Version)? GetVersion(string repository) { var key = SanitizePropertyName(repository) + ShaPropertyName; Versions.TryGetValue(key, out var sha); key = SanitizePropertyName(repository) + PackageVersionPropertyName; Versions.TryGetValue(key, out var version); + + if (sha is null && version is null) + { + return null; + } + return (sha, version); } diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrDependencyTracker.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrDependencyTracker.cs index b5a2b03e62..1ec32d783f 100644 --- a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrDependencyTracker.cs +++ b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrDependencyTracker.cs @@ -57,9 +57,7 @@ public VmrDependencyTracker( Mappings = mappings; _allVersionsFilePath = Path.Combine(VmrPath, GitInfoSourcesDir, AllVersionsPropsFile.FileName); - _repoVersions = new Lazy( - () => AllVersionsPropsFile.DeserializeFromXml(_allVersionsFilePath), - LazyThreadSafetyMode.ExecutionAndPublication); + _repoVersions = new Lazy(LoadAllVersionsFile, LazyThreadSafetyMode.ExecutionAndPublication); } public (string? Sha, string? Version)? GetDependencyVersion(SourceMapping mapping) @@ -87,5 +85,15 @@ public void UpdateDependencyVersion(SourceMapping mapping, string sha, string? v gitInfo.SerializeToXml(GetGitInfoFilePath(mapping)); } + private AllVersionsPropsFile LoadAllVersionsFile() + { + if (!File.Exists(_allVersionsFilePath)) + { + return new AllVersionsPropsFile(new()); + } + + return AllVersionsPropsFile.DeserializeFromXml(_allVersionsFilePath); + } + private string GetGitInfoFilePath(SourceMapping mapping) => Path.Combine(VmrPath, GitInfoSourcesDir, $"{mapping.Name}.props"); } diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrInitializer.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrInitializer.cs index 581f96feb0..704c748d79 100644 --- a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrInitializer.cs +++ b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrInitializer.cs @@ -46,7 +46,7 @@ public async Task InitializeRepository( bool initializeDependencies, CancellationToken cancellationToken) { - if (_dependencyTracker.GetDependencyVersion(mapping) is not null) + if (_dependencyTracker.GetDependencyVersion(mapping).HasValue) { throw new EmptySyncException($"Repository {mapping.Name} already exists"); } diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrManagerFactory.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrManagerFactory.cs deleted file mode 100644 index b98fce9dab..0000000000 --- a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrManagerFactory.cs +++ /dev/null @@ -1,59 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Threading.Tasks; -using Microsoft.Extensions.DependencyInjection; - -#nullable enable -namespace Microsoft.DotNet.DarcLib.VirtualMonoRepo; - -public interface IVmrManagerFactory -{ - Task CreateVmrInitializer(); - Task CreateVmrInitializer(IVmrManagerConfiguration configuration); - Task CreateVmrUpdater(); - Task CreateVmrUpdater(IVmrManagerConfiguration configuration); -} - -public class VmrManagerFactory : IVmrManagerFactory -{ - private readonly IServiceProvider _serviceProvider; - private readonly ISourceMappingParser _sourceMappingParser; - private readonly IVmrManagerConfiguration _configuration; - - public VmrManagerFactory( - IServiceProvider serviceProvider, - ISourceMappingParser sourceMappingParser, - IVmrManagerConfiguration configuration) - { - _serviceProvider = serviceProvider; - _sourceMappingParser = sourceMappingParser; - _configuration = configuration; - } - - public Task CreateVmrInitializer() - => CreateVmrManager(); - - public Task CreateVmrInitializer(IVmrManagerConfiguration configuration) - => CreateVmrManager(configuration); - - public Task CreateVmrUpdater() - => CreateVmrManager(); - - public Task CreateVmrUpdater(IVmrManagerConfiguration configuration) - => CreateVmrManager(configuration); - - private async Task CreateVmrManager() where T : R - { - var mappings = await _sourceMappingParser.ParseMappings(_configuration.VmrPath); - return ActivatorUtilities.CreateInstance(_serviceProvider, mappings); - } - - private async Task CreateVmrManager(IVmrManagerConfiguration configuration) where T : R - { - var mappings = await _sourceMappingParser.ParseMappings(configuration.VmrPath); - return ActivatorUtilities.CreateInstance(_serviceProvider, configuration, mappings); - } -} diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrRegistrations.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrRegistrations.cs index d48fd65647..b49a55aec4 100644 --- a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrRegistrations.cs +++ b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrRegistrations.cs @@ -3,6 +3,8 @@ // See the LICENSE file in the project root for more information. using System; +using Microsoft.DotNet.Darc.Models.VirtualMonoRepo; +using System.Collections.Generic; using Microsoft.DotNet.DarcLib.Helpers; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; @@ -38,8 +40,14 @@ private static void RegisterManagers(IServiceCollection services, string gitLoca services.TryAddTransient(sp => ActivatorUtilities.CreateInstance(sp, gitLocation)); services.TryAddTransient(); services.TryAddTransient(); - services.TryAddTransient(); - services.TryAddTransient(sp => sp.GetRequiredService().CreateVmrUpdater().GetAwaiter().GetResult()); - services.TryAddTransient(sp => sp.GetRequiredService().CreateVmrInitializer().GetAwaiter().GetResult()); + services.TryAddTransient(); + services.TryAddTransient(); + services.TryAddTransient(); + services.TryAddSingleton>(sp => + { + var configuration = sp.GetRequiredService(); + var mappingParser = sp.GetRequiredService(); + return mappingParser.ParseMappings(configuration.VmrPath).GetAwaiter().GetResult(); + }); } } From a37758db41233104b4ec21edc3a46487cbf07a5f Mon Sep 17 00:00:00 2001 From: Premek Vysoky Date: Fri, 9 Sep 2022 13:23:46 +0200 Subject: [PATCH 07/11] Fix version property --- .../src/DarcLib/Models/VirtualMonoRepo/AllVersionsPropsFile.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/Models/VirtualMonoRepo/AllVersionsPropsFile.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/Models/VirtualMonoRepo/AllVersionsPropsFile.cs index 189317170a..9d4e012cfe 100644 --- a/src/Microsoft.DotNet.Darc/src/DarcLib/Models/VirtualMonoRepo/AllVersionsPropsFile.cs +++ b/src/Microsoft.DotNet.Darc/src/DarcLib/Models/VirtualMonoRepo/AllVersionsPropsFile.cs @@ -67,7 +67,7 @@ public void UpdateVersion(string repository, string? sha, string? version) if (version is not null) { - Versions[key + ShaPropertyName] = version; + Versions[key + PackageVersionPropertyName] = version; } else if (Versions.ContainsKey(key + PackageVersionPropertyName)) { From 85936554db0945846bfa36fc77cc1b93342d6817 Mon Sep 17 00:00:00 2001 From: Premek Vysoky Date: Fri, 9 Sep 2022 13:31:35 +0200 Subject: [PATCH 08/11] Create directory for XMLs --- .../src/DarcLib/Models/MsBuildPropsFile.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/Models/MsBuildPropsFile.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/Models/MsBuildPropsFile.cs index a144ed48af..685a6ef82f 100644 --- a/src/Microsoft.DotNet.Darc/src/DarcLib/Models/MsBuildPropsFile.cs +++ b/src/Microsoft.DotNet.Darc/src/DarcLib/Models/MsBuildPropsFile.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Xml; using System.Xml.Linq; @@ -48,6 +49,12 @@ protected MsBuildPropsFile(bool? orderPropertiesAscending) public void SerializeToXml(string path) { + var parentDir = Path.GetDirectoryName(path) ?? throw new ArgumentException($"'{path}' is not a valid path."); + if (!Directory.Exists(parentDir)) + { + Directory.CreateDirectory(parentDir); + } + XmlSerializer serializer = new XmlSerializer(typeof(XmlElement)); var xmlDocument = new XmlDocument(); XmlElement root = xmlDocument.CreateElement(ProjectPropertyName); From f5b9fc6d65141db258db6fb0c6de1cb7b9ff48ba Mon Sep 17 00:00:00 2001 From: Premek Vysoky Date: Fri, 9 Sep 2022 23:17:23 +0200 Subject: [PATCH 09/11] Add type for the sha/version tuple --- .../VirtualMonoRepo/AllVersionsPropsFile.cs | 27 ++++++------ .../VirtualMonoRepo/VmrDependencyTracker.cs | 25 ++++++----- .../DarcLib/VirtualMonoRepo/VmrInitializer.cs | 4 +- .../src/DarcLib/VirtualMonoRepo/VmrUpdater.cs | 6 +-- .../AllVersionsPropsFileTests.cs | 43 ++++++++++--------- 5 files changed, 56 insertions(+), 49 deletions(-) diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/Models/VirtualMonoRepo/AllVersionsPropsFile.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/Models/VirtualMonoRepo/AllVersionsPropsFile.cs index 9d4e012cfe..2f22853864 100644 --- a/src/Microsoft.DotNet.Darc/src/DarcLib/Models/VirtualMonoRepo/AllVersionsPropsFile.cs +++ b/src/Microsoft.DotNet.Darc/src/DarcLib/Models/VirtualMonoRepo/AllVersionsPropsFile.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Xml; using Microsoft.DotNet.DarcLib.Models; +using Microsoft.DotNet.DarcLib.VirtualMonoRepo; #nullable enable namespace Microsoft.DotNet.Darc.Models.VirtualMonoRepo; @@ -14,8 +15,8 @@ public interface IAllVersionsPropsFile : IMsBuildPropsFile { Dictionary Versions { get; } - (string? Sha, string? Version)? GetVersion(string repository); - void UpdateVersion(string repository, string? sha, string? version); + VmrDependencyVersion? GetVersion(string repository); + void UpdateVersion(string repository, VmrDependencyVersion version); } /// @@ -36,38 +37,38 @@ public AllVersionsPropsFile(Dictionary versions) Versions = versions; } - public (string? Sha, string? Version)? GetVersion(string repository) + public VmrDependencyVersion? GetVersion(string repository) { var key = SanitizePropertyName(repository) + ShaPropertyName; Versions.TryGetValue(key, out var sha); - key = SanitizePropertyName(repository) + PackageVersionPropertyName; - Versions.TryGetValue(key, out var version); - - if (sha is null && version is null) + if (sha is null) { return null; } - return (sha, version); + key = SanitizePropertyName(repository) + PackageVersionPropertyName; + Versions.TryGetValue(key, out var version); + + return new(sha, version); } - public void UpdateVersion(string repository, string? sha, string? version) + public void UpdateVersion(string repository, VmrDependencyVersion version) { var key = SanitizePropertyName(repository); - if (sha is not null) + if (version.Sha is not null) { - Versions[key + ShaPropertyName] = sha; + Versions[key + ShaPropertyName] = version.Sha; } else if (Versions.ContainsKey(key + ShaPropertyName)) { Versions.Remove(key + ShaPropertyName); } - if (version is not null) + if (version.PackageVersion is not null) { - Versions[key + PackageVersionPropertyName] = version; + Versions[key + PackageVersionPropertyName] = version.PackageVersion; } else if (Versions.ContainsKey(key + PackageVersionPropertyName)) { diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrDependencyTracker.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrDependencyTracker.cs index 1ec32d783f..7ad330e65b 100644 --- a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrDependencyTracker.cs +++ b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrDependencyTracker.cs @@ -11,6 +11,8 @@ #nullable enable namespace Microsoft.DotNet.DarcLib.VirtualMonoRepo; +public record VmrDependencyVersion(string Sha, string? PackageVersion); + public interface IVmrDependencyTracker { string VmrPath { get; } @@ -21,9 +23,9 @@ public interface IVmrDependencyTracker string GetRepoSourcesPath(SourceMapping mapping) => Path.Combine(SourcesPath, mapping.Name); - void UpdateDependencyVersion(SourceMapping mapping, string sha, string? version); + void UpdateDependencyVersion(SourceMapping mapping, VmrDependencyVersion version); - (string? Sha, string? Version)? GetDependencyVersion(SourceMapping mapping); + VmrDependencyVersion? GetDependencyVersion(SourceMapping mapping); } /// @@ -37,7 +39,7 @@ public class VmrDependencyTracker : IVmrDependencyTracker public const string GitInfoSourcesDir = "git-info"; // TODO: https://github.com/dotnet/source-build/issues/2250 - private const string DefaultVersion = "7.0.100"; + private const string DefaultVersion = "8.0.100"; private readonly Lazy _repoVersions; private readonly string _allVersionsFilePath; @@ -60,26 +62,29 @@ public VmrDependencyTracker( _repoVersions = new Lazy(LoadAllVersionsFile, LazyThreadSafetyMode.ExecutionAndPublication); } - public (string? Sha, string? Version)? GetDependencyVersion(SourceMapping mapping) + public VmrDependencyVersion? GetDependencyVersion(SourceMapping mapping) => _repoVersions.Value.GetVersion(mapping.Name); - public void UpdateDependencyVersion(SourceMapping mapping, string sha, string? version) + public void UpdateDependencyVersion(SourceMapping mapping, VmrDependencyVersion version) { // TODO: https://github.com/dotnet/source-build/issues/2250 - version ??= DefaultVersion; + if (version.PackageVersion is null) + { + version = version with { PackageVersion = DefaultVersion }; + } - _repoVersions.Value.UpdateVersion(mapping.Name, sha, version); + _repoVersions.Value.UpdateVersion(mapping.Name, version); _repoVersions.Value.SerializeToXml(_allVersionsFilePath); - var (buildId, releaseLabel) = VersionFiles.DeriveBuildInfo(mapping.Name, version); + var (buildId, releaseLabel) = VersionFiles.DeriveBuildInfo(mapping.Name, version.PackageVersion); var gitInfo = new GitInfoFile { - GitCommitHash = sha, + GitCommitHash = version.Sha, OfficialBuildId = buildId, PreReleaseVersionLabel = releaseLabel, IsStable = string.IsNullOrWhiteSpace(releaseLabel), - OutputPackageVersion = version, + OutputPackageVersion = version.PackageVersion, }; gitInfo.SerializeToXml(GetGitInfoFilePath(mapping)); diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrInitializer.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrInitializer.cs index 704c748d79..d888ceaf8f 100644 --- a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrInitializer.cs +++ b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrInitializer.cs @@ -46,7 +46,7 @@ public async Task InitializeRepository( bool initializeDependencies, CancellationToken cancellationToken) { - if (_dependencyTracker.GetDependencyVersion(mapping).HasValue) + if (_dependencyTracker.GetDependencyVersion(mapping) is not null) { throw new EmptySyncException($"Repository {mapping.Name} already exists"); } @@ -66,7 +66,7 @@ public async Task InitializeRepository( await ApplyPatch(mapping, patchPath, cancellationToken); cancellationToken.ThrowIfCancellationRequested(); - _dependencyTracker.UpdateDependencyVersion(mapping, commit.Id.Sha, targetVersion); + _dependencyTracker.UpdateDependencyVersion(mapping, new(commit.Id.Sha, targetVersion)); Commands.Stage(new Repository(_dependencyTracker.VmrPath), VmrDependencyTracker.GitInfoSourcesDir); cancellationToken.ThrowIfCancellationRequested(); diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrUpdater.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrUpdater.cs index 991a47014c..a81e01460b 100644 --- a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrUpdater.cs +++ b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrUpdater.cs @@ -308,7 +308,7 @@ private async Task UpdateRepoToRevision( await ApplyPatch(mapping, patchPath, cancellationToken); } - _dependencyTracker.UpdateDependencyVersion(mapping, toRevision, targetVersion); + _dependencyTracker.UpdateDependencyVersion(mapping, new(toRevision, targetVersion)); Commands.Stage(new Repository(_dependencyTracker.VmrPath), VmrDependencyTracker.GitInfoSourcesDir); cancellationToken.ThrowIfCancellationRequested(); @@ -414,11 +414,11 @@ private string GetCurrentVersion(SourceMapping mapping) { var version = _dependencyTracker.GetDependencyVersion(mapping); - if (!version.HasValue || version.Value.Sha is null) + if (version is null) { throw new InvalidOperationException($"Repository {mapping.Name} has not been initialized yet"); } - return version.Value.Sha; + return version.Sha; } } diff --git a/src/Microsoft.DotNet.Darc/tests/Microsoft.DotNet.DarcLib.Tests/Models/VirtualMonoRepo/AllVersionsPropsFileTests.cs b/src/Microsoft.DotNet.Darc/tests/Microsoft.DotNet.DarcLib.Tests/Models/VirtualMonoRepo/AllVersionsPropsFileTests.cs index a2a736675f..c0c8c75344 100644 --- a/src/Microsoft.DotNet.Darc/tests/Microsoft.DotNet.DarcLib.Tests/Models/VirtualMonoRepo/AllVersionsPropsFileTests.cs +++ b/src/Microsoft.DotNet.Darc/tests/Microsoft.DotNet.DarcLib.Tests/Models/VirtualMonoRepo/AllVersionsPropsFileTests.cs @@ -6,6 +6,7 @@ using System.IO; using FluentAssertions; using Microsoft.DotNet.Darc.Models.VirtualMonoRepo; +using Microsoft.DotNet.DarcLib.VirtualMonoRepo; using NUnit.Framework; #nullable enable @@ -41,21 +42,21 @@ public void CleanUpOutputFile() [Test] public void AllVersionsPropsFileIsDeSerializedTest() { - string runtimeSha = "26a71c61fbda229f151afb14e274604b4926df5c"; - string runtimeVersion = "7.0.0-rc.1.22403.8"; + var runtimeVersion = new VmrDependencyVersion("26a71c61fbda229f151afb14e274604b4926df5c", "7.0.0-rc.1.22403.8"); + var sdkVersion = new VmrDependencyVersion("6e00e543bbeb8e0491420e2f6b3f7d235166596d", "7.0.100-rc.1.22404.18"); var allVersionsPropsFile = new AllVersionsPropsFile(new() { - { "runtimeGitCommitHash", runtimeSha }, - { "runtimeOutputPackageVersion", runtimeVersion }, - { "sdkGitCommitHash", "6e00e543bbeb8e0491420e2f6b3f7d235166596d" }, - { "sdkOutputPackageVersion", "7.0.100-rc.1.22404.18" }, + { "runtimeGitCommitHash", runtimeVersion.Sha }, + { "runtimeOutputPackageVersion", runtimeVersion.PackageVersion! }, + { "sdkGitCommitHash", sdkVersion.Sha }, + { "sdkOutputPackageVersion", sdkVersion.PackageVersion! }, }); void VerifyVersions() { - allVersionsPropsFile.GetVersion("runtime").Should().Be((runtimeSha, runtimeVersion)); - allVersionsPropsFile.GetVersion("sdk").Should().Be(("6e00e543bbeb8e0491420e2f6b3f7d235166596d", "7.0.100-rc.1.22404.18")); + allVersionsPropsFile.GetVersion("runtime").Should().Be(runtimeVersion); + allVersionsPropsFile.GetVersion("sdk").Should().Be(sdkVersion); } VerifyVersions(); @@ -68,10 +69,10 @@ void VerifyVersions() - {runtimeSha} - {runtimeVersion} - 6e00e543bbeb8e0491420e2f6b3f7d235166596d - 7.0.100-rc.1.22404.18 + {runtimeVersion.Sha} + {runtimeVersion.PackageVersion} + {sdkVersion.Sha} + {sdkVersion.PackageVersion} """); @@ -80,12 +81,12 @@ void VerifyVersions() VerifyVersions(); - var newRuntimeSha = "225ce682f0578db2db5644df5e7024276b39785e"; - var newRuntimeVersion = "7.0.0-rc.1.22403.8"; - allVersionsPropsFile.UpdateVersion("runtime", newRuntimeSha, newRuntimeVersion); + runtimeVersion = new VmrDependencyVersion("225ce682f0578db2db5644df5e7024276b39785e", "7.0.0-rc.1.22444.8"); - allVersionsPropsFile.GetVersion("runtime").Should().Be((newRuntimeSha, newRuntimeVersion)); - allVersionsPropsFile.GetVersion("sdk").Should().Be(("6e00e543bbeb8e0491420e2f6b3f7d235166596d", "7.0.100-rc.1.22404.18")); + allVersionsPropsFile.UpdateVersion("runtime", runtimeVersion); + + allVersionsPropsFile.GetVersion("runtime").Should().Be(runtimeVersion); + allVersionsPropsFile.GetVersion("sdk").Should().Be(sdkVersion); allVersionsPropsFile.SerializeToXml(_outputFile); @@ -95,10 +96,10 @@ void VerifyVersions() - {newRuntimeSha} - {newRuntimeVersion} - 6e00e543bbeb8e0491420e2f6b3f7d235166596d - 7.0.100-rc.1.22404.18 + {runtimeVersion.Sha} + {runtimeVersion.PackageVersion} + {sdkVersion.Sha} + {sdkVersion.PackageVersion} """); From 94e785a08d73149f0bf0a5a7d8ac3df93ba11a0b Mon Sep 17 00:00:00 2001 From: Premek Vysoky Date: Mon, 12 Sep 2022 11:09:19 +0200 Subject: [PATCH 10/11] Renamed parentDir --- .../src/DarcLib/Models/MsBuildPropsFile.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/Models/MsBuildPropsFile.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/Models/MsBuildPropsFile.cs index 685a6ef82f..1db0e995f2 100644 --- a/src/Microsoft.DotNet.Darc/src/DarcLib/Models/MsBuildPropsFile.cs +++ b/src/Microsoft.DotNet.Darc/src/DarcLib/Models/MsBuildPropsFile.cs @@ -49,10 +49,10 @@ protected MsBuildPropsFile(bool? orderPropertiesAscending) public void SerializeToXml(string path) { - var parentDir = Path.GetDirectoryName(path) ?? throw new ArgumentException($"'{path}' is not a valid path."); - if (!Directory.Exists(parentDir)) + var directory = Path.GetDirectoryName(path) ?? throw new ArgumentException($"'{path}' is not a valid path."); + if (!Directory.Exists(directory)) { - Directory.CreateDirectory(parentDir); + Directory.CreateDirectory(directory); } XmlSerializer serializer = new XmlSerializer(typeof(XmlElement)); From 5af934f50d5cb999d6642c5b21ee026a83cac374 Mon Sep 17 00:00:00 2001 From: Premek Vysoky Date: Mon, 12 Sep 2022 14:04:14 +0200 Subject: [PATCH 11/11] Disallow null for version updates --- .../VirtualMonoRepo/AllVersionsPropsFile.cs | 24 ++++--------------- .../VirtualMonoRepo/VmrDependencyTracker.cs | 2 +- .../AllVersionsPropsFileTests.cs | 2 +- 3 files changed, 6 insertions(+), 22 deletions(-) diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/Models/VirtualMonoRepo/AllVersionsPropsFile.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/Models/VirtualMonoRepo/AllVersionsPropsFile.cs index 2f22853864..0fa8434aec 100644 --- a/src/Microsoft.DotNet.Darc/src/DarcLib/Models/VirtualMonoRepo/AllVersionsPropsFile.cs +++ b/src/Microsoft.DotNet.Darc/src/DarcLib/Models/VirtualMonoRepo/AllVersionsPropsFile.cs @@ -16,7 +16,7 @@ public interface IAllVersionsPropsFile : IMsBuildPropsFile Dictionary Versions { get; } VmrDependencyVersion? GetVersion(string repository); - void UpdateVersion(string repository, VmrDependencyVersion version); + void UpdateVersion(string repository, string sha, string packageVersion); } /// @@ -53,27 +53,11 @@ public AllVersionsPropsFile(Dictionary versions) return new(sha, version); } - public void UpdateVersion(string repository, VmrDependencyVersion version) + public void UpdateVersion(string repository, string sha, string packageVersion) { var key = SanitizePropertyName(repository); - - if (version.Sha is not null) - { - Versions[key + ShaPropertyName] = version.Sha; - } - else if (Versions.ContainsKey(key + ShaPropertyName)) - { - Versions.Remove(key + ShaPropertyName); - } - - if (version.PackageVersion is not null) - { - Versions[key + PackageVersionPropertyName] = version.PackageVersion; - } - else if (Versions.ContainsKey(key + PackageVersionPropertyName)) - { - Versions.Remove(key + PackageVersionPropertyName); - } + Versions[key + ShaPropertyName] = sha; + Versions[key + PackageVersionPropertyName] = packageVersion; } public static AllVersionsPropsFile DeserializeFromXml(string path) diff --git a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrDependencyTracker.cs b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrDependencyTracker.cs index 7ad330e65b..0fb9018dcc 100644 --- a/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrDependencyTracker.cs +++ b/src/Microsoft.DotNet.Darc/src/DarcLib/VirtualMonoRepo/VmrDependencyTracker.cs @@ -73,7 +73,7 @@ public void UpdateDependencyVersion(SourceMapping mapping, VmrDependencyVersion version = version with { PackageVersion = DefaultVersion }; } - _repoVersions.Value.UpdateVersion(mapping.Name, version); + _repoVersions.Value.UpdateVersion(mapping.Name, version.Sha, version.PackageVersion); _repoVersions.Value.SerializeToXml(_allVersionsFilePath); var (buildId, releaseLabel) = VersionFiles.DeriveBuildInfo(mapping.Name, version.PackageVersion); diff --git a/src/Microsoft.DotNet.Darc/tests/Microsoft.DotNet.DarcLib.Tests/Models/VirtualMonoRepo/AllVersionsPropsFileTests.cs b/src/Microsoft.DotNet.Darc/tests/Microsoft.DotNet.DarcLib.Tests/Models/VirtualMonoRepo/AllVersionsPropsFileTests.cs index c0c8c75344..80c0968c7c 100644 --- a/src/Microsoft.DotNet.Darc/tests/Microsoft.DotNet.DarcLib.Tests/Models/VirtualMonoRepo/AllVersionsPropsFileTests.cs +++ b/src/Microsoft.DotNet.Darc/tests/Microsoft.DotNet.DarcLib.Tests/Models/VirtualMonoRepo/AllVersionsPropsFileTests.cs @@ -83,7 +83,7 @@ void VerifyVersions() runtimeVersion = new VmrDependencyVersion("225ce682f0578db2db5644df5e7024276b39785e", "7.0.0-rc.1.22444.8"); - allVersionsPropsFile.UpdateVersion("runtime", runtimeVersion); + allVersionsPropsFile.UpdateVersion("runtime", runtimeVersion.Sha, runtimeVersion.PackageVersion!); allVersionsPropsFile.GetVersion("runtime").Should().Be(runtimeVersion); allVersionsPropsFile.GetVersion("sdk").Should().Be(sdkVersion);