Skip to content

Commit

Permalink
Simplify project collection usage
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-englert committed Sep 25, 2023
1 parent 9f59678 commit 77944f9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 37 deletions.
3 changes: 3 additions & 0 deletions src/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,6 @@ dotnet_analyzer_diagnostic.severity = error

# CA1305: Specify IFormatProvider
dotnet_diagnostic.CA1305.severity = suggestion

# VSSpell001: Spell Check
dotnet_diagnostic.VSSpell001.severity = suggestion
49 changes: 12 additions & 37 deletions src/LicenseGenerator/Builder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal sealed class Builder : IDisposable
private readonly bool _offline;
private readonly string _solutionDirectory;
private readonly Regex? _excludeRegex;
private readonly Dictionary<NuGetFramework, TargetFrameworkCollection> _targetFrameworkCollections = new();
private readonly ProjectCollection _projectCollection = new();
private readonly string _solutionFile;

private ProjectInfo[] _projects = Array.Empty<ProjectInfo>();
Expand Down Expand Up @@ -58,7 +58,7 @@ public async Task<int> Build()

var solution = SolutionFile.Parse(_solutionFile);

_projects = LoadProjects(solution, _targetFrameworkCollections)
_projects = LoadProjects(solution)
.ExceptNullItems()
.ToArray();

Expand Down Expand Up @@ -336,7 +336,7 @@ private async Task<string> CreateLicenseFile(IEnumerable<PackageArchiveReader> p
var lines = stream.ReadLines().ToArray();
if (lines.FirstOrDefault()?.Contains("MIT License") == true)
{
content.AppendLine("License: MIT");
content.AppendLine(MitLicenseExpression);
}
else if (lines.Any(ExtensionMethods.IsApache2License))
{
Expand Down Expand Up @@ -436,7 +436,7 @@ private bool ShouldInclude(ProjectInfo projectInfo)
return bool.TryParse(property?.EvaluatedValue, out var include) && include;
}

private static IEnumerable<ProjectInfo?> LoadProjects(SolutionFile solution, Dictionary<NuGetFramework, TargetFrameworkCollection> targetFrameworkCollections)
private IEnumerable<ProjectInfo?> LoadProjects(SolutionFile solution)
{
foreach (var projectReference in solution.ProjectsInOrder)
{
Expand All @@ -451,7 +451,7 @@ private bool ShouldInclude(ProjectInfo projectInfo)

var project = new Project(projectReference.AbsolutePath);

projectInfo = new ProjectInfo(projectReference, project, GetTargetFrameworks(project), targetFrameworkCollections);
projectInfo = new ProjectInfo(projectReference, project, GetTargetFrameworks(project), _projectCollection);
}
catch (Exception ex)
{
Expand All @@ -477,7 +477,7 @@ private static NuGetFramework[] GetTargetFrameworks(Project project)
return frameworks ?? new[] { NuGetFramework.AnyFramework };
}

private sealed record ProjectInfo(ProjectInSolution ProjectReference, Project Project, NuGetFramework[] TargetFrameworks, Dictionary<NuGetFramework, TargetFrameworkCollection> TargetFrameworkCollections)
private sealed record ProjectInfo(ProjectInSolution ProjectReference, Project Project, NuGetFramework[] TargetFrameworks, ProjectCollection ProjectCollection)
{
public IEnumerable<FrameworkSpecificProject> GetFrameworkSpecificProjects()
{
Expand All @@ -493,9 +493,12 @@ private FrameworkSpecificProject GetProjectInFramework(NuGetFramework targetFram
if (bestMatching == null)
return new FrameworkSpecificProject(Project, TargetFrameworks.FirstOrDefault() ?? NuGetFramework.AnyFramework);

var collection = TargetFrameworkCollections.ForceValue(bestMatching, framework => new TargetFrameworkCollection(framework));
var properties = new Dictionary<string, string>
{
{ "TargetFramework", targetFramework.GetShortFolderName() }
};

return new FrameworkSpecificProject(collection.LoadProject(ProjectReference.AbsolutePath), bestMatching);
return new FrameworkSpecificProject(ProjectCollection.LoadProject(ProjectReference.AbsolutePath, properties, null), bestMatching);
}
}

Expand All @@ -506,31 +509,6 @@ private sealed record FrameworkSpecificProject(Project Project, NuGetFramework T
public LockFile LockFile => _lockFile ??= LockFileUtilities.GetLockFile(Project.GetPropertyValue("ProjectAssetsFile"), NullLogger.Instance);
}

private sealed class TargetFrameworkCollection : IDisposable
{
private readonly ProjectCollection _projectCollection;

public TargetFrameworkCollection(NuGetFramework framework)
{
var properties = new Dictionary<string, string>
{
{ "TargetFramework", framework.GetShortFolderName() }
};

_projectCollection = new ProjectCollection(properties);
}

public Project LoadProject(string filePath)
{
return _projectCollection.LoadProject(filePath);
}

public void Dispose()
{
_projectCollection.Dispose();
}
}

private static async Task<ICollection<string>> DownloadLicense(string url)
{
try
Expand All @@ -546,9 +524,6 @@ private static async Task<ICollection<string>> DownloadLicense(string url)

public void Dispose()
{
foreach (var collection in _targetFrameworkCollections.Values)
{
collection.Dispose();
}
_projectCollection.Dispose();
}
}
1 change: 1 addition & 0 deletions src/LicenseGenerator/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
internal static class Constants
{
public const string IsDeploymentTarget = "IsDeploymentTarget";
public const string MitLicenseExpression = "License: MIT";
public const string ApacheLicenseExpression = "License: Apache-2.0";
public const string ApacheLicenseUrl = "http://www.apache.org/licenses/LICENSE-2.0";
public const string MicrosoftNetLibraryLicenseExpression = "License: MICROSOFT .NET LIBRARY (http://go.microsoft.com/fwlink/?LinkId=329770)";
Expand Down

0 comments on commit 77944f9

Please sign in to comment.