-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathluaPath.ts
More file actions
168 lines (149 loc) · 6.14 KB
/
luaPath.ts
File metadata and controls
168 lines (149 loc) · 6.14 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
import * as os from 'os';
export class LuaPath {
// 获取设置的luaPath。如果是windows平台,若路径中包含空格,路径用字符串包括。
// 例如:D:\Program Files\lua-5.4.2_Win64_bin\lua54.exe 替换为 D:\Program Files\"lua-5.4.2_Win64_bin"\lua54.exe
private getSetLuaPath(strPath: string): string {
let retStr: string = strPath;
let platform: string = os.platform();
if (platform === "linux" || platform === "darwin") {
return retStr;
}
// windows平台特殊处理
let path = require("path");
let pathArr = strPath.split(path.sep);
strPath = pathArr.join('/');
pathArr = strPath.split("/");
for (var i = 1; i < pathArr.length - 1; i++) {
if (pathArr[i].indexOf(" ") > 0) {
pathArr[i] = "\"" + pathArr[i] + "\"";
}
}
let stdPath = pathArr.join('/');
return stdPath;
}
// 获取lua的版本号
// 返回的版本号为5.1, 5.2, 5.3, 5.4
private getLuaBinVersion(strCmd: string): string {
const { execSync } = require('child_process');
try {
let out = execSync(strCmd, {}, (err, stdout, stderr) => {
if (err) {
console.log(err);
return "";
}
console.log(`stdout: ${stdout}`);
});
let str = out.toString("utf-8").replace('\n', '');
if (str.indexOf("Lua 5.1") >= 0) {
return "5.1";
}
if (str.indexOf("Lua 5.2") >= 0) {
return "5.2";
}
if (str.indexOf("Lua 5.3") >= 0) {
return "5.3";
}
if (str.indexOf("Lua 5.4") >= 0) {
return "5.4";
}
return "";
} catch (e) {
//console.error(e);
//throw(e);
return "";
}
}
// 获取插件自带的lua二进制完整路径
private getLuaExePathStr(vSCodeExtensionPath: string, luaVersionStr: string): string {
let path = require("path");
let pathArr = vSCodeExtensionPath.split(path.sep);
let stdPath = pathArr.join('/');
let retStr: string = "";
let suffixStr: string = "";
let platform: string = os.platform();
let arch = os.machine();
switch (platform) {
case "win32":
let suffixVerStr: string = luaVersionStr.replace(".", "");
suffixStr = "/debugger/luasocket/win/x64/lua" + luaVersionStr + "/" + "lua" + suffixVerStr + ".exe";
break;
case "linux":
suffixStr = "/debugger/luasocket/linux/lua" + luaVersionStr + "/lua";
break;
case "darwin":
if (arch === "arm64") {
suffixStr = "/debugger/luasocket/mac/arm64/lua" + luaVersionStr + "/lua";
} else {
suffixStr = "/debugger/luasocket/mac/x64/lua" + luaVersionStr + "/lua";
}
break;
}
retStr = stdPath + suffixStr;
return retStr;
}
// 获取插件自带的socket库的cpath路径
private getLuaCPathStr(vSCodeExtensionPath: string, luaVersionStr: string): string {
let path = require("path");
let pathArr = vSCodeExtensionPath.split(path.sep);
let stdPath = pathArr.join('/');
let retStr: string = "";
let suffixStr: string = "";
let platform: string = os.platform();
let arch = os.machine();
switch (platform) {
case "win32":
suffixStr = "/debugger/luasocket/win/x64/lua" + luaVersionStr + "/?.dll";
break;
case "linux":
suffixStr = "/debugger/luasocket/linux/lua" + luaVersionStr + "/?.so";
break;
case "darwin":
if (arch === "arm64") {
suffixStr = "/debugger/luasocket/mac/arm64/lua" + luaVersionStr + "/?.so";
} else {
suffixStr = "/debugger/luasocket/mac/x64/lua" + luaVersionStr + "/?.so";
}
break;
}
retStr = stdPath + suffixStr;
return retStr;
}
// 获取用户lua 二进制位置与cpath库的后缀路径
// strPath 为传入的用户设置的lua二进制位置
// vSCodeExtensionPath 为插件运行的目录
public GetLuaExeCpathStr(strPath: string, vSCodeExtensionPath: string): string[] {
let strVect: string[] = ["", ""];
let strVer: string = "";
if (strPath === "") {
// 用户没有额外设置lua的二进制位置,获取默认的版本Lua版本号
let luaVersionStr = this.getLuaBinVersion("lua -v");
if (luaVersionStr === "") {
// 没有找到默认的lua二进制,利用插件自带的二进制版本
let newVSCodeExtensionPath: string = this.getSetLuaPath(vSCodeExtensionPath);
strVect[0] = this.getLuaExePathStr(newVSCodeExtensionPath, "5.4");
strVer = "5.4";
} else {
// 找到了默认的lua 二进制
strVect[0] = "lua";
strVer = luaVersionStr;
}
} else {
// 用户设置了lua二进制的位置
let strNewPath: string = this.getSetLuaPath(strPath);
let luaVersionStr = this.getLuaBinVersion(strNewPath + " -v");
if (luaVersionStr === "") {
// 没有找到默认的lua二进制,利用插件自带的二进制版本
let newVSCodeExtensionPath: string = this.getSetLuaPath(vSCodeExtensionPath);
strVect[0] = this.getLuaExePathStr(newVSCodeExtensionPath, "5.4");
strVer = "5.4";
} else {
// 找到
strVect[0] = strNewPath;
strVer = luaVersionStr;
}
}
//let newVSCodeExtensionPath: string = this.getSetLuaPath(vSCodeExtensionPath);
strVect[1] = this.getLuaCPathStr(vSCodeExtensionPath, strVer);
return strVect;
}
}