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 parse_file = moonc.parse_file
|
||||||
local convert_path = moonc.convert_path
|
local convert_path = moonc.convert_path
|
||||||
local compile_and_write = moonc.compile_and_write
|
local compile_and_write = moonc.compile_and_write
|
||||||
|
local path_to_target = moonc.path_to_target
|
||||||
|
|
||||||
local function scan_directory(root, collected)
|
local function scan_directory(root, collected)
|
||||||
root = normalize_dir(root)
|
root = normalize_dir(root)
|
||||||
@ -105,48 +106,22 @@ local function remove_dups(tbl, key_fn)
|
|||||||
return final
|
return final
|
||||||
end
|
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
|
-- creates tuples of input and target
|
||||||
local function get_files(fname, files)
|
local function get_files(fname, files)
|
||||||
files = files or {}
|
files = files or {}
|
||||||
|
|
||||||
if lfs.attributes(fname, "mode") == "directory" then
|
if lfs.attributes(fname, "mode") == "directory" then
|
||||||
local head = fname:match("^(.-)[^" .. dirsep .. "]*" .. dirsep .."?$")
|
|
||||||
for _, sub_fname in ipairs(scan_directory(fname)) do
|
for _, sub_fname in ipairs(scan_directory(fname)) do
|
||||||
local target_fname = convert_path(sub_fname)
|
table.insert(files, {
|
||||||
if opts.t then
|
sub_fname,
|
||||||
if head then
|
path_to_target(sub_fname, opts.t, fname)
|
||||||
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})
|
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
local target_fname = convert_path(fname)
|
table.insert(files, {
|
||||||
if opts.t then
|
fname,
|
||||||
local prefix = normalize_dir(opts.t)
|
path_to_target(fname, 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})
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return files
|
return files
|
||||||
|
@ -4,7 +4,7 @@ do
|
|||||||
local _obj_0 = require("moonscript.util")
|
local _obj_0 = require("moonscript.util")
|
||||||
split = _obj_0.split
|
split = _obj_0.split
|
||||||
end
|
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)
|
dirsep = package.config:sub(1, 1)
|
||||||
if dirsep == "\\" then
|
if dirsep == "\\" then
|
||||||
dirsep_chars = "\\/"
|
dirsep_chars = "\\/"
|
||||||
@ -146,6 +146,42 @@ compile_and_write = function(src, dest, opts)
|
|||||||
end
|
end
|
||||||
return write_file(dest, code)
|
return write_file(dest, code)
|
||||||
end
|
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 {
|
return {
|
||||||
dirsep = dirsep,
|
dirsep = dirsep,
|
||||||
mkdir = mkdir,
|
mkdir = mkdir,
|
||||||
@ -156,6 +192,7 @@ return {
|
|||||||
convert_path = convert_path,
|
convert_path = convert_path,
|
||||||
gettime = gettime,
|
gettime = gettime,
|
||||||
format_time = format_time,
|
format_time = format_time,
|
||||||
|
path_to_target = path_to_target,
|
||||||
compile_file_text = compile_file_text,
|
compile_file_text = compile_file_text,
|
||||||
compile_and_write = compile_and_write
|
compile_and_write = compile_and_write
|
||||||
}
|
}
|
||||||
|
@ -142,6 +142,40 @@ compile_and_write = (src, dest, opts={}) ->
|
|||||||
|
|
||||||
write_file dest, code
|
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
|
:dirsep
|
||||||
:mkdir
|
:mkdir
|
||||||
@ -152,6 +186,7 @@ compile_and_write = (src, dest, opts={}) ->
|
|||||||
:convert_path
|
:convert_path
|
||||||
:gettime
|
:gettime
|
||||||
:format_time
|
:format_time
|
||||||
|
:path_to_target
|
||||||
|
|
||||||
:compile_file_text
|
:compile_file_text
|
||||||
:compile_and_write
|
:compile_and_write
|
||||||
|
@ -31,6 +31,26 @@ describe "moonc", ->
|
|||||||
same moonc.convert_path, "/hello/file.moon", "/hello/file.lua"
|
same moonc.convert_path, "/hello/file.moon", "/hello/file.lua"
|
||||||
same moonc.convert_path, "/hello/world/file", "/hello/world/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", ->
|
it "should compile file text", ->
|
||||||
assert.same {
|
assert.same {
|
||||||
[[return print('hello')]]
|
[[return print('hello')]]
|
||||||
|
Loading…
Reference in New Issue
Block a user