Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Sep 28, 2020
1 parent 920fa66 commit 5c7a7ef
Show file tree
Hide file tree
Showing 32 changed files with 1,182 additions and 350 deletions.
59 changes: 51 additions & 8 deletions src/NuGet.Core/NuGet.Build.Tasks.Pack/PackTaskLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ public PackArgs GetPackArgs(IPackTaskRequest<IMSBuildItem> request)
}


LockFile assetsFile = GetAssetsFile(request);
var aliases = new Dictionary<string, string>();
foreach (var tfm in assetsFile.PackageSpec.TargetFrameworks)
{
aliases[tfm.TargetAlias] = tfm.FrameworkName.GetShortFolderName();
}
packArgs.AliasFolderNameMapping = aliases;


InitCurrentDirectoryAndFileName(request, packArgs);
InitNuspecOutputPath(request, packArgs);
PackCommandRunner.SetupCurrentDirectory(packArgs);
Expand All @@ -76,8 +85,8 @@ public PackArgs GetPackArgs(IPackTaskRequest<IMSBuildItem> request)
// This only needs to happen when packing via csproj, not nuspec.
packArgs.PackTargetArgs.AllowedOutputExtensionsInPackageBuildOutputFolder = InitOutputExtensions(request.AllowedOutputExtensionsInPackageBuildOutputFolder);
packArgs.PackTargetArgs.AllowedOutputExtensionsInSymbolsPackageBuildOutputFolder = InitOutputExtensions(request.AllowedOutputExtensionsInSymbolsPackageBuildOutputFolder);
packArgs.PackTargetArgs.TargetPathsToAssemblies = InitLibFiles(request.BuildOutputInPackage);
packArgs.PackTargetArgs.TargetPathsToSymbols = InitLibFiles(request.TargetPathsToSymbols);
packArgs.PackTargetArgs.TargetPathsToAssemblies = InitLibFiles(request.BuildOutputInPackage, packArgs.Logger);
packArgs.PackTargetArgs.TargetPathsToSymbols = InitLibFiles(request.TargetPathsToSymbols, packArgs.Logger);
packArgs.PackTargetArgs.AssemblyName = request.AssemblyName;
packArgs.PackTargetArgs.IncludeBuildOutput = request.IncludeBuildOutput;
packArgs.PackTargetArgs.BuildOutputFolder = request.BuildOutputFolders;
Expand Down Expand Up @@ -396,21 +405,20 @@ public bool BuildPackage(PackCommandRunner runner)
return runner.RunPackageBuild();
}

