Skip to content

Commit

Permalink
xrun: update xrun module
Browse files Browse the repository at this point in the history
1. optimize xrun template for plang
2. rename from mini_run to xrun
3. only call xrun's main function in xlings

Signed-off-by: sunrisepeak <speakshen@163.com>
  • Loading branch information
Sunrisepeak committed Dec 22, 2024
1 parent f12bd62 commit eb1b31d
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 39 deletions.
24 changes: 10 additions & 14 deletions core/checker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,23 @@ function main(start_target)

common.xlings_clear_screen()

-- xlings project's cache dir(xmake.lua)
local base_dir = os.projectdir()
os.cd(base_dir) -- avoid other module's side effect

local config = platform.get_config_info()
local detect_dir = config.rundir
local detect_recursion = false
if config.name ~= "xlings_name" then -- is d2x project
detect_dir = detect_dir .. "/" .. config.name
detect_recursion = true
else -- is mini_run
else -- is xrun - TODO: optimize
--detect_recursion = false
local files = project.targets()[start_target]:sourcefiles()
local file = nil
for _, f in ipairs(files) do file = f break end
detect_dir = path.directory(file)
if #files > 0 then
local file = path.absolute(files[1])
detect_dir = path.directory(file)
end
end
fwatcher.add(detect_dir, {recursion = detect_recursion})
--cprint("Watching directory: ${magenta}" .. detect_dir .. "${clear}")
Expand All @@ -186,11 +191,6 @@ function main(start_target)
-- TODO3: support more editor?
--end

local base_dir = os.projectdir()

-- xlings project's cache dir(xmake.lua)
-- print(base_dir) -- /home/speak/workspace/github/d2ds/.xlings

-- 获取 project 中所有 target
local targets = project.targets()
--local total_targets = #targets --
Expand Down Expand Up @@ -276,11 +276,7 @@ function main(start_target)
if platform.get_config_info().editor == "vscode" then
if open_target_file == false then
for _, file in ipairs((files)) do
if os.host() == "windows" then
--file = platform.get_config_info().rundir .. "\\" .. common.xlings_path_format(file)
else
-- why work?
end
file = path.absolute(file)
os.exec(platform.get_config_info().cmd_wrapper .. "code -g " .. file .. ":1")
end
open_target_file = true
Expand Down
4 changes: 2 additions & 2 deletions core/templates/c_language.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ target("clings-demo")
add_files("tests/clings.c")
]]

--- mini run config template
--- xrun config template

_xmake_file_template = [[
xlings_editor = "vscode"
xlings_runmode = "loop"
xlings_lang = "%s" -- 0.lang
local target_name = "%s" -- 1.name
local target_sourcefile = "../%s" -- 2.sourcefile
local target_sourcefile = "%s" -- 2.sourcefile
--xlings_name = target_sourcefile
target(target_name) -- 1.name
Expand Down
4 changes: 2 additions & 2 deletions core/templates/cpp_language.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ target("cpplings-demo")
add_files("tests/cpplings.cpp")
]]

--- mini run config template
--- xrun config template

_xmake_file_template = [[
xlings_editor = "vscode"
xlings_runmode = "loop"
xlings_lang = "%s" -- 0.lang
local target_name = "%s" -- 1.name
local target_sourcefile = "../%s" -- 2.sourcefile
local target_sourcefile = "%s" -- 2.sourcefile
--xlings_name = target_sourcefile
target(target_name) -- 1.name
Expand Down
7 changes: 3 additions & 4 deletions core/templates/py_language.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ target("pylings-demo")
end)
]]

--- mini run config template
--- xrun config template

_xmake_file_template = [[
xlings_editor = "vscode"
Expand All @@ -42,11 +42,10 @@ local target_sourcefile = "%s" -- 2.sourcefile
target(target_name)
set_kind("phony")
add_files("../" .. target_sourcefile)
add_files(target_sourcefile)
on_run(function (target)
import("common")
local rundir = path.directory(os.scriptdir())
common.xlings_python(path.join(rundir, target_sourcefile))
common.xlings_python(target_sourcefile)
end)
includes("%s") -- 3.xlings_file
Expand Down
18 changes: 7 additions & 11 deletions core/xlings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import("common")
import("checker")
import("init")
import("drepo")
import("mini_run")
import("config")

local xinstall = import("xim.xim")
local xrun = import("xrun.xrun")

function xlings_help()
cprint("${bright}xlings version:${clear} pre-v0.0.2")
Expand Down Expand Up @@ -54,7 +54,7 @@ function deps_check_and_install(xdeps)
version = nil, -- TODO: support version
}
cprint("${dim}---${clear}")
xinstall("-i", name, "-y", unpack(cmd_args))
xinstall("-i", name, "-y", "--disable-info", unpack(cmd_args))
end
end

Expand Down Expand Up @@ -142,21 +142,17 @@ function main()

-- TODO: optimize auto-deps install - xinstall(xx)
if command == "checker" or command == xname then
if xlings_editor then xinstall(xlings_editor, "-y") end
xinstall(xlings_lang, "-y")
if xlings_editor then xinstall(xlings_editor, "-y", "--disable-info") end
xinstall(xlings_lang, "-y", "--disable-info")
checker.main(cmd_target) -- TODO -s cmd_target
elseif command == "run" then
if os.isfile(path.join(run_dir, cmd_target)) then
mini_run(cmd_target)
else
cprint("[xlings]: ${red}file not found${clear} - " .. cmd_target)
end
xrun(cmd_target)
elseif command == "init" then
xinstall("mdbook", "-y")
xinstall("mdbook", "-y", "--disable-info")
init.xlings_init(xname, xlings_lang)
elseif command == "book" then
--os.exec("mdbook build --open book") -- book is default folder
xinstall("mdbook", "-y")
xinstall("mdbook", "-y", "--disable-info")
os.exec("mdbook serve --open " .. platform.get_config_info().bookdir) -- book is default folder
elseif command == "update" then
common.xlings_update(xname, xlings_lang)
Expand Down
19 changes: 13 additions & 6 deletions core/mini_run.lua → core/xrun/xrun.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,14 @@ function detect_file_type(source_file)
end
end

function generate_config(target_name, source_file)
function generate_config(target_name, target_sourcefile)

local config = platform.get_config_info()
local target_sourcefile = common.xlings_path_format(source_file)
local xlings_file = config.install_dir .. "/core/xmake.lua"

local file_type = detect_file_type(source_file)
local file_type = detect_file_type(target_sourcefile)
local template = templates_interface.get_template(file_type)
local xmake_file_path = config.cachedir .. "/xmake.lua"

--cprint(template.xmake_file_template)

local content = string.format(
template.xmake_file_template,
file_type,
Expand All @@ -43,6 +39,17 @@ function generate_config(target_name, source_file)
end

function main(source_file)

if os.isfile(source_file) then
source_file = path.absolute(source_file)
else
source_file = path.join(platform.get_config_info().rundir, source_file)
if not os.isfile(source_file) then
cprint("[xlings]: ${red}file not found${clear} - " .. source_file)
return
end
end

local target_name = path.basename(source_file)
generate_config(target_name, source_file)
--os.exec("pwd")
Expand Down

0 comments on commit eb1b31d

Please sign in to comment.