The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

127 lines
4.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. #include "../../Application/jucer_Headers.h"
  19. #include "jucer_VersionInfo.h"
  20. VersionInfo::VersionInfo (String versionIn, String releaseNotesIn, std::vector<Asset> assetsIn)
  21. : versionString (std::move (versionIn)),
  22. releaseNotes (std::move (releaseNotesIn)),
  23. assets (std::move (assetsIn))
  24. {}
  25. std::unique_ptr<VersionInfo> VersionInfo::fetchFromUpdateServer (const String& versionString)
  26. {
  27. return fetch ("tags/" + versionString);
  28. }
  29. std::unique_ptr<VersionInfo> VersionInfo::fetchLatestFromUpdateServer()
  30. {
  31. return fetch ("latest");
  32. }
  33. std::unique_ptr<InputStream> VersionInfo::createInputStreamForAsset (const Asset& asset, int& statusCode)
  34. {
  35. URL downloadUrl (asset.url);
  36. StringPairArray responseHeaders;
  37. return std::unique_ptr<InputStream> (downloadUrl.createInputStream (URL::InputStreamOptions (URL::ParameterHandling::inAddress)
  38. .withExtraHeaders ("Accept: application/octet-stream")
  39. .withConnectionTimeoutMs (5000)
  40. .withResponseHeaders (&responseHeaders)
  41. .withStatusCode (&statusCode)
  42. .withNumRedirectsToFollow (1)));
  43. }
  44. bool VersionInfo::isNewerVersionThanCurrent()
  45. {
  46. jassert (versionString.isNotEmpty());
  47. auto currentTokens = StringArray::fromTokens (ProjectInfo::versionString, ".", {});
  48. auto thisTokens = StringArray::fromTokens (versionString, ".", {});
  49. jassert (thisTokens.size() == 3);
  50. if (currentTokens[0].getIntValue() == thisTokens[0].getIntValue())
  51. {
  52. if (currentTokens[1].getIntValue() == thisTokens[1].getIntValue())
  53. return currentTokens[2].getIntValue() < thisTokens[2].getIntValue();
  54. return currentTokens[1].getIntValue() < thisTokens[1].getIntValue();
  55. }
  56. return currentTokens[0].getIntValue() < thisTokens[0].getIntValue();
  57. }
  58. std::unique_ptr<VersionInfo> VersionInfo::fetch (const String& endpoint)
  59. {
  60. URL latestVersionURL ("https://api.github.com/repos/juce-framework/JUCE/releases/" + endpoint);
  61. std::unique_ptr<InputStream> inStream (latestVersionURL.createInputStream (URL::InputStreamOptions (URL::ParameterHandling::inAddress)
  62. .withConnectionTimeoutMs (5000)));
  63. if (inStream == nullptr)
  64. return nullptr;
  65. auto content = inStream->readEntireStreamAsString();
  66. auto latestReleaseDetails = JSON::parse (content);
  67. auto* json = latestReleaseDetails.getDynamicObject();
  68. if (json == nullptr)
  69. return nullptr;
  70. auto versionString = json->getProperty ("tag_name").toString();
  71. if (versionString.isEmpty())
  72. return nullptr;
  73. auto* assets = json->getProperty ("assets").getArray();
  74. if (assets == nullptr)
  75. return nullptr;
  76. auto releaseNotes = json->getProperty ("body").toString();
  77. std::vector<VersionInfo::Asset> parsedAssets;
  78. for (auto& asset : *assets)
  79. {
  80. if (auto* assetJson = asset.getDynamicObject())
  81. {
  82. parsedAssets.push_back ({ assetJson->getProperty ("name").toString(),
  83. assetJson->getProperty ("url").toString() });
  84. jassert (parsedAssets.back().name.isNotEmpty());
  85. jassert (parsedAssets.back().url.isNotEmpty());
  86. }
  87. else
  88. {
  89. jassertfalse;
  90. }
  91. }
  92. return std::unique_ptr<VersionInfo> (new VersionInfo { versionString, releaseNotes, std::move (parsedAssets) });
  93. }