From 41a49eb20002b27e474124de32b8385d5dc4785f Mon Sep 17 00:00:00 2001 From: Michiel Oda Date: Thu, 7 Mar 2024 12:14:04 +0100 Subject: [PATCH] Improve exceptions to make it more clear what is going wrong. --- .../VisualStudio/Projects/Project.cs | 58 +++++++++++-------- .../VisualStudio/Projects/SdkStyleParser.cs | 52 +++++++++-------- 2 files changed, 60 insertions(+), 50 deletions(-) diff --git a/Parsers.Common/VisualStudio/Projects/Project.cs b/Parsers.Common/VisualStudio/Projects/Project.cs index 672e695..63fec6a 100644 --- a/Parsers.Common/VisualStudio/Projects/Project.cs +++ b/Parsers.Common/VisualStudio/Projects/Project.cs @@ -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; /// @@ -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); + } } } } \ No newline at end of file diff --git a/Parsers.Common/VisualStudio/Projects/SdkStyleParser.cs b/Parsers.Common/VisualStudio/Projects/SdkStyleParser.cs index bfbd7b7..10b6267 100644 --- a/Parsers.Common/VisualStudio/Projects/SdkStyleParser.cs +++ b/Parsers.Common/VisualStudio/Projects/SdkStyleParser.cs @@ -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; } @@ -86,7 +86,7 @@ public IEnumerable GetProjectReferences() string name = path; if (path.Contains("\\")) { - name = name.Substring(path.LastIndexOf("\\") + 1); + name = name.Substring(path.LastIndexOf("\\") + 1); } name = name.Replace(".csproj", ""); @@ -134,11 +134,13 @@ public IEnumerable 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; } @@ -155,35 +157,35 @@ public IEnumerable 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 GetSharedProjectCompileFiles()