Skip to content

Commit 5415059

Browse files
authored
Merge pull request #8 from unicornpkg/custom-providers
Add support for custom providers
2 parents f8a78aa + 3685ac9 commit 5415059

File tree

7 files changed

+83
-71
lines changed

7 files changed

+83
-71
lines changed

unicorn/core.lua

Lines changed: 12 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -41,62 +41,6 @@ local function getPackageData(package_name)
4141
end
4242
end
4343

44-
--- Package provider for GitHub.com.
45-
-- @param package_table table A valid package table
46-
local function install_github(package_table)
47-
for k,v in pairs(package_table.instdat.filemaps) do
48-
local http_data = unicorn.util.smartHttp("https://raw.githubusercontent.com/" .. package_table.instdat.repo_owner .."/".. package_table.instdat.repo_name .."/".. package_table.instdat.repo_ref .."/".. k)
49-
unicorn.util.fileWrite(http_data, v)
50-
end
51-
end
52-
53-
--- Package provider for GitLab.com.
54-
-- @param package_table table A valid package table
55-
local function install_gitlab(package_table)
56-
for k,v in pairs(package_table.instdat.filemaps) do
57-
local http_data = unicorn.util.smartHttp("https://gitlab.com/raw/" .. package_table.instdat.repo_owner .."/".. package_table.instdat.repo_name .."/".. package_table.instdat.repo_ref .."/".. k)
58-
unicorn.util.fileWrite(http_data, v)
59-
end
60-
end
61-
62-
--- Package provider for Bitbucket.org.
63-
-- @param package_table table A valid package table
64-
local function install_bitbucket(package_table)
65-
for k,v in pairs(package_table.instdat.filemaps) do
66-
local http_data = unicorn.util.smartHttp("https://bitbucket.org/raw/" .. package_table.instdat.repo_owner .."/".. package_table.instdat.repo_name .."/".. package_table.instdat.repo_ref .."/".. k)
67-
unicorn.util.fileWrite(http_data, v)
68-
end
69-
end
70-
71-
--- Package provider for Devbin.dev.
72-
-- @param package_table table A valid package table
73-
local function install_devbin(package_table)
74-
for k,v in pairs(package_table.instdat.filemaps) do
75-
local http_data = unicorn.util.smartHttp("https://devbin.dev/raw/" .. k)
76-
unicorn.util.fileWrite(http_data, v)
77-
end
78-
end
79-
80-
--- Package provider for Pastebin.com.
81-
-- @param package_table table A valid package table
82-
local function install_pastebin(package_table)
83-
for k,v in pairs(package_table.instdat.filemaps) do
84-
local http_data = unicorn.util.smartHttp("https://pastebin.com/raw/" .. k)
85-
unicorn.util.fileWrite(http_data, v)
86-
end
87-
end
88-
89-
--- Package provider for GitHub Gists <gists.github.com>.
90-
-- @param package_table table A valid package table
91-
local function install_gist(package_table)
92-
-- this is really simple, only works predictably with a one-file gist.
93-
for k,v in pairs(package_table.instdat.filemaps) do
94-
-- uses source code repository-like names because a Gist is a repository technically :)
95-
local http_data = unicorn.util.smartHttp("https://gist.githubusercontent.com/" .. package_table.instdat.repo_owner .."/".. package_table.instdat.repo_name .."/raw/"..package_table.instdat.repo_ref.."/"..k)
96-
unicorn.util.fileWrite(http_data, v)
97-
end
98-
end
99-
10044
--- Installs a package from a package table.
10145
-- @param package_table table A valid package table
10246
-- @return boolean
@@ -115,25 +59,22 @@ function unicorn.core.install(package_table)
11559
if getPackageData(package_table.name) then
11660
return true, getPackageData(package_table.name)
11761
end
118-
if package_table.pkgType == "com.github" then
119-
install_github(package_table)
120-
elseif package_table.pkgType == "com.github.gist" then
121-
install_gist(package_table)
122-
elseif package_table.pkgType == "com.gitlab" then
123-
install_gitlab(package_table)
124-
elseif package_table.pkgType == "org.bitbucket" then
125-
install_bitbucket(package_table)
126-
elseif package_table.pkgType == "com.pastebin" then
127-
install_pastebin(package_table)
128-
elseif package_table.pkgType == "dev.devbin" then
129-
install_devbin(package_table)
130-
else
62+
local match
63+
for _,v in pairs(fs.list("/lib/unicorn/provider/")) do -- custom provider support
64+
local provider_name = string.gsub(v, ".lua", "")
65+
if package_table.pkgType == provider_name then
66+
match = true
67+
local provider = dofile("/lib/unicorn/provider/"..v)
68+
provider(package_table)
69+
end
70+
end
71+
if not match then
13172
if package_table.pkgType == nil then
13273
error("The provided package does not have a valid package type. This is either not a package or something is wrong with the file.")
13374
else
134-
error("Package type " .. package_table.pkgType .. " is unknown. You are either missing the appropriate package provider or something is wrong with the package.")
135-
end
75+
error("Package provider " .. package_table.pkgType .. " is unknown. You are either missing the appropriate package provider or something is wrong with the package.")
13676
end
77+
end
13778
storePackageData(package_table)
13879
print("Package "..package_table.name.." installed successfully.")
13980
return true, package_table
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
local unicorn = dofile("/lib/unicorn/init.lua")
2+
-- Package provider for GitHub Gists <gists.github.com>.
3+
-- @param package_table table A valid package table
4+
local function install_gist(package_table)
5+
-- this is really simple, only works predictably with a one-file gist.
6+
for k,v in pairs(package_table.instdat.filemaps) do
7+
-- uses source code repository-like names because a Gist is a repository technically :)
8+
local http_data = unicorn.util.smartHttp("https://gist.githubusercontent.com/" .. package_table.instdat.repo_owner .."/".. package_table.instdat.repo_name .."/raw/"..package_table.instdat.repo_ref.."/"..k)
9+
unicorn.util.fileWrite(http_data, v)
10+
end
11+
end
12+
13+
return install_gist

