From aa229e9942897b26eda8b492e4314ee5a2f23f1c Mon Sep 17 00:00:00 2001
From: hisham <hisham@9ca3f7c1-7366-0410-b1a3-b5c78f85698c>
Date: Thu, 30 Jul 2009 01:26:23 +0000
Subject: generalize 'get_rockspec' into 'download', which is able to download
 .src.rocks, rocks and rockspecs

git-svn-id: http://luarocks.org/svn/luarocks/trunk@43 9ca3f7c1-7366-0410-b1a3-b5c78f85698c
---
 src/bin/luarocks              |  2 +-
 src/luarocks/download.lua     | 85 +++++++++++++++++++++++++++++++++++++++++++
 src/luarocks/get_rockspec.lua | 81 -----------------------------------------
 3 files changed, 86 insertions(+), 82 deletions(-)
 create mode 100644 src/luarocks/download.lua
 delete mode 100644 src/luarocks/get_rockspec.lua

(limited to 'src')

diff --git a/src/bin/luarocks b/src/bin/luarocks
index 2e2ac422..80b53dee 100755
--- a/src/bin/luarocks
+++ b/src/bin/luarocks
@@ -15,6 +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")
+commands.download = require("luarocks.download")
 
 command_line.run_command(...)
diff --git a/src/luarocks/download.lua b/src/luarocks/download.lua
new file mode 100644
index 00000000..5dc393d6
--- /dev/null
+++ b/src/luarocks/download.lua
@@ -0,0 +1,85 @@
+
+--- Module implementing the luarocks "download" command.
+-- Download a rock from the repository.
+module("luarocks.download", 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 rock file from a rocks server."
+help_arguments = "[--all] [--source] [--arch=<arch>] [<name> [<version>]]"
+
+help = [[
+--all          Download multiple rock files if there is more than one match.
+--source       Download .src.rock if available.
+--arch=<arch>  Download rock for a specific architecture.
+]]
+
+local function download(rock_file)
+   local rock = fetch.fetch_url(rock_file)
+   return rock ~= nil
+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 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)
+   if flags["source"] then
+      query.arch = "src"
+   elseif flags["rockspec"] then
+      query.arch = "rockspec"
+   elseif flags["arch"] then
+      query.arch = flags["arch"]
+   end
+   local results, err
+   if flags["all"] then
+      if name == "" then query.exact_name = false end
+      results, err = search.search_repos(query)
+   else
+      results, err = search.find_suitable_rock(query)
+   end
+   if type(results) == "string" then
+      return download(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 = download(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..(version and " "..version or "").."."
+   end
+end
diff --git a/src/luarocks/get_rockspec.lua b/src/luarocks/get_rockspec.lua
deleted file mode 100644
index 77fb6d21..00000000
--- a/src/luarocks/get_rockspec.lua
+++ /dev/null
@@ -1,81 +0,0 @@
-
---- 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] [<name> [<version>]]"
-
-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
-- 
cgit v1.2.3-55-g6feb