From f1e7565c7a93b213f09216aaa2c50e10df44d187 Mon Sep 17 00:00:00 2001 From: hisham Date: Thu, 16 Jul 2009 21:14:12 +0000 Subject: add get_rockspec command git-svn-id: http://luarocks.org/svn/luarocks/trunk@41 9ca3f7c1-7366-0410-b1a3-b5c78f85698c --- src/bin/luarocks | 1 + src/luarocks/fetch.lua | 13 +++++-- src/luarocks/get_rockspec.lua | 81 +++++++++++++++++++++++++++++++++++++++++++ src/luarocks/search.lua | 2 +- 4 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 src/luarocks/get_rockspec.lua (limited to 'src') diff --git a/src/bin/luarocks b/src/bin/luarocks index ae0c2b42..2e2ac422 100755 --- a/src/bin/luarocks +++ b/src/bin/luarocks @@ -15,5 +15,6 @@ commands.search = require("luarocks.search") commands.list = require("luarocks.list") commands.remove = require("luarocks.remove") commands.make = require("luarocks.make") +commands.get_rockspec = require("luarocks.get_rockspec") command_line.run_command(...) diff --git a/src/luarocks/fetch.lua b/src/luarocks/fetch.lua index 4e17c91d..fc0535a9 100644 --- a/src/luarocks/fetch.lua +++ b/src/luarocks/fetch.lua @@ -200,9 +200,11 @@ end -- Only the LuaRocks runtime loader should use -- load_local_rockspec directly. -- @param filename string: Local or remote filename of a rockspec. +-- @param location string or nil: Where to download. If not given, +-- a temporary dir is created. -- @return table or (nil, string, [string]): A table representing the rockspec -- or nil followed by an error message and optional error code. -function load_rockspec(filename) +function load_rockspec(filename, location) assert(type(filename) == "string") local name = dir.base_name(filename):match("(.*)%.rockspec") @@ -210,7 +212,14 @@ function load_rockspec(filename) return nil, "Filename '"..filename.."' does not look like a rockspec." end - local filename, err, errcode = fetch_url_at_temp_dir(filename,"luarocks-rockspec-"..name) + local err, errcode + if location then + fs.change_dir(location) + filename, err = fetch_url(filename) + fs.pop_dir() + else + filename, err, errcode = fetch_url_at_temp_dir(filename,"luarocks-rockspec-"..name) + end if not filename then return nil, err, errcode end diff --git a/src/luarocks/get_rockspec.lua b/src/luarocks/get_rockspec.lua new file mode 100644 index 00000000..77fb6d21 --- /dev/null +++ b/src/luarocks/get_rockspec.lua @@ -0,0 +1,81 @@ + +--- Module implementing the luarocks "get_rockspec" command. +-- Download a rockspec from the repository. +module("luarocks.get_rockspec", package.seeall) + +local util = require("luarocks.util") +local path = require("luarocks.path") +local fetch = require("luarocks.fetch") +local search = require("luarocks.search") + +help_summary = "Download a specific rockspec file from a rocks server." +help_arguments = "[--all] [ []]" + +help = [[ +--all Download multiple rockspec files if there is more than one match. +]] + +local function get_rockspec(rockspec_file) + local rockspec = fetch.load_rockspec(rockspec_file, ".") + if not rockspec then + return nil, "Failed loading rockspec "..rockspec_file + end + return true +end + +--- Driver function for the "get_rockspec" 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 run(...) + local flags, name, version = util.parse_flags(...) + + assert(type(version) == "string" or not version) + if type(name) ~= "string" and not flags["all"] then + return nil, "Argument missing, see help." + end + if not name then name, version = "", "" end + + local query = search.make_query(name, version) + query.arch = "rockspec" + local results, err + if flags["all"] then + if name == "" then query.exact_name = false end + results, err = search.search_repos(query) + print(results, err) + else + results, err = search.find_suitable_rock(query) + end + if type(results) == "string" then + return get_rockspec(results) + elseif type(results) == "table" and next(results) then + if flags["all"] then + local all_ok = true + local any_err = "" + for name, result in pairs(results) do + for version, versions in pairs(result) do + for _,items in pairs(versions) do + local filename = path.make_url(items.repo, name, version, items.arch) + local ok, err = get_rockspec(filename) + if not ok then + all_ok = false + any_err = any_err .. "\n" .. err + end + end + end + end + return all_ok, any_err + else + print("Multiple search results were returned.") + print() + print("Search results:") + print("---------------") + print_results(results) + return nil, "Please narrow your query or use --all." + end + else + return nil, "Could not find a result named "..name.."." + end +end diff --git a/src/luarocks/search.lua b/src/luarocks/search.lua index 06868810..e31c0c1b 100644 --- a/src/luarocks/search.lua +++ b/src/luarocks/search.lua @@ -191,7 +191,7 @@ end -- and values are tables matching version strings to an array of -- rocks servers; if no results are found, an empty table is returned. -- In case of errors, nil and and error message are returned. -local function search_repos(query) +function search_repos(query) assert(type(query) == "table") local results = {} -- cgit v1.2.3-55-g6feb