1 | #Region "Microsoft.VisualBasic::1472354ab6a5d2b672d99b8df06c3298, Microsoft.VisualBasic.Core\Scripting\ExternalCall.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 | ' Class ExternalCall |
35 | ' |
36 | ' Constructor: (+1 Overloads) Sub New |
37 | ' Function: buildArguments, Run, Shell, ToString |
38 | ' |
39 | ' Structure ShellValue |
40 | ' |
41 | ' Constructor: (+1 Overloads) Sub New |
42 | ' Function: GetObject, ToString |
43 | ' |
44 | ' |
45 | ' /********************************************************************************/ |
46 | |
47 | #End Region |
48 | |
49 | Imports System.Collections.Specialized |
50 | Imports System.Runtime.CompilerServices |
51 | Imports Microsoft.VisualBasic.ApplicationServices |
52 | Imports Microsoft.VisualBasic.CommandLine |
53 | Imports Microsoft.VisualBasic.Serialization.JSON |
54 | Imports Microsoft.VisualBasic.Text |
55 | |
56 | Namespace Scripting |
57 | |
58 | ''' <summary> |
59 | ''' Shell object for the external script running. |
60 | ''' </summary> |
61 | Public Class ExternalCall |
62 | |
63 | ''' <summary> |
64 | ''' 脚本宿主的可执行文件的路径 |
65 | ''' </summary> |
66 | ReadOnly __host As String |
67 | ReadOnly __ext As String |
68 | |
69 | ''' <summary> |
70 | ''' |
71 | ''' </summary> |
72 | ''' <param name="host">The program its file name to run the script</param> |
73 | ''' <param name="ext">File extension name of this type of script</param> |
74 | Sub New(host As String, Optional ext As String = ".txt") |
75 | __host = FileIO.FileSystem.GetFileInfo(host).FullName |
76 | __ext = ext |
77 | End Sub |
78 | |
79 | ''' <summary> |
80 | ''' |
81 | ''' </summary> |
82 | ''' <param name="script">The script content</param> |
83 | ''' <param name="args"></param> |
84 | ''' <returns></returns> |
85 | Public Function Run(script As String, Optional args As NameValueCollection = Nothing) As ShellValue |
86 | Dim tmp As String = App.GetAppSysTempFile(__ext) |
87 | Call script.SaveTo(tmp, Encodings.ASCII.CodePage) |
88 | Return Shell(path:=tmp, args:=args) |
89 | End Function |
90 | |
91 | ''' <summary> |
92 | ''' |
93 | ''' </summary> |
94 | ''' <param name="path">The script file path</param> |
95 | ''' <param name="args"></param> |
96 | ''' <returns></returns> |
97 | ''' <remarks>Perl脚本测试通过!</remarks> |
98 | Public Function Shell(path As String, Optional args As NameValueCollection = Nothing) As ShellValue |
99 | Dim param As String = buildArguments(args) |
100 | Dim IO As New IORedirect(__host, path & " " & param) |
101 | Dim code As Integer = IO.Start(WaitForExit:=True) |
102 | Return New ShellValue(IO, code) |
103 | End Function |
104 | |
105 | Private Shared Function buildArguments(args As NameValueCollection) As String |
106 | If args Is Nothing Then |
107 | Return "" |
108 | Else |
109 | Return args _ |
110 | .AllKeys _ |
111 | .Select(Function(s) |
112 | Return $"{s} {args.Get(s).CLIToken}" |
113 | End Function) _ |
114 | .JoinBy(" ") |
115 | End If |
116 | End Function |
117 | |
118 | Public Overrides Function ToString() As String |
119 | Return __host |
120 | End Function |
121 | End Class |
122 | |
123 | ''' <summary> |
124 | ''' Script shell result. |
125 | ''' </summary> |
126 | Public Structure ShellValue |
127 | ''' <summary> |
128 | ''' Standard output on the console |
129 | ''' </summary> |
130 | Public STD_OUT As String |
131 | ''' <summary> |
132 | ''' Standard error |
133 | ''' </summary> |
134 | Public STD_ERR As String |
135 | ''' <summary> |
136 | ''' Process exit code |
137 | ''' </summary> |
138 | Public state As Integer |
139 | |
140 | Sub New(io As IORedirect, exitCode As Integer) |
141 | state = exitCode |
142 | STD_OUT = io.StandardOutput |
143 | STD_ERR = io.GetError |
144 | End Sub |
145 | |
146 | ''' <summary> |
147 | ''' Parsing object from the standard output |
148 | ''' </summary> |
149 | ''' <typeparam name="T"></typeparam> |
150 | ''' <param name="parser">Object parser</param> |
151 | ''' <returns></returns> |
152 | ''' |
153 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
154 | Public Function GetObject(Of T)(parser As Func(Of String, T)) As T |
155 | Return parser(STD_OUT) |
156 | End Function |
157 | |
158 | Public Overrides Function ToString() As String |
159 | Return Me.GetJson |
160 | End Function |
161 | End Structure |
162 | End Namespace |