Simplify substitution logic by removing need for string parsing
This commit is contained in:
parent
1e5a3cf59c
commit
cd93041f76
|
|
@ -12,9 +12,13 @@
|
||||||
AssemblyFile="$(MSBuildProjectDirectory)\$(OutputPath)DependencyUpdater.dll" />
|
AssemblyFile="$(MSBuildProjectDirectory)\$(OutputPath)DependencyUpdater.dll" />
|
||||||
|
|
||||||
<Target Name="InjectVersionsIntoProjectFilesInPackage" AfterTargets="Pack">
|
<Target Name="InjectVersionsIntoProjectFilesInPackage" AfterTargets="Pack">
|
||||||
|
<ItemGroup>
|
||||||
|
<Substitution Include="TemplateAspNetCoreVersion" Value="$(AspNetCoreVersionFromLineup)" />
|
||||||
|
<Substitution Include="TemplateEntityFrameworkVersion" Value="$(EntityFrameworkVersionFromLineup)" />
|
||||||
|
</ItemGroup>
|
||||||
<SubstituteProjectFileVariables
|
<SubstituteProjectFileVariables
|
||||||
NupkgFile="$(PackageOutputPath)$(MSBuildProjectName).$(PackageVersion).nupkg"
|
NupkgFile="$(PackageOutputPath)$(MSBuildProjectName).$(PackageVersion).nupkg"
|
||||||
Substitutions="TemplateAspNetCoreVersion=$(AspNetCoreVersionFromLineup); TemplateEntityFrameworkVersion=$(EntityFrameworkVersionFromLineup)"
|
Substitutions="@(Substitution)"
|
||||||
OutDir="$(PackageOutputPath)..\" />
|
OutDir="$(PackageOutputPath)..\" />
|
||||||
</Target>
|
</Target>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
using Microsoft.Build.Framework;
|
using Microsoft.Build.Framework;
|
||||||
using Microsoft.Build.Utilities;
|
using Microsoft.Build.Utilities;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.IO.Compression;
|
using System.IO.Compression;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
@ -16,14 +15,12 @@ namespace DependencyUpdater
|
||||||
".fsproj"
|
".fsproj"
|
||||||
};
|
};
|
||||||
|
|
||||||
public string NupkgFile { get; set; }
|
[Required] public string NupkgFile { get; set; }
|
||||||
public string Substitutions { get; set; }
|
[Required] public string OutDir { get; set; }
|
||||||
public string OutDir { get; set; }
|
[Required] public ITaskItem[] Substitutions { get; set; }
|
||||||
|
|
||||||
public override bool Execute()
|
public override bool Execute()
|
||||||
{
|
{
|
||||||
var substitutionsDict = ParseSubstitutions(Substitutions);
|
|
||||||
|
|
||||||
// We can't modify the .nupkg in place because the build system still
|
// 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
|
// has a lock on the file. We can read it, but not write it. So copy
|
||||||
// to the output location and then modify the copy.
|
// to the output location and then modify the copy.
|
||||||
|
|
@ -36,7 +33,7 @@ namespace DependencyUpdater
|
||||||
foreach (var projectFile in zipFile.Entries.Where(IsProjectFile))
|
foreach (var projectFile in zipFile.Entries.Where(IsProjectFile))
|
||||||
{
|
{
|
||||||
numProjectFiles++;
|
numProjectFiles++;
|
||||||
PerformVariableSubstitutions(projectFile, substitutionsDict);
|
PerformVariableSubstitutions(projectFile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -56,23 +53,13 @@ namespace DependencyUpdater
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static IDictionary<string, string> 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)
|
private static bool IsProjectFile(ZipArchiveEntry entry)
|
||||||
{
|
{
|
||||||
return ProjectFileExtensions.Any(
|
return ProjectFileExtensions.Any(
|
||||||
extension => Path.GetExtension(entry.Name).Equals(extension, StringComparison.OrdinalIgnoreCase));
|
extension => Path.GetExtension(entry.Name).Equals(extension, StringComparison.OrdinalIgnoreCase));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void PerformVariableSubstitutions(ZipArchiveEntry entry, IDictionary<string, string> substitutions)
|
private void PerformVariableSubstitutions(ZipArchiveEntry entry)
|
||||||
{
|
{
|
||||||
using (var fileStream = entry.Open())
|
using (var fileStream = entry.Open())
|
||||||
{
|
{
|
||||||
|
|
@ -83,16 +70,16 @@ namespace DependencyUpdater
|
||||||
contents = reader.ReadToEnd();
|
contents = reader.ReadToEnd();
|
||||||
fileStream.Seek(0, SeekOrigin.Begin);
|
fileStream.Seek(0, SeekOrigin.Begin);
|
||||||
fileStream.SetLength(0);
|
fileStream.SetLength(0);
|
||||||
writer.Write(SubstituteVariables(contents, substitutions));
|
writer.Write(SubstituteVariables(contents));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string SubstituteVariables(string text, IDictionary<string, string> substitutions)
|
private string SubstituteVariables(string text)
|
||||||
{
|
{
|
||||||
foreach (var kvp in substitutions)
|
foreach (var item in Substitutions)
|
||||||
{
|
{
|
||||||
text = text.Replace($"$({kvp.Key})", kvp.Value);
|
text = text.Replace($"$({item.ItemSpec})", item.GetMetadata("Value"));
|
||||||
}
|
}
|
||||||
|
|
||||||
return text;
|
return text;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue