-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create methods to manage runtimes (#74)
* feat: create methods to manage runtimes. Refactor common structs and modules * feat: upgrade TOML to 0.6.0 and allow serializing metadata * fix: store temporary files (.wws) in the project folder * fix: bubble config loading error and remove the Display impl
- Loading branch information
1 parent
d59d0f9
commit c1b1408
Showing
19 changed files
with
848 additions
and
245 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2022 VMware, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use crate::runtimes::metadata::Checksum; | ||
use anyhow::Result; | ||
|
||
// TODO: Remove it when implementing the manager | ||
#[allow(dead_code)] | ||
/// Fetch the contents of a given file and validates it | ||
/// using the Sha256. | ||
pub async fn fetch<T: AsRef<str>>(file: T) -> Result<Vec<u8>> { | ||
let body: Vec<u8> = reqwest::get(file.as_ref()).await?.bytes().await?.into(); | ||
|
||
Ok(body) | ||
} | ||
|
||
// TODO: Remove it when implementing the manager | ||
#[allow(dead_code)] | ||
/// Fetch the contents of a given file and validates it | ||
/// using the Sha256. | ||
pub async fn fetch_and_validate<T: AsRef<str>>(file: T, checksum: &Checksum) -> Result<Vec<u8>> { | ||
let body: Vec<u8> = fetch(file).await?; | ||
checksum.validate(&body)?; | ||
|
||
Ok(body) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
//// Copyright 2022 VMware, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use super::{ | ||
metadata::{RemoteFile, Runtime as RuntimeMetadata}, | ||
modules::{javascript::JavaScriptRuntime, native::NativeRuntime}, | ||
runtime::Runtime, | ||
}; | ||
use crate::{fetch::fetch_and_validate, store::Store}; | ||
use anyhow::{anyhow, Result}; | ||
use std::path::Path; | ||
|
||
// A collection of methods to manage runtimes | ||
|
||
/// Initializes a runtime based on the file extension. In the future, | ||
/// This will contain a more complete struct that will identify local | ||
/// runtimes. | ||
pub fn init_runtime(project_root: &Path, path: &Path) -> Result<Box<dyn Runtime + Sync + Send>> { | ||
if let Some(ext) = path.extension() { | ||
let ext_as_str = ext.to_str().unwrap(); | ||
|
||
match ext_as_str { | ||
"js" => Ok(Box::new(JavaScriptRuntime::new( | ||
project_root, | ||
path.to_path_buf(), | ||
)?)), | ||
"wasm" => Ok(Box::new(NativeRuntime::new(path.to_path_buf()))), | ||
_ => Err(anyhow!(format!( | ||
"The '{}' extension does not have an associated runtime", | ||
ext_as_str | ||
))), | ||
} | ||
} else { | ||
Err(anyhow!("The given file does not have a valid extension")) | ||
} | ||
} | ||
|
||
// TODO: Remove it when implementing the full logic | ||
#[allow(dead_code)] | ||
// Install a given runtime based on its metadata | ||
pub async fn install_runtime( | ||
project_root: &Path, | ||
repository: &str, | ||
metadata: &RuntimeMetadata, | ||
) -> Result<()> { | ||
let store = Store::new( | ||
project_root, | ||
&["runtimes", repository, &metadata.name, &metadata.version], | ||
)?; | ||
|
||
// Install the different files | ||
download_file(&metadata.binary, &store).await?; | ||
|
||
if let Some(polyfill) = &metadata.polyfill { | ||
download_file(polyfill, &store).await?; | ||
} | ||
|
||
if let Some(template) = &metadata.template { | ||
download_file(template, &store).await?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
// TODO: Remove it when implementing the full logic | ||
async fn download_file(file: &RemoteFile, store: &Store) -> Result<()> { | ||
let contents = fetch_and_validate(&file.url, &file.checksum).await?; | ||
store.write(&[&file.filename], &contents) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
// Copyright 2022 VMware, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
mod metadata; | ||
pub mod manager; | ||
pub mod metadata; | ||
mod modules; | ||
mod remote_file; | ||
mod repository; | ||
pub mod runtime; |
Oops, something went wrong.