| |
|
| |
|
| |
|
| | local win32 = {}
|
| |
|
| | local fs = require("luarocks.fs")
|
| |
|
| | local cfg = require("luarocks.core.cfg")
|
| | local dir = require("luarocks.dir")
|
| | local path = require("luarocks.path")
|
| | local util = require("luarocks.util")
|
| |
|
| |
|
| |
|
| |
|
| | local _prefix = "type NUL && "
|
| | local _popen, _execute = io.popen, os.execute
|
| | io.popen = function(cmd, ...) return _popen(_prefix..cmd, ...) end
|
| | os.execute = function(cmd, ...) return _execute(_prefix..cmd, ...) end
|
| |
|
| |
|
| |
|
| |
|
| | function win32.quiet(cmd)
|
| | return cmd.." 2> NUL 1> NUL"
|
| | end
|
| |
|
| |
|
| |
|
| |
|
| | function win32.quiet_stderr(cmd)
|
| | return cmd.." 2> NUL"
|
| | end
|
| |
|
| |
|
| |
|
| |
|
| | local function split_root(path)
|
| | local root = ""
|
| |
|
| | if path:match("^.:") then
|
| | root = path:sub(1, 2)
|
| | path = path:sub(3)
|
| | end
|
| |
|
| | if path:match("^[\\/]") then
|
| | root = path:sub(1, 1)
|
| | path = path:sub(2)
|
| | end
|
| |
|
| | return root, path
|
| | end
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | function win32.Q(arg)
|
| | assert(type(arg) == "string")
|
| |
|
| |
|
| | if split_root(arg) ~= "" then
|
| | arg = arg:gsub("/", "\\")
|
| | end
|
| | if arg == "\\" then
|
| | return '\\'
|
| | end
|
| |
|
| | arg = arg:gsub('\\(\\*)"', '\\%1%1"')
|
| | arg = arg:gsub('\\+$', '%0%0')
|
| | arg = arg:gsub('"', '\\"')
|
| | arg = arg:gsub('(\\*)%%', '%1%1"%%"')
|
| | return '"' .. arg .. '"'
|
| | end
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | function win32.Qb(arg)
|
| | assert(type(arg) == "string")
|
| |
|
| |
|
| | if split_root(arg) ~= "" then
|
| | arg = arg:gsub("/", "\\")
|
| | end
|
| | if arg == "\\" then
|
| | return '\\'
|
| | end
|
| |
|
| | arg = arg:gsub('\\(\\*)"', '\\%1%1"')
|
| | arg = arg:gsub('\\+$', '%0%0')
|
| | arg = arg:gsub('"', '\\"')
|
| | arg = arg:gsub('%%', '%%%%')
|
| | return '"' .. arg .. '"'
|
| | end
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | function win32.absolute_name(pathname, relative_to)
|
| | assert(type(pathname) == "string")
|
| | assert(type(relative_to) == "string" or not relative_to)
|
| |
|
| | relative_to = relative_to or fs.current_dir()
|
| | local root, rest = split_root(pathname)
|
| | if root:match("[\\/]$") then
|
| |
|
| | return pathname
|
| | else
|
| |
|
| |
|
| | return relative_to .. "/" .. rest
|
| | end
|
| | end
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | function win32.root_of(pathname)
|
| | return (split_root(fs.absolute_name(pathname)))
|
| | end
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | function win32.wrap_script(script, target, deps_mode, name, version, ...)
|
| | assert(type(script) == "string" or not script)
|
| | assert(type(target) == "string")
|
| | assert(type(deps_mode) == "string")
|
| | assert(type(name) == "string" or not name)
|
| | assert(type(version) == "string" or not version)
|
| |
|
| | local batname = target .. ".bat"
|
| | local wrapper = io.open(batname, "wb")
|
| | if not wrapper then
|
| | return nil, "Could not open "..batname.." for writing."
|
| | end
|
| |
|
| | local lpath, lcpath = path.package_paths(deps_mode)
|
| |
|
| | local luainit = {
|
| | "package.path="..util.LQ(lpath..";").."..package.path",
|
| | "package.cpath="..util.LQ(lcpath..";").."..package.cpath",
|
| | }
|
| | if target == "luarocks" or target == "luarocks-admin" then
|
| | luainit = {
|
| | "package.path="..util.LQ(package.path),
|
| | "package.cpath="..util.LQ(package.cpath),
|
| | }
|
| | end
|
| | if name and version then
|
| | local addctx = "local k,l,_=pcall(require,'luarocks.loader') _=k " ..
|
| | "and l.add_context('"..name.."','"..version.."')"
|
| | table.insert(luainit, addctx)
|
| | end
|
| |
|
| | local argv = {
|
| | fs.Qb(dir.path(cfg.variables["LUA_BINDIR"], cfg.lua_interpreter)),
|
| | "-e",
|
| | fs.Qb(table.concat(luainit, ";")),
|
| | script and fs.Qb(script) or "",
|
| | ...
|
| | }
|
| |
|
| | wrapper:write("@echo off\r\n")
|
| | wrapper:write("setlocal\r\n")
|
| | wrapper:write("set "..fs.Qb("LUAROCKS_SYSCONFDIR="..cfg.sysconfdir) .. "\r\n")
|
| | wrapper:write(table.concat(argv, " ") .. " %*\r\n")
|
| | wrapper:write("exit /b %ERRORLEVEL%\r\n")
|
| | wrapper:close()
|
| | return true
|
| | end
|
| |
|
| | function win32.is_actual_binary(name)
|
| | name = name:lower()
|
| | if name:match("%.bat$") or name:match("%.exe$") then
|
| | return true
|
| | end
|
| | return false
|
| | end
|
| |
|
| | function win32.copy_binary(filename, dest)
|
| | local ok, err = fs.copy(filename, dest)
|
| | if not ok then
|
| | return nil, err
|
| | end
|
| | local exe_pattern = "%.[Ee][Xx][Ee]$"
|
| | local base = dir.base_name(filename)
|
| | dest = dir.dir_name(dest)
|
| | if base:match(exe_pattern) then
|
| | base = base:gsub(exe_pattern, ".lua")
|
| | local helpname = dest.."/"..base
|
| | local helper = io.open(helpname, "w")
|
| | if not helper then
|
| | return nil, "Could not open "..helpname.." for writing."
|
| | end
|
| | helper:write('package.path=\"'..package.path:gsub("\\","\\\\")..';\"..package.path\n')
|
| | helper:write('package.cpath=\"'..package.path:gsub("\\","\\\\")..';\"..package.cpath\n')
|
| | helper:close()
|
| | end
|
| | return true
|
| | end
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | function win32.replace_file(old_file, new_file)
|
| | os.remove(old_file)
|
| | return os.rename(new_file, old_file)
|
| | end
|
| |
|
| | function win32.is_dir(file)
|
| | file = fs.absolute_name(file)
|
| | file = dir.normalize(file)
|
| | local fd, _, code = io.open(file, "r")
|
| | if code == 13 then
|
| | fd, _, code = io.open(file .. "\\", "r")
|
| | if code == 2 then
|
| | return true
|
| | end
|
| | end
|
| | if fd then
|
| | fd:close()
|
| | end
|
| | return false
|
| | end
|
| |
|
| | function win32.is_file(file)
|
| | file = fs.absolute_name(file)
|
| | file = dir.normalize(file)
|
| | local fd, _, code = io.open(file, "r")
|
| | if code == 13 then
|
| | fd, _, code = io.open(file .. "\\", "r")
|
| | if code == 2 then
|
| | return false
|
| | elseif code == 22 then
|
| | return true
|
| | end
|
| | end
|
| | if fd then
|
| | fd:close()
|
| | return true
|
| | end
|
| | return false
|
| | end
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | function win32.is_writable(file)
|
| | assert(file)
|
| | file = dir.normalize(file)
|
| | local result
|
| | local tmpname = 'tmpluarockstestwritable.deleteme'
|
| | if fs.is_dir(file) then
|
| | local file2 = dir.path(file, tmpname)
|
| | local fh = io.open(file2, 'wb')
|
| | result = fh ~= nil
|
| | if fh then fh:close() end
|
| | if result then
|
| |
|
| |
|
| |
|
| | result = fs.exists(file2)
|
| | end
|
| | os.remove(file2)
|
| | else
|
| | local fh = io.open(file, 'r+b')
|
| | result = fh ~= nil
|
| | if fh then fh:close() end
|
| | end
|
| | return result
|
| | end
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | function win32.make_temp_dir(name_pattern)
|
| | assert(type(name_pattern) == "string")
|
| | name_pattern = dir.normalize(name_pattern)
|
| |
|
| | local temp_dir = os.getenv("TMP") .. "/luarocks_" .. name_pattern:gsub("/", "_") .. "-" .. tostring(math.floor(math.random() * 10000))
|
| | local ok, err = fs.make_dir(temp_dir)
|
| | if ok then
|
| | return temp_dir
|
| | else
|
| | return nil, err
|
| | end
|
| | end
|
| |
|
| | function win32.tmpname()
|
| | return os.getenv("TMP")..os.tmpname()
|
| | end
|
| |
|
| | function win32.current_user()
|
| | return os.getenv("USERNAME")
|
| | end
|
| |
|
| | function win32.export_cmd(var, val)
|
| | return ("SET %s=%s"):format(var, val)
|
| | end
|
| |
|
| | function win32.system_cache_dir()
|
| | return dir.path(fs.system_temp_dir(), "cache")
|
| | end
|
| |
|
| | return win32
|
| |
|