Skip to content

Commit

Permalink
Improve exceptions to make it more clear what is going wrong.
Browse files Browse the repository at this point in the history
  • Loading branch information
MichielOda committed Mar 7, 2024
1 parent 9a43220 commit 41a49eb
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 50 deletions.
58 changes: 33 additions & 25 deletions Parsers.Common/VisualStudio/Projects/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Skyline.DataMiner.CICD.Parsers.Common.VisualStudio.Projects
using System.Xml.Linq;

using Skyline.DataMiner.CICD.FileSystem;
using Skyline.DataMiner.CICD.Parsers.Common.Exceptions;
using Skyline.DataMiner.CICD.Parsers.Common.Extensions;

/// <summary>
Expand Down Expand Up @@ -140,38 +141,45 @@ public static Project Load(string path, string projectName)
throw new FileNotFoundException("Could not find project file: " + path);
}

string projectDir = FileSystem.Path.GetDirectoryName(path);
var xmlContent = FileSystem.File.ReadAllText(path, Encoding.UTF8);
var document = XDocument.Parse(xmlContent);
try
{
string projectDir = FileSystem.Path.GetDirectoryName(path);
var xmlContent = FileSystem.File.ReadAllText(path, Encoding.UTF8);
var document = XDocument.Parse(xmlContent);

IProjectParser parser = ProjectParserFactory.GetParser(document, projectDir);
IProjectParser parser = ProjectParserFactory.GetParser(document, projectDir);

string name = projectName;
string assemblyName = parser.GetAssemblyName();
if (!String.IsNullOrEmpty(assemblyName))
{
name = assemblyName;
}

var project = new Project
{
AssemblyName = name,
Path = path,
ProjectStyle = parser.GetProjectStyle(),
};
string name = projectName;
string assemblyName = parser.GetAssemblyName();
if (!String.IsNullOrEmpty(assemblyName))
{
name = assemblyName;
}

project._references.AddRange(parser.GetReferences());
project._projectReferences.AddRange(parser.GetProjectReferences());
project._packageReferences.AddRange(parser.GetPackageReferences());
var project = new Project
{
AssemblyName = name,
Path = path,
ProjectStyle = parser.GetProjectStyle(),
};

var files = parser.GetCompileFiles().ToList();
project._references.AddRange(parser.GetReferences());
project._projectReferences.AddRange(parser.GetProjectReferences());
project._packageReferences.AddRange(parser.GetPackageReferences());

project._files.AddRange(files);
project._files.AddRange(parser.GetSharedProjectCompileFiles());
var files = parser.GetCompileFiles().ToList();

project.TargetFrameworkMoniker = parser.GetTargetFrameworkMoniker();
project._files.AddRange(files);
project._files.AddRange(parser.GetSharedProjectCompileFiles());

return project;
project.TargetFrameworkMoniker = parser.GetTargetFrameworkMoniker();

return project;
}
catch (Exception e)
{
throw new ParserException($"Failed to load project '{projectName}' ({path}).", e);
}
}
}
}
52 changes: 27 additions & 25 deletions Parsers.Common/VisualStudio/Projects/SdkStyleParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal class SdkStyleParser : IProjectParser

internal SdkStyleParser(XDocument document, string projectDir)
{
this.document = document ?? throw new ArgumentNullException(nameof(document));
this.document = document ?? throw new ArgumentNullException(nameof(document));
this.projectDir = projectDir;
}

Expand Down Expand Up @@ -86,7 +86,7 @@ public IEnumerable<ProjectReference> GetProjectReferences()
string name = path;
if (path.Contains("\\"))
{
name = name.Substring(path.LastIndexOf("\\") + 1);
name = name.Substring(path.LastIndexOf("\\") + 1);
}

name = name.Replace(".csproj", "");
Expand Down Expand Up @@ -134,11 +134,13 @@ public IEnumerable<ProjectFile> GetCompileFiles()

var directories = FileSystem.Directory.EnumerateDirectories(projectDir);

foreach(var directory in directories)
foreach (var directory in directories)
{
string directoryName = directory.Substring(relativePathOffset);

if(directoryName.Equals("bin", StringComparison.OrdinalIgnoreCase) || directoryName.Equals("obj", StringComparison.OrdinalIgnoreCase))
if (directoryName.Equals("bin", StringComparison.OrdinalIgnoreCase) ||
directoryName.Equals("obj", StringComparison.OrdinalIgnoreCase) ||
directoryName.Equals(".vs", StringComparison.OrdinalIgnoreCase))
{
continue;
}
Expand All @@ -155,35 +157,35 @@ public IEnumerable<ProjectFile> GetCompileFiles()

public string GetTargetFrameworkMoniker()
{
var propertyGroups = document
?.Element("Project")
?.Elements("PropertyGroup");
var propertyGroups = document
?.Element("Project")
?.Elements("PropertyGroup");

if (propertyGroups == null)
{
throw new ParserException("No PropertyGroup tags found in the csproj file!");
}
if (propertyGroups == null)
{
throw new ParserException("No PropertyGroup tags found in the csproj file!");
}

foreach (XElement propertyGroup in propertyGroups)
{
var targetFrameworkElement = propertyGroup.Element("TargetFramework");
foreach (XElement propertyGroup in propertyGroups)
{
var targetFrameworkElement = propertyGroup.Element("TargetFramework");

if (targetFrameworkElement == null)
{
continue;
}
if (targetFrameworkElement == null)
{
continue;
}

// SDK style projects support multi-targeting. Return first item.
string tfms = targetFrameworkElement.Value;
// SDK style projects support multi-targeting. Return first item.
string tfms = targetFrameworkElement.Value;

// https://learn.microsoft.com/en-us/dotnet/standard/frameworks
string sdkStyleTfm = tfms.Split(';')[0];
var tfm = NuGetFramework.ParseFolder(sdkStyleTfm);
// https://learn.microsoft.com/en-us/dotnet/standard/frameworks
string sdkStyleTfm = tfms.Split(';')[0];
var tfm = NuGetFramework.ParseFolder(sdkStyleTfm);

return tfm.DotNetFrameworkName;
return tfm.DotNetFrameworkName;
}

throw new ParserException("No TargetFramework tag found in the csproj file!");
throw new ParserException("No TargetFramework tag found in the csproj file!");
}

public IEnumerable<ProjectFile> GetSharedProjectCompileFiles()
Expand Down

0 comments on commit 41a49eb

Please sign in to comment.