diff options
Diffstat (limited to 'src/luarocks/path.lua')
-rw-r--r-- | src/luarocks/path.lua | 224 |
1 files changed, 224 insertions, 0 deletions
diff --git a/src/luarocks/path.lua b/src/luarocks/path.lua new file mode 100644 index 00000000..e236db01 --- /dev/null +++ b/src/luarocks/path.lua | |||
@@ -0,0 +1,224 @@ | |||
1 | |||
2 | --- Path and filename handling functions. | ||
3 | -- All paths are configured in this module, making it a single | ||
4 | -- point where the layout of the local installation is defined in LuaRocks. | ||
5 | module("luarocks.path", package.seeall) | ||
6 | |||
7 | local fs = require("luarocks.fs") | ||
8 | local cfg = require("luarocks.cfg") | ||
9 | |||
10 | --- Infer rockspec filename from a rock filename. | ||
11 | -- @param rock_name string: Pathname of a rock file. | ||
12 | -- @return string: Filename of the rockspec, without path. | ||
13 | function rockspec_name_from_rock(rock_name) | ||
14 | assert(type(rock_name) == "string") | ||
15 | local base_name = fs.base_name(rock_name) | ||
16 | return base_name:match("(.*)%.[^.]*.rock") .. ".rockspec" | ||
17 | end | ||
18 | |||
19 | --- Get the repository directory for all versions of a package. | ||
20 | -- @param name string: The package name. | ||
21 | -- @return string: The resulting path -- does not guarantee that | ||
22 | -- @param repo string or nil: If given, specifies the local repository to use. | ||
23 | -- the package (and by extension, the path) exists. | ||
24 | function versions_dir(name, repo) | ||
25 | assert(type(name) == "string") | ||
26 | assert(not repo or type(repo) == "string") | ||
27 | |||
28 | return fs.make_path(repo or cfg.rocks_dir, name) | ||
29 | end | ||
30 | |||
31 | --- Get the local installation directory (prefix) for a package. | ||
32 | -- @param name string: The package name. | ||
33 | -- @param version string: The package version. | ||
34 | -- @param repo string or nil: If given, specifies the local repository to use. | ||
35 | -- @return string: The resulting path -- does not guarantee that | ||
36 | -- the package (and by extension, the path) exists. | ||
37 | function install_dir(name, version, repo) | ||
38 | assert(type(name) == "string") | ||
39 | assert(type(version) == "string") | ||
40 | assert(not repo or type(repo) == "string") | ||
41 | |||
42 | return fs.make_path(repo or cfg.rocks_dir, name, version) | ||
43 | end | ||
44 | |||
45 | --- Get the local filename of the rockspec of an installed rock. | ||
46 | -- @param name string: The package name. | ||
47 | -- @param version string: The package version. | ||
48 | -- @param repo string or nil: If given, specifies the local repository to use. | ||
49 | -- @return string: The resulting path -- does not guarantee that | ||
50 | -- the package (and by extension, the file) exists. | ||
51 | function rockspec_file(name, version, repo) | ||
52 | assert(type(name) == "string") | ||
53 | assert(type(version) == "string") | ||
54 | assert(not repo or type(repo) == "string") | ||
55 | |||
56 | return fs.make_path(repo or cfg.rocks_dir, name, version, name.."-"..version..".rockspec") | ||
57 | end | ||
58 | |||
59 | --- Get the local installation directory for C libraries of a package. | ||
60 | -- @param name string: The package name. | ||
61 | -- @param version string: The package version. | ||
62 | -- @param repo string or nil: If given, specifies the local repository to use. | ||
63 | -- @return string: The resulting path -- does not guarantee that | ||
64 | -- the package (and by extension, the path) exists. | ||
65 | function lib_dir(name, version, repo) | ||
66 | assert(type(name) == "string") | ||
67 | assert(type(version) == "string") | ||
68 | assert(not repo or type(repo) == "string") | ||
69 | |||
70 | return fs.make_path(repo or cfg.rocks_dir, name, version, "lib") | ||
71 | end | ||
72 | |||
73 | --- Get the local installation directory for Lua modules of a package. | ||
74 | -- @param name string: The package name. | ||
75 | -- @param version string: The package version. | ||
76 | -- @param repo string or nil: If given, specifies the local repository to use. | ||
77 | -- @return string: The resulting path -- does not guarantee that | ||
78 | -- the package (and by extension, the path) exists. | ||
79 | function lua_dir(name, version, repo) | ||
80 | assert(type(name) == "string") | ||
81 | assert(type(version) == "string") | ||
82 | assert(not repo or type(repo) == "string") | ||
83 | |||
84 | return fs.make_path(repo or cfg.rocks_dir, name, version, "lua") | ||
85 | end | ||
86 | |||
87 | --- Get the local installation directory for documentation of a package. | ||
88 | -- @param name string: The package name. | ||
89 | -- @param version string: The package version. | ||
90 | -- @param repo string or nil: If given, specifies the local repository to use. | ||
91 | -- @return string: The resulting path -- does not guarantee that | ||
92 | -- the package (and by extension, the path) exists. | ||
93 | function doc_dir(name, version, repo) | ||
94 | assert(type(name) == "string") | ||
95 | assert(type(version) == "string") | ||
96 | assert(not repo or type(repo) == "string") | ||
97 | |||
98 | return fs.make_path(repo or cfg.rocks_dir, name, version, "doc") | ||
99 | end | ||
100 | |||
101 | --- Get the local installation directory for configuration files of a package. | ||
102 | -- @param name string: The package name. | ||
103 | -- @param version string: The package version. | ||
104 | -- @param repo string or nil: If given, specifies the local repository to use. | ||
105 | -- @return string: The resulting path -- does not guarantee that | ||
106 | -- the package (and by extension, the path) exists. | ||
107 | function conf_dir(name, version, repo) | ||
108 | assert(type(name) == "string") | ||
109 | assert(type(version) == "string") | ||
110 | assert(not repo or type(repo) == "string") | ||
111 | |||
112 | return fs.make_path(repo or cfg.rocks_dir, name, version, "conf") | ||
113 | end | ||
114 | |||
115 | --- Get the local installation directory for command-line scripts | ||
116 | -- of a package. | ||
117 | -- @param name string: The package name. | ||
118 | -- @param version string: The package version. | ||
119 | -- @param repo string or nil: If given, specifies the local repository to use. | ||
120 | -- @return string: The resulting path -- does not guarantee that | ||
121 | -- the package (and by extension, the path) exists. | ||
122 | function bin_dir(name, version, repo) | ||
123 | assert(type(name) == "string") | ||
124 | assert(type(version) == "string") | ||
125 | assert(not repo or type(repo) == "string") | ||
126 | |||
127 | return fs.make_path(repo or cfg.rocks_dir, name, version, "bin") | ||
128 | end | ||
129 | |||
130 | --- Extract name, version and arch of a rock filename. | ||
131 | -- @param rock_file string: pathname of a rock | ||
132 | -- @return (string, string, string) or nil: name, version and arch | ||
133 | -- of rock, or nil if name could not be parsed | ||
134 | function parse_rock_name(rock_file) | ||
135 | assert(type(rock_file) == "string") | ||
136 | return fs.base_name(rock_file):match("(.*)-([^-]+-%d+)%.([^.]+)%.rock$") | ||
137 | end | ||
138 | |||
139 | --- Extract name and version of a rockspec filename. | ||
140 | -- @param rockspec_file string: pathname of a rockspec | ||
141 | -- @return (string, string) or nil: name and version | ||
142 | -- of rockspec, or nil if name could not be parsed | ||
143 | function parse_rockspec_name(rockspec_file) | ||
144 | assert(type(rockspec_file) == "string") | ||
145 | return fs.base_name(rockspec_file):match("(.*)-([^-]+-%d+)%.(rockspec)") | ||
146 | end | ||
147 | |||
148 | --- Make a rockspec or rock URL. | ||
149 | -- @param pathname string: Base URL or pathname. | ||
150 | -- @param name string: Package name. | ||
151 | -- @param version string: Package version. | ||
152 | -- @param arch string: Architecture identifier, or "rockspec" or "installed". | ||
153 | -- @return string: A URL or pathname following LuaRocks naming conventions. | ||
154 | function make_url(pathname, name, version, arch) | ||
155 | assert(type(pathname) == "string") | ||
156 | assert(type(name) == "string") | ||
157 | assert(type(version) == "string") | ||
158 | assert(type(arch) == "string") | ||
159 | |||
160 | local filename = name.."-"..version | ||
161 | if arch == "installed" then | ||
162 | filename = fs.make_path(name, version, filename..".rockspec") | ||
163 | elseif arch == "rockspec" then | ||
164 | filename = filename..".rockspec" | ||
165 | else | ||
166 | filename = filename.."."..arch..".rock" | ||
167 | end | ||
168 | return fs.make_path(pathname, filename) | ||
169 | end | ||
170 | |||
171 | --- Convert a pathname to a module identifier. | ||
172 | -- In Unix, for example, a path "foo/bar/baz.lua" is converted to | ||
173 | -- "foo.bar.baz"; "bla/init.lua" returns "bla"; "foo.so" returns "foo". | ||
174 | -- @param file string: Pathname of module | ||
175 | -- @return string: The module identifier, or nil if given path is | ||
176 | -- not a conformant module path (the function does not check if the | ||
177 | -- path actually exists). | ||
178 | function path_to_module(file) | ||
179 | assert(type(file) == "string") | ||
180 | |||
181 | local name = file:match("(.*)%."..cfg.lua_extension.."$") | ||
182 | if name then | ||
183 | name = name:gsub(fs.dir_separator, ".") | ||
184 | local init = name:match("(.*)%.init$") | ||
185 | if init then | ||
186 | name = init | ||
187 | end | ||
188 | else | ||
189 | name = file:match("(.*)%."..cfg.lib_extension.."$") | ||
190 | if name then | ||
191 | name = name:gsub(fs.dir_separator, ".") | ||
192 | end | ||
193 | end | ||
194 | return name | ||
195 | end | ||
196 | |||
197 | --- Obtain the directory name where a module should be stored. | ||
198 | -- For example, on Unix, "foo.bar.baz" will return "foo/bar". | ||
199 | -- @param mod string: A module name in Lua dot-separated format. | ||
200 | -- @return string: A directory name using the platform's separator. | ||
201 | function module_to_path(mod) | ||
202 | assert(type(mod) == "string") | ||
203 | return (mod:gsub("[^.]*$", ""):gsub("%.", fs.dir_separator)) | ||
204 | end | ||
205 | |||
206 | --- Set up path-related variables for a given rock. | ||
207 | -- Create a "variables" table in the rockspec table, containing | ||
208 | -- adjusted variables according to the configuration file. | ||
209 | -- @param rockspec table: The rockspec table. | ||
210 | function configure_paths(rockspec) | ||
211 | assert(type(rockspec) == "table") | ||
212 | local vars = {} | ||
213 | for k,v in pairs(cfg.variables) do | ||
214 | vars[k] = v | ||
215 | end | ||
216 | local name, version = rockspec.name, rockspec.version | ||
217 | vars.PREFIX = install_dir(name, version) | ||
218 | vars.LUADIR = lua_dir(name, version) | ||
219 | vars.LIBDIR = lib_dir(name, version) | ||
220 | vars.CONFDIR = conf_dir(name, version) | ||
221 | vars.BINDIR = bin_dir(name, version) | ||
222 | vars.DOCDIR = doc_dir(name, version) | ||
223 | rockspec.variables = vars | ||
224 | end | ||