Skip to content

Commit

Permalink
Merge pull request #221 from kinode-dao/v0.7.2
Browse files Browse the repository at this point in the history
v0.7.2
  • Loading branch information
nick1udwig authored Sep 6, 2024
2 parents 15885a4 + c786e65 commit 2a467e0
Show file tree
Hide file tree
Showing 11 changed files with 378 additions and 252 deletions.
222 changes: 77 additions & 145 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "kit"
version = "0.7.1"
version = "0.7.2"
edition = "2021"

[build-dependencies]
Expand Down Expand Up @@ -30,6 +30,7 @@ alloy = { version = "0.1.3", features = [
alloy-sol-macro = "0.7.6"
alloy-sol-types = "0.7.6"
base64 = "0.21"
cargo_metadata = "0.18"
clap = { version = "4.4", features = ["cargo", "string"] }
color-eyre = { version = "0.6", features = ["capture-spantrace"] }
dirs = "5.0"
Expand Down
5 changes: 4 additions & 1 deletion src/boot_fake_node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,10 @@ pub async fn find_releases_with_asset_if_online(
fn get_local_versions_with_prefix(prefix: &str) -> Result<Vec<String>> {
let mut versions = Vec::new();

for entry in fs::read_dir(Path::new(prefix).parent().unwrap())? {
let path = Path::new(prefix)
.parent()
.ok_or_else(|| eyre!("couldnt find directory with local runtimes"))?;
for entry in fs::read_dir(&path)? {
let entry = entry?;
let path = entry.path();
if let Some(str_path) = path.to_str() {
Expand Down
105 changes: 91 additions & 14 deletions src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const KINODE_WIT_0_8_0_URL: &str =
const WASI_VERSION: &str = "19.0.1"; // TODO: un-hardcode
const DEFAULT_WORLD_0_7_0: &str = "process";
const DEFAULT_WORLD_0_8_0: &str = "process-v0";
const KINODE_PROCESS_LIB_CRATE_NAME: &str = "kinode_process_lib";

#[derive(Debug, Clone, Serialize, Deserialize)]
struct CargoFile {
Expand Down Expand Up @@ -203,12 +204,14 @@ pub fn run_command(cmd: &mut Command, verbose: bool) -> Result<Option<(String, S
#[instrument(level = "trace", skip_all)]
pub async fn download_file(url: &str, path: &Path) -> Result<()> {
fs::create_dir_all(&KIT_CACHE)?;
let hex_url = hex::encode(url);
let hex_url_path = format!("{}/{}", KIT_CACHE, hex_url);
let hex_url_path = Path::new(&hex_url_path);
let mut hasher = Sha256::new();
hasher.update(url.as_bytes());
let hashed_url = hasher.finalize();
let hashed_url_path = Path::new(KIT_CACHE)
.join(format!("{hashed_url:x}"));

let content = if hex_url_path.exists() {
fs::read(hex_url_path)?
let content = if hashed_url_path.exists() {
fs::read(hashed_url_path)?
} else {
let response = reqwest::get(url).await?;

Expand All @@ -221,7 +224,7 @@ pub async fn download_file(url: &str, path: &Path) -> Result<()> {
}

let content = response.bytes().await?.to_vec();
fs::write(hex_url_path, &content)?;
fs::write(hashed_url_path, &content)?;
content
};

Expand Down Expand Up @@ -331,6 +334,78 @@ fn file_with_extension_exists(dir: &Path, extension: &str) -> bool {
false
}

#[instrument(level = "trace", skip_all)]
fn parse_version_from_url(url: &str) -> Result<semver::VersionReq> {
let re = regex::Regex::new(r"\?tag=v([0-9]+\.[0-9]+\.[0-9]+)$").unwrap();
if let Some(caps) = re.captures(url) {
if let Some(version) = caps.get(1) {
return Ok(semver::VersionReq::parse(&format!("^{}", version.as_str()))?);
}
}
Err(eyre!("No valid version found in the URL"))
}

#[instrument(level = "trace", skip_all)]
fn find_crate_versions(
crate_name: &str,
packages: &HashMap<cargo_metadata::PackageId, &cargo_metadata::Package>,
) -> Result<HashMap<semver::VersionReq, Vec<String>>> {
let mut versions = HashMap::new();

// Iterate over all packages
for package in packages.values() {
// Check each dependency of the package
for dependency in &package.dependencies {
if dependency.name == crate_name {
let version = if dependency.req != semver::VersionReq::default() {
dependency.req.clone()
} else {
if let Some(ref source) = dependency.source {
parse_version_from_url(source)?
} else {
semver::VersionReq::default()
}
};
versions
.entry(version)
.or_insert_with(Vec::new)
.push(package.name.clone());
}
}
}

Ok(versions)
}

#[instrument(level = "trace", skip_all)]
fn check_process_lib_version(cargo_toml_path: &Path) -> Result<()> {
let metadata = cargo_metadata::MetadataCommand::new()
.manifest_path(cargo_toml_path)
.exec()?;
let packages: HashMap<cargo_metadata::PackageId, &cargo_metadata::Package> = metadata
.packages
.iter()
.map(|package| (package.id.clone(), package))
.collect();
let versions = find_crate_versions(KINODE_PROCESS_LIB_CRATE_NAME, &packages)?;
if versions.len() > 1 {
return Err(
eyre!(
"Found different versions of {} in different crates:{}",
KINODE_PROCESS_LIB_CRATE_NAME,
versions.iter().fold(String::new(), |s, (version, crates)| {
format!("{s}\n{version}\t{crates:?}")
})
)
.with_suggestion(|| format!(
"Set all {} versions to be the same to avoid hard-to-debug errors.",
KINODE_PROCESS_LIB_CRATE_NAME,
))
);
}
Ok(())
}

#[instrument(level = "trace", skip_all)]
fn get_most_recent_modified_time(
dir: &Path,
Expand Down Expand Up @@ -568,7 +643,11 @@ async fn compile_rust_wasm_process(
// Adapt the module using wasm-tools

// For use inside of process_dir
let wasm_file_name = process_dir.file_name().and_then(|s| s.to_str()).unwrap();
let wasm_file_name = process_dir
.file_name()
.and_then(|s| s.to_str())
.unwrap()
.replace("-", "_");

let wasm_file_prefix = Path::new("target/wasm32-wasi/release");
let wasm_file = wasm_file_prefix.join(&format!("{}.wasm", wasm_file_name));
Expand Down Expand Up @@ -699,20 +778,16 @@ async fn build_wit_dir(
wit_version: Option<u32>,
) -> Result<()> {
let wit_dir = process_dir.join("target").join("wit");
if wit_dir.exists() {
fs::remove_dir_all(&wit_dir)?;
}
let wit_url = match wit_version {
None => KINODE_WIT_0_7_0_URL,
Some(0) | _ => KINODE_WIT_0_8_0_URL,
};
download_file(wit_url, &wit_dir.join("kinode.wit")).await?;
for (file_name, contents) in apis {
let destination = wit_dir.join(file_name);
if destination.exists() {
// check if contents have not changed -> no-op
let old_contents = fs::read(&destination)?;
if &old_contents == contents {
continue;
}
}
fs::write(&destination, contents)?;
}
Ok(())
Expand Down Expand Up @@ -1254,6 +1329,8 @@ pub async fn execute(
fs::create_dir_all(package_dir.join("target"))?;
fs::write(&build_with_features_path, features)?;

check_process_lib_version(&package_dir.join("Cargo.toml"))?;

let ui_dir = package_dir.join("ui");
if !ui_dir.exists() {
if ui_only {
Expand Down
7 changes: 5 additions & 2 deletions src/inject_message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ pub async fn send_request_inner(url: &str, json_data: Value) -> Result<reqwest::
format!("{}{}", url, ENDPOINT)
};
let client = reqwest::Client::new();
debug!("POSTing to {url}:\n{json_data:#?}");
let response = client.post(&url).json(&json_data).send().await?;

Ok(response)
Expand Down Expand Up @@ -154,11 +155,13 @@ pub async fn parse_response(response: reqwest::Response) -> Result<Response> {
.transpose()?
.and_then(|b| decode(b).ok());

Ok(Response {
let parsed_response = Response {
body,
lazy_load_blob_utf8: blob.clone().map(|b| String::from_utf8(b).ok()),
lazy_load_blob: blob,
})
};
debug!("Got response: {parsed_response}");
Ok(parsed_response)
}
}

Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,7 @@ async fn main() -> Result<()> {

if let Err(e) = result {
error!("{:?}", e);
std::process::exit(1);
};
Ok(())
}
92 changes: 63 additions & 29 deletions src/new/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,57 @@ fn replace_vars(
template_package_name: &str,
package_name: &str,
publisher: &str,
extension: &str,
) -> String {
let template_package_name_kebab = template_package_name.replace("_", "-");
let template_package_name_upper_camel = snake_to_upper_camel_case(template_package_name);
let template_package_name_snake = template_package_name.replace("-", "_");
let template_package_name_upper_camel = snake_to_upper_camel_case(&template_package_name_snake);

let package_name_kebab = package_name.replace("_", "-");
let package_name_upper_camel = snake_to_upper_camel_case(package_name);
let package_name_snake = package_name.replace("-", "_");
let package_name_upper_camel = snake_to_upper_camel_case(&package_name_snake);

let (publisher_dotted_snake, publisher_dotted_kebab) = replace_dots(publisher);
let publisher_dotted_upper_camel = snake_to_upper_camel_case(&publisher_dotted_snake);
let input = input
// wit
.replace(
&format!("{template_package_name_kebab}-"),
&format!("{package_name_kebab}-"),
)
// rust imports
.replace(
&format!("{template_package_name_snake}::"),
&format!("{package_name_snake}::"),
)
// manifest.json
.replace(
&format!("{template_package_name_kebab}.wasm"),
&format!("{package_name_snake}.wasm"),
)
// tests manifest.json
.replace(
&format!("{template_package_name_kebab}_test.wasm"),
&format!("{package_name_snake}_test.wasm"),
);
let input = if extension == "wit" {
input
.replace(
&format!("{template_package_name}-"),
&format!("{package_name_kebab}-"),
)
.replace(&template_package_name_kebab, &package_name_kebab)
.replace(template_package_name, package_name)
} else {
input
.replace(
&format!("{template_package_name}-"),
&format!("{package_name_kebab}-"),
)
.replace(template_package_name, package_name)
.replace(&template_package_name_kebab, &package_name_kebab)
};
input
.replace(template_package_name, package_name)
.replace(&template_package_name_kebab, &package_name_kebab)
.replace(
&template_package_name_upper_camel,
&package_name_upper_camel,
Expand Down Expand Up @@ -193,27 +232,6 @@ pub fn execute(
return Err(eyre!("`publisher` '{}' must be URL safe.", publisher));
}

match language {
Language::Rust => {
if package_name.contains('-') {
let error = if !is_from_dir {
eyre!(
"rust `package_name`s cannot contain `-`s (given '{}')",
package_name,
)
} else {
eyre!(
"rust `package_name` (derived from given directory {:?}) cannot contain `-`s (given '{}')",
new_dir,
package_name,
)
};
return Err(error);
}
}
_ => {}
}

let ui_infix = if ui {
"ui".to_string()
} else {
Expand Down Expand Up @@ -241,10 +259,25 @@ pub fn execute(
}
})
.and_then(|stripped| {
let modified_path =
replace_vars(&stripped, &template.to_string(), &package_name, &publisher);
let modified_content =
replace_vars(content, &template.to_string(), &package_name, &publisher);
let extension = PathBuf::from(path);
let extension = extension
.extension()
.and_then(|s| s.to_str())
.unwrap_or_default();
let modified_path = replace_vars(
&stripped,
&template.to_string(),
&package_name,
&publisher,
extension,
);
let modified_content = replace_vars(
content,
&template.to_string(),
&package_name,
&publisher,
extension,
);
Some((modified_path, modified_content))
})
})
Expand Down Expand Up @@ -283,6 +316,7 @@ pub fn execute(
&template.to_string(),
&package_name,
&publisher,
"js",
),
);
}
Expand Down
Loading

0 comments on commit 2a467e0

Please sign in to comment.