mirror of
https://github.com/leafo/moonscript.git
synced 2025-01-09 00:04:22 +00:00
path_to_target
This commit is contained in:
parent
a4e03813fc
commit
5904373ab8
43
bin/moonc
43
bin/moonc
@ -68,6 +68,7 @@ local parse_dir = moonc.parse_dir
|
||||
local parse_file = moonc.parse_file
|
||||
local convert_path = moonc.convert_path
|
||||
local compile_and_write = moonc.compile_and_write
|
||||
local path_to_target = moonc.path_to_target
|
||||
|
||||
local function scan_directory(root, collected)
|
||||
root = normalize_dir(root)
|
||||
@ -105,48 +106,22 @@ local function remove_dups(tbl, key_fn)
|
||||
return final
|
||||
end
|
||||
|
||||
local function is_abs_path(path)
|
||||
local first = path:sub(1, 1)
|
||||
if dirsep == "\\" then
|
||||
return first == "/" or first == "\\" or path:sub(2,1) == ":"
|
||||
else
|
||||
return first == dirsep
|
||||
end
|
||||
end
|
||||
|
||||
-- creates tuples of input and target
|
||||
local function get_files(fname, files)
|
||||
files = files or {}
|
||||
|
||||
if lfs.attributes(fname, "mode") == "directory" then
|
||||
local head = fname:match("^(.-)[^" .. dirsep .. "]*" .. dirsep .."?$")
|
||||
for _, sub_fname in ipairs(scan_directory(fname)) do
|
||||
local target_fname = convert_path(sub_fname)
|
||||
if opts.t then
|
||||
if head then
|
||||
local start, stop = target_fname:find(head, 1, true)
|
||||
if start == 1 then
|
||||
target_fname = target_fname:sub(stop + 1)
|
||||
end
|
||||
end
|
||||
|
||||
target_fname = normalize_dir(opts.t) .. target_fname
|
||||
end
|
||||
|
||||
table.insert(files, {sub_fname, target_fname})
|
||||
table.insert(files, {
|
||||
sub_fname,
|
||||
path_to_target(sub_fname, opts.t, fname)
|
||||
})
|
||||
end
|
||||
else
|
||||
local target_fname = convert_path(fname)
|
||||
if opts.t then
|
||||
local prefix = normalize_dir(opts.t)
|
||||
|
||||
if is_abs_path(target_fname) then
|
||||
target_fname = parse_file(target_fname)
|
||||
end
|
||||
target_fname = prefix .. target_fname
|
||||
end
|
||||
|
||||
table.insert(files, {fname, target_fname})
|
||||
table.insert(files, {
|
||||
fname,
|
||||
path_to_target(fname, opts.t)
|
||||
})
|
||||
end
|
||||
|
||||
return files
|
||||
|
@ -4,7 +4,7 @@ do
|
||||
local _obj_0 = require("moonscript.util")
|
||||
split = _obj_0.split
|
||||
end
|
||||
local dirsep, dirsep_chars, mkdir, normalize_dir, parse_dir, parse_file, convert_path, format_time, gettime, compile_file_text, write_file, compile_and_write
|
||||
local dirsep, dirsep_chars, mkdir, normalize_dir, parse_dir, parse_file, convert_path, format_time, gettime, compile_file_text, write_file, compile_and_write, is_abs_path, path_to_target
|
||||
dirsep = package.config:sub(1, 1)
|
||||
if dirsep == "\\" then
|
||||
dirsep_chars = "\\/"
|
||||
@ -146,6 +146,42 @@ compile_and_write = function(src, dest, opts)
|
||||
end
|
||||
return write_file(dest, code)
|
||||
end
|
||||
is_abs_path = function(path)
|
||||
local first = path:sub(1, 1)
|
||||
if dirsep == "\\" then
|
||||
return first == "/" or first == "\\" or path:sub(2, 1) == ":"
|
||||
else
|
||||
return first == dirsep
|
||||
end
|
||||
end
|
||||
path_to_target = function(path, target_dir, base_dir)
|
||||
if target_dir == nil then
|
||||
target_dir = nil
|
||||
end
|
||||
if base_dir == nil then
|
||||
base_dir = nil
|
||||
end
|
||||
local target = convert_path(path)
|
||||
if target_dir then
|
||||
target_dir = normalize_dir(target_dir)
|
||||
end
|
||||
if base_dir and target_dir then
|
||||
local head = base_dir:match("^(.-)[^" .. tostring(dirsep_chars) .. "]*[" .. tostring(dirsep_chars) .. "]?$")
|
||||
if head then
|
||||
local start, stop = target:find(head, 1, true)
|
||||
if start == 1 then
|
||||
target = target:sub(stop + 1)
|
||||
end
|
||||
end
|
||||
end
|
||||
if target_dir then
|
||||
if is_abs_path(target) then
|
||||
target = parse_file(target)
|
||||
end
|
||||
target = target_dir .. target
|
||||
end
|
||||
return target
|
||||
end
|
||||
return {
|
||||
dirsep = dirsep,
|
||||
mkdir = mkdir,
|
||||
@ -156,6 +192,7 @@ return {
|
||||
convert_path = convert_path,
|
||||
gettime = gettime,
|
||||
format_time = format_time,
|
||||
path_to_target = path_to_target,
|
||||
compile_file_text = compile_file_text,
|
||||
compile_and_write = compile_and_write
|
||||
}
|
||||
|
@ -142,6 +142,40 @@ compile_and_write = (src, dest, opts={}) ->
|
||||
|
||||
write_file dest, code
|
||||
|
||||
is_abs_path = (path) ->
|
||||
first = path\sub 1, 1
|
||||
if dirsep == "\\"
|
||||
first == "/" or first == "\\" or path\sub(2,1) == ":"
|
||||
else
|
||||
first == dirsep
|
||||
|
||||
|
||||
-- calcuate where a path should be compiled to
|
||||
-- target_dir: the directory to place the file (optional, from -t flag)
|
||||
-- base_dir: the directory where the file came from when globbing recursively
|
||||
path_to_target = (path, target_dir=nil, base_dir=nil) ->
|
||||
target = convert_path path
|
||||
|
||||
if target_dir
|
||||
target_dir = normalize_dir target_dir
|
||||
|
||||
if base_dir and target_dir
|
||||
-- one directory back
|
||||
head = base_dir\match("^(.-)[^#{dirsep_chars}]*[#{dirsep_chars}]?$")
|
||||
|
||||
if head
|
||||
start, stop = target\find head, 1, true
|
||||
if start == 1
|
||||
target = target\sub(stop + 1)
|
||||
|
||||
if target_dir
|
||||
if is_abs_path target
|
||||
target = parse_file target
|
||||
|
||||
target = target_dir .. target
|
||||
|
||||
target
|
||||
|
||||
{
|
||||
:dirsep
|
||||
:mkdir
|
||||
@ -152,6 +186,7 @@ compile_and_write = (src, dest, opts={}) ->
|
||||
:convert_path
|
||||
:gettime
|
||||
:format_time
|
||||
:path_to_target
|
||||
|
||||
:compile_file_text
|
||||
:compile_and_write
|
||||
|
@ -31,6 +31,26 @@ describe "moonc", ->
|
||||
same moonc.convert_path, "/hello/file.moon", "/hello/file.lua"
|
||||
same moonc.convert_path, "/hello/world/file", "/hello/world/file.lua"
|
||||
|
||||
it "calculate target", ->
|
||||
p = moonc.path_to_target
|
||||
|
||||
assert.same "test.lua", p "test.moon"
|
||||
assert.same "hello/world.lua", p "hello/world.moon"
|
||||
assert.same "compiled/test.lua", p "test.moon", "compiled"
|
||||
|
||||
assert.same "/home/leafo/test.lua", p "/home/leafo/test.moon"
|
||||
assert.same "compiled/test.lua", p "/home/leafo/test.moon", "compiled"
|
||||
assert.same "/compiled/test.lua", p "/home/leafo/test.moon", "/compiled/"
|
||||
|
||||
assert.same "moonscript/hello.lua", p "moonscript/hello.moon", nil, "moonscript"
|
||||
assert.same "out/moonscript/hello.lua", p "moonscript/hello.moon", "out", "moonscript"
|
||||
|
||||
assert.same "out/moonscript/package/hello.lua",
|
||||
p "moonscript/package/hello.moon", "out", "moonscript/"
|
||||
|
||||
assert.same "/out/moonscript/package/hello.lua",
|
||||
p "/home/leafo/moonscript/package/hello.moon", "/out", "/home/leafo/moonscript"
|
||||
|
||||
it "should compile file text", ->
|
||||
assert.same {
|
||||
[[return print('hello')]]
|
||||
|
Loading…
Reference in New Issue
Block a user