Skip to content

Commit

Permalink
get appropriate foundry version & kinostate for fake node version
Browse files Browse the repository at this point in the history
  • Loading branch information
nick1udwig committed Nov 6, 2024
1 parent fa3346c commit 21a8b36
Show file tree
Hide file tree
Showing 14 changed files with 358 additions and 107 deletions.
62 changes: 62 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ alloy-sol-macro = "0.7.6"
alloy-sol-types = "0.7.6"
base64 = "0.21"
cargo_metadata = "0.18"
chrono = "0.4"
clap = { version = "4.4", features = ["cargo", "string"] }
color-eyre = { version = "0.6", features = ["capture-spantrace"] }
dirs = "5.0"
Expand Down
113 changes: 83 additions & 30 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,45 @@ use std::io::{self, Write};
use std::path::Path;

const TEMPLATES_DIR: &str = "src/new/templates";
const CHAIN_KINOSTATE_DIR: &str = "src/chain/kinostate";
const TARGET_DIR: &str = "target";
const INCLUDES: &str = "includes.rs";
const NEW_INCLUDES: &str = "new_includes.rs";
const CHAIN_INCLUDES: &str = "chain_includes.rs";

/// create target/new_includes.rs to build templates into binary
fn make_new_includes() -> anyhow::Result<()> {
let mut output_buffer = Vec::new();
writeln!(
&mut output_buffer,
"const PATH_TO_CONTENT: &[(&str, &str)] = &["
)?;
writeln!(
output_buffer,
" (\"{}\", include_str!(\"{}\")),",
"componentize.mjs", "../src/new/componentize.mjs",
)?;

visit_dirs(Path::new(TEMPLATES_DIR), &mut output_buffer)?;

writeln!(&mut output_buffer, "];")?;

let target_dir = Path::new(TARGET_DIR);
let new_output_path = target_dir.join(NEW_INCLUDES);
// create *_includes.rs if it does not exist
if !target_dir.exists() {
fs::create_dir_all(target_dir)?;
}
if !new_output_path.exists() {
fs::write(&new_output_path, &output_buffer)?;
} else {
let existing_file = fs::read(&new_output_path)?;
if output_buffer != existing_file {
fs::write(&new_output_path, &output_buffer)?;
}
}

Ok(())
}

