From 378c93e7d693f57c1cded37b4929b6d2f00df0e7 Mon Sep 17 00:00:00 2001 From: Steve Sanderson Date: Wed, 30 Aug 2017 22:27:47 +0100 Subject: [PATCH] Also inject EntityFramework package versions dynamically --- src/Directory.Build.props | 12 +++--- src/Directory.Build.targets | 6 +-- .../Company.WebApplication1.csproj | 12 +++--- .../Company.WebApplication1.csproj | 12 +++--- ...e.cs => SubstituteProjectFileVariables.cs} | 41 +++++++++++-------- 5 files changed, 46 insertions(+), 37 deletions(-) rename tools/DependencyUpdater/{UpdateAspNetReference.cs => SubstituteProjectFileVariables.cs} (58%) diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 93d17fb6ce..e5054e52fc 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -12,7 +12,8 @@ $(VersionPrefix)-$(VersionSuffix) $(MSBuildThisFileDirectory)..\artifacts\tmp\ version=$(PackageVersion) - @(PackageReference->WithMetadataValue('Identity', 'Microsoft.AspNetCore.All')->Metadata('Version')) + @(PackageReference->WithMetadataValue('Identity', 'Microsoft.AspNetCore')->Metadata('Version')) + @(PackageReference->WithMetadataValue('Identity', 'Microsoft.EntityFrameworkCore')->Metadata('Version')) - + + diff --git a/src/Directory.Build.targets b/src/Directory.Build.targets index bedfa31a75..85a4173c5e 100644 --- a/src/Directory.Build.targets +++ b/src/Directory.Build.targets @@ -8,13 +8,13 @@ --> - diff --git a/src/Microsoft.DotNet.Web.ProjectTemplates.2.0/content/RazorPagesWeb-CSharp/Company.WebApplication1.csproj b/src/Microsoft.DotNet.Web.ProjectTemplates.2.0/content/RazorPagesWeb-CSharp/Company.WebApplication1.csproj index 292929d3e4..874e8dd7e9 100644 --- a/src/Microsoft.DotNet.Web.ProjectTemplates.2.0/content/RazorPagesWeb-CSharp/Company.WebApplication1.csproj +++ b/src/Microsoft.DotNet.Web.ProjectTemplates.2.0/content/RazorPagesWeb-CSharp/Company.WebApplication1.csproj @@ -11,7 +11,7 @@ - + @@ -23,15 +23,15 @@ - - - - + + + + - + diff --git a/src/Microsoft.DotNet.Web.ProjectTemplates.2.0/content/StarterWeb-CSharp/Company.WebApplication1.csproj b/src/Microsoft.DotNet.Web.ProjectTemplates.2.0/content/StarterWeb-CSharp/Company.WebApplication1.csproj index a3ff9ac842..98231a3d79 100644 --- a/src/Microsoft.DotNet.Web.ProjectTemplates.2.0/content/StarterWeb-CSharp/Company.WebApplication1.csproj +++ b/src/Microsoft.DotNet.Web.ProjectTemplates.2.0/content/StarterWeb-CSharp/Company.WebApplication1.csproj @@ -16,7 +16,7 @@ - + @@ -28,16 +28,16 @@ - - - - + + + + - + diff --git a/tools/DependencyUpdater/UpdateAspNetReference.cs b/tools/DependencyUpdater/SubstituteProjectFileVariables.cs similarity index 58% rename from tools/DependencyUpdater/UpdateAspNetReference.cs rename to tools/DependencyUpdater/SubstituteProjectFileVariables.cs index 305a271bc0..4d5368a466 100644 --- a/tools/DependencyUpdater/UpdateAspNetReference.cs +++ b/tools/DependencyUpdater/SubstituteProjectFileVariables.cs @@ -1,12 +1,13 @@ using Microsoft.Build.Utilities; using System; +using System.Collections.Generic; using System.IO; using System.IO.Compression; using System.Linq; namespace DependencyUpdater { - public class UpdateAspNetReference : Task + public class SubstituteProjectFileVariables : Task { private static string[] ProjectFileExtensions = new[] { @@ -15,15 +16,12 @@ namespace DependencyUpdater }; public string NupkgFile { get; set; } - public string AspNetVersion { get; set; } + public string Substitutions { get; set; } public string OutDir { get; set; } public override bool Execute() { - if (string.IsNullOrEmpty(AspNetVersion)) - { - throw new ArgumentException($"No value specified for {nameof(AspNetVersion)}."); - } + var substitutionsDict = ParseSubstitutions(Substitutions); // We can't modify the .nupkg in place because the build system still // has a lock on the file. We can read it, but not write it. So copy @@ -35,20 +33,30 @@ namespace DependencyUpdater { foreach (var projectFile in zipFile.Entries.Where(IsProjectFile)) { - PerformVariableSubstitution(projectFile); + PerformVariableSubstitutions(projectFile, substitutionsDict); } } return true; } + private static IDictionary ParseSubstitutions(string substitutions) + { + // Takes input of the form "key1=val1; key2=val2" (as is common in MSBuild) + return substitutions.Split(new[] { ';' }) + .Select(pair => pair.Trim()) + .Where(pair => !string.IsNullOrEmpty(pair) && pair.IndexOf('=') > 0) + .Select(pair => pair.Split('=')) + .ToDictionary(splitPair => splitPair[0].Trim(), splitPair => splitPair[1].Trim()); + } + private static bool IsProjectFile(ZipArchiveEntry entry) { return ProjectFileExtensions.Any( extension => Path.GetExtension(entry.Name).Equals(extension, StringComparison.OrdinalIgnoreCase)); } - private void PerformVariableSubstitution(ZipArchiveEntry entry) + private static void PerformVariableSubstitutions(ZipArchiveEntry entry, IDictionary substitutions) { using (var fileStream = entry.Open()) { @@ -59,20 +67,19 @@ namespace DependencyUpdater contents = reader.ReadToEnd(); fileStream.Seek(0, SeekOrigin.Begin); fileStream.SetLength(0); - writer.Write(SubstituteVariables(contents)); + writer.Write(SubstituteVariables(contents, substitutions)); } } } - private string SubstituteVariables(string projectFileContents) + private static string SubstituteVariables(string text, IDictionary substitutions) { - // Currently we only need a way of updating ASP.NET package - // reference versions, so that's all this does. In the future, - // we could generalise this into a system for injecting - // versions for all packages based on the KoreBuild lineup. - return projectFileContents.Replace( - "$(TemplateAspNetCoreVersion)", - AspNetVersion); + foreach (var kvp in substitutions) + { + text = text.Replace($"$({kvp.Key})", kvp.Value); + } + + return text; } } }