microsoft-visualbasic-runtime/CommandLine/CLI/Scripting.vb

131 lines
4.7 KiB
VB.net
Raw Normal View History

2020-01-02 19:08:37 +08:00
#Region "Microsoft.VisualBasic::59b88f692c6293c9bfc2b7e7af2d6e63, Microsoft.VisualBasic.Core\CommandLine\CLI\Scripting.vb"
2018-08-02 20:14:48 +08:00
' Author:
'
' asuka (amethyst.asuka@gcmodeller.org)
' xie (genetics@smrucc.org)
' xieguigang (xie.guigang@live.com)
'
' Copyright (c) 2018 GPL3 Licensed
'
'
' GNU GENERAL PUBLIC LICENSE (GPL3)
'
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program. If not, see <http://www.gnu.org/licenses/>.
' /********************************************************************************/
' Summaries:
' Module ScriptingExtensions
'
' Function: Bash, Cmd
'
' Sub: start
'
'
' /********************************************************************************/
#End Region
Imports System.Runtime.CompilerServices
Imports System.Text
Imports Microsoft.VisualBasic.ApplicationServices
Imports ValueTuple = System.Collections.Generic.KeyValuePair(Of String, String)
Namespace CommandLine
Module ScriptingExtensions
2019-11-15 23:01:04 +08:00
Public Function Cmd(program$, argv$, environment As IEnumerable(Of ValueTuple), folkNew As Boolean, stdin$, isShellCommand As Boolean) As String
2018-08-02 20:14:48 +08:00
Dim bat As New StringBuilder(1024)
' 切换工作目录至当前的进程所处的文件夹
Dim Drive As String = FileIO.FileSystem _
.GetDirectoryInfo(App.CurrentDirectory) _
.Root _
.Name _
.Replace("\", "") _
.Replace("/", "")
Call bat.AppendLine("@echo off")
Call bat.AppendLine(Drive)
Call bat.AppendLine("CD " & App.CurrentDirectory.CLIPath)
If Not environment Is Nothing Then
' 写入临时的环境变量
For Each para As ValueTuple In environment
Call bat.AppendLine($"set {para.Key}={para.Value}")
Next
End If
Call bat.AppendLine()
If folkNew Then
' 在新的窗口打开
Call bat.AppendLine($"start ""{program}"" {argv}")
Else
' 生成IO重定向的命令行
' https://stackoverflow.com/questions/3680977/can-a-batch-file-capture-the-exit-codes-of-the-commands-it-is-invoking
Call bat.AppendLine("set errorlevel=")
2019-11-15 23:01:04 +08:00
Call bat.start(program, argv, stdin, isShellCommand)
2018-08-02 20:14:48 +08:00
Call bat.AppendLine("exit /b %errorlevel%")
End If
Return bat.ToString
End Function
<Extension>
2019-11-15 23:01:04 +08:00
Private Sub start(script As StringBuilder, program$, argv$, stdin$, isShellCommand As Boolean)
2018-08-02 20:14:48 +08:00
If stdin.StringEmpty Then
2019-11-15 23:01:04 +08:00
If isShellCommand Then
Call script.AppendLine($"{program} {argv}")
Else
Call script.AppendLine($"""{program}"" {argv}")
End If
2018-08-02 20:14:48 +08:00
Else
2019-11-15 23:01:04 +08:00
If isShellCommand Then
Call script.AppendLine($"echo {stdin} | {program} {argv}")
Else
Call script.AppendLine($"echo {stdin} | ""{program}"" {argv}")
End If
2018-08-02 20:14:48 +08:00
End If
End Sub
2019-11-15 23:01:04 +08:00
Public Function Bash(program$, argv$, environment As IEnumerable(Of ValueTuple), folkNew As Boolean, stdin$, isShellCommand As Boolean) As String
2018-08-02 20:14:48 +08:00
Dim shell As New StringBuilder("#!/bin/bash")
Call shell.AppendLine()
Call shell.AppendLine()
If Not environment Is Nothing Then
For Each param As ValueTuple In environment
' To set it for current shell And all processes started from current shell
' shorter, less portable version
' export VARNAME="my value"
Call shell.AppendLine($"export {param.Key}=""{param.Value}""")
Next
End If
Call shell.AppendLine($"cd {App.CurrentDirectory.CLIPath}")
2019-11-15 23:01:04 +08:00
Call shell.start(program, argv, stdin, isShellCommand)
2018-08-02 20:14:48 +08:00
Return shell.ToString
End Function
End Module
End Namespace