Skip to content

Commit

Permalink
Add DataMinerProjectType to Project (Parsers.Common) (#48)
Browse files Browse the repository at this point in the history
* Add DataMinerProjectType enum and necessary logic to parse it.

* Actually add it to the project properly

* Change location of the DataMinerType

* Small cleanup

* Add unknown

* no message

* Move back to the PropertyGroup>DataMinerType tag

* Add other useful properties to Project

* Fix SonarCloud
  • Loading branch information
MichielOda authored Jan 27, 2025
1 parent a989010 commit 3458299
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 7 deletions.
94 changes: 94 additions & 0 deletions Parsers.Common/VisualStudio/Projects/DataMinerProjectType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
namespace Skyline.DataMiner.CICD.Parsers.Common.VisualStudio.Projects
{
using System.Collections.Generic;

/// <summary>
/// Represents the DataMiner project type.
/// </summary>
public enum DataMinerProjectType
{
/// <summary>
/// Unknown value.
/// </summary>
Unknown,

/// <summary>
/// Represents a DataMiner Install Package project.
/// </summary>
Package,

/// <summary>
/// Represents a DataMiner Automation Script project.
/// </summary>
AutomationScript,

/// <summary>
/// Represents a DataMiner Automation Script project that will be a library.
/// </summary>
AutomationScriptLibrary,

/// <summary>
/// Represents a DataMiner Ad Hoc Data Source project.
/// </summary>
AdHocDataSource,

/// <summary>
/// Represents a DataMiner User-Defined API project.
/// </summary>
UserDefinedApi,
}

/// <summary>
/// Class to convert from and to the <see cref="DataMinerProjectType"/> enum.
/// </summary>
public static class DataMinerProjectTypeConverter
{
private static readonly Dictionary<string, DataMinerProjectType> StringToEnum = new Dictionary<string, DataMinerProjectType>
{
["Package"] = DataMinerProjectType.Package,
["AutomationScript"] = DataMinerProjectType.AutomationScript,
["AutomationScriptLibrary"] = DataMinerProjectType.AutomationScriptLibrary,
["AdHocDataSource"] = DataMinerProjectType.AdHocDataSource,
["UserDefinedApi"] = DataMinerProjectType.UserDefinedApi,
};

private static readonly Dictionary<DataMinerProjectType, string> EnumToString = new Dictionary<DataMinerProjectType, string>
{
[DataMinerProjectType.Package] = "Package",
[DataMinerProjectType.AutomationScript] = "AutomationScript",
[DataMinerProjectType.AutomationScriptLibrary] = "AutomationScriptLibrary",
[DataMinerProjectType.AdHocDataSource] = "AdHocDataSource",
[DataMinerProjectType.UserDefinedApi] = "UserDefinedApi",
};

/// <summary>
/// Tries to convert the specified value to the <see cref="DataMinerProjectType"/> enum.
/// </summary>
/// <param name="value">Value to convert.</param>
/// <returns>Converted value when successful, Unknown when not.</returns>
public static DataMinerProjectType ToEnum(string value)
{
if (StringToEnum.TryGetValue(value, out DataMinerProjectType t))
{
return t;
}

return DataMinerProjectType.Unknown;
}

/// <summary>
/// Tries to convert the specified <see cref="DataMinerProjectType"/> value to the string version for the project. Will return null when unable to convert to enum.
/// </summary>
/// <param name="value">Value to convert.</param>
/// <returns>Converted value when successful, null when not.</returns>
public static string ToString(DataMinerProjectType value)
{
if (EnumToString.TryGetValue(value, out string s))
{
return s;
}

return null;
}
}
}
2 changes: 2 additions & 0 deletions Parsers.Common/VisualStudio/Projects/IProjectParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ internal interface IProjectParser
string GetTargetFrameworkMoniker();

IEnumerable<ProjectFile> GetSharedProjectCompileFiles();

DataMinerProjectType? GetDataMinerProjectType();
}
}
2 changes: 2 additions & 0 deletions Parsers.Common/VisualStudio/Projects/LegacyStyleParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ public string GetTargetFrameworkMoniker()
return Constants.TargetFramework462;
}

public DataMinerProjectType? GetDataMinerProjectType() => null; // We don't support this in legacy style