private IEnumerable<OutputLibFile> InitLibFiles(IMSBuildItem[] libFiles)
private IEnumerable<OutputLibFile> InitLibFiles(IMSBuildItem[] libFiles, ILogger logger)
{
var assemblies = new List<OutputLibFile>();
if (libFiles == null)
{
return assemblies;
}


foreach (var assembly in libFiles)
{
var finalOutputPath = assembly.GetProperty("FinalOutputPath");

// Fallback to using Identity if FinalOutputPath is not set.
// See bug https://github.com/NuGet/Home/issues/5408
// See bug https://github.com/NuGet/Home/issues/5408
if (string.IsNullOrEmpty(finalOutputPath))
{
finalOutputPath = assembly.GetProperty(IdentityProperty);
Expand All @@ -437,6 +445,19 @@ private IEnumerable<OutputLibFile> InitLibFiles(IMSBuildItem[] libFiles)
throw new PackagingException(NuGetLogCode.NU5027, string.Format(CultureInfo.CurrentCulture, Strings.InvalidTargetFramework, finalOutputPath));
}

NuGetFramework framework = NuGetFramework.Parse(targetFramework);
var isNet5EraTfm = framework.Version.Major >= 5 && StringComparer.OrdinalIgnoreCase.Equals(FrameworkConstants.FrameworkIdentifiers.NetCoreApp, framework.Framework);
if (isNet5EraTfm)
{
var dotIndex = targetFramework.IndexOf('.');
var dashIndex = targetFramework.IndexOf('-');
var frameworkVersionHasDot = (dashIndex > -1 && dotIndex > -1 && dotIndex < dashIndex) || (dashIndex == -1 && dotIndex > -1);
if (!frameworkVersionHasDot)
{
logger.LogWarning(string.Format(CultureInfo.CurrentCulture, Strings.MissingRequiredDot, NuGetLogCode.NU5502, targetFramework));
}
}

assemblies.Add(new OutputLibFile()
{
FinalOutputPath = finalOutputPath,
Expand Down Expand Up @@ -480,6 +501,28 @@ private ICollection<PackageType> ParsePackageTypes(IPackTaskRequest<IMSBuildItem
return listOfPackageTypes;
}

private LockFile GetAssetsFile(IPackTaskRequest<IMSBuildItem> request)
{
if (request.PackItem == null)
{
throw new PackagingException(NuGetLogCode.NU5028, Strings.NoPackItemProvided);
}

string assetsFilePath = Path.Combine(request.RestoreOutputPath, LockFileFormat.AssetsFileName);

if (!File.Exists(assetsFilePath))
{
throw new InvalidOperationException(string.Format(
CultureInfo.CurrentCulture,
Strings.AssetsFileNotFound,
assetsFilePath));
}
// The assets file is necessary for project and package references. Pack should not do any traversal,
// so we leave that work up to restore (which produces the assets file).
var lockFileFormat = new LockFileFormat();
return lockFileFormat.Read(assetsFilePath);
}

private void InitCurrentDirectoryAndFileName(IPackTaskRequest<IMSBuildItem> request, PackArgs packArgs)
{
if (request.PackItem == null)
Expand Down Expand Up @@ -655,7 +698,7 @@ private IEnumerable<ContentMetadata> GetContentMetadata(IMSBuildItem packageFile
var newTargetPath = Path.Combine(targetPath, identity);
// We need to do this because evaluated identity in the above line of code can be an empty string
// in the case when the original identity string was the absolute path to a file in project directory, and is in
// the same directory as the csproj file.
// the same directory as the csproj file.
newTargetPath = PathUtility.EnsureTrailingSlash(newTargetPath);
newTargetPaths.Add(newTargetPath);
}
Expand Down Expand Up @@ -813,7 +856,7 @@ private static void InitializeProjectDependencies(

var versionToUse = new VersionRange(targetLibrary.Version);

// Use the project reference version obtained at build time if it exists, otherwise fallback to the one in assets file.
// Use the project reference version obtained at build time if it exists, otherwise fallback to the one in assets file.
if (projectRefToVersionMap.TryGetValue(projectReference.ProjectPath, out var projectRefVersion))
{
versionToUse = VersionRange.Parse(projectRefVersion, allowFloating: false);
Expand Down Expand Up @@ -872,7 +915,7 @@ private static void InitializePackageDependencies(
// Add each package dependency.
foreach (var packageDependency in packageDependencies)
{
// If we have a floating package dependency like 1.2.3-xyz-*, we
// If we have a floating package dependency like 1.2.3-xyz-*, we
// use the version of the package that restore resolved it to.
if (packageDependency.LibraryRange.VersionRange.IsFloating)
{
Expand Down
23 changes: 20 additions & 3 deletions src/NuGet.Core/NuGet.Build.Tasks.Pack/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion src/NuGet.Core/NuGet.Build.Tasks.Pack/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,25 @@
<value>PackageVersion string specified '{0}' is invalid.</value>
<comment>{0} is the version.</comment>
</data>
<data name="InvalidPlatformVersion" xml:space="preserve">
<value>Platform version is missing from '{0}'</value>
<comment>{0} is the target framework string.</comment>
</data>
<data name="InvalidTargetFramework" xml:space="preserve">
<value>Invalid target framework for the file '{0}'.</value>
</data>
<data name="IsPackableFalseError" xml:space="preserve">
<value>This project cannot be packaged because packaging has been disabled. Add &lt;IsPackable&gt;true&lt;/IsPackable&gt; to the project file to enable producing a package from this project.</value>
</data>
<data name="MissingRequiredDot" xml:space="preserve">
<value>{0}: Dots in TargetFramework versions are required. The missing dot in '{1}' might cause future problems.</value>
<comment>{0} is the NuGetLogCode, {1} is the target framework string.</comment>
</data>
<data name="NoPackItemProvided" xml:space="preserve">
<value>No project was provided to the PackTask.</value>
</data>
<data name="NuGetLicenses_LicenseUrlCannotBeUsedInConjuctionWithLicense" xml:space="preserve">
<value>The PackageLicenseUrl is being deprecated and cannot be used in conjunction with the PackageLicenseFile or PackageLicenseExpression.</value>
<comment>Please don't localize PackageLicenseUrl, PackageLicenseFile and PackageLicenseExpression.</comment>
</data>
</root>
</root>
1 change: 1 addition & 0 deletions src/NuGet.Core/NuGet.Commands/CommandArgs/PackArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class PackArgs
public bool Tool { get; set; }
public string Version { get; set; }
public bool Deterministic { get; set; }
public IDictionary<string, string> AliasFolderNameMapping { get; set; }
public WarningProperties WarningProperties { get; set; }
public MSBuildPackTargetArgs PackTargetArgs { get; set; }
public Dictionary<string, string> Properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,13 @@ private void InitCommonPackageBuilderProperties(PackageBuilder builder)
builder.MinClientVersion = _packArgs.MinClientVersion;
}

if (_packArgs.AliasFolderNameMapping != null)
{
#pragma warning disable CS0618 // This API is not meant to be used outside of the NuGet SDK.
builder.AliasFolderNameMapping = _packArgs.AliasFolderNameMapping;
#pragma warning restore CS0618
}

CheckForUnsupportedFrameworks(builder);

ExcludeFiles(builder.Files);
Expand Down Expand Up @@ -992,7 +999,7 @@ private static string ResolvePath(IPackageFile packageFile, string basePath)
private bool BuildSymbolsPackage(string path)
{
PackageBuilder symbolsBuilder = CreatePackageBuilderFromNuspec(path);
if (_packArgs.SymbolPackageFormat == SymbolPackageFormat.Snupkg) // Snupkgs can only have 1 PackageType.
if (_packArgs.SymbolPackageFormat == SymbolPackageFormat.Snupkg) // Snupkgs can only have 1 PackageType.
{
symbolsBuilder.PackageTypes.Clear();
symbolsBuilder.PackageTypes.Add(PackageType.SymbolsPackage);
Expand Down
1 change: 1 addition & 0 deletions src/NuGet.Core/NuGet.Commands/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,4 @@
[assembly: SuppressMessage("Build", "CA1307:The behavior of 'string.GetHashCode()' could vary based on the current user's locale settings. Replace this call in 'Microsoft.Extensions.Internal.HashCodeCombiner.Add(string)' with a call to 'string.GetHashCode(System.StringComparison)'.", Justification = "<Pending>", Scope = "member", Target = "~M:Microsoft.Extensions.Internal.HashCodeCombiner.Add(System.String)")]
[assembly: SuppressMessage("ApiDesign", "RS0026:Do not add multiple public overloads with optional parameters", Justification = "API already shipped", Scope = "member", Target = "~M:NuGet.Commands.RestoreSummary.Log(NuGet.Common.ILogger,System.Collections.Generic.IEnumerable{NuGet.Commands.RestoreSummary},System.Boolean)")]
[assembly: SuppressMessage("ApiDesign", "RS0026:Do not add multiple public overloads with optional parameters", Justification = "API already shipped", Scope = "member", Target = "~M:NuGet.Commands.RestoreSummary.Log(NuGet.Common.ILogger,System.Collections.Generic.IReadOnlyList{NuGet.Commands.RestoreSummary},System.Boolean)")]
[assembly: SuppressMessage("Usage", "CA2227:Collection properties should be read only", Justification = "In order to preserve the external API, we can't pass this value in through a constructor, and instead we add it post-construction.", Scope = "member", Target = "~P:NuGet.Commands.PackArgs.AliasFolderNameMapping")]
4 changes: 4 additions & 0 deletions src/NuGet.Core/NuGet.Commands/MSBuildProjectFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public PackageBuilder CreateBuilder(string basePath, NuGetVersion version, strin
Files.Clear();
builder.Files.Clear();

#pragma warning disable CS0618 // This API is not meant to be used outside the NuGet SDK.
builder.AliasFolderNameMapping = PackArgs.AliasFolderNameMapping;
#pragma warning restore CS0618

AddOutputFiles(builder);

// Add content files if there are any. They could come from a project or nuspec file
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
NuGet.Commands.VerifyArgs.PackagePaths.get -> System.Collections.Generic.IReadOnlyList<string>
NuGet.Commands.VerifyArgs.PackagePaths.set -> void
static NuGet.Commands.MSBuildProjectFrameworkUtility.GetProjectFramework(string projectFilePath, string targetFrameworkMoniker, string targetPlatformMoniker, string targetPlatformMinVersion) -> NuGet.Frameworks.NuGetFramework
NuGet.Commands.PackArgs.AliasFolderNameMapping.get -> System.Collections.Generic.IDictionary<string, string>
NuGet.Commands.PackArgs.AliasFolderNameMapping.set -> void
static NuGet.Commands.MSBuildProjectFrameworkUtility.GetProjectFramework(string projectFilePath, string targetFrameworkMoniker, string targetPlatformMoniker, string targetPlatformMinVersion) -> NuGet.Frameworks.NuGetFramework
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
NuGet.Commands.VerifyArgs.PackagePaths.get -> System.Collections.Generic.IReadOnlyList<string>
NuGet.Commands.VerifyArgs.PackagePaths.set -> void
NuGet.Commands.PackArgs.AliasFolderNameMapping.get -> System.Collections.Generic.IDictionary<string, string>
NuGet.Commands.PackArgs.AliasFolderNameMapping.set -> void
static NuGet.Commands.MSBuildProjectFrameworkUtility.GetProjectFramework(string projectFilePath, string targetFrameworkMoniker, string targetPlatformMoniker, string targetPlatformMinVersion) -> NuGet.Frameworks.NuGetFramework
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
NuGet.Commands.VerifyArgs.PackagePaths.get -> System.Collections.Generic.IReadOnlyList<string>
NuGet.Commands.VerifyArgs.PackagePaths.set -> void
NuGet.Commands.PackArgs.AliasFolderNameMapping.get -> System.Collections.Generic.IDictionary<string, string>
NuGet.Commands.PackArgs.AliasFolderNameMapping.set -> void
static NuGet.Commands.MSBuildProjectFrameworkUtility.GetProjectFramework(string projectFilePath, string targetFrameworkMoniker, string targetPlatformMoniker, string targetPlatformMinVersion) -> NuGet.Frameworks.NuGetFramework
Loading

0 comments on commit 5c7a7ef

Please sign in to comment.