Skip to content

Commit e35c298

Browse files
committed
Move all path-related functions out from moonscript.cmd.moonc
- By 'path-related', I mean ones that purely manipulate or work on paths, with no actual filesystem accesses involved - A few new functions are also added
1 parent 8f96658 commit e35c298

File tree

7 files changed

+206
-127
lines changed

7 files changed

+206
-127
lines changed

moonscript/cmd/moonc.lua

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
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, iterate_path, format_time, gettime, compile_file_text, write_file, compile_and_write, is_abs_path, path_to_target
5-
dirsep = package.config:sub(1, 1)
6-
if dirsep == "\\" then
7-
dirsep_chars = "\\/"
8-
else
9-
dirsep_chars = dirsep
4+
local dirsep, normalize_dir, normalize_path, parse_dir, parse_file, parse_subtree, convert_path
5+
do
6+
local _obj_0 = require("moonscript.cmd.path_handling")
7+
dirsep, normalize_dir, normalize_path, parse_dir, parse_file, parse_subtree, convert_path = _obj_0.dirsep, _obj_0.normalize_dir, _obj_0.normalize_path, _obj_0.parse_dir, _obj_0.parse_file, _obj_0.parse_subtree, _obj_0.convert_path
108
end
9+
local mkdir, format_time, gettime, compile_file_text, write_file, compile_and_write, path_to_target
1110
mkdir = function(path)
1211
local chunks = split(path, dirsep)
1312
local accum
@@ -18,35 +17,6 @@ mkdir = function(path)
1817
end
1918
return lfs.attributes(path, "mode")
2019
end
21-
normalize_dir = function(path)
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
33-
end
34-
parse_dir = function(path)
35-
return (path:match("^(.-)[^" .. tostring(dirsep_chars) .. "]*$"))
36-
end
37-
parse_file = function(path)
38-
return (path:match("^.-([^" .. tostring(dirsep_chars) .. "]*)$"))
39-
end
40-
convert_path = function(path)
41-
local new_path = path:gsub("%.moon$", ".lua")
42-
if new_path == path then
43-
new_path = path .. ".lua"
44-
end
45-
return new_path
46-
end
47-
iterate_path = function(path)
48-
return path:gmatch("([^" .. tostring(dirsep_chars) .. "]+)")
49-
end
5020
format_time = function(time)
5121
return ("%.3fms"):format(time * 1000)
5222
end
@@ -161,14 +131,6 @@ compile_and_write = function(src, dest, opts)
161131
end
162132
return write_file(dest, code)
163133
end
164-
is_abs_path = function(path)
165-
local first = path:sub(1, 1)
166-
if dirsep == "\\" then
167-
return first == "/" or first == "\\" or path:sub(2, 1) == ":"
168-
else
169-
return first == dirsep
170-
end
171-
end
172134
path_to_target = function(path, target_dir, base_dir)
173135
if target_dir == nil then
174136
target_dir = nil
@@ -198,14 +160,7 @@ path_to_target = function(path, target_dir, base_dir)
198160
return target
199161
end
200162
return {
201-
dirsep = dirsep,
202163
mkdir = mkdir,
203-
normalize_dir = normalize_dir,
204-
parse_dir = parse_dir,
205-
parse_file = parse_file,
206-
iterate_path = iterate_path,
207-
convert_path = convert_path,
208-
is_abs_path = is_abs_path,
209164
gettime = gettime,
210165
format_time = format_time,
211166
path_to_target = path_to_target,

moonscript/cmd/moonc.moon

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,10 @@
33
lfs = require "lfs"
44

55
import split from require "moonscript.util"
6+
import dirsep, normalize_dir, normalize_path, parse_dir, parse_file, parse_subtree, convert_path from require "moonscript.cmd.path_handling"
67

78
local *
89

9-
dirsep = package.config\sub 1,1
10-
dirsep_chars = if dirsep == "\\"
11-
"\\/" -- windows
12-
else
13-
dirsep
1410

1511
-- similar to mkdir -p
1612
mkdir = (path) ->
@@ -23,35 +19,6 @@ mkdir = (path) ->
2319

2420
lfs.attributes path, "mode"
2521

26-
-- Strips excess / and ensures path ends with /
27-
normalize_dir = (path) ->
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
35-
36-
-- parse the directory out of a path
37-
parse_dir = (path) ->
38-
(path\match "^(.-)[^#{dirsep_chars}]*$")
39-
40-
-- parse the filename out of a path
41-
parse_file = (path) ->
42-
(path\match "^.-([^#{dirsep_chars}]*)$")
43-
44-
-- converts .moon to a .lua path for calcuating compile target
45-
convert_path = (path) ->
46-
new_path = path\gsub "%.moon$", ".lua"
47-
if new_path == path
48-
new_path = path .. ".lua"
49-
new_path
50-
51-
-- Iterates over the directories (and file) in a path
52-
iterate_path = (path) ->
53-
path\gmatch "([^#{dirsep_chars}]+)"
54-
5522
format_time = (time) ->
5623
"%.3fms"\format time*1000
5724

@@ -157,14 +124,6 @@ compile_and_write = (src, dest, opts={}) ->
157124

158125
write_file dest, code
159126

160-
is_abs_path = (path) ->
161-
first = path\sub 1, 1
162-
if dirsep == "\\"
163-
first == "/" or first == "\\" or path\sub(2,1) == ":"
164-
else
165-
first == dirsep
166-
167-
168127
-- calcuate where a path should be compiled to
169128
-- target_dir: the directory to place the file (optional, from -t flag)
170129
-- base_dir: the directory where the file came from when globbing recursively
@@ -192,14 +151,7 @@ path_to_target = (path, target_dir=nil, base_dir=nil) ->
192151
target
193152

194153
{
195-
:dirsep
196154
:mkdir
197-
:normalize_dir
198-
:parse_dir
199-
:parse_file
200-
:iterate_path
201-
:convert_path
202-
:is_abs_path
203155
:gettime
204156
:format_time
205157
:path_to_target

moonscript/cmd/path_handling.lua

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
local dirsep, dirsep_chars, is_abs_path, normalize_dir, normalize_path, parse_dir, parse_file, parse_subtree, parse_root, convert_path, iterate_path
2+
dirsep = package.config:sub(1, 1)
3+
if dirsep == "\\" then
4+
dirsep_chars = "\\/"
5+
else
6+
dirsep_chars = dirsep
7+
end
8+
is_abs_path = function(path)
9+
local first = path:sub(1, 1)
10+
if dirsep == "\\" then
11+
return first == "/" or first == "\\" or path:sub(2, 1) == ":"
12+
else
13+
return first == dirsep
14+
end
15+
end
16+
normalize_dir = function(path)
17+
local normalized_dir
18+
if is_abs_path(path) then
19+
normalized_dir = dirsep
20+
else
21+
normalized_dir = ""
22+
end
23+
for path_element in iterate_path(path) do
24+
normalized_dir = normalized_dir .. (path_element .. dirsep)
25+
end
26+
return normalized_dir
27+
end
28+
normalize_path = function(path)
29+
local path_elements = { }
30+
for path_element in iterate_path(path) do
31+
table.insert(path_elements, path_element)
32+
end
33+
local normalized_path
34+
if is_abs_path(path) then
35+
normalized_path = dirsep
36+
else
37+
normalized_path = ""
38+
end
39+
for i = 1, #path_elements - 1 do
40+
local path_element = path_elements[i]
41+
normalized_path = normalized_path .. (path_element .. dirsep)
42+
end
43+
return normalized_path .. path_elements[#path_elements]
44+
end
45+
parse_dir = function(path)
46+
return (path:match("^(.-)[^" .. tostring(dirsep_chars) .. "]*$"))
47+
end
48+
parse_file = function(path)
49+
return (path:match("^.-([^" .. tostring(dirsep_chars) .. "]*)$"))
50+
end
51+
parse_subtree = function(path)
52+
return (path:match("^.-[" .. tostring(dirsep_chars) .. "]+(.*)$"))
53+
end
54+
parse_root = function(path)
55+
return (path:match("^(.-[" .. tostring(dirsep_chars) .. "]+).*$"))
56+
end
57+
convert_path = function(path)
58+
local new_path = path:gsub("%.moon$", ".lua")
59+
if new_path == path then
60+
new_path = path .. ".lua"
61+
end
62+
return new_path
63+
end
64+
iterate_path = function(path)
65+
return path:gmatch("([^" .. tostring(dirsep_chars) .. "]+)")
66+
end
67+
return {
68+
dirsep = dirsep,
69+
is_abs_path = is_abs_path,
70+
normalize_dir = normalize_dir,
71+
normalize_path = normalize_path,
72+
parse_dir = parse_dir,
73+
parse_file = parse_file,
74+
parse_subtree = parse_subtree,
75+
parse_root = parse_root,
76+
convert_path = convert_path,
77+
iterate_path = iterate_path
78+
}

moonscript/cmd/path_handling.moon

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
-- Path-handling functions; these are on their own to allow test filesystem
2+
-- stub functions to make use of them independently of the modules they are
3+
-- testing
4+
local *
5+
6+
dirsep = package.config\sub 1,1
7+
dirsep_chars = if dirsep == "\\"
8+
"\\/" -- windows
9+
else
10+
dirsep
11+
12+
is_abs_path = (path) ->
13+
first = path\sub 1, 1
14+
if dirsep == "\\"
15+
first == "/" or first == "\\" or path\sub(2,1) == ":"
16+
else
17+
first == dirsep
18+
19+
-- Strips excess / and ensures path ends with /
20+
normalize_dir = (path) ->
21+
normalized_dir = if is_abs_path(path)
22+
dirsep
23+
else
24+
""
25+
for path_element in iterate_path(path)
26+
normalized_dir ..= path_element .. dirsep
27+
return normalized_dir
28+
29+
-- Strips excess and trailing /
30+
normalize_path = (path) ->
31+
path_elements = {}
32+
for path_element in iterate_path(path)
33+
table.insert path_elements, path_element
34+
35+
normalized_path = if is_abs_path(path)
36+
dirsep
37+
else
38+
""
39+
40+
for i = 1, #path_elements - 1
41+
path_element = path_elements[i]
42+
normalized_path ..= path_element .. dirsep
43+
return normalized_path .. path_elements[#path_elements]
44+
45+
-- parse the directory out of a path
46+
parse_dir = (path) ->
47+
(path\match "^(.-)[^#{dirsep_chars}]*$")
48+
49+
-- parse the filename out of a path
50+
parse_file = (path) ->
51+
(path\match "^.-([^#{dirsep_chars}]*)$")
52+
53+
-- parse the subtree (all but the top directory) out of a path
54+
-- Invariants:
55+
-- If input is already normalized, the output will also be in normalized form
56+
parse_subtree = (path) ->
57+
(path\match "^.-[#{dirsep_chars}]+(.*)$")
58+
59+
-- parse the very first directory out of a path
60+
parse_root = (path) ->
61+
(path\match "^(.-[#{dirsep_chars}]+).*$")
62+
63+
-- converts .moon to a .lua path for calcuating compile target
64+
convert_path = (path) ->
65+
new_path = path\gsub "%.moon$", ".lua"
66+
if new_path == path
67+
new_path = path .. ".lua"
68+
new_path
69+
70+
-- Iterates over the directories (and file) in a path
71+
iterate_path = (path) ->
72+
path\gmatch "([^#{dirsep_chars}]+)"
73+
74+
{
75+
:dirsep
76+
:is_abs_path
77+
:normalize_dir
78+
:normalize_path
79+
:parse_dir
80+
:parse_file
81+
:parse_subtree
82+
:parse_root
83+
:convert_path
84+
:iterate_path
85+
}

moonscript/cmd/watchers.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ do
6969
local _base_0 = {
7070
get_dirs = function(self)
7171
local parse_dir
72-
parse_dir = require("moonscript.cmd.moonc").parse_dir
72+
parse_dir = require("moonscript.cmd.path_handling").parse_dir
7373
local dirs
7474
do
7575
local _accum_0 = { }

moonscript/cmd/watchers.moon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class InotifyWacher extends Watcher
2222
pcall -> require "inotify"
2323

2424
get_dirs: =>
25-
import parse_dir from require "moonscript.cmd.moonc"
25+
import parse_dir from require "moonscript.cmd.path_handling"
2626
dirs = for {file_path} in *@file_list
2727
dir = parse_dir file_path
2828
dir = "./" if dir == ""

0 commit comments

Comments
 (0)