public IEnumerable<ProjectFile> GetSharedProjectCompileFiles()
{
IEnumerable<XElement> imports = document
Expand Down
39 changes: 35 additions & 4 deletions Parsers.Common/VisualStudio/Projects/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,16 @@ private Project()
/// <summary>
/// Gets the project name.
/// </summary>
/// <value>The project name.</value>
public string ProjectName { get; set; }

/// <summary>
/// Gets the project directory.
/// </summary>
public string ProjectDirectory { get; set; }

/// <summary>
/// Gets the assembly name.
/// </summary>
public string AssemblyName { get; private set; }

/// <summary>
Expand All @@ -109,9 +118,14 @@ private Project()
public string TargetFrameworkMoniker { get; private set; }

/// <summary>
/// Gets the project files.
/// Gets the DataMiner project type.
/// </summary>
/// <value>The project files.</value>
public DataMinerProjectType? DataMinerProjectType { get; set; }

/// <summary>
/// Gets the project C# files.
/// </summary>
/// <value>The project C# files.</value>
public IEnumerable<ProjectFile> Files => _files;

/// <summary>
Expand Down Expand Up @@ -144,7 +158,19 @@ private Project()
/// <param name="projectName">The name of the project.</param>
/// <returns>The loaded project.</returns>
/// <exception cref="FileNotFoundException">The file specified in <paramref name="path"/> does not exist.</exception>
[Obsolete("Use the Load method with only the path argument.")]
public static Project Load(string path, string projectName)
{
return Load(path);
}

/// <summary>
/// Loads the projects with the specified path.
/// </summary>
/// <param name="path">The path of the project file to load.</param>
/// <returns>The loaded project.</returns>
/// <exception cref="FileNotFoundException">The file specified in <paramref name="path"/> does not exist.</exception>
public static Project Load(string path)
{
if (!FileSystem.File.Exists(path))
{
Expand All @@ -154,6 +180,9 @@ public static Project Load(string path, string projectName)
// Make sure to use the full path
path = FileSystem.Path.GetFullPath(path);

string projectDir = FileSystem.Path.GetDirectoryName(path);
string projectName = FileSystem.Path.GetFileNameWithoutExtension(path);

string extension = FileSystem.Path.GetExtension(path);
if (!SupportedProjectExtensions.Contains(extension))
{
Expand All @@ -162,7 +191,6 @@ public static Project Load(string path, string projectName)

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

Expand All @@ -180,6 +208,8 @@ public static Project Load(string path, string projectName)
AssemblyName = name,
Path = path,
ProjectStyle = parser.GetProjectStyle(),
ProjectDirectory = projectDir,
ProjectName = projectName,
};

project._references.AddRange(parser.GetReferences());
Expand All @@ -192,6 +222,7 @@ public static Project Load(string path, string projectName)
project._files.AddRange(parser.GetSharedProjectCompileFiles());

project.TargetFrameworkMoniker = parser.GetTargetFrameworkMoniker();
project.DataMinerProjectType = parser.GetDataMinerProjectType();

return project;
}
Expand Down
27 changes: 27 additions & 0 deletions Parsers.Common/VisualStudio/Projects/SdkStyleParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,33 @@ public string GetTargetFrameworkMoniker()
throw new ParserException("No TargetFramework tag found in the csproj file!");
}

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

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

foreach (XElement propertyGroup in propertyGroups)
{
var typeElement = propertyGroup.Element("DataMinerType");

if (typeElement == null)
{
continue;
}

return DataMinerProjectTypeConverter.ToEnum(typeElement.Value);
}

// Tag does not exist.
return null;
}

public IEnumerable<ProjectFile> GetSharedProjectCompileFiles()
{
IEnumerable<XElement> imports = document
Expand Down
33 changes: 31 additions & 2 deletions Parsers.CommonTests/VisualStudio/Projects/ProjectTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Parsers.CommonTests.VisualStudio.Projects
#pragma warning disable CS0618 // Type or member is obsolete
namespace Parsers.CommonTests.VisualStudio.Projects
{
using System;
using System.IO;
Expand Down Expand Up @@ -27,7 +28,35 @@ public void Load_GeneralValid()

// Assert
result.Should().NotBeNull();
result.AssemblyName.Should().BeEquivalentTo("Basic");
result.AssemblyName.Should().BeEquivalentTo("Basic2");
result.ProjectName.Should().BeEquivalentTo("Basic");
result.ProjectDirectory.Should().BeEquivalentTo(dir);
result.DataMinerProjectType.Should().BeNull();
result.Path.Should().BeEquivalentTo(path);
result.References.Should().NotBeNullOrEmpty();
result.PackageReferences.Should().NotBeNullOrEmpty();
result.Files.Should().NotBeNullOrEmpty();
result.ProjectReferences.Should().NotBeNullOrEmpty();
result.TargetFrameworkMoniker.Should().NotBeNullOrWhiteSpace();
}

[TestMethod]
public void Load_GeneralValid_LoadViaPathOnly()
{
// Arrange
var baseDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var dir = Path.GetFullPath(Path.Combine(baseDir, @"VisualStudio\TestFiles\ProjectsForTesting"));
var path = Path.Combine(dir, "Basic.csproj");

// Act
var result = Project.Load(path);

// Assert
result.Should().NotBeNull();
result.AssemblyName.Should().BeEquivalentTo("Basic2");
result.ProjectName.Should().BeEquivalentTo("Basic");
result.ProjectDirectory.Should().BeEquivalentTo(dir);
result.DataMinerProjectType.Should().BeNull();
result.Path.Should().BeEquivalentTo(path);
result.References.Should().NotBeNullOrEmpty();
result.PackageReferences.Should().NotBeNullOrEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Basic</RootNamespace>
<AssemblyName>Basic</AssemblyName>
<AssemblyName>Basic2</AssemblyName>
<TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
Expand Down

0 comments on commit 3458299

Please sign in to comment.