feat: add support for parsing auto_update from shorebird.yaml (#65)
* feat: add support for parsing auto_update from shorebird.yaml The actual support will be in the C++ engine, but this keeps all yaml parsing in the Rust code for simplicity. --------- Co-authored-by: Felix Angelov <felix@shorebird.dev>
This commit is contained in:
@@ -55,6 +55,11 @@ SHOREBIRD_EXPORT
|
||||
bool shorebird_init(const struct AppParameters *c_params,
|
||||
const char *c_yaml);
|
||||
|
||||
/**
|
||||
* Returns if the app should run the updater automatically on launch.
|
||||
*/
|
||||
SHOREBIRD_EXPORT bool shorebird_should_auto_update(void);
|
||||
|
||||
/**
|
||||
* The currently running patch number, or 0 if the release has not been
|
||||
* patched.
|
||||
|
||||
@@ -109,6 +109,16 @@ pub extern "C" fn shorebird_init(
|
||||
)
|
||||
}
|
||||
|
||||
/// Returns if the app should run the updater automatically on launch.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn shorebird_should_auto_update() -> bool {
|
||||
log_on_error(
|
||||
|| updater::should_auto_update(),
|
||||
"fetching update behavior",
|
||||
true,
|
||||
)
|
||||
}
|
||||
|
||||
/// The currently running patch number, or 0 if the release has not been
|
||||
/// patched.
|
||||
#[no_mangle]
|
||||
@@ -336,6 +346,25 @@ mod test {
|
||||
free_parameters(c_params);
|
||||
}
|
||||
|
||||
#[serial]
|
||||
#[test]
|
||||
fn yaml_parsing() {
|
||||
testing_reset_config();
|
||||
let tmp_dir = TempDir::new("example").unwrap();
|
||||
let c_params = parameters(&tmp_dir, "/dir/lib/arm64/libapp.so");
|
||||
let c_yaml = c_string(
|
||||
"
|
||||
app_id: foo
|
||||
channel: bar
|
||||
base_url: baz
|
||||
auto_update: false",
|
||||
);
|
||||
assert_eq!(shorebird_init(&c_params, c_yaml), true);
|
||||
free_c_string(c_yaml);
|
||||
free_parameters(c_params);
|
||||
assert_eq!(shorebird_should_auto_update(), false);
|
||||
}
|
||||
|
||||
#[serial]
|
||||
#[test]
|
||||
fn empty_state_no_update() {
|
||||
|
||||
@@ -88,6 +88,8 @@ impl UpdaterState {
|
||||
self.failed_patches.iter().any(|v| v == &patch_number)
|
||||
}
|
||||
|
||||
// TODO(eseidel): Should return Result instead of logging, the c_api
|
||||
// layer can log if desired.
|
||||
pub fn mark_patch_as_bad(&mut self, patch_number: usize) {
|
||||
if self.is_known_good_patch(patch_number) {
|
||||
warn!("Tried to report failed launch for a known good patch. Ignoring.");
|
||||
@@ -102,6 +104,8 @@ impl UpdaterState {
|
||||
self.failed_patches.push(patch_number);
|
||||
}
|
||||
|
||||
// TODO(eseidel): Should return Result instead of logging, the c_api
|
||||
// layer can log if desired.
|
||||
pub fn mark_patch_as_good(&mut self, patch_number: usize) {
|
||||
if self.is_known_bad_patch(patch_number) {
|
||||
warn!("Tried to report successful launch for a known bad patch. Ignoring.");
|
||||
|
||||
@@ -73,6 +73,7 @@ where
|
||||
pub struct UpdateConfig {
|
||||
pub cache_dir: PathBuf,
|
||||
pub download_dir: PathBuf,
|
||||
pub auto_update: bool,
|
||||
pub channel: String,
|
||||
pub app_id: String,
|
||||
pub release_version: String,
|
||||
@@ -102,6 +103,7 @@ pub fn set_config(
|
||||
.as_deref()
|
||||
.unwrap_or(DEFAULT_CHANNEL)
|
||||
.to_owned(),
|
||||
auto_update: yaml.auto_update.unwrap_or(true),
|
||||
app_id: yaml.app_id.to_string(),
|
||||
release_version: app_config.release_version.to_string(),
|
||||
libapp_path,
|
||||
|
||||
@@ -117,6 +117,10 @@ pub fn init(app_config: AppConfig, yaml: &str) -> Result<(), UpdateError> {
|
||||
.map_err(|err| UpdateError::InvalidState(err.to_string()))
|
||||
}
|
||||
|
||||
pub fn should_auto_update() -> anyhow::Result<bool> {
|
||||
with_config(|config| Ok(config.auto_update))
|
||||
}
|
||||
|
||||
fn check_for_update_internal() -> anyhow::Result<PatchCheckResponse> {
|
||||
with_config(|config| {
|
||||
// Load UpdaterState from disk
|
||||
|
||||
@@ -10,6 +10,8 @@ pub struct YamlConfig {
|
||||
pub channel: Option<String>,
|
||||
/// Update URL. Defaults to the default update URL if not set.
|
||||
pub base_url: Option<String>,
|
||||
/// Update behavior. Defaults to true if not set.
|
||||
pub auto_update: Option<bool>,
|
||||
}
|
||||
|
||||
impl YamlConfig {
|
||||
|
||||
Reference in New Issue
Block a user