Skip to content

Commit c706100

Browse files
committed
Improve package creation for custom install script + other changes
1 parent 0460da1 commit c706100

File tree

4 files changed

+132
-48
lines changed

4 files changed

+132
-48
lines changed

Sdk/Sdk.csproj

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

2829
<ItemGroup>
@@ -48,4 +49,10 @@
4849
<BuildOutputInPackage Include="@(ReferenceCopyLocalPaths)" TargetPath="%(ReferenceCopyLocalPaths.DestinationSubPath)" />
4950
</ItemGroup>
5051
</Target>
52+
53+
<Target Name="LocalTesting" AfterTargets="Pack" Condition="'$(Configuration)' == 'Debug'">
54+
<RemoveDir Directories="$(USERPROFILE)\.nuget\packages\skyline.dataminer.sdk" />
55+
<Copy SourceFiles="$(MSBuildProjectDirectory)\$(BaseOutputPath)$(Configuration)\$(AssemblyName).$(PackageVersion).nupkg" DestinationFolder="$(USERPROFILE)\Documents\MyNugets" />
56+
<Message Text="### Finished copying new package. ###" Importance="High" />
57+
</Target>
5158
</Project>

Sdk/Sdk/Sdk.targets

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
ProjectFile="$(MSBuildProjectFile)"
1212
ProjectType="$(DataMinerType)"
1313
BaseOutputPath="$(BaseOutputPath)"
14-
Configuraiton="$(Configuration)"
14+
Configuration="$(Configuration)"
1515
Version="$(Version)"
16-
MinimumSupportedDmVersion="$(MinimumSupportedDmVersion)"
16+
MinimumRequiredDmVersion="$(MinimumRequiredDmVersion)"
1717
/>
1818
</Target>
1919
</Project>

Sdk/SubTasks/AutomationScriptStyle.cs

+90-35
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
using System;
44
using System.Collections.Generic;
55
using System.IO;
6+
using System.Linq;
7+
using System.Reflection.Metadata;
68
using System.Text;
79
using System.Threading.Tasks;
10+
using System.Xml.Linq;
811

912
using Skyline.AppInstaller;
1013
using Skyline.DataMiner.CICD.Assemblers.Automation;
@@ -17,51 +20,43 @@
1720

