-
Notifications
You must be signed in to change notification settings - Fork 3
/
project.lua
88 lines (80 loc) · 2.36 KB
/
project.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
local utils = require("textadept-nim.utils")
local check_type = utils.check_type
local file_exists = utils.file_exists
local _M = {}
local sep = WIN32 and "\\" or "/"
local function parse_nimble(filename)
check_type("string", filename)
local project = {}
project.backend = "c" -- nimble builds project in C by default
-- parse nimble file
for line in io.lines(filename)
do
local key, val = line:match("^%s*(%S+)%s*=%s*(%S+)")
if key == "bin"
then
project.bin = val
elseif key == "srcDir"
then
project.srcdir = val
elseif key == "backend"
then
project.backend = val
elseif line:match("setCommand") ~= nil
then
project.backend = line:match("setCommand%s+\"(%a+)\"")
end
end
return project
end
function _M.detect_project(filename)
-- Trying to obtain project root file
-- If not succeed returns passed filename
check_type("string", filename)
local root_dir = io.get_project_root(buffer.filename) or
filename:match("^([%p%w]-)[^/\\]+$") or
"."
local nimble_file = ""
lfs.dir_foreach(root_dir, function(n)
if n:match("%.nimble") or n:match("%.babel") then
nimble_file = n
return false
end
end,
"!.*")
if #nimble_file > 0
then
local project = parse_nimble(nimble_file)
if project.bin ~= nil
then -- root file is a file that transforms to a binary
project.root = root_dir..sep..(project.srcdir or "")..
sep..project.bin..".nim"
if file_exists(project.root)
then
return project
end
end
-- if project builds no binaries
-- trying to consider as root a file with name similar to nimble file
project.root = tostring(nimble_file:match("(.*%.)[^/\\]+")).."nim"
if not file_exists(project.root)
then
-- if it does not exists checking it in srcdir(if exists)
project.root = root_dir..sep..(project.srcdir or "")..
project.root:match(".*([/\\][^/\\]+)$")
if not file_exists(project.root)
then -- finally give up, project root will be a given file
project.root = filename
end
end
return project
end
local dummy_project =
{
backend = "c",
srcdir = root_dir,
root = filename
} -- When no nimble file detected - just return dummy project for one file
return dummy_project
end
return _M