fn visit_dirs(dir: &Path, output_buffer: &mut Vec<u8>) -> io::Result<()> {
if !dir.is_dir() {
Expand Down Expand Up @@ -47,6 +84,48 @@ fn visit_dirs(dir: &Path, output_buffer: &mut Vec<u8>) -> io::Result<()> {
Ok(())
}

fn make_chain_includes() -> anyhow::Result<()> {
let mut output_buffer = Vec::new();
writeln!(
&mut output_buffer,
"const FOUNDRY_COMMIT_TO_CONTENT: &[(&str, &str)] = &["
)?;

for entry in fs::read_dir(CHAIN_KINOSTATE_DIR)? {
let entry = entry?;
let path = entry.path();
let commit = path
.file_stem()
.and_then(|c| c.to_str())
.ok_or_else(|| anyhow::anyhow!("couldn't get commit from {path:?}"))?;
writeln!(
output_buffer,
" (\"{}\", include_str!(\"{}\")),",
commit,
Path::new("..").join(&path).display(),
)?;
}

writeln!(&mut output_buffer, "];")?;

let target_dir = Path::new(TARGET_DIR);
let chain_output_path = target_dir.join(CHAIN_INCLUDES);
// create *_includes.rs if it does not exist
if !target_dir.exists() {
fs::create_dir_all(target_dir)?;
}
if !chain_output_path.exists() {
fs::write(&chain_output_path, &output_buffer)?;
} else {
let existing_file = fs::read(&chain_output_path)?;
if output_buffer != existing_file {
fs::write(&chain_output_path, &output_buffer)?;
}
}

Ok(())
}

fn add_commit_hash(repo: &git2::Repository) -> anyhow::Result<()> {
let sha = repo
.head()?
Expand All @@ -70,36 +149,10 @@ fn add_branch_name(repo: &git2::Repository) -> anyhow::Result<()> {
}

fn main() -> anyhow::Result<()> {
let mut output_buffer = Vec::new();
writeln!(
&mut output_buffer,
"const PATH_TO_CONTENT: &[(&str, &str)] = &["
)?;
writeln!(
output_buffer,
" (\"{}\", include_str!(\"{}\")),",
"componentize.mjs", "../src/new/componentize.mjs",
)?;

visit_dirs(Path::new(TEMPLATES_DIR), &mut output_buffer)?;

writeln!(&mut output_buffer, "];")?;

let target_dir = Path::new(TARGET_DIR);
let output_path = target_dir.join(INCLUDES);
// create includes.rs if it does not exist
if !target_dir.exists() {
fs::create_dir_all(target_dir)?;
}
if !output_path.exists() {
fs::write(&output_path, &output_buffer)?;
} else {
let existing_file = fs::read(&output_path)?;
if output_buffer != existing_file {
fs::write(&output_path, &output_buffer)?;
}
}
make_new_includes()?;
make_chain_includes()?;

// write version info into binary
let repo = git2::Repository::open(".")?;

add_commit_hash(&repo)?;
Expand Down
22 changes: 16 additions & 6 deletions src/boot_fake_node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ pub fn get_platform_runtime_name(is_simulation_mode: bool) -> Result<String> {
}

#[instrument(level = "trace", skip_all)]
pub async fn get_runtime_binary(version: &str, is_simulation_mode: bool) -> Result<PathBuf> {
pub async fn get_runtime_binary(version: &str, is_simulation_mode: bool) -> Result<(PathBuf, String)> {
let zip_name = get_platform_runtime_name(is_simulation_mode)?;

let version = if version != "latest" {
Expand Down Expand Up @@ -193,7 +193,7 @@ pub async fn get_runtime_binary(version: &str, is_simulation_mode: bool) -> Resu
get_runtime_binary_inner(&version, &zip_name, &runtime_dir).await?;
}

Ok(runtime_path)
Ok((runtime_path, version))
}

#[instrument(level = "trace", skip_all)]
Expand Down Expand Up @@ -403,13 +403,13 @@ pub async fn execute(
) -> Result<()> {
let detached = false; // TODO: to argument?
// TODO: factor out with run_tests?
let runtime_path = match runtime_path {
let (runtime_path, version) = match runtime_path {
None => get_runtime_binary(&version, true).await?,
Some(runtime_path) => {
if !runtime_path.exists() {
return Err(eyre!("--runtime-path {:?} does not exist.", runtime_path));
}
if runtime_path.is_dir() {
let runtime_path = if runtime_path.is_dir() {
// Compile the runtime binary
compile_runtime(&runtime_path, release, true)?;
runtime_path
Expand All @@ -418,9 +418,18 @@ pub async fn execute(
.join("kinode")
} else {
runtime_path
}
};
let Some((output, _)) = build::run_command(
Command::new("bash").args(["-c", &format!("{} --version", runtime_path.display())]),
false,
)? else {
return Err(eyre!("couldn't get Kinode version"));
};
let version = output.split('\n').last().unwrap().split(' ').last().unwrap();
(runtime_path, version.to_string())
}
};
let version = version.strip_prefix("v").unwrap_or_else(|| &version);

let mut task_handles = Vec::new();

Expand Down Expand Up @@ -455,8 +464,9 @@ pub async fn execute(
}

// boot fakechain
let version = version.parse()?;
let anvil_process =
chain::start_chain(fakechain_port, true, recv_kill_in_start_chain, false).await?;
chain::start_chain(fakechain_port, recv_kill_in_start_chain, Some(version), false).await?;

if let Some(rpc) = rpc {
args.extend_from_slice(&["--rpc".into(), rpc.into()]);
Expand Down
5 changes: 4 additions & 1 deletion src/boot_real_node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ pub async fn execute(
let detached = false; // TODO: to argument?
// TODO: factor out with run_tests?
let runtime_path = match runtime_path {
None => get_runtime_binary(&version, false).await?,
None => {
let (runtime_path, _) = get_runtime_binary(&version, false).await?;
runtime_path
}
Some(runtime_path) => {
if !runtime_path.exists() {
return Err(eyre!("--runtime-path {:?} does not exist.", runtime_path));
Expand Down
18 changes: 13 additions & 5 deletions src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use zip::write::FileOptions;
use kinode_process_lib::{kernel_types::Erc721Metadata, PackageId};

use crate::publish::make_local_file_link_path;
use crate::run_tests::types::BroadcastRecvBool;
use crate::setup::{
check_js_deps, check_py_deps, check_rust_deps, get_deps, get_newest_valid_node_version,
get_python_version, REQUIRED_PY_PACKAGE,
Expand Down Expand Up @@ -53,6 +54,11 @@ struct CargoPackage {
name: String,
}

pub fn make_fake_kill_chan() -> BroadcastRecvBool {
let (_send_to_kill, recv_kill) = tokio::sync::broadcast::channel(1);
recv_kill
}

pub fn make_pkg_publisher(metadata: &Erc721Metadata) -> String {
let package_name = metadata.properties.package_name.as_str();
let publisher = metadata.properties.publisher.as_str();
Expand Down Expand Up @@ -1244,7 +1250,7 @@ fn get_ui_dirs(
}

#[instrument(level = "trace", skip_all)]
fn check_and_populate_dependencies(
async fn check_and_populate_dependencies(
package_dir: &Path,
metadata: &Erc721Metadata,
skip_deps_check: bool,
Expand All @@ -1255,6 +1261,7 @@ fn check_and_populate_dependencies(
let mut checked_js = false;
let mut apis = HashMap::new();
let mut dependencies = HashSet::new();
let mut recv_kill = make_fake_kill_chan();
// Do we need to do an `is_cluded()` check here?
// I think no because we may want to, e.g., build a process that
// depends on another that is already built but hasn't changed.
Expand All @@ -1264,14 +1271,14 @@ fn check_and_populate_dependencies(
if path.is_dir() {
if path.join(RUST_SRC_PATH).exists() && !checked_rust && !skip_deps_check {
let deps = check_rust_deps()?;
get_deps(deps, verbose)?;
get_deps(deps, &mut recv_kill, verbose).await?;
checked_rust = true;
} else if path.join(PYTHON_SRC_PATH).exists() && !checked_py {
check_py_deps()?;
checked_py = true;
} else if path.join(JAVASCRIPT_SRC_PATH).exists() && !checked_js && !skip_deps_check {
let deps = check_js_deps()?;
get_deps(deps, verbose)?;
get_deps(deps, &mut recv_kill, verbose).await?;
checked_js = true;
} else if Some("api") == path.file_name().and_then(|s| s.to_str()) {
// read api files: to be used in build
Expand Down Expand Up @@ -1380,7 +1387,7 @@ async fn compile_package(
let metadata = read_and_update_metadata(package_dir)?;
let mut wasm_paths = HashSet::new();
let (mut apis, dependencies) =
check_and_populate_dependencies(package_dir, &metadata, skip_deps_check, verbose)?;
check_and_populate_dependencies(package_dir, &metadata, skip_deps_check, verbose).await?;

if !ignore_deps && !dependencies.is_empty() {
fetch_dependencies(
Expand Down Expand Up @@ -1566,10 +1573,11 @@ pub async fn execute(

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

let mut recv_kill = make_fake_kill_chan();
if !no_ui {
if !skip_deps_check {
let deps = check_js_deps()?;
get_deps(deps, verbose)?;
get_deps(deps, &mut recv_kill, verbose).await?;
}
let valid_node = get_newest_valid_node_version(None, None)?;
let ui_dirs = get_ui_dirs(package_dir, &include, &exclude)?;
Expand Down
1 change: 1 addition & 0 deletions src/chain/kinostate/008922d51.json

Large diffs are not rendered by default.

File renamed without changes.
Loading

0 comments on commit 21a8b36

Please sign in to comment.