| 1 | #Region "Microsoft.VisualBasic::853388d640c5306b7d2e55cc81669b5f, Microsoft.VisualBasic.Core\CommandLine\CLI\Scripting.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 ScriptingExtensions |
| 35 | ' |
| 36 | ' Function: Bash, Cmd |
| 37 | ' |
| 38 | ' Sub: start |
| 39 | ' |
| 40 | ' |
| 41 | ' /********************************************************************************/ |
| 42 | |
| 43 | #End Region |
| 44 | |
| 45 | Imports System.Runtime.CompilerServices |
| 46 | Imports System.Text |
| 47 | Imports Microsoft.VisualBasic.ApplicationServices |
| 48 | Imports ValueTuple = System.Collections.Generic.KeyValuePair(Of String, String) |
| 49 | |
| 50 | Namespace CommandLine |
| 51 | |
| 52 | Module ScriptingExtensions |
| 53 | |
| 54 | Public Function Cmd(program$, argv$, environment As IEnumerable(Of ValueTuple), folkNew As Boolean, stdin$) As String |
| 55 | Dim bat As New StringBuilder(1024) |
| 56 | |
| 57 | ' 切换工作目录至当前的进程所处的文件夹 |
| 58 | Dim Drive As String = FileIO.FileSystem _ |
| 59 | .GetDirectoryInfo(App.CurrentDirectory) _ |
| 60 | .Root _ |
| 61 | .Name _ |
| 62 | .Replace("\", "") _ |
| 63 | .Replace("/", "") |
| 64 | |
| 65 | Call bat.AppendLine("@echo off") |
| 66 | Call bat.AppendLine(Drive) |
| 67 | Call bat.AppendLine("CD " & App.CurrentDirectory.CLIPath) |
| 68 | |
| 69 | If Not environment Is Nothing Then |
| 70 | ' 写入临时的环境变量 |
| 71 | For Each para As ValueTuple In environment |
| 72 | Call bat.AppendLine($"set {para.Key}={para.Value}") |
| 73 | Next |
| 74 | End If |
| 75 | |
| 76 | Call bat.AppendLine() |
| 77 | |
| 78 | If folkNew Then |
| 79 | ' 在新的窗口打开 |
| 80 | Call bat.AppendLine($"start ""{program}"" {argv}") |
| 81 | Else |
| 82 | ' 生成IO重定向的命令行 |
| 83 | ' https://stackoverflow.com/questions/3680977/can-a-batch-file-capture-the-exit-codes-of-the-commands-it-is-invoking |
| 84 | Call bat.AppendLine("set errorlevel=") |
| 85 | Call bat.start(program, argv, stdin) |
| 86 | Call bat.AppendLine("exit /b %errorlevel%") |
| 87 | End If |
| 88 | |
| 89 | Return bat.ToString |
| 90 | End Function |
| 91 | |
| 92 | <Extension> |
| 93 | Private Sub start(script As StringBuilder, program$, argv$, stdin$) |
| 94 | If stdin.StringEmpty Then |
| 95 | Call script.AppendLine($"""{program}"" {argv}") |
| 96 | Else |
| 97 | Call script.AppendLine($"echo {stdin} | ""{program}"" {argv}") |
| 98 | End If |
| 99 | End Sub |
| 100 | |
| 101 | Public Function Bash(program$, argv$, environment As IEnumerable(Of ValueTuple), folkNew As Boolean, stdin$) As String |
| 102 | Dim shell As New StringBuilder("#!/bin/bash") |
| 103 | |
| 104 | Call shell.AppendLine() |
| 105 | Call shell.AppendLine() |
| 106 | |
| 107 | If Not environment Is Nothing Then |
| 108 | For Each param As ValueTuple In environment |
| 109 | ' To set it for current shell And all processes started from current shell |
| 110 | ' shorter, less portable version |
| 111 | ' export VARNAME="my value" |
| 112 | Call shell.AppendLine($"export {param.Key}=""{param.Value}""") |
| 113 | Next |
| 114 | End If |
| 115 | |
| 116 | Call shell.AppendLine($"cd {App.CurrentDirectory.CLIPath}") |
| 117 | Call shell.start(program, argv, stdin) |
| 118 | |
| 119 | Return shell.ToString |
| 120 | End Function |
| 121 | End Module |
| 122 | End Namespace |