3
3
using System ;
4
4
using System . Collections . Generic ;
5
5
using System . IO ;
6
+ using System . Linq ;
7
+ using System . Reflection . Metadata ;
6
8
using System . Text ;
7
9
using System . Threading . Tasks ;
10
+ using System . Xml . Linq ;
8
11
9
12
using Skyline . AppInstaller ;
10
13
using Skyline . DataMiner . CICD . Assemblers . Automation ;
17
20
18
21
internal static class AutomationScriptStyle
19
22
{
20
- public static async Task < PackageResult > TryCreatePackage ( PackageCreationData data , bool createAsTempFile = false )
23
+ public static async Task < InstallPackageResult > TryCreateInstallPackage ( PackageCreationData data )
21
24
{
22
- var result = new PackageResult ( ) ;
25
+ var result = new InstallPackageResult ( ) ;
23
26
24
27
try
25
28
{
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 ) ;
32
30
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 ) ;
40
32
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 ) ) ;
45
36
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
+ }
48
45
49
- AutomationScriptBuilder automationScriptBuilder = new AutomationScriptBuilder ( script , scriptProjects , allScripts , data . Project . ProjectDirectory ) ;
50
- BuildResultItems buildResultItems = await automationScriptBuilder . BuildAsync ( ) ;
46
+ return result ;
47
+ }
51
48
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 ( ) ;
58
52
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 ) ) ;
65
60
66
61
AddNuGetAssemblies ( buildResultItems , appPackageAutomationScriptBuilder ) ;
67
62
AddDllAssemblies ( buildResultItems , appPackageAutomationScriptBuilder ) ;
@@ -78,6 +73,57 @@ public static async Task<PackageResult> TryCreatePackage(PackageCreationData dat
78
73
return result ;
79
74
}
80
75
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
+
81
127
private static void AddDllAssemblies ( BuildResultItems buildResultItems , AppPackageAutomationScript . AppPackageAutomationScriptBuilder appPackageAutomationScriptBuilder )
82
128
{
83
129
foreach ( DllAssemblyReference assemblyReference in buildResultItems . DllAssemblies )
@@ -125,6 +171,15 @@ public class PackageResult
125
171
public bool IsSuccess { get ; set ; }
126
172
}
127
173
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
+
128
183
private static byte [ ] ConvertToBytes ( string @string )
129
184
{
130
185
// Convert to byte[].
0 commit comments