-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.go
More file actions
296 lines (292 loc) · 8.11 KB
/
Copy pathcommand.go
File metadata and controls
296 lines (292 loc) · 8.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package main
import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
)
func runExec(run Run, srcPath string, names Names) bool {
var cmds [][]string = [][]string{}
if osName == "windows" && run.Windows != nil && len(*run.Windows) > 0 {
cmds = *run.Windows
} else if osName == "linux" && run.Linux != nil && len(*run.Linux) > 0 {
cmds = *run.Linux
} else if osName == "darwin" && run.Darwin != nil && len(*run.Darwin) > 0 {
cmds = *run.Darwin
} else if run.Default != nil && len(*run.Default) > 0 {
cmds = *run.Default
} else {
log.Println("错误: 未找到适用于当前操作系统的命令。")
return false
}
srcPath = CleanPath(srcPath)
var cmdsLen int = len(cmds)
totalCMD++
var dir string = ""
// var cmdExist = false
for cmdsI, cmd := range cmds {
var noEmbCmd = false
var cmdLen int = len(cmd)
var customVariableKey string = ""
for cmdI, c := range cmd {
var nKey string = "$SRC"
c = CleanPath(c)
var pathArr []string = strings.Split(srcPath, string(filepath.Separator))
var pathArrLen = len(pathArr)
var fileFullName string = pathArr[pathArrLen-1]
var dirPath string = ""
if pathArrLen > 1 {
fileFullName = pathArr[pathArrLen-1]
pathArr = pathArr[:pathArrLen-1]
dirPath = strings.Join(pathArr, string(filepath.Separator))
}
var fileNameArr []string = strings.Split(fileFullName, ".")
var fileNameArrLen = len(fileNameArr)
var extName string = fileNameArr[fileNameArrLen-1]
var fileName string = fileNameArr[0]
if fileNameArrLen > 2 {
fileNameArr = fileNameArr[:fileNameArrLen-1]
fileName = strings.Join(fileNameArr, ".")
} else if fileNameArrLen == 1 {
extName = ""
}
nKey = "$SOLUTION"
if strings.Contains(c, nKey) {
c = strings.ReplaceAll(c, nKey, names.Solution)
}
nKey = "$PROJECT"
if strings.Contains(c, nKey) {
c = strings.ReplaceAll(c, nKey, names.Project)
}
nKey = "$JOBNAME"
if strings.Contains(c, nKey) {
c = strings.ReplaceAll(c, nKey, names.Replace)
}
nKey = "$SRCFILE"
if strings.Contains(c, nKey) {
c = strings.ReplaceAll(c, nKey, fileFullName)
}
nKey = "$SRCNAME"
if strings.Contains(c, nKey) {
c = strings.ReplaceAll(c, nKey, fileName)
}
nKey = "$SRCEXT"
if strings.Contains(c, nKey) {
if IsDirectory(srcPath) == 0 {
c = strings.ReplaceAll(c, nKey, extName)
} else {
c = strings.ReplaceAll(c, nKey, "")
}
}
nKey = "$SRCDIRNAME"
if strings.Contains(c, nKey) {
c = strings.ReplaceAll(c, nKey, pathArr[len(pathArr)-1])
}
nKey = "$SRCDIR"
if strings.Contains(c, nKey) {
c = strings.ReplaceAll(c, nKey, dirPath)
}
nKey = "$SRC"
if strings.Contains(c, nKey) {
c = strings.ReplaceAll(c, nKey, srcPath)
}
nKey = "$"
for key, val := range customVariables {
c = strings.ReplaceAll(c, nKey+key, val)
}
// fmt.Println("SRCFILE", fileFullName, "SRCNAME", fileName, "SRCEXT", extName, "SRCDIRNAME", pathArr[len(pathArr)-1], "SRCDIR", dirPath, "SRC", srcPath)
cmd[cmdI] = CleanPath(c)
}
if len(cmd) == 0 {
continue
}
log.Printf("运行命令 %d / %d : %s\n", cmdsI+1, cmdsLen, strings.Join(cmd, " "))
var err error = nil
var isOK bool = true
switch cmd[0] {
case "$CMDDIR":
if cmdLen == 1 {
dir = ""
} else if cmdLen >= 2 {
dir = cmd[1]
}
case "$BAK":
if cmdLen == 1 || (cmdLen == 2 && len(cmd[1]) == 0) {
isOK = backup(srcPath, names)
}
if cmdLen >= 2 {
isOK = backup(cmd[1], names)
}
case "$RES":
var resCmd Names = names
if cmdLen >= 2 && len(cmd[1]) > 0 {
resCmd.Solution = cmd[1]
}
if cmdLen >= 3 && len(cmd[2]) > 0 {
resCmd.Project = cmd[2]
}
if cmdLen >= 4 && len(cmd[3]) > 0 {
resCmd.Replace = cmd[3]
}
if cmdLen == 2 {
isOK = restoreSolution(resCmd.Solution)
} else if cmdLen >= 3 {
isOK = restoreProject(resCmd.Solution, resCmd.Project)
} else if cmdLen == 1 || cmdLen == 4 {
isOK = restoreJob(resCmd.Solution, resCmd.Project, resCmd.Replace)
}
case "$MD":
if cmdLen >= 2 {
err = MakeDirectory(cmd[1])
}
case "$CP":
if cmdLen >= 3 {
err = Copy(cmd[1], cmd[2])
} else if cmdLen == 2 {
err = Copy(srcPath, cmd[1])
}
case "$MV":
if cmdLen >= 3 {
err = Move(cmd[1], cmd[2])
} else if cmdLen == 2 {
err = Move(srcPath, cmd[1])
}
case "$SMV":
if cmdLen >= 3 {
err = MoveSecure(cmd[1], cmd[2])
} else if cmdLen == 2 {
err = MoveSecure(srcPath, cmd[1])
}
case "$RM":
err = Remove(cmd[1])
case "$SRM":
err = RemoveSecure(cmd[1])
case "$REN":
if cmdLen >= 3 {
err = RenamePath(cmd[1], cmd[2])
} else if cmdLen == 2 {
err = RenamePath(srcPath, cmd[1])
}
// case "$ZHCODECONV":
// var lenCh [2]int
// lenCh, err = zhcodeconv.InitWithCmd(cmd, srcPath)
// log.Printf("非 ASCII 变量和函数名转换: %s (%d B -> %d B)\n", srcPath, lenCh[0], lenCh[1])
// case "$MINIFY":
// var lenCh [2]int
// lenCh, err = minify.InitWithCmd(cmd, srcPath)
// log.Printf("代码压缩: %s (%d B -> %d B)\n", srcPath, lenCh[0], lenCh[1])
case "$UNSET":
if cmdLen >= 2 {
if v, ok := customVariables[cmd[1]]; ok {
delete(customVariables, cmd[1])
log.Printf("删除变量: %s (%d B) 总变量数: %d\n", cmd[1], len(v), len(customVariables))
}
}
case "$SET":
if cmdLen >= 3 {
customVariables[cmd[1]] = cmd[2]
}
case "$CMDSET":
if cmdLen >= 3 {
customVariableKey = cmd[1]
}
noEmbCmd = true
case "$RUNSLN":
if cmdLen >= 2 {
cmd[0] = osExecFile[3] + string(filepath.Separator) + osExecFile[1] + osExecFile[2]
var nowExeArr []string = strings.Split(cmd[1], string(filepath.Separator))
var endIndex int = len(nowExeArr) - 1
dir = strings.Join(nowExeArr[:endIndex], string(filepath.Separator))
cmd = []string{cmd[0], nowExeArr[endIndex], "-nr"}
noEmbCmd = true
}
default:
if len(cmd[0]) > 1 && cmd[0][0] == '$' {
var newCmd string = string(filepath.Separator) + osExecFile[1] + "_" + cmd[0][1:] + osExecFile[2]
if Exists(osExecFile[0] + newCmd) {
cmd[0] = osExecFile[0] + newCmd
// cmdExist = true
} else {
cmd[0] = osExecFile[3] + newCmd
}
}
noEmbCmd = true
}
if err != nil {
log.Printf("错误: 文件操作 %s 失败: %s\n", cmd[0], err)
return false
} else if !isOK {
return false
}
if noEmbCmd {
if len(customVariableKey) > 0 {
cmd = cmd[2:]
}
// if !cmdExist {
// if !Exists(cmd[0]) {
// log.Printf("错误: 要执行的外部命令 %s 不存在: %s\n", cmd[0], err)
// return false
// }
// }
if !runCMD(cmd, dir, customVariableKey) {
return false
}
}
}
return true
}
func runCMD(cmd []string, dir string, customVariableKey string) bool {
totalEXE++
var ex *exec.Cmd = exec.Command(cmd[0], cmd[1:]...)
if len(dir) > 0 {
ex.Dir = dir
}
var err error = nil
var out bytes.Buffer = bytes.Buffer{}
// var stderr bytes.Buffer
if len(customVariableKey) > 0 {
ex.Stdout = &out
// ex.Stderr = &stderr
ex.Stderr = os.Stderr
err = ex.Run()
if err == nil {
customVariables[customVariableKey] = out.String()
// log.Printf("命令结果保存到变量: %s (%d B) 总变量数: %d", customVariableKey, len(customVariables[customVariableKey]), len(customVariables))
}
} else {
ex.Stdout = os.Stdout
ex.Stderr = os.Stderr
err = ex.Run()
}
if err != nil {
log.Println("错误:执行命令失败:", err)
// if stderr.Len() > 0 {
// log.Println(stderr.String())
// }
return false
} else if len(customVariableKey) > 0 {
var outStr string = out.String()
if len(outStr) == 0 {
log.Println("警告: 命令没有输出,变量为空。")
} else {
log.Printf("已设置变量 %s 为: %s (%d B) 总变量数: %d", customVariableKey, trimString(outStr), len(outStr), len(customVariables))
}
}
if exitError, ok := err.(*exec.ExitError); ok {
if status, ok := exitError.Sys().(syscall.WaitStatus); ok {
fmt.Printf("错误: 命令退出代码: %d\n", status.ExitStatus())
// if stderr.Len() > 0 {
// log.Println(stderr.String())
// }
return false
}
} else {
log.Println("命令运行成功。")
return true
}
return true
}