From 5bd20308b881c63dd886b71395dbc9baaab25dfd Mon Sep 17 00:00:00 2001 From: Hisham Date: Fri, 28 Oct 2016 20:48:00 -0200 Subject: Keep only functions shared among commands --- src/luarocks/build.lua | 111 +--------------------------------------------- src/luarocks/download.lua | 45 +------------------ src/luarocks/pack.lua | 43 ++---------------- src/luarocks/remove.lua | 50 --------------------- src/luarocks/search.lua | 72 +----------------------------- 5 files changed, 9 insertions(+), 312 deletions(-) diff --git a/src/luarocks/build.lua b/src/luarocks/build.lua index f3b054d2..ceaa20dd 100644 --- a/src/luarocks/build.lua +++ b/src/luarocks/build.lua @@ -1,43 +1,15 @@ ---- Module implementing the LuaRocks "build" command. --- Builds a rock, compiling its C parts if any. local build = {} -local pack = require("luarocks.pack") local path = require("luarocks.path") local util = require("luarocks.util") -local repos = require("luarocks.repos") local fetch = require("luarocks.fetch") local fs = require("luarocks.fs") local dir = require("luarocks.dir") local deps = require("luarocks.deps") -local writer = require("luarocks.manif.writer") -local remove = require("luarocks.remove") local cfg = require("luarocks.core.cfg") - -build.help_summary = "Build/compile a rock." -build.help_arguments = "[--pack-binary-rock] [--keep] {|| []}" -build.help = [[ -Build and install a rock, compiling its C parts if any. -Argument may be a rockspec file, a source rock file -or the name of a rock to be fetched from a repository. - ---pack-binary-rock Do not install rock. Instead, produce a .rock file - with the contents of compilation in the current - directory. - ---keep Do not remove previously installed versions of the - rock after building a new one. This behavior can - be made permanent by setting keep_other_versions=true - in the configuration file. - ---branch= Override the `source.branch` field in the loaded - rockspec. Allows to specify a different branch to - fetch. Particularly for SCM rocks. - ---only-deps Installs only the dependencies of the rock. - -]]..util.deps_mode_help() +local repos = require("luarocks.repos") +local writer = require("luarocks.manif.writer") --- Install files to a given location. -- Takes a table where the array part is a list of filenames to be copied. @@ -361,83 +333,4 @@ function build.build_rockspec(rockspec_file, need_to_fetch, minimal_mode, deps_m return name, version end ---- Build and install a rock. --- @param rock_file string: local or remote filename of a rock. --- @param need_to_fetch boolean: true if sources need to be fetched, --- false if the rockspec was obtained from inside a source rock. --- @param deps_mode: string: Which trees to check dependencies for: --- "one" for the current default tree, "all" for all trees, --- "order" for all trees with priority >= the current default, "none" for no trees. --- @param build_only_deps boolean: true to build the listed dependencies only. --- @return boolean or (nil, string, [string]): True if build was successful, --- or false and an error message and an optional error code. -function build.build_rock(rock_file, need_to_fetch, deps_mode, build_only_deps) - assert(type(rock_file) == "string") - assert(type(need_to_fetch) == "boolean") - - local ok, err, errcode - local unpack_dir - unpack_dir, err, errcode = fetch.fetch_and_unpack_rock(rock_file) - if not unpack_dir then - return nil, err, errcode - end - local rockspec_file = path.rockspec_name_from_rock(rock_file) - ok, err = fs.change_dir(unpack_dir) - if not ok then return nil, err end - ok, err, errcode = build.build_rockspec(rockspec_file, need_to_fetch, false, deps_mode, build_only_deps) - fs.pop_dir() - return ok, err, errcode -end - -local function do_build(name, version, deps_mode, build_only_deps) - if name:match("%.rockspec$") then - return build.build_rockspec(name, true, false, deps_mode, build_only_deps) - elseif name:match("%.src%.rock$") then - return build.build_rock(name, false, deps_mode, build_only_deps) - elseif name:match("%.all%.rock$") then - local install = require("luarocks.install") - local install_fun = build_only_deps and install.install_binary_rock_deps or install.install_binary_rock - return install_fun(name, deps_mode) - elseif name:match("%.rock$") then - return build.build_rock(name, true, deps_mode, build_only_deps) - elseif not name:match(dir.separator) then - local search = require("luarocks.search") - return search.act_on_src_or_rockspec(do_build, name:lower(), version, nil, deps_mode, build_only_deps) - end - return nil, "Don't know what to do with "..name -end - ---- Driver function for "build" command. --- @param name string: A local or remote rockspec or rock file. --- If a package name is given, forwards the request to "search" and, --- if returned a result, installs the matching rock. --- @param version string: When passing a package name, a version number may --- also be given. --- @return boolean or (nil, string, exitcode): True if build was successful; nil and an --- error message otherwise. exitcode is optionally returned. -function build.command(flags, name, version) - if type(name) ~= "string" then - return nil, "Argument missing. "..util.see_help("build") - end - assert(type(version) == "string" or not version) - - if flags["pack-binary-rock"] then - return pack.pack_binary_rock(name, version, do_build, name, version, deps.get_deps_mode(flags)) - else - local ok, err = fs.check_command_permissions(flags) - if not ok then return nil, err, cfg.errorcodes.PERMISSIONDENIED end - ok, err = do_build(name, version, deps.get_deps_mode(flags), flags["only-deps"]) - if not ok then return nil, err end - name, version = ok, err - if flags["only-deps"] then - return name, version - end - if (not flags["keep"]) and not cfg.keep_other_versions then - local ok, err = remove.remove_other_versions(name, version, flags["force"], flags["force-fast"]) - if not ok then util.printerr(err) end - end - return name, version - end -end - return build diff --git a/src/luarocks/download.lua b/src/luarocks/download.lua index 557d1b65..c0f2678f 100644 --- a/src/luarocks/download.lua +++ b/src/luarocks/download.lua @@ -1,9 +1,5 @@ - ---- Module implementing the luarocks "download" command. --- Download a rock from the repository. local download = {} -local util = require("luarocks.util") local path = require("luarocks.path") local fetch = require("luarocks.fetch") local search = require("luarocks.search") @@ -11,16 +7,6 @@ local fs = require("luarocks.fs") local dir = require("luarocks.dir") local cfg = require("luarocks.core.cfg") -download.help_summary = "Download a specific rock file from a rocks server." -download.help_arguments = "[--all] [--arch= | --source | --rockspec] [ []]" - -download.help = [[ ---all Download all files if there are multiple matches. ---source Download .src.rock if available. ---rockspec Download .rockspec if available. ---arch= Download rock for a specific architecture. -]] - local function get_file(filename) local protocol, pathname = dir.split_url(filename) if protocol == "file" then @@ -46,8 +32,8 @@ function download.download(arch, name, version, all) local has_result = false local all_ok = true local any_err = "" - for name, result in pairs(results) do - for version, items in pairs(result) do + for _, result in pairs(results) do + for _, items in pairs(result) do for _, item in ipairs(items) do -- Ignore provided rocks. if item.arch ~= "installed" then @@ -77,31 +63,4 @@ function download.download(arch, name, version, all) (search_err and ": "..search_err or ".") end ---- Driver function for the "download" command. --- @param name string: a rock name. --- @param version string or nil: if the name of a package is given, a --- version may also be passed. --- @return boolean or (nil, string): true if successful or nil followed --- by an error message. -function download.command(flags, name, version) - assert(type(version) == "string" or not version) - if type(name) ~= "string" and not flags["all"] then - return nil, "Argument missing. "..util.see_help("download") - end - if not name then name, version = "", "" end - - local arch - - if flags["source"] then - arch = "src" - elseif flags["rockspec"] then - arch = "rockspec" - elseif flags["arch"] then - arch = flags["arch"] - end - - local dl, err = download.download(arch, name:lower(), version, flags["all"]) - return dl and true, err -end - return download diff --git a/src/luarocks/pack.lua b/src/luarocks/pack.lua index 655cbf37..b721a209 100644 --- a/src/luarocks/pack.lua +++ b/src/luarocks/pack.lua @@ -1,6 +1,5 @@ ---- Module implementing the LuaRocks "pack" command. --- Creates a rock, packing sources or binaries. +-- Create rock files, packing sources or binaries. local pack = {} local unpack = unpack or table.unpack @@ -15,15 +14,6 @@ local dir = require("luarocks.dir") local manif = require("luarocks.manif") local search = require("luarocks.search") -pack.help_summary = "Create a rock, packing sources or binaries." -pack.help_arguments = "{| []}" -pack.help = [[ -Argument may be a rockspec file, for creating a source rock, -or the name of an installed package, for creating a binary rock. -In the latter case, the app version may be given as a second -argument. -]] - --- Create a source rock. -- Packages a rockspec and its required source files in a rock -- file with the .src.rock extension, which can later be built and @@ -86,7 +76,7 @@ end -- @param tree string or nil: An optional tree to pick the package from. -- @return string or (nil, string): The filename of the resulting -- .src.rock file; or nil and an error message. -local function do_pack_binary_rock(name, version, tree) +function pack.pack_installed_rock(name, version, tree) assert(type(name) == "string") assert(type(version) == "string" or not version) @@ -160,34 +150,7 @@ function pack.pack_binary_rock(name, version, cmd, ...) if not rname then rname, rversion = name, version end - return do_pack_binary_rock(rname, rversion, temp_dir) -end - ---- Driver function for the "pack" command. --- @param arg string: may be a rockspec file, for creating a source rock, --- or the name of an installed package, for creating a binary rock. --- @param version string or nil: if the name of a package is given, a --- version may also be passed. --- @return boolean or (nil, string): true if successful or nil followed --- by an error message. -function pack.command(flags, arg, version) - assert(type(version) == "string" or not version) - if type(arg) ~= "string" then - return nil, "Argument missing. "..util.see_help("pack") - end - - local file, err - if arg:match(".*%.rockspec") then - file, err = pack.pack_source_rock(arg) - else - file, err = do_pack_binary_rock(arg:lower(), version, flags["tree"]) - end - if err then - return nil, err - else - util.printout("Packed: "..file) - return true - end + return pack.pack_installed_rock(rname, rversion, temp_dir) end return pack diff --git a/src/luarocks/remove.lua b/src/luarocks/remove.lua index e7f37604..6cc8334f 100644 --- a/src/luarocks/remove.lua +++ b/src/luarocks/remove.lua @@ -1,6 +1,3 @@ - ---- Module implementing the LuaRocks "remove" command. --- Uninstalls rocks. local remove = {} local search = require("luarocks.search") @@ -10,19 +7,6 @@ local repos = require("luarocks.repos") local path = require("luarocks.path") local util = require("luarocks.util") local cfg = require("luarocks.core.cfg") -local fs = require("luarocks.fs") - -remove.help_summary = "Uninstall a rock." -remove.help_arguments = "[--force|--force-fast] []" -remove.help = [[ -Argument is the name of a rock to be uninstalled. -If a version is not given, try to remove all versions at once. -Will only perform the removal if it does not break dependencies. -To override this check and force the removal, use --force. -To perform a forced removal without reporting dependency issues, -use --force-fast. - -]]..util.deps_mode_help() --- Obtain a list of packages that depend on the given set of packages -- (where all packages of the set are versions of one program). @@ -128,38 +112,4 @@ function remove.remove_other_versions(name, version, force, fast) return true end ---- Driver function for the "remove" command. --- @param name string: name of a rock. If a version is given, refer to --- a specific version; otherwise, try to remove all versions. --- @param version string: When passing a package name, a version number --- may also be given. --- @return boolean or (nil, string, exitcode): True if removal was --- successful, nil and an error message otherwise. exitcode is optionally returned. -function remove.command(flags, name, version) - if type(name) ~= "string" then - return nil, "Argument missing. "..util.see_help("remove") - end - - local deps_mode = flags["deps-mode"] or cfg.deps_mode - - local ok, err = fs.check_command_permissions(flags) - if not ok then return nil, err, cfg.errorcodes.PERMISSIONDENIED end - - local rock_type = name:match("%.(rock)$") or name:match("%.(rockspec)$") - local filename = name - if rock_type then - name, version = path.parse_name(filename) - if not name then return nil, "Invalid "..rock_type.." filename: "..filename end - end - - local results = {} - name = name:lower() - search.manifest_search(results, cfg.rocks_dir, search.make_query(name, version)) - if not results[name] then - return nil, "Could not find rock '"..name..(version and " "..version or "").."' in "..path.rocks_tree_to_string(cfg.root_dir) - end - - return remove.remove_search_results(results, name, deps_mode, flags["force"], flags["force-fast"]) -end - return remove diff --git a/src/luarocks/search.lua b/src/luarocks/search.lua index 44eff694..c59f9534 100644 --- a/src/luarocks/search.lua +++ b/src/luarocks/search.lua @@ -1,9 +1,5 @@ - ---- Module implementing the LuaRocks "search" command. --- Queries LuaRocks servers. local search = {} - local dir = require("luarocks.dir") local path = require("luarocks.path") local manif = require("luarocks.manif") @@ -11,17 +7,6 @@ local deps = require("luarocks.deps") local cfg = require("luarocks.core.cfg") local util = require("luarocks.util") -search.help_summary = "Query the LuaRocks servers." -search.help_arguments = "[--source] [--binary] { [] | --all }" -search.help = [[ ---source Return only rockspecs and source rocks, - to be used with the "build" command. ---binary Return only pure Lua and binary rocks (rocks that can be used - with the "install" command without requiring a C toolchain). ---all List all contents of the server that are suitable to - this platform, do not filter by name. -]] - --- Convert the arch field of a query table to table format. -- @param query table: A query table. local function query_arch_as_table(query) @@ -55,7 +40,7 @@ end -- identifier), "rockspec" or "installed" -- @param repo string: Pathname of a local repository of URL of -- rocks server. -local function store_result(results, name, version, arch, repo) +function search.store_result(results, name, version, arch, repo) assert(type(results) == "table") assert(type(name) == "string") assert(type(version) == "string") @@ -108,7 +93,7 @@ local function store_if_match(results, repo, name, version, arch, query) if match_name(query, name) then if query.arch[arch] or query.arch["any"] then if deps.match_constraints(deps.parse_version(version), query.constraints) then - store_result(results, name, version, arch, repo) + search.store_result(results, name, version, arch, repo) end end end @@ -370,28 +355,6 @@ function search.print_results(results, porcelain) end end ---- Splits a list of search results into two lists, one for "source" results --- to be used with the "build" command, and one for "binary" results to be --- used with the "install" command. --- @param results table: A search results table. --- @return (table, table): Two tables, one for source and one for binary --- results. -local function split_source_and_binary_results(results) - local sources, binaries = {}, {} - for name, versions in pairs(results) do - for version, repositories in pairs(versions) do - for _, repo in ipairs(repositories) do - local where = sources - if repo.arch == "all" or repo.arch == cfg.arch then - where = binaries - end - store_result(where, name, version, repo.arch, repo.repo) - end - end - end - return sources, binaries -end - --- Given a name and optionally a version, try to find in the rocks -- servers a single .src.rock or .rockspec file that satisfies -- the request, and run the given function on it; or display to the @@ -448,35 +411,4 @@ function search.pick_installed_rock(name, version, given_tree) return name, version, repo, repo_url end ---- Driver function for "search" command. --- @param name string: A substring of a rock name to search. --- @param version string or nil: a version may also be passed. --- @return boolean or (nil, string): True if build was successful; nil and an --- error message otherwise. -function search.command(flags, name, version) - if flags["all"] then - name, version = "", nil - end - - if type(name) ~= "string" and not flags["all"] then - return nil, "Enter name and version or use --all. "..util.see_help("search") - end - - local query = search.make_query(name:lower(), version) - query.exact_name = false - local results, err = search.search_repos(query) - local porcelain = flags["porcelain"] - util.title("Search results:", porcelain, "=") - local sources, binaries = split_source_and_binary_results(results) - if next(sources) and not flags["binary"] then - util.title("Rockspecs and source rocks:", porcelain) - search.print_results(sources, porcelain) - end - if next(binaries) and not flags["source"] then - util.title("Binary and pure-Lua rocks:", porcelain) - search.print_results(binaries, porcelain) - end - return true -end - return search -- cgit v1.2.3-55-g6feb