Skip to content

Commit 6346d48

Browse files
committed
refactor(zupdater): extract getting next release and installing to functions
1 parent 5c06dfc commit 6346d48

File tree

1 file changed

+55
-32
lines changed

1 file changed

+55
-32
lines changed

src/zupdater/zupdater.cpp

Lines changed: 55 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
namespace fs = std::filesystem;
1616

1717
static bool headless;
18+
static std::string repo = getRepo();
19+
static std::string channel = getReleaseChannel();
20+
static std::string current_version = getReleaseTag();
1821

1922
std::ofstream out("updater.log", std::ios::binary);
2023

@@ -104,30 +107,17 @@ static bool is_in_osx_application_bundle()
104107
#endif
105108
}
106109

107-
int32_t main(int32_t argc, char* argv[])
110+
static void require_python()
108111
{
109-
common_main_setup(App::updater, argc, argv);
110-
111-
bool cache = used_switch(argc, argv, "-cache") > 0;
112-
headless = used_switch(argc, argv, "-headless") > 0;
113-
114-
#ifdef __APPLE__
115-
if (!is_in_osx_application_bundle() && argc > 0)
116-
{
117-
chdir(fs::path(argv[0]).parent_path().c_str());
118-
}
119-
#endif
120-
121112
std::string py_version = get_output(PYTHON, {"--version"}, "Python3 is required to run the updater");
122113
if (!py_version.starts_with("Python 3"))
123114
{
124115
fatal("Python3 is required, but found " + py_version);
125116
}
117+
}
126118

127-
std::string repo = getRepo();
128-
std::string channel = getReleaseChannel();
129-
std::string current_version = getReleaseTag();
130-
119+
static std::tuple<std::string, std::string> get_next_release()
120+
{
131121
auto [next_release_output, next_release_map] = get_output_map(PYTHON, {
132122
"tools/updater.py",
133123
"--repo", repo,
@@ -140,27 +130,18 @@ int32_t main(int32_t argc, char* argv[])
140130
}
141131
std::string new_version = next_release_map["tag_name"];
142132
std::string asset_url = next_release_map["asset_url"];
133+
return {new_version, asset_url};
134+
}
143135

144-
if (current_version == new_version)
145-
{
146-
std::string msg = fmt::format("Already on latest version: {}. Would you like to continue anyway?", new_version);
147-
if (!prompt(msg))
148-
return 0;
149-
}
150-
else
151-
{
152-
std::string msg = fmt::format("Would you like to upgrade from {} to {}? This should take less than a minute.", current_version, new_version);
153-
if (!prompt(msg))
154-
return 0;
155-
}
156-
136+
static bool install_release(std::string asset_url, bool use_cache, std::string& error)
137+
{
157138
std::vector<std::string> args = {
158139
"tools/updater.py",
159140
"--repo", repo,
160141
"--channel", channel,
161142
"--asset-url", asset_url,
162143
};
163-
if (cache) args.push_back("--cache");
144+
if (use_cache) args.push_back("--cache");
164145
auto updater_output = get_output(PYTHON, args);
165146
if (updater_output.empty())
166147
{
@@ -169,10 +150,52 @@ int32_t main(int32_t argc, char* argv[])
169150
printf("updater.py:\n%s\n", updater_output.c_str());
170151

171152
bool success = updater_output.find("success!") != std::string::npos;
153+
if (!success)
154+
error = updater_output;
155+
return success;
156+
}
157+
158+
int32_t main(int32_t argc, char* argv[])
159+
{
160+
common_main_setup(App::updater, argc, argv);
161+
162+
bool cache = used_switch(argc, argv, "-cache") > 0;
163+
headless = used_switch(argc, argv, "-headless") > 0;
164+
165+
#ifdef __APPLE__
166+
if (!is_in_osx_application_bundle() && argc > 0)
167+
{
168+
chdir(fs::path(argv[0]).parent_path().c_str());
169+
}
170+
#endif
171+
172+
require_python();
173+
174+
auto [new_version, asset_url] = get_next_release();
175+
if (new_version.empty() || asset_url.empty())
176+
{
177+
fatal("Could not find next version");
178+
}
179+
180+
if (current_version == new_version)
181+
{
182+
std::string msg = fmt::format("Already on latest version: {}. Would you like to continue anyway?", new_version);
183+
if (!prompt(msg))
184+
return 0;
185+
}
186+
else
187+
{
188+
std::string msg = fmt::format("Would you like to upgrade from {} to {}? This should take less than a minute.", current_version, new_version);
189+
if (!prompt(msg))
190+
return 0;
191+
}
192+
193+
std::string error;
194+
bool success = install_release(asset_url, cache, error);
172195
if (success)
173196
done("Done!");
174197
else
175-
fatal("Failed: " + updater_output);
198+
fatal("Failed: " + error);
176199

177200
return 0;
178201
}

0 commit comments

Comments
 (0)