190930
This commit is contained in:
13
Pilz.Updating/Scripts/Script.vb
Normal file
13
Pilz.Updating/Scripts/Script.vb
Normal file
@@ -0,0 +1,13 @@
|
||||
Imports Pilz.Updating.Model
|
||||
|
||||
Namespace Scripts
|
||||
|
||||
Public Class Script
|
||||
|
||||
Public Property Priority As ScriptPriority = ScriptPriority.Before
|
||||
Public Property Language As CodeLanguage = CodeLanguage.CSharp
|
||||
Public Property Code As String
|
||||
|
||||
End Class
|
||||
|
||||
End Namespace
|
||||
71
Pilz.Updating/Scripts/ScriptManager.vb
Normal file
71
Pilz.Updating/Scripts/ScriptManager.vb
Normal file
@@ -0,0 +1,71 @@
|
||||
Imports System.CodeDom.Compiler
|
||||
Imports System.Reflection
|
||||
Imports Microsoft.CSharp
|
||||
Imports Pilz.Updating.Model
|
||||
Imports Pilz.Updating.Scripts
|
||||
|
||||
Namespace Scripts
|
||||
|
||||
Public Class ScriptManager
|
||||
|
||||
Public Property ApplicationPath As String
|
||||
Public Property UpdatePackagePath As String
|
||||
|
||||
Public Sub New(applicationPath As String, updatePackagePath As String)
|
||||
Me.ApplicationPath = applicationPath
|
||||
Me.UpdatePackagePath = updatePackagePath
|
||||
End Sub
|
||||
|
||||
Private Function CompileScript(script As Script) As CompilerResults
|
||||
Dim provider As CodeDomProvider = Nothing
|
||||
Dim params As New CompilerParameters
|
||||
|
||||
'Create code provider
|
||||
Select Case script.Language
|
||||
Case CodeLanguage.CSharp
|
||||
provider = New CSharpCodeProvider
|
||||
Case CodeLanguage.VB
|
||||
provider = New VBCodeProvider
|
||||
End Select
|
||||
|
||||
'Set general options
|
||||
params.GenerateExecutable = False
|
||||
params.GenerateInMemory = True
|
||||
|
||||
'Set references
|
||||
params.ReferencedAssemblies.Add()
|
||||
|
||||
'Compile
|
||||
Dim res As CompilerResults = provider.CompileAssemblyFromSource(params, script.Code)
|
||||
|
||||
Return res
|
||||
End Function
|
||||
|
||||
Public Function CheckScriptForErrors(script As Script) As CompilerErrorCollection
|
||||
Return CompileScript(script).Errors
|
||||
End Function
|
||||
|
||||
Public Sub ExecuteScript(script As Script)
|
||||
'Compile script
|
||||
Dim res As CompilerResults = CompileScript(script)
|
||||
|
||||
If Not res.Errors.HasErrors Then
|
||||
'Get Method
|
||||
Dim mi As MethodInfo = res.CompiledAssembly.GetType("Program")?.GetMethod("Main")
|
||||
|
||||
If mi IsNot Nothing Then
|
||||
'Create params
|
||||
Dim params As New Dictionary(Of String, String) From {
|
||||
{"ApplicationPath", ApplicationPath},
|
||||
{"UpdatePackagePath", UpdatePackagePath}
|
||||
}
|
||||
|
||||
'Execute method
|
||||
mi.Invoke(Nothing, {params})
|
||||
End If
|
||||
End If
|
||||
End Sub
|
||||
|
||||
End Class
|
||||
|
||||
End Namespace
|
||||
8
Pilz.Updating/Scripts/ScriptPriority.vb
Normal file
8
Pilz.Updating/Scripts/ScriptPriority.vb
Normal file
@@ -0,0 +1,8 @@
|
||||
Namespace Scripts
|
||||
|
||||
Public Enum ScriptPriority
|
||||
Before
|
||||
After
|
||||
End Enum
|
||||
|
||||
End Namespace
|
||||
Reference in New Issue
Block a user