Skip to content

Commit

Permalink
Major change: Added support for all projects. (#52)
Browse files Browse the repository at this point in the history
* Major change: Added support for all projects.
Looping the solution will return more projects than before where it only returned specific projects.

* Change to new Load overload method to keep backwardscompatibility

* Remove unused using

---------

Co-authored-by: Michiel Oda <michiel.oda@skyline.be>
  • Loading branch information
janstaelensskyline and MichielOda authored Dec 12, 2024
1 parent 6729e0e commit da792b2
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
16 changes: 16 additions & 0 deletions Parsers.Common/VisualStudio/Projects/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ private Project()
{
}

/// <summary>
/// Gets the type of projects that are supported to be loaded.
/// </summary>
internal static readonly string[] SupportedProjectExtensions =
{
".csproj",
".projitems",
".shproj"
};

/// <summary>
/// Gets the project name.
/// </summary>
Expand Down Expand Up @@ -144,6 +154,12 @@ public static Project Load(string path, string projectName)
// Make sure to use the full path
path = FileSystem.Path.GetFullPath(path);

string extension = FileSystem.Path.GetExtension(path);
if (!SupportedProjectExtensions.Contains(extension))
{
throw new NotImplementedException("Project Load does not support this project type.");
}

try
{
string projectDir = FileSystem.Path.GetDirectoryName(path);
Expand Down
54 changes: 49 additions & 5 deletions Parsers.Common/VisualStudio/Solution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,18 @@ public class Solution
/// </summary>
/// <param name="path">The path to the solution file.</param>
/// <exception cref="FileNotFoundException">The specified solution file does not exist.</exception>
protected Solution(string path)
protected Solution(string path) : this(path, false)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="Solution"/> class with the specified path.
/// Not all projects will be able to be loaded with the <see cref="LoadProject"/> method.
/// </summary>
/// <param name="path">The path to the solution file.</param>
/// <param name="allowAll">Allow all projects, regardless of type.</param>
/// <exception cref="FileNotFoundException">The specified solution file does not exist.</exception>
private Solution(string path, bool allowAll)
{
if (!_fileSystem.File.Exists(path))
{
Expand All @@ -37,7 +48,7 @@ protected Solution(string path)
SolutionPath = path;
SolutionDirectory = _fileSystem.Path.GetDirectoryName(path);

Load();
Load(allowAll);
}

/// <summary>
Expand Down Expand Up @@ -90,19 +101,46 @@ public static Solution Load(string solutionPath, ILogCollector logCollector = nu
return new Solution(solutionPath);
}

/// <summary>
/// Loads the specified solution path.
/// </summary>
/// <param name="solutionPath">The solution path.</param>
/// <param name="allowAll">Allow all projects, regardless of type.</param>
/// <param name="logCollector">The log collector.</param>
/// <returns>Parsed solution.</returns>
/// <exception cref="ArgumentException">Value cannot be null or whitespace. - solutionPath</exception>
/// <exception cref="FileNotFoundException">The specified solution file does not exist.</exception>
public static Solution Load(string solutionPath, bool allowAll, ILogCollector logCollector = null)
{
if (String.IsNullOrWhiteSpace(solutionPath))
{
throw new ArgumentException("Value cannot be null or whitespace.", nameof(solutionPath));
}

logCollector?.ReportDebug($"Creating solution from '{solutionPath}'.");

return new Solution(solutionPath, allowAll);
}

/// <summary>
/// Loads the project of the solution.
/// </summary>
/// <param name="projectInSolution">The project of the solution.</param>
/// <returns>The loaded project.</returns>
/// <exception cref="ArgumentNullException"><paramref name="projectInSolution"/> is <see langword="null"/>.</exception>
/// <exception cref="NotSupportedException">Specified project is not supported to be loaded.</exception>
public Project LoadProject(ProjectInSolution projectInSolution)
{
if (projectInSolution is null)
{
throw new ArgumentNullException(nameof(projectInSolution));
}

if (!Project.SupportedProjectExtensions.Contains(_fileSystem.Path.GetExtension(projectInSolution.AbsolutePath)))
{
throw new NotSupportedException($"Project {projectInSolution.Name} is not supported to be loaded.");
}

if (!_loadedProjects.TryGetValue(projectInSolution.Guid, out Project project))
{
project = Project.Load(projectInSolution.AbsolutePath, projectInSolution.Name);
Expand Down Expand Up @@ -133,7 +171,7 @@ private void AddSolutionItem(Guid guid, SolutionItem item)
_solutionItems[guid] = item;
}

private void Load()
private void Load(bool allowAll)
{
var content = _fileSystem.File.ReadAllText(SolutionPath);
var parser = new Parser(content);
Expand All @@ -143,7 +181,7 @@ private void Load()

foreach (var p in projects)
{
ProcessProject(p);
ProcessProject(p, allowAll);
}

var nestedProjects = globalSections.FirstOrDefault(section => String.Equals(section.Name, "NestedProjects", StringComparison.OrdinalIgnoreCase));
Expand All @@ -163,7 +201,7 @@ private void Load()
}
}

private void ProcessProject(SolutionParser.Model.SlnProject p)
private void ProcessProject(SolutionParser.Model.SlnProject p, bool allowAll)
{
SolutionItem solutionItem;

Expand All @@ -181,8 +219,14 @@ private void ProcessProject(SolutionParser.Model.SlnProject p)
}
}
}
else if (allowAll)
{
// Allow all projects to be added
solutionItem = new ProjectInSolution(this, p);
}
else if (p.TypeGuid == SolutionProjectTypeIDs.MsBuildProject || p.TypeGuid == SolutionProjectTypeIDs.NetCoreProject)
{
// Allow only the C# projects that can be loaded with Project.Load
solutionItem = new ProjectInSolution(this, p);
}
else
Expand Down

0 comments on commit da792b2

Please sign in to comment.