Skip to content

Commit

Permalink
Better exception handling (#22)
Browse files Browse the repository at this point in the history
* Unzipped TestFiles in CommonTests

* Improve exceptions to make it more clear what is going wrong.
  • Loading branch information
MichielOda authored Mar 7, 2024
1 parent c8234df commit 47251c3
Show file tree
Hide file tree
Showing 67 changed files with 2,330 additions and 94 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
38 changes: 0 additions & 38 deletions Parsers.CommonTests/AssemblyInitialize.cs

This file was deleted.

16 changes: 10 additions & 6 deletions Parsers.CommonTests/Parsers.CommonTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<Compile Remove="VisualStudio\TestFiles\**" />
</ItemGroup>

<ItemGroup>
<Content Include="VisualStudio\TestFiles\**">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
Expand All @@ -22,10 +32,4 @@
<ProjectReference Include="..\Parsers.Common\Parsers.Common.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="VisualStudio\TestFiles.zip">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Binary file removed Parsers.CommonTests/VisualStudio/TestFiles.zip
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{3048B168-DC53-4143-ADF1-DCD95B8539ED}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Basic</RootNamespace>
<AssemblyName>Basic</AssemblyName>
<TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="MyReference">
<HintPath>..\MyHintPath.dll</HintPath>
<DataMinerDllPath>C:\MyOverrideDllPath.dll</DataMinerDllPath>
</Reference>
<ProjectReference Include="..\SubFolder\MyProjectReference.csproj">
<Project>{798B58BA-BAB8-4C52-8C48-1A8AF2B5CCCA}</Project>
<Name>MyFirstProjectReference</Name>
<DataMinerDllPath>C:\MyOverrideDllPath.dll</DataMinerDllPath>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Skyline.DataMiner.Dev.Automation" Version="10.0.0.5" />
<PackageReference Include="StyleCop.Analyzers">
<Version>1.1.118</Version>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Compile Include="Files\Class1.cs" />
<Compile Include="Files\Script.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Script_1b
{
public class Class1
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{3048B168-DC53-4143-ADF1-DCD95B8539ED}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Files</RootNamespace>
<AssemblyName>Files</AssemblyName>
<TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
Loading

0 comments on commit 47251c3

Please sign in to comment.