unicorn/provider/com.github.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
local unicorn = dofile("/lib/unicorn/init.lua")
2+
3+
--- Package provider for GitHub.com.
4+
-- @param package_table table A valid package table
5+
local function install_github(package_table)
6+
for k,v in pairs(package_table.instdat.filemaps) do
7+
local http_data = unicorn.util.smartHttp("https://raw.githubusercontent.com/" .. package_table.instdat.repo_owner .."/".. package_table.instdat.repo_name .."/".. package_table.instdat.repo_ref .."/".. k)
8+
unicorn.util.fileWrite(http_data, v)
9+
end
10+
end
11+
12+
return install_github

unicorn/provider/com.gitlab.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
local unicorn = dofile("/lib/unicorn/init.lua")
2+
3+
--- Package provider for GitLab.com.
4+
-- @param package_table table A valid package table
5+
local function install_gitlab(package_table)
6+
for k,v in pairs(package_table.instdat.filemaps) do
7+
local http_data = unicorn.util.smartHttp("https://gitlab.com/raw/" .. package_table.instdat.repo_owner .."/".. package_table.instdat.repo_name .."/".. package_table.instdat.repo_ref .."/".. k)
8+
unicorn.util.fileWrite(http_data, v)
9+
end
10+
end

unicorn/provider/com.pastebin.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
local unicorn = dofile("/lib/unicorn/init.lua")
2+
3+
--- Package provider for Pastebin.com.
4+
-- @param package_table table A valid package table
5+
local function install_pastebin(package_table)
6+
for k,v in pairs(package_table.instdat.filemaps) do
7+
local http_data = unicorn.util.smartHttp("https://pastebin.com/raw/" .. k)
8+
unicorn.util.fileWrite(http_data, v)
9+
end
10+
end
11+
12+
return install_pastebin

unicorn/provider/dev.devbin.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
local unicorn = dofile("/lib/unicorn/init.lua")
2+
3+
--- Package provider for Devbin.dev.
4+
-- @param package_table table A valid package table
5+
local function install_devbin(package_table)
6+
for k,v in pairs(package_table.instdat.filemaps) do
7+
local http_data = unicorn.util.smartHttp("https://devbin.dev/raw/" .. k)
8+
unicorn.util.fileWrite(http_data, v)
9+
end
10+
end
11+
12+
return install_devbin

unicorn/provider/org.bitbucket.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
local unicorn = dofile("/lib/unicorn/init.lua")
2+
3+
--- Package provider for Bitbucket.org.
4+
-- @param package_table table A valid package table
5+
local function install_bitbucket(package_table)
6+
for k,v in pairs(package_table.instdat.filemaps) do
7+
local http_data = unicorn.util.smartHttp("https://bitbucket.org/raw/" .. package_table.instdat.repo_owner .."/".. package_table.instdat.repo_name .."/".. package_table.instdat.repo_ref .."/".. k)
8+
unicorn.util.fileWrite(http_data, v)
9+
end
10+
end
11+
12+
return install_bitbucket

0 commit comments

Comments
 (0)