Skip to content

Commit 8387506

Browse files
committed
Support SolutionFilter files
1 parent 1246b3d commit 8387506

File tree

3 files changed

+47
-10
lines changed

3 files changed

+47
-10
lines changed

Sdk/Helpers/ProjectReferenceHelper.cs

+44-8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.IO;
66
using System.Linq;
7+
using System.Text.Json;
78
using System.Text.RegularExpressions;
89
using System.Xml;
910
using System.Xml.Linq;
@@ -18,13 +19,13 @@ public static bool TryResolveProjectReferences(Project packageProject, out List<
1819
includedProjectPaths = null;
1920
errorMessage = null;
2021

21-
string solutionFolder = FileSystem.Instance.Directory.GetParentDirectory(packageProject.ProjectDirectory);
22+
string rootFolder = FileSystem.Instance.Directory.GetParentDirectory(packageProject.ProjectDirectory);
2223
const string xmlFileName = "ProjectReferences.xml";
2324
string xmlFilePath = FileSystem.Instance.Path.Combine(packageProject.ProjectDirectory, "PackageContent", xmlFileName);
2425

2526
try
2627
{
27-
includedProjectPaths = ResolveProjectReferences(xmlFilePath, solutionFolder);
28+
includedProjectPaths = ResolveProjectReferences(xmlFilePath, rootFolder);
2829
return true;
2930
}
3031
catch (FileNotFoundException)
@@ -43,7 +44,7 @@ public static bool TryResolveProjectReferences(Project packageProject, out List<
4344
return false;
4445
}
4546

46-
private static List<string> ResolveProjectReferences(string xmlFilePath, string solutionFolder)
47+
private static List<string> ResolveProjectReferences(string xmlFilePath, string rootFolder)
4748
{
4849
// Load the XML file
4950
var doc = XDocument.Load(xmlFilePath);
@@ -55,6 +56,33 @@ private static List<string> ResolveProjectReferences(string xmlFilePath, string
5556
.Select(attr => attr.Value)
5657
.ToList();
5758

59+
var includeSolutionFilters = doc.Descendants(ns + "SolutionFilter")
60+
.Attributes("Include")
61+
.Select(attr => attr.Value)
62+
.ToList();
63+
64+
// Readout solution filter files and add them to the include patterns
65+
foreach (string includeSolutionFilter in includeSolutionFilters)
66+
{
67+
var filters = FileSystem.Instance.Directory.GetFiles(rootFolder, "*.slnf", SearchOption.AllDirectories)
68+
.Where(path => MatchesPattern(path, includeSolutionFilter));
69+
70+
foreach (string filter in filters)
71+
{
72+
string json = FileSystem.Instance.File.ReadAllText(filter);
73+
JsonDocument jsonFilter = JsonDocument.Parse(json);
74+
75+
if (jsonFilter.RootElement.TryGetProperty("solution", out JsonElement solution) && solution.TryGetProperty("projects", out JsonElement projects)
76+
&& projects.ValueKind== JsonValueKind.Array)
77+
{
78+
foreach (JsonElement project in projects.EnumerateArray())
79+
{
80+
includePatterns.Add($"..\\{project}");
81+
}
82+
}
83+
}
84+
}
85+
5886
var excludePatterns = doc.Descendants(ns + "ProjectReference")
5987
.Attributes("Exclude")
6088
.Select(attr => attr.Value)
@@ -73,8 +101,8 @@ private static List<string> ResolveProjectReferences(string xmlFilePath, string
73101
var includedProjects = new HashSet<string>();
74102
foreach (var pattern in includePatterns)
75103
{
76-
var resolvedPaths = Directory.GetFiles(solutionFolder, "*.csproj", SearchOption.AllDirectories)
77-
.Where(path => MatchesPattern(path, pattern));
104+
var resolvedPaths = FileSystem.Instance.Directory.GetFiles(rootFolder, "*.csproj", SearchOption.AllDirectories)
105+
.Where(path => MatchesPattern(path, pattern));
78106
foreach (var path in resolvedPaths)
79107
{
80108
includedProjects.Add(path);
@@ -84,15 +112,15 @@ private static List<string> ResolveProjectReferences(string xmlFilePath, string
84112
// Apply Exclude patterns
85113
foreach (var pattern in excludePatterns)
86114
{
87-
var resolvedPaths = Directory.GetFiles(solutionFolder, "*.csproj", SearchOption.AllDirectories)
88-
.Where(path => MatchesPattern(path, pattern));
115+
var resolvedPaths = FileSystem.Instance.Directory.GetFiles(rootFolder, "*.csproj", SearchOption.AllDirectories)
116+
.Where(path => MatchesPattern(path, pattern));
89117
foreach (var path in resolvedPaths)
90118
{
91119
includedProjects.Remove(path);
92120
}
93121
}
94122

95-
return includedProjects.ToList();
123+
return includedProjects.Distinct().ToList();
96124
}
97125

98126
private static bool MatchesPattern(string filePath, string pattern)
@@ -102,5 +130,13 @@ private static bool MatchesPattern(string filePath, string pattern)
102130
string normalizedPattern = pattern.Replace("*", ".*").Replace(@"\", @"\\");
103131
return Regex.IsMatch(filePath, normalizedPattern, RegexOptions.IgnoreCase);
104132
}
133+
134+
private class SolutionFilter
135+
{
136+
public string Path { get; set; }
137+
138+
public string[] Projects { get; set; }
139+
}
140+
105141
}
106142
}

Sdk/Sdk.csproj

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<BuildOutputTargetFolder>Sdk</BuildOutputTargetFolder>
2424
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
2525
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage</TargetsForTfmSpecificBuildOutput>
26+
<PackageVersion>0.1.0</PackageVersion>
2627
</PropertyGroup>
2728

2829
<ItemGroup>
@@ -40,6 +41,7 @@
4041
<PackageReference Include="Skyline.DataMiner.CICD.Parsers.Common" Version="1.0.13-foxtrot" PrivateAssets="all" />
4142
<PackageReference Include="Skyline.DataMiner.CICD.Assemblers.Automation" Version="1.0.16-alpha" PrivateAssets="all" />
4243
<PackageReference Include="Skyline.DataMiner.Core.AppPackageCreator" Version="1.0.4" PrivateAssets="all" />
44+
<PackageReference Include="System.Text.Json" Version="9.0.0" PrivateAssets="all" />
4345
</ItemGroup>
4446

4547
<Target Name="CopyProjectReferencesToPackage" DependsOnTargets="ResolveReferences">
@@ -50,7 +52,7 @@
5052
</Target>
5153

5254
<Target Name="LocalTesting" AfterTargets="Pack" Condition="'$(Configuration)' == 'Debug'">
53-
<RemoveDir Directories="$(USERPROFILE)\.nuget\packages\skyline.dataminer.sdk" />
55+
<RemoveDir Directories="$(USERPROFILE)\.nuget\packages\skyline.dataminer.sdk\$(PackageVersion)" />
5456
<Copy SourceFiles="$(MSBuildProjectDirectory)\$(BaseOutputPath)$(Configuration)\$(AssemblyName).$(PackageVersion).nupkg" DestinationFolder="$(USERPROFILE)\Documents\MyNugets" />
5557
<Message Text="### Finished copying new package. ###" Importance="High" />
5658
</Target>

Sdk/Tasks/DmappCreation.cs

-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ private void AddProjectToPackage(PackageCreationData preparedData, AppPackage.Ap
144144
Log.LogError("Including a package project inside another package project is not supported.");
145145
break;
146146

147-
case DataMinerProjectType.Unknown:
148147
default:
149148
Log.LogError($"Project {preparedData.Project.ProjectName} could not be added to package due to an unknown DataMinerType.");
150149
return;

0 commit comments

Comments
 (0)