1- // clang-format off
2- #include " utils/cortex_utils.h"
3- // clang-format on
41#include " cortex_upd_cmd.h"
52#include " httplib.h"
63#include " nlohmann/json.hpp"
107#include " utils/file_manager_utils.h"
118#include " utils/logging_utils.h"
129#include " utils/system_info_utils.h"
10+ #include " utils/url_parser.h"
1311
1412namespace commands {
1513
16- CortexUpdCmd::CortexUpdCmd () {}
17-
1814void CortexUpdCmd::Exec (std::string v) {
1915 {
2016 auto config = file_manager_utils::GetCortexConfig ();
@@ -38,14 +34,7 @@ void CortexUpdCmd::Exec(std::string v) {
3834}
3935
4036bool CortexUpdCmd::GetStableAndBeta (const std::string& v) {
41- // Check if the architecture and OS are supported
4237 auto system_info = system_info_utils::GetSystemInfo ();
43- if (system_info.arch == system_info_utils::kUnsupported ||
44- system_info.os == system_info_utils::kUnsupported ) {
45- CTL_ERR (" Unsupported OS or architecture: " << system_info.os << " , "
46- << system_info.arch );
47- return false ;
48- }
4938 CTL_INF (" OS: " << system_info.os << " , Arch: " << system_info.arch );
5039
5140 // Download file
@@ -84,38 +73,35 @@ bool CortexUpdCmd::GetStableAndBeta(const std::string& v) {
8473 for (auto & asset : assets) {
8574 auto asset_name = asset[" name" ].get <std::string>();
8675 if (asset_name == matched_variant) {
87- std::string host{" https://github.com" };
88-
89- auto full_url = asset[" browser_download_url" ].get <std::string>();
90- std::string path = full_url.substr (host.length ());
91-
92- auto fileName = asset[" name" ].get <std::string>();
93- CTL_INF (" URL: " << full_url);
94-
95- auto download_task = DownloadTask{.id = " cortex" ,
96- .type = DownloadType::Cortex,
97- .error = std::nullopt ,
98- .items = {DownloadItem{
99- .id = " cortex" ,
100- .host = host,
101- .fileName = fileName,
102- .type = DownloadType::Cortex,
103- .path = path,
104- }}};
105-
106- DownloadService download_service;
107- download_service.AddDownloadTask (
108- download_task,
109- [this ](const std::string& absolute_path, bool unused) {
76+ auto download_url =
77+ asset[" browser_download_url" ].get <std::string>();
78+ auto file_name = asset[" name" ].get <std::string>();
79+ CTL_INF (" Download url: " << download_url);
80+
81+ auto local_path =
82+ file_manager_utils::GetExecutableFolderContainerPath () /
83+ " cortex" / asset_name;
84+ auto download_task{DownloadTask{.id = " cortex" ,
85+ .type = DownloadType::Cortex,
86+ .items = {DownloadItem{
87+ .id = " cortex" ,
88+ .downloadUrl = download_url,
89+ .localPath = local_path,
90+ }}}};
91+
92+ DownloadService ().AddDownloadTask (
93+ download_task, [](const DownloadTask& finishedTask) {
11094 // try to unzip the downloaded file
111- std::filesystem:: path download_path{absolute_path};
112- CTL_INF ( " Downloaded engine path: " << download_path .string ());
95+ CTL_INF ( " Downloaded engine path: "
96+ << finishedTask. items [ 0 ]. localPath .string ());
11397
114- std::filesystem::path extract_path =
115- download_path.parent_path ().parent_path ();
98+ auto extract_path = finishedTask.items [0 ]
99+ .localPath .parent_path ()
100+ .parent_path ();
116101
117- archive_utils::ExtractArchive (download_path.string (),
118- extract_path.string ());
102+ archive_utils::ExtractArchive (
103+ finishedTask.items [0 ].localPath .string (),
104+ extract_path.string ());
119105
120106 CTL_INF (" Finished!" );
121107 });
@@ -145,56 +131,59 @@ bool CortexUpdCmd::GetStableAndBeta(const std::string& v) {
145131}
146132
147133bool CortexUpdCmd::GetNightly (const std::string& v) {
148- // Check if the architecture and OS are supported
149134 auto system_info = system_info_utils::GetSystemInfo ();
150- if (system_info.arch == system_info_utils::kUnsupported ||
151- system_info.os == system_info_utils::kUnsupported ) {
152- CTL_ERR (" Unsupported OS or architecture: " << system_info.os << " , "
153- << system_info.arch );
154- return false ;
155- }
156135 CTL_INF (" OS: " << system_info.os << " , Arch: " << system_info.arch );
157136
158137 // Download file
159138 std::string version = v.empty () ? " latest" : std::move (v);
160- std::ostringstream release_path;
161- release_path << " cortex/" << version << " /" << system_info.os << " -"
162- << system_info.arch << " /" << kNightlyFileName ;
163- CTL_INF (" Engine release path: " << kNightlyHost << " /" << release_path.str ());
164-
165- auto download_task = DownloadTask{.id = " cortex" ,
166- .type = DownloadType::Cortex,
167- .error = std::nullopt ,
168- .items = {DownloadItem{
169- .id = " cortex" ,
170- .host = kNightlyHost ,
171- .fileName = kNightlyFileName ,
172- .type = DownloadType::Cortex,
173- .path = release_path.str (),
174- }}};
175-
176- DownloadService download_service;
177- download_service.AddDownloadTask (
178- download_task, [this ](const std::string& absolute_path, bool unused) {
139+ std::string os_arch{system_info.os + " -" + system_info.arch };
140+ const char * paths[] = {
141+ " cortex" ,
142+ version.c_str (),
143+ os_arch.c_str (),
144+ kNightlyFileName ,
145+ };
146+ std::vector<std::string> path_list (paths, std::end (paths));
147+ auto url_obj = url_parser::Url{
148+ .protocol = " https" ,
149+ .host = kNightlyHost ,
150+ .pathParams = path_list,
151+ };
152+
153+ CTL_INF (" Engine release path: " << url_parser::FromUrl (url_obj));
154+
155+ std::filesystem::path localPath =
156+ file_manager_utils::GetExecutableFolderContainerPath () / " cortex" /
157+ path_list.back ();
158+ auto download_task =
159+ DownloadTask{.id = " cortex" ,
160+ .type = DownloadType::Cortex,
161+ .items = {DownloadItem{
162+ .id = " cortex" ,
163+ .downloadUrl = url_parser::FromUrl (url_obj),
164+ .localPath = localPath,
165+ }}};
166+
167+ DownloadService ().AddDownloadTask (
168+ download_task, [](const DownloadTask& finishedTask) {
179169 // try to unzip the downloaded file
180- std::filesystem:: path download_path{absolute_path};
181- CTL_INF ( " Downloaded engine path: " << download_path .string ());
170+ CTL_INF ( " Downloaded engine path: "
171+ << finishedTask. items [ 0 ]. localPath .string ());
182172
183- std::filesystem::path extract_path =
184- download_path .parent_path ().parent_path ();
173+ auto extract_path =
174+ finishedTask. items [ 0 ]. localPath .parent_path ().parent_path ();
185175
186- archive_utils::ExtractArchive (download_path .string (),
176+ archive_utils::ExtractArchive (finishedTask. items [ 0 ]. localPath .string (),
187177 extract_path.string ());
188178
189179 CTL_INF (" Finished!" );
190180 });
191181
192- // Replace binay file
182+ // Replace binary file
193183 auto executable_path = file_manager_utils::GetExecutableFolderContainerPath ();
194184 auto src = std::filesystem::temp_directory_path () / " cortex" / kCortexBinary /
195185 GetCortexBinary ();
196186 auto dst = executable_path / GetCortexBinary ();
197187 return ReplaceBinaryInflight (src, dst);
198188}
199-
200- } // namespace commands
189+ } // namespace commands
0 commit comments