You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

updater.cpp 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include <updater.hpp>
  2. #include <settings.hpp>
  3. #include <app/common.hpp>
  4. #include <network.hpp>
  5. #include <system.hpp>
  6. #include <app.hpp>
  7. #include <window.hpp>
  8. #include <asset.hpp>
  9. #include <thread>
  10. namespace rack {
  11. namespace updater {
  12. std::string version;
  13. std::string changelogUrl;
  14. float progress = 0.f;
  15. static std::string downloadUrl;
  16. static void checkVersion() {
  17. std::string versionUrl = app::API_URL + "/version";
  18. json_t* resJ = network::requestJson(network::METHOD_GET, versionUrl, NULL);
  19. if (!resJ) {
  20. WARN("Request for version failed");
  21. return;
  22. }
  23. DEFER({
  24. json_decref(resJ);
  25. });
  26. json_t* versionJ = json_object_get(resJ, "version");
  27. if (versionJ)
  28. version = json_string_value(versionJ);
  29. json_t* changelogUrlJ = json_object_get(resJ, "changelogUrl");
  30. if (changelogUrlJ)
  31. changelogUrl = json_string_value(changelogUrlJ);
  32. json_t* downloadUrlsJ = json_object_get(resJ, "downloadUrls");
  33. if (downloadUrlsJ) {
  34. json_t* downloadUrlJ = json_object_get(downloadUrlsJ, app::APP_ARCH.c_str());
  35. if (downloadUrlJ)
  36. downloadUrl = json_string_value(downloadUrlJ);
  37. }
  38. }
  39. void init() {
  40. if (!settings::devMode) {
  41. std::thread t([] {
  42. checkVersion();
  43. });
  44. t.detach();
  45. }
  46. }
  47. void update() {
  48. if (downloadUrl == "")
  49. return;
  50. // Download update
  51. std::string filename = string::filename(network::urlPath(downloadUrl));
  52. std::string path = asset::user(filename);
  53. INFO("Downloading update %s to %s", downloadUrl.c_str(), path.c_str());
  54. network::requestDownload(downloadUrl, path, &progress);
  55. #if defined ARCH_WIN
  56. // Launch the installer
  57. INFO("Launching update %s", path.c_str());
  58. system::runProcessDetached(path);
  59. #elif defined ARCH_MAC
  60. std::string cmd;
  61. // std::string appPath = asset::userDir + "/Rack.app";
  62. // cmd = "rm -rf '" + appPath + "'";
  63. // std::system(cmd.c_str());
  64. // // Unzip app using Apple's unzipper, since Rack's unzipper doesn't handle the metadata stuff correctly.
  65. // cmd = "unzip -q '" + path + "' -d '" + asset::userDir + "'";
  66. // std::system(cmd.c_str());
  67. // // Open app in Finder
  68. // cmd = "open -R '" + appPath + "'";
  69. // std::system(cmd.c_str());
  70. // Open Archive Utility
  71. cmd = "open '" + path + "'";
  72. std::system(cmd.c_str());
  73. #elif defined ARCH_LIN
  74. system::openFolder(asset::user(""));
  75. #endif
  76. APP->window->close();
  77. }
  78. bool isUpdateAvailable() {
  79. return (version != "") && (version != app::APP_VERSION);
  80. }
  81. } // namespace updater
  82. } // namespace rack