diff options
author | Mikayla Hutchinson <m.j.hutchinson@gmail.com> | 2019-01-10 10:27:12 +0300 |
---|---|---|
committer | Mikayla Hutchinson <m.j.hutchinson@gmail.com> | 2019-01-11 04:42:16 +0300 |
commit | 31a88cd65c90f757b27bda456abcb94cf560d787 (patch) | |
tree | 2afdb854799f4a7cdb6e27b6ddadc466ef1de175 /main/msbuild | |
parent | a60641c26645e91ecb9e0de8a359787dfcf6d3cf (diff) |
New build task that downloads nupkgs
Diffstat (limited to 'main/msbuild')
-rw-r--r-- | main/msbuild/DownloadFile.targets | 18 | ||||
-rw-r--r-- | main/msbuild/MDBuildTasks.targets | 85 | ||||
-rw-r--r-- | main/msbuild/MDBuildTasks/MDBuildTasks.csproj | 5 | ||||
-rw-r--r-- | main/msbuild/MDBuildTasks/MDDownloadFiles.cs | 28 | ||||
-rw-r--r-- | main/msbuild/MDBuildTasks/MDDownloadNupkg.cs | 113 |
5 files changed, 226 insertions, 23 deletions
diff --git a/main/msbuild/DownloadFile.targets b/main/msbuild/DownloadFile.targets deleted file mode 100644 index d5f2935f48..0000000000 --- a/main/msbuild/DownloadFile.targets +++ /dev/null @@ -1,18 +0,0 @@ -<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> - <UsingTask - TaskName="MDDownloadFiles" - TaskFactory="CodeTaskFactory" - AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" > - <ParameterGroup> - <Downloads ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" /> - </ParameterGroup> - <Task> - <Reference Include="System.IO.Compression" /> - <Reference Include="System.IO.Compression.FileSystem" /> - <Code Language="cs" Source="$(MSBuildThisFileDirectory)/MDBuildTasks/MDDownloadFiles.cs" /> - </Task> - </UsingTask> - <Target Name="MDDownloadFiles" Condition="'@(MDDownload)' != ''" AfterTargets="Build"> - <MDDownloadFiles Downloads="@(MDDownload)" /> - </Target> -</Project> diff --git a/main/msbuild/MDBuildTasks.targets b/main/msbuild/MDBuildTasks.targets new file mode 100644 index 0000000000..e41473f8fc --- /dev/null +++ b/main/msbuild/MDBuildTasks.targets @@ -0,0 +1,85 @@ +<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <ItemGroup> + <!-- + the one built into MSBuild 15.98+ is broken: + https://github.com/Microsoft/msbuild/issues/3726 + --> + <PackageReference Include="RoslynCodeTaskFactory" Version="2.0.7" /> + </ItemGroup> + <UsingTask + TaskName="MDDownloadFiles" + TaskFactory="CodeTaskFactory" + AssemblyFile="$(RoslynCodeTaskFactory)" + Condition=" '$(RoslynCodeTaskFactory)' != '' "> + <!-- + TaskFactory="RoslynCodeTaskFactory" + AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" > + --> + <ParameterGroup> + <Downloads ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" /> + </ParameterGroup> + <Task> + <Reference Include="System.IO.Compression" /> + <Reference Include="System.IO.Compression.FileSystem" /> + <Code Language="cs" Source="$(MSBuildThisFileDirectory)/MDBuildTasks/MDDownloadFiles.cs" /> + </Task> + </UsingTask> + + <UsingTask + TaskName="MDDownloadNupkg" + TaskFactory="CodeTaskFactory" + AssemblyFile="$(RoslynCodeTaskFactory)" + Condition=" '$(RoslynCodeTaskFactory)' != '' "> + <!-- + TaskFactory="RoslynCodeTaskFactory" + AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" > + --> + <!-- + this causes a RoslynCodeTaskFactory warning + but it's useful to uncomment for msbuild intellisense + --> + <!-- + <ParameterGroup> + <Packages ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" /> + <PackagesDirectory ParameterType="System.String" Required="true" /> + <StampFile ParameterType="System.String" Required="true" /> + <PackagesConfigFile ParameterType="System.String" Required="true" /> + </ParameterGroup> + --> + <Task> + <Code Language="cs" Source="$(MSBuildThisFileDirectory)/MDBuildTasks/MDDownloadNupkg.cs" /> + </Task> + </UsingTask> + + <Target Name="MDDownloadFiles" Condition="'@(MDDownload)' != ''" AfterTargets="Build"> + <MDDownloadFiles Downloads="@(MDDownload)" /> + </Target> + + <Target Name="MDDownloadNupkg" Condition="'@(MDDownloadNupkg)' != ''" BeforeTargets="Build" DependsOnTargets="_MDDownloadNupkgGather;_MDDownloadNupkgIfNeeded"> + <Message Text="Downloaded nupkgs: @(MDDownloadedNupkg)" /> + </Target> + + <Target Name="_MDDownloadNupkgGather"> + <PropertyGroup> + <MDDownloadNupkgDirectory Condition="'$(MDDownloadNupkgDirectory)'==''">$(SolutionDir)packages\</MDDownloadNupkgDirectory> + <MDDownloadNupkgDirectory Condition="!HasTrailingSlash('$(MDDownloadNupkgDirectory)')">$(MDDownloadNupkgDirectory)\</MDDownloadNupkgDirectory> + <_MDDownloadNupkgStamp>$(IntermediateOutputPath)MDDownloadFiles\stamp</_MDDownloadNupkgStamp> + <_MDDownloadNupkgPkgCfg>$(IntermediateOutputPath)MDDownloadFiles\packages.config</_MDDownloadNupkgPkgCfg> + </PropertyGroup> + <ItemGroup> + <MDDownloadedNupkg Include="@(MDDownloadNupkg->'$(MDDownloadNupkgDirectory)%(Identity).%(Version)\%(Identity).%(Version).nupkg')" /> + <FileWrites Include="$(_MDDownloadNupkgStamp);$(_MDDownloadNupkgPkgCfg)" /> + </ItemGroup> + </Target> + + <Target Name="_MDDownloadNupkgIfNeeded" Inputs="$(MSBuildAllProjects)" Outputs="@(MDDownloadedNupkg);$(_MDDownloadNupkgStamp)" > + <MDDownloadNupkg + ToolPath="$(MSBuildThisFileDirectory)\..\external\nuget-binary" + ToolExe="nuget.exe" + Packages="@(MDDownloadNupkg)" + PackagesDirectory="$(MDDownloadNupkgDirectory)" + StampFile="$(_MDDownloadNupkgStamp)" + PackagesConfigFile="$(_MDDownloadNupkgPkgCfg)" + /> + </Target> +</Project> diff --git a/main/msbuild/MDBuildTasks/MDBuildTasks.csproj b/main/msbuild/MDBuildTasks/MDBuildTasks.csproj index c556e99042..73f2aebe84 100644 --- a/main/msbuild/MDBuildTasks/MDBuildTasks.csproj +++ b/main/msbuild/MDBuildTasks/MDBuildTasks.csproj @@ -37,10 +37,11 @@ <ItemGroup> <Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="MDDownloadFiles.cs" /> + <Compile Include="MDDownloadNupkg.cs" /> </ItemGroup> <ItemGroup> - <None Include="..\DownloadFile.targets"> - <Link>DownloadFile.targets</Link> + <None Include="..\MDBuildTasks.targets"> + <Link>MDBuildTasks.targets</Link> </None> </ItemGroup> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> diff --git a/main/msbuild/MDBuildTasks/MDDownloadFiles.cs b/main/msbuild/MDBuildTasks/MDDownloadFiles.cs index 3dfeb6c0b2..c71c17218a 100644 --- a/main/msbuild/MDBuildTasks/MDDownloadFiles.cs +++ b/main/msbuild/MDBuildTasks/MDDownloadFiles.cs @@ -1,4 +1,26 @@ -using System; +// +// Copyright (c) Microsoft Corp (https://www.microsoft.com) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + + +using System; using System.IO; using System.IO.Compression; using System.Net; @@ -175,11 +197,11 @@ namespace MDBuildTasks // get sha1 unless sha2 is true static string GetFileSha (string filename, bool useSha2) { - using (FileStream fileStream = new FileStream (filename, FileMode.Open, FileAccess.Read, FileShare.Read)) + using (var fileStream = new FileStream (filename, FileMode.Open, FileAccess.Read, FileShare.Read)) { if (useSha2) { - using (var provider = SHA256Managed.Create ()) { + using (var provider = SHA256.Create ()) { byte [] hash = provider.ComputeHash (fileStream); var sb = new StringBuilder (hash.Length); foreach (var b in hash) { diff --git a/main/msbuild/MDBuildTasks/MDDownloadNupkg.cs b/main/msbuild/MDBuildTasks/MDDownloadNupkg.cs new file mode 100644 index 0000000000..3c318b135b --- /dev/null +++ b/main/msbuild/MDBuildTasks/MDDownloadNupkg.cs @@ -0,0 +1,113 @@ +// +// Copyright (c) Microsoft Corp (https://www.microsoft.com) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +using System.IO; +using System.Text; + +namespace MDBuildTasks +{ + public class MDDownloadNupkg : ToolTask + { + [Required] + public ITaskItem[] Packages { get; set; } + + [Required] + public string PackagesDirectory { get; set; } + + [Required] + public string StampFile { get; set; } + + [Required] + public string PackagesConfigFile { get; set; } + + protected override string ToolName => "nuget"; + + protected override string GenerateFullPathToTool () => ToolExe; + + protected override string GenerateCommandLineCommands () + { + var builder = new StringBuilder (); + void AppendSpace () + { + if (builder.Length > 0 && builder[builder.Length - 1] != ' ') { + builder.Append (' '); + } + } + void Append(string text) + { + AppendSpace (); + builder.Append (text); + } + void AppendQuoted (string text) + { + AppendSpace (); + builder.Append ('"'); + builder.Append (text.Replace ("\"", "\\\"")); + builder.Append ('"'); + } + + Append ("restore"); + + Append ("-PackagesDirectory"); + AppendQuoted (PackagesDirectory); + + //Append ("-PackageSaveMode"); + //Append ("nuspec;nupkg"); + + AppendQuoted (PackagesConfigFile); + + return builder.ToString (); + } + + public override bool Execute () + { + Directory.CreateDirectory (Path.GetDirectoryName (PackagesConfigFile)); + + using (var f = File.CreateText (PackagesConfigFile)) { + f.WriteLine ("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); + f.WriteLine ("<packages>"); + foreach (var package in Packages) { + var version = package.GetMetadata ("Version"); + if (string.IsNullOrEmpty (version)) { + Log.LogError ($"Package {package.ItemSpec} has no version"); + } + f.WriteLine ($"<package id=\"{package.ItemSpec}\" version=\"{version}\" />"); + } + f.WriteLine ("</packages>"); + } + + var success = base.Execute (); + + if (success && !string.IsNullOrEmpty (StampFile)) { + Directory.CreateDirectory (Path.GetDirectoryName (StampFile)); + File.WriteAllText (StampFile, ""); + } else { + if (File.Exists (StampFile)) { + File.Delete (StampFile); + } + } + + return success; + } + } +} |