forked from mpeterv/luacheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.lua
executable file
·131 lines (111 loc) · 3.84 KB
/
install.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env lua
local dirsep = package.config:sub(1, 1)
local is_windows = dirsep == "\\"
package.path = "src" .. dirsep .. "?.lua"
local has_luacheck, luacheck = pcall(require, "luacheck.init")
assert(has_luacheck, "couldn't find luacheck module")
local has_argparse, argparse = pcall(require, "luacheck.argparse")
assert(has_argparse, "couldn't find argparse module")
local lua_executable = assert(arg[-1], "couldn't detect Lua executable")
local parser = argparse("<lua> install.lua", "Luacheck " .. luacheck._VERSION .. " installer.")
parser:argument("path", ([[
Installation path.
Luacheck executable scripts will be installed into <path>%sbin.
Luacheck modules will be installed into <path>%ssrc.
Pass . to build luacheck executable script without installing.]]):format(dirsep, dirsep))
parser:option("--lua", "Absolute path to lua interpreter or its name if it's in PATH.", lua_executable)
local args = parser:parse()
local function run_command(cmd)
if is_windows then
cmd = cmd .. " >NUL"
else
cmd = cmd .. " >/dev/null"
end
print(" Running " .. cmd)
local ok = os.execute(cmd)
assert(ok == true or ok == 0, "couldn't run " .. cmd)
end
local function mkdir(dir)
if is_windows then
run_command(([[if not exist "%s" md "%s"]]):format(dir, dir))
else
run_command(([[mkdir -p "%s"]]):format(dir))
end
end
local function copy(src, dest)
if is_windows then
run_command(([[copy /y "%s" "%s"]]):format(src, dest))
else
run_command(([[cp "%s" "%s"]]):format(src, dest))
end
end
print(("Installing luacheck %s into %s"):format(luacheck._VERSION, args.path))
print()
local luacheck_executable = "bin" .. dirsep .. "luacheck"
local luacheck_src_dir = args.path .. dirsep .. "src"
local luacheck_lib_dir = luacheck_src_dir .. dirsep .. "luacheck"
local luacheck_bin_dir = args.path .. dirsep .. "bin"
if is_windows then
print(" Detected Windows environment")
luacheck_executable = luacheck_executable .. ".bat"
else
-- Close enough.
print(" Detected POSIX environment")
end
print(" Writing luacheck executable to " .. luacheck_executable)
local fh = assert(io.open(luacheck_executable, "wb"), "couldn't open " .. luacheck_executable)
if is_windows then
fh:write(([=[
@echo off
"%s" -e "package.path=[[%%~dp0..\src\?.lua;%%~dp0..\src\?\init.lua;]]..package.path" "%%~dp0luacheck.lua" %%*
]=]):format(args.lua))
else
fh:write(([=[
#!/bin/sh
exec "%s" -e "package.path=[[$(dirname "$0")/../src/?.lua;$(dirname "$0")/../src/?/init.lua;]]..package.path" "$(dirname "$0")/luacheck.lua" "$@"
]=]):format(args.lua))
end
fh:close()
if not is_windows then
run_command(([[chmod +x "%s"]]):format(luacheck_executable))
end
if args.path == "." then
print()
print(("Built luacheck %s executable script (%s)."):format(luacheck._VERSION, luacheck_executable))
return
end
print(" Installing luacheck modules into " .. luacheck_src_dir)
mkdir(luacheck_lib_dir)
for _, filename in ipairs {
"main.lua",
"init.lua",
"config.lua",
"linearize.lua",
"analyze.lua",
"reachability.lua",
"core_utils.lua",
"check.lua",
"parser.lua",
"lexer.lua",
"filter.lua",
"options.lua",
"inline_options.lua",
"stds.lua",
"expand_rockspec.lua",
"multithreading.lua",
"cache.lua",
"format.lua",
"version.lua",
"fs.lua",
"globbing.lua",
"utils.lua",
"argparse.lua"} do
copy("src" .. dirsep .. "luacheck" .. dirsep .. filename, luacheck_lib_dir)
end
print(" Installing luacheck executables into " .. luacheck_bin_dir)
mkdir(luacheck_bin_dir)
copy(luacheck_executable, luacheck_bin_dir)
copy("bin" .. dirsep .. "luacheck.lua", luacheck_bin_dir)
print()
print(("Installed luacheck %s into %s."):format(luacheck._VERSION, args.path))
print(("Please ensure that %s is in PATH."):format(luacheck_bin_dir))