diff --git a/engine/src/flutter/shell/common/shorebird/BUILD.gn b/engine/src/flutter/shell/common/shorebird/BUILD.gn index 4e31c785af768..2c7def6991542 100644 --- a/engine/src/flutter/shell/common/shorebird/BUILD.gn +++ b/engine/src/flutter/shell/common/shorebird/BUILD.gn @@ -41,11 +41,15 @@ if (enable_unittests) { executable("shorebird_unittests") { testonly = true - sources = [ "snapshots_data_handle_unittests.cc" ] + sources = [ + "shorebird_unittests.cc", + "snapshots_data_handle_unittests.cc", + ] # This only includes snapshots_data_handle and not shorebird because # shorebird fails to link due to a missing updater lib. deps = [ + ":shorebird", ":shorebird_fixtures", ":snapshots_data_handle", "//flutter/runtime", diff --git a/engine/src/flutter/shell/common/shorebird/shorebird.cc b/engine/src/flutter/shell/common/shorebird/shorebird.cc index 602a0521ae7b0..130f4cc3bae08 100644 --- a/engine/src/flutter/shell/common/shorebird/shorebird.cc +++ b/engine/src/flutter/shell/common/shorebird/shorebird.cc @@ -80,16 +80,47 @@ FileCallbacks ShorebirdFileCallbacks() { }; } +// Given the contents of a yaml file, return the given value if it exists, +// otherwise return an empty string. +// Does not support nested keys. +std::string GetValueFromYaml(const std::string& yaml, const std::string& key) { + std::stringstream ss(yaml); + std::string line; + std::string prefix = key + ":"; + while (std::getline(ss, line, '\n')) { + if (line.find(prefix) != std::string::npos) { + auto ret = line.substr(line.find(prefix) + prefix.size()); + + // Remove leading and trailing spaces + while (!ret.empty() && std::isspace(ret.front())) { + ret.erase(0, 1); + } + while (!ret.empty() && std::isspace(ret.back())) { + ret.pop_back(); + } + return ret; + } + } + return ""; +} + // FIXME: consolidate this with the other ConfigureShorebird bool ConfigureShorebird(const ShorebirdConfigArgs& args, std::string& patch_path) { patch_path = args.release_app_library_path; auto shorebird_updater_dir_name = "shorebird_updater"; + // Parse app id from shorebird.yaml + std::string app_id = GetValueFromYaml(args.shorebird_yaml, "appid"); + if (app_id.empty()) { + FML_LOG(ERROR) << "Shorebird updater: appid not found in shorebird.yaml"; + return false; + } + auto code_cache_dir = fml::paths::JoinPaths( - {std::move(args.code_cache_path), shorebird_updater_dir_name}); + {std::move(args.code_cache_path), shorebird_updater_dir_name, app_id}); auto app_storage_dir = fml::paths::JoinPaths( - {std::move(args.app_storage_path), shorebird_updater_dir_name}); + {std::move(args.app_storage_path), shorebird_updater_dir_name, app_id}); fml::CreateDirectory(fml::paths::GetCachesDirectory(), {shorebird_updater_dir_name}, diff --git a/engine/src/flutter/shell/common/shorebird/shorebird.h b/engine/src/flutter/shell/common/shorebird/shorebird.h index 3e9ccce9ee3f6..c6873c5014a00 100644 --- a/engine/src/flutter/shell/common/shorebird/shorebird.h +++ b/engine/src/flutter/shell/common/shorebird/shorebird.h @@ -40,6 +40,8 @@ void ConfigureShorebird(std::string code_cache_path, const std::string& version, const std::string& version_code); +std::string GetValueFromYaml(const std::string& yaml, const std::string& key); + } // namespace flutter #endif // FLUTTER_SHELL_COMMON_SHOREBIRD_SHOREBIRD_H_ diff --git a/engine/src/flutter/shell/common/shorebird/shorebird_unittests.cc b/engine/src/flutter/shell/common/shorebird/shorebird_unittests.cc new file mode 100644 index 0000000000000..7c108ac1e6231 --- /dev/null +++ b/engine/src/flutter/shell/common/shorebird/shorebird_unittests.cc @@ -0,0 +1,21 @@ +#include "flutter/shell/common/shorebird/shorebird.h" + +#include "gtest/gtest.h" + +namespace flutter { +namespace testing { +TEST(Shorebird, GetValueFromYamlValueExists) { + std::string yaml = "appid: com.example.app\nversion: 1.0.0\n"; + std::string key = "appid"; + std::string value = GetValueFromYaml(yaml, key); + EXPECT_EQ(value, "com.example.app"); +} + +TEST(Shorebird, GetValueFromYamlValueDoesNotExist) { + std::string yaml = "appid: com.example.app\nversion: 1.0.0\n"; + std::string key = "appid2"; + std::string value = GetValueFromYaml(yaml, key); + EXPECT_EQ(value, ""); +} +} // namespace testing +} // namespace flutter \ No newline at end of file diff --git a/engine/src/flutter/shell/platform/linux/fl_shorebird.cc b/engine/src/flutter/shell/platform/linux/fl_shorebird.cc index 39e62f4c35416..eb913ad720c61 100644 --- a/engine/src/flutter/shell/platform/linux/fl_shorebird.cc +++ b/engine/src/flutter/shell/platform/linux/fl_shorebird.cc @@ -24,20 +24,8 @@ gboolean SetUpShorebird(const char* assets_path, std::string& patch_path) { return false; } - // Read appid from shorebird.yaml - std::string appid = ""; - std::stringstream ss(shorebird_yaml_contents); - std::string line; - std::string appid_prefix = "appid:"; - while (std::getline(ss, line, '\n')) { - if (line.find(appid_prefix) != std::string::npos) { - appid = line.substr(line.find(appid_prefix) + appid_prefix.size()); - break; - } - } - std::string code_cache_path = - fml::paths::JoinPaths({g_get_home_dir(), ".shorebird_cache", appid}); + fml::paths::JoinPaths({g_get_home_dir(), ".shorebird_cache"}); auto executable_location = fml::paths::GetExecutableDirectoryPath().second; auto app_path = fml::paths::JoinPaths({executable_location, "lib", "libapp.so"});