Skip to content

Commit 7ee641a

Browse files
committed
moonscript.cmd.moonc: Improve normalize_dir, add iterate_path
1 parent 18a8146 commit 7ee641a

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

moonscript/cmd/moonc.lua

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
local lfs = require("lfs")
22
local split
33
split = require("moonscript.util").split
4-
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
4+
local dirsep, dirsep_chars, mkdir, normalize_dir, parse_dir, parse_file, convert_path, iterate_path, format_time, gettime, compile_file_text, write_file, compile_and_write, is_abs_path, path_to_target
55
dirsep = package.config:sub(1, 1)
66
if dirsep == "\\" then
77
dirsep_chars = "\\/"
@@ -19,7 +19,17 @@ mkdir = function(path)
1919
return lfs.attributes(path, "mode")
2020
end
2121
normalize_dir = function(path)
22-
return path:match("^(.-)[" .. tostring(dirsep_chars) .. "]*$") .. dirsep
22+
local normalized_dir
23+
if is_abs_path(path) then
24+
normalized_dir = dirsep
25+
else
26+
normalized_dir = ""
27+
end
28+
for path_element in iterate_path(path) do
29+
normalized_dir = normalized_dir .. (path_element .. dirsep)
30+
end
31+
return normalized_dir
32+
end
2333
end
2434
parse_dir = function(path)
2535
return (path:match("^(.-)[^" .. tostring(dirsep_chars) .. "]*$"))
@@ -34,6 +44,9 @@ convert_path = function(path)
3444
end
3545
return new_path
3646
end
47+
iterate_path = function(path)
48+
return path:gmatch("([^" .. tostring(dirsep_chars) .. "]+)")
49+
end
3750
format_time = function(time)
3851
return ("%.3fms"):format(time * 1000)
3952
end
@@ -190,6 +203,7 @@ return {
190203
normalize_dir = normalize_dir,
191204
parse_dir = parse_dir,
192205
parse_file = parse_file,
206+
iterate_path = iterate_path,
193207
convert_path = convert_path,
194208
gettime = gettime,
195209
format_time = format_time,

moonscript/cmd/moonc.moon

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,15 @@ mkdir = (path) ->
2323

2424
lfs.attributes path, "mode"
2525

26-
-- strips excess / and ensures path ends with /
26+
-- Strips excess / and ensures path ends with /
2727
normalize_dir = (path) ->
28-
path\match("^(.-)[#{dirsep_chars}]*$") .. dirsep
28+
normalized_dir = if is_abs_path(path)
29+
dirsep
30+
else
31+
""
32+
for path_element in iterate_path(path)
33+
normalized_dir ..= path_element .. dirsep
34+
return normalized_dir
2935

3036
-- parse the directory out of a path
3137
parse_dir = (path) ->
@@ -42,6 +48,10 @@ convert_path = (path) ->
4248
new_path = path .. ".lua"
4349
new_path
4450

51+
-- Iterates over the directories (and file) in a path
52+
iterate_path = (path) ->
53+
path\gmatch "([^#{dirsep_chars}]+)"
54+
4555
format_time = (time) ->
4656
"%.3fms"\format time*1000
4757

@@ -187,6 +197,7 @@ path_to_target = (path, target_dir=nil, base_dir=nil) ->
187197
:normalize_dir
188198
:parse_dir
189199
:parse_file
200+
:iterate_path
190201
:convert_path
191202
:gettime
192203
:format_time

spec/cmd_spec.moon

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,15 @@ describe "moonc", ->
1414

1515
it "should normalize dir", ->
1616
same moonc.normalize_dir, "hello/world/", "hello/world/"
17+
same moonc.normalize_dir, "/hello/world/", "/hello/world/"
1718
same moonc.normalize_dir, "hello/world//", "hello/world/"
18-
same moonc.normalize_dir, "", "/" -- wrong
19+
same moonc.normalize_dir, "/hello/world//", "/hello/world/"
20+
same moonc.normalize_dir, "hello//world//", "hello/world/"
21+
same moonc.normalize_dir, "/hello//world//", "/hello/world/"
22+
same moonc.normalize_dir, "", ""
23+
same moonc.normalize_dir, "/", "/"
1924
same moonc.normalize_dir, "hello", "hello/"
25+
same moonc.normalize_dir, "/hello", "/hello/"
2026

2127
it "should parse dir", ->
2228
same moonc.parse_dir, "/hello/world/file", "/hello/world/"
@@ -35,6 +41,25 @@ describe "moonc", ->
3541
same moonc.convert_path, "/hello/file.moon", "/hello/file.lua"
3642
same moonc.convert_path, "/hello/world/file", "/hello/world/file.lua"
3743

44+
it "iterates paths", ->
45+
single = {"foo"}
46+
single_path = "foo"
47+
nested = {"foo", "bar"}
48+
nested_path = "foo/bar"
49+
nested_file = {"foo", "bar.baz"}
50+
nested_file_path = "foo/bar.baz"
51+
52+
same_iterated_path = (path, comparison_tbl) ->
53+
i = 0
54+
for path_element in moonc.iterate_path(path)
55+
i += 1
56+
assert.same comparison_tbl[i], path_element
57+
assert.same #comparison_tbl, i
58+
59+
same_iterated_path single_path, single
60+
same_iterated_path nested_path, nested
61+
same_iterated_path nested_file_path, nested_file
62+
3863
it "calculate target", ->
3964
p = moonc.path_to_target
4065

0 commit comments

Comments
 (0)