Mercurial > repo
annotate share/lua/5.2/luarocks/fs/win32.lua @ 10250:132ea6f8d26a
<oerjan> revert
author | HackBot |
---|---|
date | Wed, 15 Feb 2017 00:29:45 +0000 |
parents | d137f631bad5 |
children |
rev | line source |
---|---|
1132
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
1 --- Windows implementation of filesystem and platform abstractions. |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
2 -- Download http://unxutils.sourceforge.net/ for Windows GNU utilities |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
3 -- used by this module. |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
4 module("luarocks.fs.win32", package.seeall) |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
5 |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
6 local fs = require("luarocks.fs") |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
7 |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
8 local cfg = require("luarocks.cfg") |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
9 local dir = require("luarocks.dir") |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
10 |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
11 --- Annotate command string for quiet execution. |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
12 -- @param cmd string: A command-line string. |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
13 -- @return string: The command-line, with silencing annotation. |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
14 function quiet(cmd) |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
15 return cmd.." 2> NUL 1> NUL" |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
16 end |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
17 |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
18 --- Quote argument for shell processing. Fixes paths on Windows. |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
19 -- Adds single quotes and escapes. |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
20 -- @param arg string: Unquoted argument. |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
21 -- @return string: Quoted argument. |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
22 function Q(arg) |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
23 assert(type(arg) == "string") |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
24 -- Quote DIR for Windows |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
25 if arg:match("^[%.a-zA-Z]?:?[\\/]") then |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
26 return '"' .. arg:gsub("/", "\\"):gsub('"', '\\"') .. '"' |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
27 end |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
28 -- URLs and anything else |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
29 return '"' .. arg:gsub('"', '\\"') .. '"' |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
30 end |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
31 |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
32 --- Return an absolute pathname from a potentially relative one. |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
33 -- @param pathname string: pathname to convert. |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
34 -- @param relative_to string or nil: path to prepend when making |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
35 -- pathname absolute, or the current dir in the dir stack if |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
36 -- not given. |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
37 -- @return string: The pathname converted to absolute. |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
38 function absolute_name(pathname, relative_to) |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
39 assert(type(pathname) == "string") |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
40 assert(type(relative_to) == "string" or not relative_to) |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
41 |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
42 relative_to = relative_to or fs.current_dir() |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
43 -- FIXME I'm not sure this first \\ should be there at all. |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
44 -- What are the Windows rules for drive letters? |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
45 if pathname:match("^[\\.a-zA-Z]?:?[\\/]") then |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
46 return pathname |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
47 else |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
48 return relative_to .. "/" .. pathname |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
49 end |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
50 end |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
51 |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
52 --- Create a wrapper to make a script executable from the command-line. |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
53 -- @param file string: Pathname of script to be made executable. |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
54 -- @param dest string: Directory where to put the wrapper. |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
55 -- @return boolean or (nil, string): True if succeeded, or nil and |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
56 -- an error message. |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
57 function wrap_script(file, dest) |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
58 assert(type(file) == "string") |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
59 assert(type(dest) == "string") |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
60 |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
61 local base = dir.base_name(file) |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
62 local wrapname = fs.is_dir(dest) and dest.."/"..base or dest |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
63 wrapname = wrapname..".bat" |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
64 local wrapper = io.open(wrapname, "w") |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
65 if not wrapper then |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
66 return nil, "Could not open "..wrapname.." for writing." |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
67 end |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
68 wrapper:write("@echo off\n") |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
69 wrapper:write("setlocal\n") |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
70 wrapper:write('set LUA_PATH='..package.path..";%LUA_PATH%\n") |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
71 wrapper:write('set LUA_CPATH='..package.cpath..";%LUA_CPATH%\n") |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
72 wrapper:write('"'..dir.path(cfg.variables["LUA_BINDIR"], cfg.lua_interpreter)..'" -lluarocks.loader "'..file..'" %*\n') |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
73 wrapper:write("endlocal\n") |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
74 wrapper:close() |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
75 return true |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
76 end |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
77 |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
78 function is_actual_binary(name) |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
79 name = name:lower() |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
80 if name:match("%.bat$") or name:match("%.exe$") then |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
81 return true |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
82 end |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
83 return false |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
84 end |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
85 |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
86 function copy_binary(filename, dest) |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
87 local ok, err = fs.copy(filename, dest) |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
88 if not ok then |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
89 return nil, err |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
90 end |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
91 local exe_pattern = "%.[Ee][Xx][Ee]$" |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
92 local base = dir.base_name(filename) |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
93 local dest = dir.dir_name(dest) |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
94 if base:match(exe_pattern) then |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
95 base = base:gsub(exe_pattern, ".lua") |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
96 local helpname = dest.."/"..base |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
97 local helper = io.open(helpname, "w") |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
98 if not helper then |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
99 return nil, "Could not open "..helpname.." for writing." |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
100 end |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
101 helper:write('package.path=\"'..package.path:gsub("\\","\\\\")..';\"..package.path\n') |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
102 helper:write('package.cpath=\"'..package.path:gsub("\\","\\\\")..';\"..package.cpath\n') |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
103 helper:close() |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
104 end |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
105 return true |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
106 end |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
107 |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
108 function chmod(filename, mode) |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
109 return true |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
110 end |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
111 |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
112 function get_permissions(filename) |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
113 return "" |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
114 end |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
115 |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
116 --- Move a file on top of the other. |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
117 -- The new file ceases to exist under its original name, |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
118 -- and takes over the name of the old file. |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
119 -- On Windows this is done by removing the original file and |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
120 -- renaming the new file to its original name. |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
121 -- @param old_file The name of the original file, |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
122 -- which will be the new name of new_file. |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
123 -- @param new_file The name of the new file, |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
124 -- which will replace old_file. |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
125 -- @return boolean or (nil, string): True if succeeded, or nil and |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
126 -- an error message. |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
127 function replace_file(old_file, new_file) |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
128 os.remove(old_file) |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
129 return os.rename(new_file, old_file) |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
130 end |
d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
HackBot
parents:
diff
changeset
|
131 |