Skip to content

Commit

Permalink
Allow passing in build and platform configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
sensslen committed Aug 14, 2024
1 parent 10f0f97 commit 1ec82b3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 23 deletions.
8 changes: 2 additions & 6 deletions src/NuGetUtility/NuGetUtility.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,14 @@
<PackageReference Include="Microsoft.Build" ExcludeAssets="runtime" Version="17.3.*" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
<PackageReference Include="Microsoft.Build" ExcludeAssets="runtime" Version="17.6.*" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.Build" ExcludeAssets="runtime" Version="17.9.*" />
<PackageReference Include="Microsoft.Build" ExcludeAssets="runtime" Version="17.10.*" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net472'">
<PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.1" />
<PackageReference Include="System.Collections.Immutable" Version="8.0.0" />
<PackageReference Include="Microsoft.Build" ExcludeAssets="runtime" Version="17.9.*" />
<PackageReference Include="Microsoft.Build" ExcludeAssets="runtime" Version="17.10.*" />
<PackageReference Include="PolySharp" Version="1.14.*">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
18 changes: 14 additions & 4 deletions src/NuGetUtility/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class Program
{
[Option(ShortName = "i",
LongName = "input",
Description = "The project (or solution) file who's dependencies should be analyzed")]
Description = "The project (or solution) file for which to analyze dependencies")]
public string? InputFile { get; } = null;

[Option(ShortName = "ji",
Expand Down Expand Up @@ -90,9 +90,19 @@ public class Program

[Option(LongName = "exclude-projects-matching",
ShortName = "exclude-projects",
Description = "This option allows to specify project name(s) to exclude from the analysis. This can be useful to exclude test projects from the analysis when supplying a solution file as input. Wildcard characters (*) are supported to specify ranges of ignored projects. The input can either be a file name containing a list of project names in json format or a plain string that is then used as a single entry.")]
Description = "This option allows to specify project name(s) to exclude from the analysis. This can be useful to exclude test projects from the analysis when supplying a solution file as input. Wildcard characters (*) are supported to specify ranges of ignored projects. The input can either be a file name containing a list of project names in json format or a plain string that is then used as a single entry. Note: ignored projects will not be evaluated in any form and it's transitive dependencies will NOT be evaluated.")]
public string? ExcludedProjects { get; } = null;

[Option(LongName = "build-configuration",
ShortName = "c",
Description = "This option allows to specify the build configuration to use. Generally it is not needed to specify this parameter. However when dealing with ci builds that only build a specific configuration, this parameter may be needed.")]
public string? BuildConfiguration { get; } = null;

[Option(LongName = "platform",
ShortName = "p",
Description = "This option allows to specify the platform used to restore. Generally it is not needed to specify this parameter. However when dealing with ci builds that only build a specific configuration, this parameter may be needed.")]
public string? Platform { get; } = null;

private static string GetVersion()
=> typeof(Program).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? string.Empty;

Expand All @@ -107,7 +117,7 @@ private async Task<int> OnExecuteAsync(CancellationToken cancellationToken)
IFileDownloader urlLicenseFileDownloader = GetFileDownloader(httpClient);
IOutputFormatter output = GetOutputFormatter();

MsBuildAbstraction msBuild = new MsBuildAbstraction();
MsBuildAbstraction msBuild = new MsBuildAbstraction(BuildConfiguration);
var projectCollector = new ProjectsCollector(msBuild);
var projectReader = new ReferencedPackageReader(msBuild, new LockFileFactory(), GetPackagesConfigReader());
var validator = new LicenseValidator.LicenseValidator(licenseMappings,
Expand Down Expand Up @@ -152,7 +162,7 @@ private static IPackagesConfigReader GetPackagesConfigReader()
#if NETFRAMEWORK
return new WindowsPackagesConfigReader();
#else
return OperatingSystem.IsWindows() ? new WindowsPackagesConfigReader() : new FailingPackagesConfigReader();
return OperatingSystem.IsWindows() ? new WindowsPackagesConfigReader() : new FailingPackagesConfigReader();
#endif
}

Expand Down
29 changes: 16 additions & 13 deletions src/NuGetUtility/Wrapper/MsBuildWrapper/MsBuildAbstraction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using Microsoft.Build.Construction;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Exceptions;
using Microsoft.Build.Execution;
using Microsoft.Build.Framework;
using Microsoft.Build.Locator;
Expand All @@ -14,11 +13,17 @@ namespace NuGetUtility.Wrapper.MsBuildWrapper
public class MsBuildAbstraction : IMsBuildAbstraction
{
private const string CollectPackageReferences = "CollectPackageReferences";
private readonly Dictionary<string, string> _globalProjectProperties = new();
private readonly string? _buildConfiguration;
private readonly string? _platform;
private ProjectCollection? _projectCollection;

public MsBuildAbstraction()
public ProjectCollection ProjectCollection => _projectCollection ??= InitializeProjectCollection(_buildConfiguration, _platform);

public MsBuildAbstraction(string? buildConfiguration = null, string? platform = null)
{
RegisterMsBuildLocatorIfNeeded();
_buildConfiguration = buildConfiguration;
_platform = platform;
}

public IEnumerable<PackageReference> GetPackageReferencesFromProjectForFramework(IProject project,
Expand Down Expand Up @@ -48,9 +53,7 @@ public IProject GetProject(string projectPath)
}
#endif

ProjectRootElement rootElement = TryGetProjectRootElement(projectPath);

var project = new Project(rootElement, _globalProjectProperties, null);
Project project = ProjectCollection.LoadProject(projectPath);

return new ProjectWrapper(project);
}
Expand All @@ -70,18 +73,18 @@ private static void RegisterMsBuildLocatorIfNeeded()
}
}

private static ProjectRootElement TryGetProjectRootElement(string projectPath)
private static ProjectCollection InitializeProjectCollection(string? buildConfiguration, string? platform)
{
try
ProjectCollection projectCollection = ProjectCollection.GlobalProjectCollection;
if (buildConfiguration != null)
{
return ProjectRootElement.Open(projectPath, ProjectCollection.GlobalProjectCollection)!;
projectCollection.SetGlobalProperty("Configuration", buildConfiguration);
}
catch (InvalidProjectFileException e)
if (platform != null)
{
throw new MsBuildAbstractionException($"Failed to open project: {projectPath}", e);
projectCollection.SetGlobalProperty("Platform", platform);
}
return projectCollection;
}

protected void AddGlobalProjectProperty(string name, string value) => _globalProjectProperties.Add(name, value);
}
}

0 comments on commit 1ec82b3

Please sign in to comment.