Skip to content

Commit 366908f

Browse files
committed
core: add api run_in_script
1 parent 14fe6fd commit 366908f

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

core/common.lua

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function xlings_config_file_parse(filename)
5252
end
5353
}
5454
}
55-
55+
5656
if not file then
5757
cprint("${yellow}[xlings]: " .. err)
5858
os.sleep(1000)
@@ -105,19 +105,47 @@ function xlings_save_config_to_file(config, file)
105105
end
106106

107107
function xlings_exec(cmd)
108-
os.exec(platform.get_config_info().cmd_wrapper .. tostring(cmd))
108+
if is_host("windows") then
109+
xlings_run_in_script(cmd)
110+
else
111+
os.exec(cmd)
112+
end
113+
end
114+
115+
function xlings_run_in_script(content, use_adnim)
116+
if is_host("windows") then
117+
xlings_run_bat_script(content, use_adnim)
118+
else
119+
xlings_run_shell_script(content, use_adnim)
120+
end
109121
end
110122

111123
function xlings_run_bat_script(content, use_adnim) -- only for windows
112124
local script_file = path.join(platform.get_config_info().rcachedir, "win_tmp.bat")
113-
xlings_create_file_and_write(script_file, content)
125+
xlings_create_file_and_write(script_file, string.format([[
126+
@echo off
127+
128+
::dont modify this file, this file is generated by xlings
129+
130+
%s
131+
]], content or ""))
114132
if use_adnim then
115133
os.exec([[powershell -Command "Start-Process ']] .. script_file .. [[' -Verb RunAs -Wait]])
116134
else
117135
os.exec(script_file)
118136
end
119137
end
120138

139+
function xlings_run_shell_script(content, use_adnim) -- only for linux
140+
local script_file = path.join(platform.get_config_info().rcachedir, "linux_tmp.sh")
141+
xlings_create_file_and_write(script_file, content)
142+
if use_adnim then
143+
os.exec([[sudo bash ']] .. script_file .. [[']])
144+
else
145+
os.exec("bash " .. script_file)
146+
end
147+
end
148+
121149
function xlings_python(file)
122150
if os.host() == "windows" then
123151
file = file:gsub("/", "\\")
@@ -212,13 +240,13 @@ function xlings_path_format(path)
212240
path = tostring(path)
213241

214242
path = path:gsub("\\", "/")
215-
243+
216244
-- remove ./
217245
path = path:gsub("%./", "")
218-
246+
219247
-- remove ../
220248
--path = path:gsub("%.%.%/", "")
221-
249+
222250
-- /// -> /
223251
path = path:gsub("/+", "/")
224252

@@ -253,7 +281,7 @@ function get_linux_distribution()
253281
if os_release then
254282
local content = os_release:read("*a")
255283
os_release:close()
256-
284+
257285
local id = content:match('ID=["]*([^"\n]+)["]*')
258286
local version = content:match('VERSION_ID=["]*([^"\n]+)["]*')
259287
local name = content:match('PRETTY_NAME=["]*([^"\n]+)["]*')
@@ -263,7 +291,7 @@ function get_linux_distribution()
263291
name = name:lower()
264292
name = name:gsub("%s+%d[%d%.]*%s*.*$", "")
265293
end
266-
294+
267295
return {
268296
id = id or "unknown",
269297
version = version or "unknown",

core/d2x/actions/checker.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ function main(start_target, opt)
331331
if open_file_cmd_template then
332332
for _, file in ipairs((files)) do
333333
file = path.absolute(file)
334-
os.exec(platform.get_config_info().cmd_wrapper .. string.format(open_file_cmd_template, file))
334+
common.xlings_run_in_script(string.format(open_file_cmd_template, file))
335335
end
336336
end
337337

core/xim/libxpkg/system.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ end
2323

2424
function subos_sysrootdir()
2525
local osname = os.host()
26-
if is_host("linux") then
26+
if is_host("linux") then
2727
osname = "linux"
2828
end
2929
return path.join(platform.get_config_info().subosdir, osname)
@@ -58,6 +58,10 @@ function exec(cmd, opt)
5858
end
5959
end
6060

61+
function run_in_script(content, admin)
62+
common.run_in_script(content, admin)
63+
end
64+
6165
function unix_api()
6266
return {
6367
append_to_shell_profile = __unix_append_to_shell_profile,
@@ -104,4 +108,4 @@ function __unix_append_to_shell_profile(config)
104108
if config.fish then
105109
config_shell(fish_profile, config.fish)
106110
end
107-
end
111+
end

0 commit comments

Comments
 (0)