1821
internal static class AutomationScriptStyle
1922
{
20-
public static async Task<PackageResult> TryCreatePackage(PackageCreationData data, bool createAsTempFile = false)
23+
public static async Task<InstallPackageResult> TryCreateInstallPackage(PackageCreationData data)
2124
{
22-
var result = new PackageResult();
25+
var result = new InstallPackageResult();
2326

2427
try
2528
{
26-
var script = Script.Load(FileSystem.Instance.Path.Combine(data.Project.ProjectDirectory, $"{data.Project.ProjectName}.xml"));
27-
var scriptProjects = new Dictionary<string, Project>
28-
{
29-
// Will always be one
30-
[data.Project.ProjectName] = data.Project,
31-
};
29+
BuildResultItems buildResultItems = await BuildScript(data);
3230

33-
List<Script> allScripts = new List<Script>();
34-
foreach (Project linkedProject in data.LinkedProjects)
35-
{
36-
if (!linkedProject.DataMinerProjectType.IsAutomationScriptStyle())
37-
{
38-
continue;
39-
}
31+
string filePath = ConvertToInstallScript(data, buildResultItems);
4032

41-
if (!ProjectToItemConverter.TryConvertToScript(linkedProject, out Script linkedScript))
42-
{
43-
continue;
44-
}
33+
List<string> assemblies = new List<string>();
34+
assemblies.AddRange(buildResultItems.DllAssemblies.Select(reference => reference.AssemblyPath));
35+
assemblies.AddRange(buildResultItems.Assemblies.Select(reference => reference.AssemblyPath));
4536

46-
allScripts.Add(linkedScript);
47-
}
37+
result.Script = new AppPackageScript(filePath, assemblies);
38+
result.IsSuccess = true;
39+
}
40+
catch (Exception e)
41+
{
42+
result.ErrorMessage = $"Unexpected exception during package creation for '{data.Project.ProjectName}': {e}";
43+
result.IsSuccess = false;
44+
}
4845

49-
AutomationScriptBuilder automationScriptBuilder = new AutomationScriptBuilder(script, scriptProjects, allScripts, data.Project.ProjectDirectory);
50-
BuildResultItems buildResultItems = await automationScriptBuilder.BuildAsync();
46+
return result;
47+
}
5148

52-
AppPackageAutomationScript.AppPackageAutomationScriptBuilder appPackageAutomationScriptBuilder;
53-
if (createAsTempFile)
54-
{
55-
// Create temp file
56-
string tempFileName = FileSystem.Instance.Path.GetTempFileName() + ".xml";
57-
FileSystem.Instance.File.WriteAllText(tempFileName, buildResultItems.Document);
49+
public static async Task<PackageResult> TryCreatePackage(PackageCreationData data)
50+
{
51+
var result = new PackageResult();
5852

59-
appPackageAutomationScriptBuilder = new AppPackageAutomationScript.AppPackageAutomationScriptBuilder(script.Name, data.Version, tempFileName);
60-
}
61-
else
62-
{
63-
appPackageAutomationScriptBuilder = new AppPackageAutomationScript.AppPackageAutomationScriptBuilder(script.Name, data.Version, ConvertToBytes(buildResultItems.Document));
64-
}
53+
try
54+
{
55+
BuildResultItems buildResultItems = await BuildScript(data);
56+
57+
var appPackageAutomationScriptBuilder = new AppPackageAutomationScript.AppPackageAutomationScriptBuilder(data.Project.ProjectName,
58+
data.Version,
59+
ConvertToBytes(buildResultItems.Document));
6560

6661
AddNuGetAssemblies(buildResultItems, appPackageAutomationScriptBuilder);
6762
AddDllAssemblies(buildResultItems, appPackageAutomationScriptBuilder);
@@ -78,6 +73,57 @@ public static async Task<PackageResult> TryCreatePackage(PackageCreationData dat
7873
return result;
7974
}
8075

76+
private static async Task<BuildResultItems> BuildScript(PackageCreationData data)
77+
{
78+
var script = Script.Load(FileSystem.Instance.Path.Combine(data.Project.ProjectDirectory, $"{data.Project.ProjectName}.xml"));
79+
var scriptProjects = new Dictionary<string, Project>
80+
{
81+
// Will always be one
82+
[data.Project.ProjectName] = data.Project,
83+
};
84+
85+
List<Script> allScripts = new List<Script>();
86+
foreach (Project linkedProject in data.LinkedProjects)
87+
{
88+
if (!linkedProject.DataMinerProjectType.IsAutomationScriptStyle())
89+
{
90+
continue;
91+
}
92+
93+
if (!ProjectToItemConverter.TryConvertToScript(linkedProject, out Script linkedScript))
94+
{
95+
continue;
96+
}
97+
98+
allScripts.Add(linkedScript);
99+
}
100+
101+
AutomationScriptBuilder automationScriptBuilder =
102+
new AutomationScriptBuilder(script, scriptProjects, allScripts, data.Project.ProjectDirectory);
103+
BuildResultItems buildResultItems = await automationScriptBuilder.BuildAsync();
104+
return buildResultItems;
105+
}
106+
107+
private static string ConvertToInstallScript(PackageCreationData data, BuildResultItems buildResultItems)
108+
{
109+
// Create temp file, needs to be called Install.xml
110+
string tempDirectory = FileSystem.Instance.Path.Combine(data.TemporaryDirectory, Guid.NewGuid().ToString());
111+
FileSystem.Instance.Directory.CreateDirectory(tempDirectory);
112+
string filePath = FileSystem.Instance.Path.Combine(tempDirectory, "Install.xml");
113+
114+
XDocument doc = XDocument.Parse(buildResultItems.Document);
115+
var ns = doc.Root.GetDefaultNamespace();
116+
117+
foreach (var item in doc.Descendants(ns + "Param"))
118+
{
119+
// Remove the front part of the path as InstallScript won't look in the usual places
120+
item.Value = Path.GetFileName(item.Value);
121+
}
122+
123+
FileSystem.Instance.File.WriteAllText(filePath, doc.ToString());
124+
return filePath;
125+
}
126+
81127
private static void AddDllAssemblies(BuildResultItems buildResultItems, AppPackageAutomationScript.AppPackageAutomationScriptBuilder appPackageAutomationScriptBuilder)
82128
{
83129
foreach (DllAssemblyReference assemblyReference in buildResultItems.DllAssemblies)
@@ -125,6 +171,15 @@ public class PackageResult
125171
public bool IsSuccess { get; set; }
126172
}
127173

174+
public class InstallPackageResult
175+
{
176+
public IAppPackageScript Script { get; set; }
177+
178+
public string ErrorMessage { get; set; }
179+
180+
public bool IsSuccess { get; set; }
181+
}
182+
128183
private static byte[] ConvertToBytes(string @string)
129184
{
130185
// Convert to byte[].

Sdk/Tasks/DmappCreation.cs

+33-11
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace Skyline.DataMiner.Sdk
1212

1313
using Skyline.AppInstaller;
1414
using Skyline.DataMiner.CICD.Common;
15+
using Skyline.DataMiner.CICD.FileSystem;
1516
using Skyline.DataMiner.CICD.Parsers.Common.VisualStudio.Projects;
1617
using Skyline.DataMiner.Sdk.SubTasks;
1718

@@ -32,13 +33,23 @@ public class DmappCreation : Task, ICancelableTask
3233
public override bool Execute()
3334
{
3435
Stopwatch timer = Stopwatch.StartNew();
36+
PackageCreationData preparedData;
3537

3638
try
3739
{
38-
DataMinerProjectType dataMinerProjectType = DataMinerProjectTypeConverter.ToEnum(ProjectType);
39-
40-
var preparedData = PrepareData();
40+
preparedData = PrepareData();
41+
}
42+
catch (Exception e)
43+
{
44+
Log.LogError("Failed to prepare the data needed for package creation. See build output for more information.");
45+
Log.LogMessage(MessageImportance.High, $"Failed to prepare the data needed for package creation: {e}");
46+
return false;
47+
}
4148

49+
try
50+
{
51+
DataMinerProjectType dataMinerProjectType = DataMinerProjectTypeConverter.ToEnum(ProjectType);
52+
4253
if (!TryCreateAppPackageBuilder(preparedData, dataMinerProjectType, out AppPackage.AppPackageBuilder appPackageBuilder))
4354
{
4455
return false;
@@ -72,8 +83,17 @@ public override bool Execute()
7283
// Add included content
7384
// Add referenced content
7485

86+
//foreach (var file in filesFromDllsFolder)
87+
//{
88+
// appPackageBuilder.WithAssembly(file, "C:\\Skyline DataMiner\\ProtocolScripts\\DllImport");
89+
//}
90+
91+
//string companionFilesDirectory =
92+
// FileSystem.Instance.Path.Combine(preparedData.Project.ProjectDirectory, "PackageContent", "CompanionFiles");
93+
//appPackageBuilder.WithCompanionFiles(companionFilesDirectory);
94+
7595
Log.LogWarning($"Type '{dataMinerProjectType}' is currently not supported yet.");
76-
return false;
96+
break;
7797

7898
case DataMinerProjectType.Unknown:
7999
default:
@@ -99,6 +119,8 @@ public override bool Execute()
99119
}
100120
finally
101121
{
122+
FileSystem.Instance.Directory.DeleteDirectory(preparedData.TemporaryDirectory);
123+
102124
timer.Stop();
103125
Log.LogMessage(MessageImportance.High, $"Package creation took {timer.ElapsedMilliseconds} ms.");
104126
}
@@ -115,18 +137,15 @@ private bool TryCreateAppPackageBuilder(PackageCreationData preparedData, DataMi
115137
return true;
116138
}
117139

118-
// Create package with this project as the install script.
119-
AutomationScriptStyle.PackageResult packageResult = AutomationScriptStyle.TryCreatePackage(preparedData, createAsTempFile: true).WaitAndUnwrapException();
140+
var packageResult = AutomationScriptStyle.TryCreateInstallPackage(preparedData).WaitAndUnwrapException();
120141

121142
if (!packageResult.IsSuccess)
122143
{
123144
Log.LogError(packageResult.ErrorMessage);
124145
return false;
125146
}
126147

127-
var installScript = new AppPackageScript(packageResult.Script.Script, packageResult.Script.Assemblies.Select(assembly => assembly.AssemblyFilePath));
128-
appPackageBuilder = new AppPackage.AppPackageBuilder(preparedData.Project.ProjectName, Version, preparedData.MinimumRequiredDmVersion, installScript);
129-
148+
appPackageBuilder = new AppPackage.AppPackageBuilder(preparedData.Project.ProjectName, Version, preparedData.MinimumRequiredDmVersion, packageResult.Script);
130149
return true;
131150
}
132151

@@ -159,13 +178,14 @@ private PackageCreationData PrepareData()
159178
{
160179
version = dmVersion.ToStrictString();
161180
}
162-
181+
163182
return new PackageCreationData
164183
{
165184
Project = project,
166185
LinkedProjects = referencedProjects,
167186
Version = Version,
168-
MinimumRequiredDmVersion = version
187+
MinimumRequiredDmVersion = version,
188+
TemporaryDirectory = FileSystem.Instance.Directory.CreateTemporaryDirectory(),
169189
};
170190
}
171191

@@ -178,6 +198,8 @@ internal class PackageCreationData
178198
public string Version { get; set; }
179199

180200
public string MinimumRequiredDmVersion { get; set; }
201+
202+
public string TemporaryDirectory { get; set; }
181203
}
182204
}
183205
}

0 commit comments

Comments
 (0)