Skip to content

Commit 1307c3c

Browse files
committed
Tackle package project
1 parent 693b264 commit 1307c3c

6 files changed

+313
-67
lines changed

Sdk/SubTasks/DataMinerProjectTypeHelper.cs Sdk/Helpers/DataMinerProjectTypeHelper.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
namespace Skyline.DataMiner.Sdk.SubTasks
1+
namespace Skyline.DataMiner.Sdk.Helpers
22
{
33
using Skyline.DataMiner.CICD.Parsers.Common.VisualStudio.Projects;
44

5-
public static class DataMinerProjectTypeHelper
5+
internal static class DataMinerProjectTypeHelper
66
{
77
public static bool IsAutomationScriptStyle(this DataMinerProjectType? type)
88
{

Sdk/Helpers/ProjectReferenceHelper.cs

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
namespace Skyline.DataMiner.Sdk.Helpers
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Linq;
7+
using System.Text.RegularExpressions;
8+
using System.Xml;
9+
using System.Xml.Linq;
10+
11+
using Skyline.DataMiner.CICD.FileSystem;
12+
using Skyline.DataMiner.CICD.Parsers.Common.VisualStudio.Projects;
13+
14+
internal static class ProjectReferenceHelper
15+
{
16+
public static bool TryResolveProjectReferences(Project packageProject, out List<string> includedProjectPaths, out string errorMessage)
17+
{
18+
includedProjectPaths = null;
19+
errorMessage = null;
20+
21+
string solutionFolder = FileSystem.Instance.Directory.GetParentDirectory(packageProject.ProjectDirectory);
22+
const string xmlFileName = "ProjectReferences.xml";
23+
string xmlFilePath = FileSystem.Instance.Path.Combine(packageProject.ProjectDirectory, "PackageContent", xmlFileName);
24+
25+
try
26+
{
27+
includedProjectPaths = ResolveProjectReferences(xmlFilePath, solutionFolder);
28+
return true;
29+
}
30+
catch (FileNotFoundException)
31+
{
32+
errorMessage = $"{xmlFileName} could not be found. Please make sure this file has been added with the default content.";
33+
}
34+
catch (XmlException xe)
35+
{
36+
errorMessage = $"XmlException: Please validate the {xmlFileName} file. ({xe.Message})";
37+
}
38+
catch (Exception e)
39+
{
40+
errorMessage = $"Unexpected exception occured: {e}";
41+
}
42+
43+
return false;
44+
}
45+
46+
private static List<string> ResolveProjectReferences(string xmlFilePath, string solutionFolder)
47+
{
48+
// Load the XML file
49+
var doc = XDocument.Load(xmlFilePath);
50+
XNamespace ns = "http://www.skyline.be/projectReferences";
51+
52+
// Parse Include and Exclude elements
53+
var includePatterns = doc.Descendants(ns + "ProjectReference")
54+
.Attributes("Include")
55+
.Select(attr => attr.Value)
56+
.ToList();
57+
58+
var excludePatterns = doc.Descendants(ns + "ProjectReference")
59+
.Attributes("Exclude")
60+
.Select(attr => attr.Value)
61+
.ToList();
62+
63+
if (!includePatterns.Any())
64+
{
65+
includePatterns = new List<string>
66+
{
67+
// If nothing is included, include everything by default.
68+
@"..\*\*.csproj"
69+
};
70+
}
71+
72+
// Resolve Include patterns
73+
var includedProjects = new HashSet<string>();
74+
foreach (var pattern in includePatterns)
75+
{
76+
var resolvedPaths = Directory.GetFiles(solutionFolder, "*.csproj", SearchOption.AllDirectories)
77+
.Where(path => MatchesPattern(path, pattern));
78+
foreach (var path in resolvedPaths)
79+
{
80+
includedProjects.Add(path);
81+
}
82+
}
83+
84+
// Apply Exclude patterns
85+
foreach (var pattern in excludePatterns)
86+
{
87+
var resolvedPaths = Directory.GetFiles(solutionFolder, "*.csproj", SearchOption.AllDirectories)
88+
.Where(path => MatchesPattern(path, pattern));
89+
foreach (var path in resolvedPaths)
90+
{
91+
includedProjects.Remove(path);
92+
}
93+
}
94+
95+
return includedProjects.ToList();
96+
}
97+
98+
private static bool MatchesPattern(string filePath, string pattern)
99+
{
100+
// Convert the pattern to a regex for matching, if necessary
101+
// For now, handle '*' as wildcard
102+
string normalizedPattern = pattern.Replace("*", ".*").Replace(@"\", @"\\");
103+
return Regex.IsMatch(filePath, normalizedPattern, RegexOptions.IgnoreCase);
104+
}
105+
}
106+
}

Sdk/SubTasks/ProjectToItemConverter.cs Sdk/Helpers/ProjectToItemConverter.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
namespace Skyline.DataMiner.Sdk.SubTasks
1+
namespace Skyline.DataMiner.Sdk.Helpers
22
{
33
using Skyline.DataMiner.CICD.FileSystem;
44
using Skyline.DataMiner.CICD.Parsers.Automation.Xml;
55
using Skyline.DataMiner.CICD.Parsers.Common.VisualStudio.Projects;
66

7-
public static class ProjectToItemConverter
7+
internal static class ProjectToItemConverter
88
{
99
public static bool TryConvertToScript(Project project, out Script script)
1010
{

Sdk/Sdk.csproj

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<Nullable>disable</Nullable>
66
<AssemblyName>Skyline.DataMiner.Sdk</AssemblyName>
77
<RootNamespace>Skyline.DataMiner.Sdk</RootNamespace>
8-
<PackageTags>Skyline;DataMiner</PackageTags>
8+
<PackageTags>Skyline;DataMiner;MSBuildSdk</PackageTags>
99
<PackageProjectUrl>https://skyline.be</PackageProjectUrl>
1010
<PackageReadmeFile>README.md</PackageReadmeFile>
1111
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
@@ -18,11 +18,14 @@
1818
<Description>TBD</Description>
1919
<RepositoryUrl>https://github.com/SkylineCommunications/Skyline.DataMiner.Sdk</RepositoryUrl>
2020
<RepositoryType>git</RepositoryType>
21+
<NoWarn>1701;1702;NU5100</NoWarn>
22+
</PropertyGroup>
23+
24+
<PropertyGroup>
2125
<PackageType>MSBuildSdk</PackageType>
2226
<BuildOutputTargetFolder>Sdk</BuildOutputTargetFolder>
2327
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
2428
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage</TargetsForTfmSpecificBuildOutput>
25-
<NoWarn>1701;1702;NU5100</NoWarn>
2629
<PackageVersion>0.1.0</PackageVersion>
2730
</PropertyGroup>
2831

Sdk/SubTasks/AutomationScriptStyle.cs

+21-20
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Collections.Generic;
55
using System.IO;
66
using System.Linq;
7-
using System.Reflection.Metadata;
87
using System.Text;
98
using System.Threading.Tasks;
109
using System.Xml.Linq;
@@ -15,8 +14,9 @@
1514
using Skyline.DataMiner.CICD.FileSystem;
1615
using Skyline.DataMiner.CICD.Parsers.Automation.Xml;
1716
using Skyline.DataMiner.CICD.Parsers.Common.VisualStudio.Projects;
17+
using Skyline.DataMiner.Sdk.Helpers;
1818

19-
using static Skyline.DataMiner.Sdk.DmappCreation;
19+
using static Tasks.DmappCreation;
2020

2121
internal static class AutomationScriptStyle
2222
{
@@ -128,19 +128,20 @@ private static void AddDllAssemblies(BuildResultItems buildResultItems, AppPacka
128128
{
129129
foreach (DllAssemblyReference assemblyReference in buildResultItems.DllAssemblies)
130130
{
131-
if (assemblyReference.AssemblyPath == null)
131+
if (assemblyReference.AssemblyPath == null || AppPackage.AppPackageBuilder.IsDllToBeIgnored(assemblyReference.AssemblyPath))
132132
{
133133
continue;
134134
}
135135

136+
136137
string folder = @"C:\Skyline DataMiner\ProtocolScripts";
137138
if (assemblyReference.IsFilesPackage)
138139
{
139140
folder = @"C:\Skyline DataMiner\Files";
140141
}
141142

142-
var destinationFolderPath = FileSystem.Instance.Path.Combine(folder, assemblyReference.DllImport);
143-
var destinationDirectory = FileSystem.Instance.Path.GetDirectoryName(destinationFolderPath);
143+
var destinationDllPath = FileSystem.Instance.Path.Combine(folder, assemblyReference.DllImport);
144+
var destinationDirectory = FileSystem.Instance.Path.GetDirectoryName(destinationDllPath);
144145

145146
appPackageAutomationScriptBuilder.WithAssembly(assemblyReference.AssemblyPath, destinationDirectory);
146147
}
@@ -162,7 +163,20 @@ private static void AddNuGetAssemblies(BuildResultItems buildResultItems, AppPac
162163
}
163164
}
164165

165-
public class PackageResult
166+
private static byte[] ConvertToBytes(string @string)
167+
{
168+
// Convert to byte[].
169+
var memoryStream = new MemoryStream();
170+
using (var streamWriter = new StreamWriter(memoryStream, new UTF8Encoding(true)))
171+
{
172+
streamWriter.Write(@string);
173+
}
174+
175+
byte[] content = memoryStream.ToArray();
176+
return content;
177+
}
178+
179+
internal class PackageResult
166180
{
167181
public IAppPackageAutomationScript Script { get; set; }
168182

@@ -171,26 +185,13 @@ public class PackageResult
171185
public bool IsSuccess { get; set; }
172186
}
173187

174-
public class InstallPackageResult
188+
internal class InstallPackageResult
175189
{
176190
public IAppPackageScript Script { get; set; }
177191

178192
public string ErrorMessage { get; set; }
179193

180194
public bool IsSuccess { get; set; }
181195
}
182-
183-
private static byte[] ConvertToBytes(string @string)
184-
{
185-
// Convert to byte[].
186-
var memoryStream = new MemoryStream();
187-
using (var streamWriter = new StreamWriter(memoryStream, new UTF8Encoding(true)))
188-
{
189-
streamWriter.Write(@string);
190-
}
191-
192-
byte[] content = memoryStream.ToArray();
193-
return content;
194-
}
195196
}
196197
}

0 commit comments

Comments
 (0)