Skip to content
This repository was archived by the owner on Jan 10, 2024. It is now read-only.

Commit

Permalink
Task377921:Support DotnetPlatform nuget packages in Nue (#18)
Browse files Browse the repository at this point in the history
* Task377921:Support DotnetPlatform nuget packages in Nue

* Updated: To set Feed field for packageInfo.

* Updated

* Rename parameter name

* Updated: Modify to regular expression to match

* Updated

* Fix Bug: if the TFM is netcoreapp3.0 or net5.0, this should cover everything like: netstandard2.0, netstandard1.0

* Updated
  • Loading branch information
v-haiboz authored Mar 3, 2021
1 parent ebe58e2 commit 8f40cb3
Show file tree
Hide file tree
Showing 8 changed files with 336 additions and 107 deletions.
33 changes: 29 additions & 4 deletions Nue/Nue.Core/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,14 @@ where Path.GetFileName(c).Equals(tfm, StringComparison.CurrentCultureIgnoreCase)
// As an example, if the TFM is net45, this should cover everything like:
// net45, net451, net452
var lenientMatch = new Regex($@"^(?<full>(?<base>{tfm})(?<version>[0-9\.0-9]*))$", RegexOptions.IgnoreCase);
folder = GetWinningFolder(folderPaths, lenientMatch);
folder = GetWinningFolder(folderPaths, lenientMatch);

if (!string.IsNullOrWhiteSpace(folder)) return folder;
// As an example, if the TFM is netcoreapp3.0 or net5.0, this should cover everything like:
// netstandard2.0, netstandard1.0
var tfmBaseOfNetCore = "netstandard";
var netCoreRegex = new Regex($@"^(?<full>(?<base>{tfmBaseOfNetCore})(?<version>[0-9\.0-9]*))$", RegexOptions.IgnoreCase);
folder = GetWinningFolder(folderPaths, netCoreRegex);

if (!string.IsNullOrWhiteSpace(folder)) return folder;
// Now we just match the base, e.g. for net we should get:
Expand All @@ -65,25 +72,37 @@ where Path.GetFileName(c).Equals(tfm, StringComparison.CurrentCultureIgnoreCase)
if (!string.IsNullOrWhiteSpace(folder)) return folder;
// Now do an even more lenient match within
var preciseTfmRegex = new Regex($@"(?<full>(?<version>{tfmBase})(?<version>[0-9\.0-9]+))", RegexOptions.IgnoreCase);
folder = GetWinningFolder(folderPaths, preciseTfmRegex);
folder = GetWinningFolder(folderPaths, preciseTfmRegex);


if (!string.IsNullOrWhiteSpace(folder)) return folder;
// Given that we have found nothing, is there anything that matches the first 3 characters?
var broadAssumptionRegex = new Regex($@"(?<full>(?<version>{tfmBase.Substring(0, 3)})(?<version>[0-9\.0-9]+))", RegexOptions.IgnoreCase);
folder = GetWinningFolder(folderPaths, broadAssumptionRegex);



return folder;
}

public static Regex WildCardToRegex(string pattern)
{
return new Regex("^" + Regex.Escape(pattern).Replace("\\?", ".").Replace("\\*", ".*") + "$", RegexOptions.Compiled);
}

