1 #Region "Microsoft.VisualBasic::a7c52218321e230cbb462fe66a5867a5, Microsoft.VisualBasic.Core\Extensions\CodeDOM\VBC.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Module VBC
35     
36     '         Function: (+2 Overloads) CompileCode, (+2 Overloads) CreateParameters
37     
38     
39     ' /********************************************************************************/
40
41 #End Region
42
43 Imports System.CodeDom.Compiler
44 Imports System.Reflection
45 Imports System.Text
46 Imports Microsoft.VisualBasic.Language
47
48 Namespace Emit.CodeDOM_VBC
49
50     ''' <summary>
51     ''' Extension wrappers for VisualBasic compiler
52     ''' </summary>
53     ''' <remarks>
54     ''' https://www.codeproject.com/articles/113169/c-as-a-scripting-language-in-your-net-applications
55     ''' </remarks>
56     Public Module VBC
57
58         ''' <summary>
59         ''' Construct of the ``vbc.exe`` compiler parameters <see cref="CompilerParameters"/>.
60         ''' </summary>
61         ''' <param name="ref"></param>
62         ''' <param name="SDK"></param>
63         ''' <param name="dll"></param>
64         ''' <returns></returns>
65         Public Function CreateParameters(ref As IEnumerable(Of String), Optional SDK$ = net46Default, Optional dll As Boolean = TrueAs CompilerParameters
66             Dim args As CompilerParameters = If(dll, DllProfile, ExecutableProfile)
67             Dim libs As New List(Of String)
68
69             libs += From path As String
70                     In ref
71                     Where Array.IndexOf(DotNETFramework, path.BaseName) = -1
72                     Select path '             libs += {
73                 SDK & "\System.dll",
74                 SDK & "\System.Core.dll",
75                 SDK & "\System.Data.dll",
76                 SDK & "\System.Data.DataSetExtensions.dll",
77                 SDK & "\System.Xml.dll",
78                 SDK & "\System.Xml.Linq.dll"
79             }
80             Call args.ReferencedAssemblies.AddRange(libs)
81             Return args
82         End Function
83
84         ''' <summary>
85         ''' <see cref="App.References"/>
86         ''' </summary>
87         ''' <param name="SDK$"></param>
88         ''' <param name="dll"></param>
89         ''' <returns></returns>
90         Public Function CreateParameters(references As String(), Optional SDK$ = net46Default, Optional dll As Boolean = TrueAs CompilerParameters
91             Return CreateParameters(ref:=references, dll:=dll, SDK:=SDK)
92         End Function
93
94         ''' <summary>
95         ''' 
96         ''' </summary>
97         ''' <param name="code">VisualBasic源代码</param>
98         ''' <param name="output">The output ``*.exe`` file.</param>
99         ''' <returns></returns>
100         Public Function CompileCode(code As String, output As StringOptional ByRef errInfo As String = ""As Assembly
101             Dim params As New CompilerParameters() With { ' Make sure we generate an EXE, not a DLL
102                 .GenerateExecutable = True,
103                 .OutputAssembly = output
104             }
105
106             Return VBC.CompileCode(code, params, errInfo)
107         End Function
108
109         ''' <summary>
110         ''' If the code compile failure, then this function will returns nothing.
111         ''' </summary>
112         ''' <param name="code">VisualBasic源代码</param>
113         ''' <returns><see cref="Assembly"/> from the source <paramref name="code"/></returns>
114         Public Function CompileCode(code As String, args As CompilerParameters, Optional ByRef errInfo As String = ""As Assembly
115             Dim codeProvider As New VBCodeProvider()
116 #Disable Warning
117             Dim icc As ICodeCompiler = codeProvider.CreateCompiler
118 #Enable Warning
119             Dim results As CompilerResults = icc.CompileAssemblyFromSource(args, code)
120
121             If results.Errors.Count > 0 Then   ' There were compiler errors
122                 Dim err As New StringBuilder("There were compiler errors:")
123                 Call err.AppendLine()
124                 Call err.AppendLine()
125
126                 For Each CompErr As CompilerError In results.Errors
127                     Dim errDetail As String = "Line number " & CompErr.Line &
128                         ", Error Number: " & CompErr.ErrorNumber &
129                         ", '" & CompErr.ErrorText & ";"
130
131                     Call err.AppendLine(errDetail)
132                     Call err.AppendLine()
133                 Next
134
135                 errInfo = err.ToString
136
137                 Return Nothing
138             Else
139                 ' Successful Compile
140                 Return results.CompiledAssembly
141             End If
142         End Function
143     End Module
144 End Namespace