Skip to content

Commit

Permalink
Adding a FilterProfile option to filter out the closure of specified …
Browse files Browse the repository at this point in the history
…list

of packages
  • Loading branch information
ramarag committed Jan 24, 2017
1 parent dc0b6ea commit a07294d
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public class GenerateRuntimeConfigurationFiles : TaskBase
[Required]
public string TargetFramework { get; set; }

[Required]
public string TargetFrameworkMoniker { get; set; }

[Required]
public string RuntimeConfigPath { get; set; }

Expand Down Expand Up @@ -56,7 +59,7 @@ protected override void ExecuteCore()
{
LockFile lockFile = new LockFileCache(BuildEngine4).GetLockFile(AssetsFilePath);
ProjectContext projectContext = lockFile.CreateProjectContext(
NuGetUtils.ParseFrameworkName(TargetFramework),
NuGetUtils.ParseFrameworkName(TargetFrameworkMoniker),
RuntimeIdentifier,
PlatformLibraryName);

Expand Down Expand Up @@ -99,6 +102,7 @@ private void AddFramework(RuntimeOptions runtimeOptions, ProjectContext projectC
}

runtimeOptions.Framework = framework;
runtimeOptions.tfm = TargetFramework;
}
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/Tasks/Microsoft.NET.Build.Tasks/LockFileExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public static ProjectContext CreateProjectContext(
this LockFile lockFile,
NuGetFramework framework,
string runtime,
string platformLibraryName)
string platformLibraryName,
LockFile filterlockFile = null)
{
if (lockFile == null)
{
Expand All @@ -28,6 +29,12 @@ public static ProjectContext CreateProjectContext(
}

LockFileTarget lockFileTarget = lockFile.GetTarget(framework, runtime);
LockFileTarget filterlockFileTarget = null;

if (filterlockFile != null)
{
filterlockFileTarget = filterlockFile.GetTarget(framework, runtime);
}

if (lockFileTarget == null)
{
Expand All @@ -39,7 +46,7 @@ public static ProjectContext CreateProjectContext(
throw new BuildErrorException(Strings.AssetsFileMissingTarget, lockFile.Path, targetMoniker, framework.GetShortFolderName(), runtime);
}

return new ProjectContext(lockFile, lockFileTarget, platformLibraryName);
return new ProjectContext(lockFile, lockFileTarget, platformLibraryName, filterlockFileTarget);
}

public static LockFileTargetLibrary GetLibrary(this LockFileTarget lockFileTarget, string libraryName)
Expand Down Expand Up @@ -76,6 +83,7 @@ public static Dictionary<string, string> GetProjectFileDependencies(this LockFil

return projectDeps;
}


public static HashSet<string> GetPlatformExclusionList(
this LockFileTarget lockFileTarget,
Expand Down
42 changes: 41 additions & 1 deletion src/Tasks/Microsoft.NET.Build.Tasks/ProjectContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Microsoft.NET.Build.Tasks
public class ProjectContext
{
private readonly LockFile _lockFile;
private readonly LockFileTarget _filterlockFileTarget;
private readonly LockFileTarget _lockFileTarget;
private readonly string _platformLibraryName;

Expand All @@ -21,9 +22,10 @@ public class ProjectContext
public LockFile LockFile => _lockFile;
public LockFileTarget LockFileTarget => _lockFileTarget;

public ProjectContext(LockFile lockFile, LockFileTarget lockFileTarget, string platformLibraryName)
public ProjectContext(LockFile lockFile, LockFileTarget lockFileTarget, string platformLibraryName, LockFileTarget filterlockFileTarget = null)
{
_lockFile = lockFile;
_filterlockFileTarget = filterlockFileTarget;
_lockFileTarget = lockFileTarget;
_platformLibraryName = platformLibraryName;

Expand Down Expand Up @@ -54,6 +56,14 @@ public IEnumerable<LockFileTargetLibrary> GetRuntimeLibraries(IEnumerable<string
allExclusionList.UnionWith(privateAssetsExclusionList);
}

if(_filterlockFileTarget != null)
{
IEnumerable<LockFileTargetLibrary> filterLibraries = _filterlockFileTarget.Libraries;
Dictionary<string, LockFileTargetLibrary> filterLookup = filterLibraries.ToDictionary(e => e.Name, StringComparer.OrdinalIgnoreCase);

allExclusionList.UnionWith(GetIntersection(filterLookup, libraryLookup));
}

return runtimeLibraries.Filter(allExclusionList).ToArray();
}

Expand Down Expand Up @@ -171,5 +181,35 @@ public HashSet<string> GetPrivateAssetsExclusionList(

return privateAssetsToExclude;
}
private static HashSet<string> GetIntersection(
IDictionary<string, LockFileTargetLibrary> collection1,
IDictionary<string, LockFileTargetLibrary> collection2)
{
var exclusionList = new HashSet<string>();
var iterated = collection1;
var lookup = collection2;

if (collection1.Count > collection2.Count)
{
iterated = collection2;
lookup = collection1;
}
foreach (var entry in iterated)
{
LockFileTargetLibrary library = lookup[entry.Key];

if (library != null)
{
LockFileTargetLibrary dependency = entry.Value;

if (library.Version.Equals(dependency.Version))
{
exclusionList.Add(entry.Key);
}
}
}

return exclusionList;
}
}
}
20 changes: 16 additions & 4 deletions src/Tasks/Microsoft.NET.Build.Tasks/ResolvePublishAssemblies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ public class ResolvePublishAssemblies : TaskBase
public string PlatformLibraryName { get; set; }

public ITaskItem[] PrivateAssetsPackageReferences { get; set; }

public bool PreserveCacheLayout { get; set; }

public string FilterProjectAssetsFile { get; set; }

/// <summary>
/// All the assemblies to publish.
/// </summary>
Expand All @@ -42,16 +45,25 @@ public ITaskItem[] AssembliesToPublish

protected override void ExecuteCore()
{
LockFile lockFile = new LockFileCache(BuildEngine4).GetLockFile(AssetsFilePath);
var lockFileCache = new LockFileCache(BuildEngine4);
LockFile lockFile = lockFileCache.GetLockFile(AssetsFilePath);
IEnumerable<string> privateAssetsPackageIds = PackageReferenceConverter.GetPackageIds(PrivateAssetsPackageReferences);
IPackageResolver packageResolver = NuGetPackageResolver.CreateResolver(lockFile, ProjectPath);

LockFile filterLockFile = null;
if (!string.IsNullOrEmpty(FilterProjectAssetsFile))
{
filterLockFile = lockFileCache.GetLockFile(FilterProjectAssetsFile);

}
ProjectContext projectContext = lockFile.CreateProjectContext(
NuGetUtils.ParseFrameworkName(TargetFramework),
RuntimeIdentifier,
PlatformLibraryName);
PlatformLibraryName,
filterLockFile
);

IEnumerable<ResolvedFile> resolvedAssemblies =
IEnumerable<ResolvedFile> resolvedAssemblies =
new PublishAssembliesResolver(packageResolver)
.WithPrivateAssets(privateAssetsPackageIds)
.WithPreserveCacheLayout(PreserveCacheLayout)
Expand Down
2 changes: 2 additions & 0 deletions src/Tasks/Microsoft.NET.Build.Tasks/RuntimeOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace Microsoft.NET.Build.Tasks
{
internal class RuntimeOptions
{
public string tfm { get; set; }

public RuntimeConfigFramework Framework { get; set; }

public List<string> AdditionalProbingPaths { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,12 @@ Copyright (c) .NET Foundation. All rights reserved.
<UsingTask TaskName="ResolvePublishAssemblies" AssemblyFile="$(MicrosoftNETBuildTasksAssembly)" />
<Target Name="RunResolvePublishAssemblies"
DependsOnTargets="ComputePrivateAssetsPackageReferences;
ComputePackagesToBeFiltered;
_DefaultMicrosoftNETPlatformLibrary">

<ResolvePublishAssemblies ProjectPath="$(MSBuildProjectFullPath)"
AssetsFilePath="$(ProjectAssetsFile)"
FilterProjectAssetsFile="$(FilterProjectAssetsFile)"
TargetFramework="$(TargetFrameworkMoniker)"
RuntimeIdentifier="$(RuntimeIdentifier)"
PlatformLibraryName="$(MicrosoftNETPlatformLibrary)"
Expand All @@ -242,6 +244,33 @@ Copyright (c) .NET Foundation. All rights reserved.
</ResolvePublishAssemblies>

</Target>

<!--
============================================================
ComputePackagesToBeFiltered
Gets the closure of all the specified packages in $(FilterProjFile)
These will be removed from the assets that are published
============================================================
-->

<Target Name="ComputePackagesToBeFiltered"
Condition="'$(FilterProjFile)'!=''" >
<PropertyGroup>
<FilterProjFileDir>$(MSBuildProjectExtensionsPath)\filterprofile</FilterProjFileDir>
<FilterProjectAssetsFile>$(FilterProjFileDir)\project.assets.json</FilterProjectAssetsFile>
</PropertyGroup>
<MSBuild Projects="$(FilterProjFile)"
Targets="Restore"
Properties="RestoreGraphProjectInput=$(FilterProjFile);
DisableImplicitFrameworkReferences=true;
RestoreOutputPath=$(FilterProjFileDir);
TargetFramework=$(TargetFramework);
RuntimeIdentifier=$(RuntimeIdentifier)"/>



</Target>

<!--
============================================================
Expand Down Expand Up @@ -498,7 +527,8 @@ Copyright (c) .NET Foundation. All rights reserved.
</PropertyGroup>

<GenerateRuntimeConfigurationFiles AssetsFilePath="$(ProjectAssetsFile)"
TargetFramework="$(TargetFrameworkMoniker)"
TargetFrameworkMoniker="$(TargetFrameworkMoniker)"
TargetFramework="$(TargetFramework)"
RuntimeConfigPath="$(PublishRuntimeConfigFilePath)"
RuntimeIdentifier="$(RuntimeIdentifier)"
PlatformLibraryName="$(MicrosoftNETPlatformLibrary)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ Copyright (c) .NET Foundation. All rights reserved.
Outputs="$(ProjectRuntimeConfigFilePath);$(ProjectRuntimeConfigDevFilePath)">

<GenerateRuntimeConfigurationFiles AssetsFilePath="$(ProjectAssetsFile)"
TargetFramework="$(TargetFrameworkMoniker)"
TargetFrameworkMoniker="$(TargetFrameworkMoniker)"
TargetFramework="$(TargetFramework)"
RuntimeConfigPath="$(ProjectRuntimeConfigFilePath)"
RuntimeConfigDevPath="$(ProjectRuntimeConfigDevFilePath)"
RuntimeIdentifier="$(RuntimeIdentifier)"
Expand Down

0 comments on commit a07294d

Please sign in to comment.