Skip to content

Commit a634720

Browse files
committed
Fix #1293: Exit with code 1 if an error occurred
Builds should now, in most cases, properly fail if buildaction failed.
1 parent fd7220d commit a634720

File tree

5 files changed

+178
-60
lines changed

5 files changed

+178
-60
lines changed

utils/buildactions/install_cef.lua

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,6 @@ function make_cef_download_url()
1616
return CEF_URL_PREFIX..http.escapeUrlParam(CEF_VERSION)..CEF_URL_SUFFIX
1717
end
1818

19-
function errormsg(title, message)
20-
term.pushColor(term.red)
21-
io.write(title)
22-
if message then
23-
term.setTextColor(term.purple)
24-
print(" " .. message)
25-
else
26-
print()
27-
end
28-
term.popColor()
29-
end
30-
3119
function update_install_cef(version, hash)
3220
local filename = "utils/buildactions/install_cef.lua"
3321
local f = io.open(filename)
@@ -73,12 +61,14 @@ newaction {
7361
local resource, result_str, result_code = http.get("https://cef-builds.spotifycdn.com/index.json")
7462
if result_str ~= "OK" or result_code ~= 200 then
7563
errormsg(("Could not get page with status code %s: "):format(response_code), result_str)
64+
os.exit(1)
7665
return
7766
end
7867

7968
local meta, err = json.decode(resource)
8069
if err then
8170
errormsg("Could not parse json meta data:", err)
71+
os.exit(1)
8272
return
8373
end
8474

@@ -108,7 +98,11 @@ newaction {
10898
end
10999

110100
if not os.isdir(CEF_PATH) then
111-
os.mkdir(CEF_PATH)
101+
if not os.mkdir(CEF_PATH) then
102+
errmsg("ERROR: Could not create cef folder")
103+
os.exit(1)
104+
return
105+
end
112106
end
113107

114108
-- Check file hash
@@ -120,9 +114,8 @@ newaction {
120114

121115
-- Download CEF
122116
print("Downloading CEF " .. CEF_VERSION .. "...")
123-
local result_str, response_code = http.download(make_cef_download_url(), archive_path)
124-
if result_str ~= "OK" or response_code ~= 200 then
125-
errormsg(("Could not download CEF with status code %s: "):format(response_code), result_str)
117+
if not http.download_print_errors(make_cef_download_url(), archive_path) then
118+
os.exit(1)
126119
return
127120
end
128121

@@ -152,18 +145,40 @@ newaction {
152145
end
153146

154147
-- Delete old CEF files
155-
os.rmdir(CEF_PATH)
156-
os.mkdir(CEF_PATH)
148+
if not os.rmdir(CEF_PATH) then
149+
errmsg("ERROR: Could not delete cef folder")
150+
os.exit(1)
151+
return
152+
end
153+
154+
if not os.mkdir(CEF_PATH) then
155+
errmsg("ERROR: Could not create cef folder (2)")
156+
os.exit(1)
157+
return
158+
end
157159

158160
-- Extract first bz2 and then tar
159-
os.extract_archive(archive_path, CEF_PATH, true) -- Extract .tar.bz2 to .tar
160-
os.extract_archive(CEF_PATH.."temp.tar", CEF_PATH, true) -- Extract .tar
161+
if not os.extract_archive(archive_path, CEF_PATH, true) then -- Extract .tar.bz2 to .tar
162+
errmsg("ERROR: Could not extract .tar.bz2")
163+
os.exit(1)
164+
return
165+
end
166+
167+
if not os.extract_archive(CEF_PATH.."temp.tar", CEF_PATH, true) then -- Extract .tar
168+
errmsg("ERROR: Could not extract .tar")
169+
os.exit(1)
170+
return
171+
end
161172

162173
-- Move all files from cef_binary*/* to ./
163174
os.expanddir_wildcard(CEF_PATH.."cef_binary*", CEF_PATH)
164175

165176
-- Delete .tar archive, but keep .tar.bz2 for checksumming
166-
os.remove(CEF_PATH.."temp.tar")
177+
if not os.remove(CEF_PATH.."temp.tar") then
178+
errmsg("ERROR: Could not remove temp.tar")
179+
os.exit(1)
180+
return
181+
end
167182
end
168183
}
169184

utils/buildactions/install_data.lua

Lines changed: 86 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,41 +21,110 @@ newaction {
2121

2222
execute = function()
2323
-- Make Bin directory if not exists
24-
os.mkdir(BIN_DIR)
24+
local success, message = os.mkdir(BIN_DIR)
25+
if not success then
26+
errmsg("ERROR: Couldn't create Bin directory", "\n"..message)
27+
os.exit(1)
28+
return
29+
end
2530

2631
-- Copy data files
2732
if os.host() == "windows" then
28-
os.copydir(DATA_DIR, BIN_DIR)
33+
local success, message = os.copydir(DATA_DIR, BIN_DIR)
34+
if not success then
35+
errmsg("ERROR: Couldn't create copy data directory", "\n"..message)
36+
os.exit(1)
37+
return
38+
end
2939
end
3040

3141
-- Copy configs if they don't already exist
32-
os.copydir("Server/mods/deathmatch", BIN_DIR.."/server/mods/deathmatch", "*.conf", false, true)
33-
os.copydir("Server/mods/deathmatch", BIN_DIR.."/server/mods/deathmatch", "*.xml", false, true)
42+
local success, message = os.copydir("Server/mods/deathmatch", BIN_DIR.."/server/mods/deathmatch", "*.conf", false, true)
43+
if not success then
44+
errmsg("ERROR: Couldn't copy server config files", "\n"..message)
45+
os.exit(1)
46+
return
47+
end
48+
49+
local success, message = os.copydir("Server/mods/deathmatch", BIN_DIR.."/server/mods/deathmatch", "*.xml", false, true)
50+
if not success then
51+
errmsg("ERROR: Couldn't copy server xml files", "\n"..message)
52+
os.exit(1)
53+
return
54+
end
3455

3556
-- Make sure server/x64 directory exists
36-
os.mkdir(BIN_DIR.."/server/x64")
57+
local success, message = os.mkdir(BIN_DIR.."/server/x64")
58+
if not success then
59+
errmsg("ERROR: Couldn't create server/x64 directory", "\n"..message)
60+
os.exit(1)
61+
return
62+
end
3763

3864
if os.host() == "windows" then
39-
http.download_print_errors(NET_PATH_X86_WIN, BIN_DIR.."/server/net.dll")
40-
http.download_print_errors(NET_PATH_X64_WIN, BIN_DIR.."/server/x64/net.dll")
41-
http.download_print_errors(NETC_PATH_WIN, BIN_DIR.."/MTA/netc.dll")
65+
local success = http.download_print_errors(NET_PATH_X86_WIN, BIN_DIR.."/server/net.dll")
66+
success = success and http.download_print_errors(NET_PATH_X64_WIN, BIN_DIR.."/server/x64/net.dll")
67+
success = success and http.download_print_errors(NETC_PATH_WIN, BIN_DIR.."/MTA/netc.dll")
68+
69+
-- A download failed
70+
if not success then
71+
os.exit(1)
72+
return
73+
end
4274

43-
os.copyfile(BIN_DIR.."/MTA/netc.dll", BIN_DIR.."/MTA/netc_d.dll")
44-
os.copyfile(BIN_DIR.."/server/net.dll", BIN_DIR.."/server/net_d.dll")
45-
os.copyfile(BIN_DIR.."/server/x64/net.dll", BIN_DIR.."/server/x64/net_d.dll")
75+
if not os.copyfile(BIN_DIR.."/MTA/netc.dll", BIN_DIR.."/MTA/netc_d.dll") then
76+
errmsg("ERROR: Could not copy netc.dll")
77+
os.exit(1)
78+
return
79+
end
80+
81+
if not os.copyfile(BIN_DIR.."/server/net.dll", BIN_DIR.."/server/net_d.dll") then
82+
errmsg("ERROR: Could not copy server/net.dll")
83+
os.exit(1)
84+
return
85+
end
86+
87+
if not os.copyfile(BIN_DIR.."/server/x64/net.dll", BIN_DIR.."/server/x64/net_d.dll") then
88+
errmsg("ERROR: Could not copy server/x64/net.dll")
89+
os.exit(1)
90+
return
91+
end
4692
elseif os.host() == "macosx" then
4793
local c = string.char(27)
4894
print(string.format("Listen, I ain't leaving here till you tell me where the macOS net builds are.\n " ..
4995
" So come on bub, for old times' sake, huh?\n\t%s[45m%s[37mDid you just call me... BLOB?%s[0m\n", c,c,c,c))
5096

51-
http.download_print_errors(NET_PATH_X64_MACOS, BIN_DIR.."/server/x64/net.dylib")
52-
os.copyfile(BIN_DIR.."/server/x64/net.dylib", BIN_DIR.."/server/x64/net_d.dylib")
97+
if not http.download_print_errors(NET_PATH_X64_MACOS, BIN_DIR.."/server/x64/net.dylib") then
98+
os.exit(1)
99+
return
100+
end
101+
102+
if not os.copyfile(BIN_DIR.."/server/x64/net.dylib", BIN_DIR.."/server/x64/net_d.dylib") then
103+
errmsg("ERROR: Could not copy server/x64/net.dylib")
104+
os.exit(1)
105+
return
106+
end
53107
else
54-
http.download_print_errors(NET_PATH_X86_LINUX, BIN_DIR.."/server/net.so")
55-
http.download_print_errors(NET_PATH_X64_LINUX, BIN_DIR.."/server/x64/net.so")
108+
local success = http.download_print_errors(NET_PATH_X86_LINUX, BIN_DIR.."/server/net.so")
109+
success = success and http.download_print_errors(NET_PATH_X64_LINUX, BIN_DIR.."/server/x64/net.so")
110+
111+
-- A download failed
112+
if not success then
113+
os.exit(1)
114+
return
115+
end
116+
117+
if not os.copyfile(BIN_DIR.."/server/net.so", BIN_DIR.."/server/net_d.so") then
118+
errmsg("ERROR: Could not copy server/net.so")
119+
os.exit(1)
120+
return
121+
end
56122

57-
os.copyfile(BIN_DIR.."/server/net.so", BIN_DIR.."/server/net_d.so")
58-
os.copyfile(BIN_DIR.."/server/x64/net.so", BIN_DIR.."/server/x64/net_d.so")
123+
if not os.copyfile(BIN_DIR.."/server/x64/net.so", BIN_DIR.."/server/x64/net_d.so") then
124+
errmsg("ERROR: Could not copy server/x64/net.so")
125+
os.exit(1)
126+
return
127+
end
59128
end
60129
end
61130
}

utils/buildactions/install_resources.lua

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,35 @@
1-
premake.modules.install_resources = {}
1+
require 'utils'
22

3-
local unzip = function(zip_path, target_path)
4-
zip_path = path.translate(zip_path)
5-
target_path = path.translate(target_path)
3+
premake.modules.install_resources = {}
64

7-
if os.host() == "windows" then
8-
os.executef("call \"utils\\7z\\7za.exe\" x \"%s\" -aoa -o\"%s\"", zip_path, target_path)
9-
else
10-
os.executef("unzip \"%s\" -d \"%s\"", zip_path, target_path)
11-
end
12-
end
5+
-- Config variables
6+
local RESOURCES_URL = "https://mirror.mtasa.com/mtasa/resources/mtasa-resources-latest.zip"
7+
local EXTRACT_DIR = "Bin/server/mods/deathmatch/resources/"
138

149
newaction {
1510
trigger = "install_resources",
1611
description = "Installs the resources to the bin directory",
1712

1813
execute = function()
1914
-- Download resources
20-
http.download("http://mirror.mtasa.com/mtasa/resources/mtasa-resources-latest.zip", "temp_resources.zip")
15+
if not http.download_print_errors(RESOURCES_URL, "temp_resources.zip") then
16+
os.exit(1)
17+
return
18+
end
2119

2220
-- Extract resources
23-
unzip("temp_resources.zip", "Bin/server/mods/deathmatch/resources/")
24-
21+
if not os.extract_archive("temp_resources.zip", EXTRACT_DIR, true) then
22+
errmsg("ERROR: Couldn't unzip resources")
23+
os.exit(1)
24+
return
25+
end
26+
2527
-- Cleanup
26-
os.remove("temp_resources.zip")
28+
if not os.remove("temp_resources.zip") then
29+
errmsg("ERROR: Couldn't delete downloaded resources zip file")
30+
os.exit(1)
31+
return
32+
end
2733
end
2834
}
2935

utils/buildactions/install_unifont.lua

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,17 @@ newaction {
2727
-- Download Unifont
2828
print("Downloading Unifont...")
2929
if not http.download_print_errors(UNIFONT_BASEURL..UNIFONT_TAG.."/"..UNIFONT_DOWNLOAD_FILENAME, archive_path) then
30+
os.exit(1)
3031
return
3132
end
3233

3334
-- Check downloaded file hash
34-
if os.sha256_file(archive_path) ~= UNIFONT_HASH then
35-
print("Unifont hash mismatch!")
35+
local download_hash = os.sha256_file(archive_path)
36+
if download_hash ~= UNIFONT_HASH then
37+
errormsg("ERROR: Unifont hash mismatch!", ("\nExpected %s, got %s"):format(UNIFONT_HASH, download_hash))
3638
-- Delete bad file
3739
os.remove(archive_path)
40+
os.exit(1)
3841
return
3942
end
4043

utils/buildactions/utils.lua

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,17 @@ function os.expanddir_wildcard(from, to)
7979
if not dir then return end
8080

8181
-- TODO: Optimize this
82-
os.copydir(dir, to)
83-
os.rmdir(dir)
82+
if not os.copydir(dir, to) then
83+
errmsg("ERROR: Couldn't copy directory", ("\nTried to copy %s to %s"):format(dir, to))
84+
os.exit(1)
85+
return
86+
end
87+
88+
if not os.rmdir(dir) then
89+
errmsg("ERROR: Couldn't remove directory", ("\nTried to remove %s"):format(dir))
90+
os.exit(1)
91+
return
92+
end
8493
end
8594

8695
function os.sha256_file(path)
@@ -110,21 +119,37 @@ function os.extract_archive(archive_path, target_path, override)
110119
local flags = override and "-aoa" or "-aos"
111120

112121
if os.host() == "windows" then
113-
os.executef("call \"utils\\7z\\7za.exe\" x \"%s\" %s -o\"%s\"", archive_path, flags, target_path)
122+
return os.executef("call \"utils\\7z\\7za.exe\" x \"%s\" %s -o\"%s\"", archive_path, flags, target_path)
114123
else
115-
os.executef("7z x \"%s\" %s -o\"%s\"", archive_path, flags, target_path)
124+
if not os.executef("7z x \"%s\" %s -o\"%s\"", archive_path, flags, target_path) then
125+
return os.executef("unzip \"%s\" -d \"%s\"", archive_path, target_path)
126+
end
116127
end
128+
129+
return false
117130
end
118131

119132
function http.download_print_errors(url, file, options)
120133
local result_str, response_code = http.download(url, file, options)
121134
if result_str ~= "OK" then
122-
print( "\nERROR: Failed to download " .. url .. "\n" .. result_str .. " (" .. response_code .. ")" )
135+
errormsg("ERROR: Download failed", "\nFailed to download " .. url .. "\n" .. result_str .. " (" .. response_code .. ")" )
123136
if response_code == 0 then
124137
-- No response code means server was unreachable
125-
print( "Check premake5 is not blocked by firewall rules" )
138+
errormsg("NOTICE: Server was unreachable", "\nCheck server address is correct and that premake5 is not blocked by firewall rules")
126139
end
127140
return false
128141
end
129142
return true
130143
end
144+
145+
function errormsg(title, message)
146+
term.pushColor(term.red)
147+
io.write(title)
148+
if message then
149+
term.setTextColor(term.purple)
150+
print(" " .. message)
151+
else
152+
print()
153+
end
154+
term.popColor()
155+
end

0 commit comments

Comments
 (0)