-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub.app.lua
139 lines (104 loc) · 3.34 KB
/
github.app.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
-- Reanmachine TekOS
-- APP: Github
-- Constants
asset_type_app = "app"
asset_type_lib = "lib"
github_content_path = "https://raw.githubusercontent.com"
-- Functions
function get_asset_remote_path(name, asset_type)
if asset_type == asset_type_app then
return name..".app.lua"
end
if asset_type == asset_type_lib then
return name..".lib.lua"
end
error("Unknown asset type: "..asset_type)
end
function get_asset_target_path(name, asset_type)
if asset_type == asset_type_app then
return "/"..name
end
if asset_type == asset_type_lib then
return "/libs/"..name
end
error("Unknown asset type: "..asset_type)
end
---
-- Downloads a file from a github project
-- @param project The project identity (eg Reanmachine/something)
-- @param revision The revision to get (if none, will use 'master')
-- @param file The file to download (relative path from project root)
-- @param dest The destination file to download it into locally
function get_github_resource(project, revision, file)
if not project then error("project cannot be nil") end
if type(project) ~= "string" then error("project must be a string") end
-- Default the revision to master
if not revision then
revision = "master"
end
local project_content_path = github_content_path.."/"..project
local project_revision_path = project_content_path.."/"..revision
local project_file_path = project_revision_path.."/"..file
local web_result = http.get(project_file_path)
if not web_result then
return {found=false, code=0, data=""}
end
local found = web_result.getResponseCode() == 200
return {found=found, code=web_result.getResponseCode(), data=web_result.readAll()}
end
function get_asset(project, revision, name, asset_type)
local target_file = get_asset_remote_path(name, asset_type)
local result = get_github_resource(project, revision, target_file)
if not result or not result.found then
local reason = nil
if result.code == 404 then reason = "File not Found" end
if result.code == 403 then reason = "Forbidden" end
local message = string.format(
"Unable to Retrieve %s '%s' from repository '%s'",
asset_type,
name,
project)
if not reason then
print(message..": "..reason)
else
print(message)
end
return false
end
local target_path = get_asset_target_path(name, asset_type)
local target_output = fs.open(target_path, "w")
target_output.write(result.data)
target_output.flush()
target_output.close()
print(string.format(
"%s '%s' downloaded successfully to '%s'",
asset_type,
name,
target_path))
end
function usage()
print("github <project> <type> <name>")
print(" <project> - The GitHub User/Repository path of the project. (eg: mojang/minecraft)")
print(" <type> - 'app' or 'lib' to denote package type.")
print(" <name> - The name relative to the root of the project.")
end
-- Application Logic
local arg = { ... }
if #arg < 3 then
print("ERROR: not enough arugments")
usage()
exit()
end
local project = arg[1]
local asset_type = arg[2]
local asset_name = arg[3]
if asset_type == asset_type_app then
print(string.format("Downloading App '%s' from '%s'...", asset_name, project))
elseif asset_type == asset_type_lib then
print(string.format("Downloading Lib '%s' from '%s'...", asset_name, project))
else
print("ERROR: invalid asset type '"..asset_type.."'")
usage()
exit()
end
get_asset(project, nil, asset_name, asset_type)