public static bool CopyLibraryContent(string source, string destination, PackageAtom package, out List<string> binaries)
{
binaries = new List<string>();
var docFiles = new List<string>();
var docFiles = new List<string>();

try
{
binaries = Directory.GetFiles(source, "*.*", SearchOption.TopDirectoryOnly)
.Where(s => s.EndsWith(".dll") || s.EndsWith(".winmd")).ToList();
if (package.CustomProperties.ExcludedDlls != null && package.CustomProperties.ExcludedDlls.Count != 0)
{
binaries = binaries.Where(b => !package.CustomProperties.ExcludedDlls.Any(d => d.IsMatch(Path.GetFileName(b)))).ToList();
}
}
catch
{
Expand All @@ -97,7 +116,13 @@ public static bool CopyLibraryContent(string source, string destination, Package

try
{
docFiles = Directory.GetFiles(source, "*.xml", SearchOption.TopDirectoryOnly).ToList();
docFiles = Directory.GetFiles(source, "*.xml", SearchOption.TopDirectoryOnly).ToList();

if (package.CustomProperties.ExcludedDlls != null && package.CustomProperties.ExcludedDlls.Count != 0)
{
docFiles = docFiles.Where(b => !package.CustomProperties.ExcludedDlls.Any(d => d.IsMatch(Path.GetFileName(b)))).ToList();
}

foreach (var docFile in docFiles)
File.Copy(docFile, Path.Combine(destination, Path.GetFileName(docFile)), true);
}
Expand Down
9 changes: 6 additions & 3 deletions Nue/Nue.Core/PackageAtom.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;

using System.Text.RegularExpressions;

namespace Nue.Core
{
public class PackageAtom
Expand All @@ -24,7 +25,9 @@ public class PackageAtom
[JsonProperty("isPrerelease")]
public bool IsPrerelease { get; set; }

public bool IsPowerShellPackage { get; set; }
public bool IsPowerShellPackage { get; set; }

public bool IsDotnetPlatform { get; set; }

[JsonProperty("customProperties")]
public PackageAdditionalProperties CustomProperties { get; set; }
Expand Down Expand Up @@ -57,7 +60,7 @@ public class PackageAdditionalProperties
public string TFM { get; set; }

[JsonProperty("excludedDlls")]
public string[] ExcludedDlls { get; set; }
public List<Regex> ExcludedDlls { get; set; }
}

public enum VersionOption
Expand Down
157 changes: 82 additions & 75 deletions Nue/Nue.StandardResolver/Nue.StandardResolver.csproj
Original file line number Diff line number Diff line change
@@ -1,76 +1,83 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{C8046141-6E78-44B8-BAD8-D83A19E8E77B}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Nue.StandardResolver</RootNamespace>
<AssemblyName>Nue.StandardResolver</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Build" />
<Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Console, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<Private>True</Private>
</Reference>
<Reference Include="System.Core" />
<Reference Include="System.IdentityModel" />
<Reference Include="System.IO.Compression, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL" />
<Reference Include="System.Net.Http.WebRequest" />
<Reference Include="System.Security" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Resolver.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Nue.Core\Nue.Core.csproj">
<Project>{6d7568ae-c944-4c70-8da6-54f983689972}</Project>
<Name>Nue.Core</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<Content Include="custom.nuget.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{C8046141-6E78-44B8-BAD8-D83A19E8E77B}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Nue.StandardResolver</RootNamespace>
<AssemblyName>Nue.StandardResolver</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Build" />
<Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Console, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<Private>True</Private>
</Reference>
<Reference Include="System.Core" />
<Reference Include="System.IdentityModel" />
<Reference Include="System.IO.Compression, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL" />
<Reference Include="System.IO.Compression.FileSystem" />
<Reference Include="System.IO.Compression.ZipFile, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.Compression.ZipFile.4.3.0\lib\net46\System.IO.Compression.ZipFile.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Net.Http.WebRequest" />
<Reference Include="System.Security" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="PackageDownloder.cs" />
<Compile Include="Resolver.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Nue.Core\Nue.Core.csproj">
<Project>{6d7568ae-c944-4c70-8da6-54f983689972}</Project>
<Name>Nue.Core</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<Content Include="custom.nuget.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
108 changes: 108 additions & 0 deletions Nue/Nue.StandardResolver/PackageDownloder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using System;
using System.IO;
using System.IO.Compression;
using System.Net;

namespace Nue.StandardResolver
{
public class PackageDownloder
{
private string rootUrl = "http://nuget.org/api/v2/package/";
private string outPutPath;
private string fileName;
private string packageId;
private string version;
private bool overwrite;
public string Url
{
get
{
var url = CombineUriToString(rootUrl, packageId);
if (!string.IsNullOrEmpty(version))
{
url = $"{url}/{version}";
}

return url;
}
}

public PackageDownloder(string outPutPath, string packageId, string version, bool overwrite = false)
{
this.outPutPath = outPutPath;
this.packageId = packageId;
this.overwrite = overwrite;
this.version = version;
}

public void DownloadPackage()
{
if (string.IsNullOrEmpty(outPutPath))
{
Console.WriteLine($"[error] outPutPath cannot be empty"); return;
}

if (string.IsNullOrEmpty(packageId))
{
Console.WriteLine($"[error] packageId cannot be empty"); return;
}

if (!Directory.Exists(outPutPath))
{
Directory.CreateDirectory(outPutPath);
}

var request = WebRequest.Create(Url) as HttpWebRequest;
var response = request.GetResponse() as HttpWebResponse;
fileName = response.Headers["Content-Disposition"];
if (string.IsNullOrEmpty(fileName))
{
fileName = response.ResponseUri.Segments[response.ResponseUri.Segments.Length - 1];
}
else
{
fileName = fileName.Remove(0, fileName.IndexOf("filename=") + 9);
}

using (var responseStream = response.GetResponseStream())
{
long totalLength = response.ContentLength;
using (var stream = new FileStream(Path.Combine(outPutPath, fileName), overwrite ? FileMode.Create : FileMode.CreateNew))
{
byte[] bArr = new byte[1024];
int size;
while ((size = responseStream.Read(bArr, 0, bArr.Length)) > 0)
{
stream.Write(bArr, 0, size);
}
}
}
}

public void Unzip()
{
ModifyFileNameExtension();
var sourceFileName = Path.Combine(outPutPath, fileName);
var zipPath = Path.Combine(outPutPath, $"{Path.GetFileNameWithoutExtension(sourceFileName)}");
if (!Directory.Exists(zipPath))
{
Directory.CreateDirectory(zipPath);
}

ZipFile.ExtractToDirectory(fileName, zipPath);
}

private void ModifyFileNameExtension()
{
var sourceFileName = Path.Combine(outPutPath, fileName);
var destFileName = Path.Combine(outPutPath, $"{Path.GetFileNameWithoutExtension(sourceFileName)}.zip");
File.Move(sourceFileName, destFileName);
fileName = destFileName;
}

private string CombineUriToString(string baseUri, string relativeOrAbsoluteUri)
{
return new Uri(new Uri(baseUri), relativeOrAbsoluteUri).ToString();
}
}
}
Loading

0 comments on commit 8f40cb3

Please sign in to comment.