public override bool Execute() { OutputPath = OutputPath.Replace('\\', '/'); Directory.CreateDirectory(Path.GetDirectoryName(OutputPath)); DependencyVersionsFile depsFile; if (File.Exists(OutputPath)) { if (!DependencyVersionsFile.TryLoad(OutputPath, out depsFile)) { depsFile = DependencyVersionsFile.Create(AddOverrideImport, AdditionalImports); Log.LogWarning($"Could not load the existing deps file from {OutputPath}. This file will be overwritten."); } } else { depsFile = DependencyVersionsFile.Create(AddOverrideImport, AdditionalImports); } var varNames = new HashSet <string>(); foreach (var pkg in Packages) { var packageVersion = pkg.GetMetadata("Version"); if (string.IsNullOrEmpty(packageVersion)) { Log.LogError("Package {0} is missing the Version metadata", pkg.ItemSpec); continue; } string packageVarName; if (!string.IsNullOrEmpty(pkg.GetMetadata("VariableName"))) { packageVarName = pkg.GetMetadata("VariableName"); if (!packageVarName.EndsWith("Version", StringComparison.Ordinal)) { Log.LogError("VariableName for {0} must end in 'Version'", pkg.ItemSpec); continue; } } else { packageVarName = DependencyVersionsFile.GetVariableName(pkg.ItemSpec); } if (varNames.Contains(packageVarName)) { Log.LogError("Multiple packages would produce {0} in the generated dependencies.props file. Set VariableName to differentiate the packages manually", packageVarName); continue; } var item = depsFile.Update(packageVarName, packageVersion); if (!SuppressVariableLabels) { item.SetLabel(pkg.ItemSpec); } } depsFile.Save(OutputPath); Log.LogMessage(MessageImportance.Normal, $"Generated {OutputPath}"); return(!Log.HasLoggedErrors); }
public void GeneratesVariableName(string id, string varName) { Assert.Equal(varName, DependencyVersionsFile.GetVariableName(id)); }
public override bool Execute() { var unifiedPackageList = new Dictionary <string, PackageReferenceInfo>(StringComparer.OrdinalIgnoreCase); var projects = new ProjectInfoFactory(Log).CreateMany(Projects, Properties, false, _cts.Token); var packageRefs = projects.SelectMany(p => p.Frameworks).SelectMany(f => f.Dependencies); foreach (var packageRef in packageRefs) { if (packageRef.Value.IsImplicitlyDefined) { // skip PackageReferences added by the SDK continue; } if (packageRef.Value.NoWarn.Contains(KoreBuildErrors.Prefix + KoreBuildErrors.ConflictingPackageReferenceVersions)) { // Make it possible to suppress version conflicts while generating this file. continue; } if (unifiedPackageList.TryGetValue(packageRef.Value.Id, out var other)) { if (other.Version != packageRef.Value.Version) { Log.LogKoreBuildError(KoreBuildErrors.ConflictingPackageReferenceVersions, $"Conflicting dependency versions for {packageRef.Value.Id}: {other.Project.FileName} references '{other.Version}' but {packageRef.Value.Project.FileName} references '{packageRef.Value.Version}'"); } } else { unifiedPackageList.Add(packageRef.Value.Id, packageRef.Value); Log.LogMessage(MessageImportance.Low, $"Found {packageRef.Value.Id} = {packageRef.Value.Version}"); } } if (Log.HasLoggedErrors) { return(false); } var items = unifiedPackageList.Values.Select(p => new TaskItem(p.Id, new Hashtable { ["Version"] = p.Version })).ToArray(); var task = new GeneratePackageVersionPropsFile { AddOverrideImport = true, SuppressVariableLabels = true, Packages = items, BuildEngine = BuildEngine, HostObject = HostObject, OutputPath = DependenciesFile, }; if (!task.Execute()) { return(false); } var otherImports = OtherImports != null ? OtherImports.Select(p => p.ItemSpec) : Array.Empty <string>(); foreach (var proj in projects.Select(p => p.FullPath).Concat(otherImports)) { var project = ProjectRootElement.Open(proj, ProjectCollection.GlobalProjectCollection, preserveFormatting: true); var changed = false; foreach (var item in project.Items.Where(i => i.ItemType == "PackageReference")) { var noWarn = item.Metadata.FirstOrDefault(m => m.Name == "NoWarn"); if (noWarn != null && noWarn.Value.Contains(KoreBuildErrors.Prefix + KoreBuildErrors.ConflictingPackageReferenceVersions)) { continue; } var versionMetadata = item.Metadata.LastOrDefault(p => p.Name == "Version"); if (versionMetadata != null && versionMetadata.Value.StartsWith("$(")) { continue; } changed = true; var varName = $"$({DependencyVersionsFile.GetVariableName(item.Include)})"; if (versionMetadata == null) { item.AddMetadata("Version", varName, expressAsAttribute: true); } else { versionMetadata.Value = varName; } } if (changed) { Log.LogMessage(MessageImportance.High, $"Updated {proj}"); project.Save(proj); } else { Log.LogMessage(MessageImportance.Normal, $"Skipping {proj}. Already up to date."); } } return(!Log.HasLoggedErrors); }