Skip to content

Commit

Permalink
Merge pull request #262 from kinode-dao/develop
Browse files Browse the repository at this point in the history
v0.8.2
  • Loading branch information
nick1udwig authored Nov 11, 2024
2 parents 50fd0d8 + e9cec99 commit 1c1d42e
Show file tree
Hide file tree
Showing 14 changed files with 415 additions and 113 deletions.
64 changes: 63 additions & 1 deletion Cargo.lock

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

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.8.1"
version = "0.8.2"
edition = "2021"

[build-dependencies]
Expand Down 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
40 changes: 33 additions & 7 deletions src/boot_fake_node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@ 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 +196,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 +406,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 +421,26 @@ 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')
.rev()
.nth(1)
.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 +475,14 @@ pub async fn execute(
}

// boot fakechain
let anvil_process =
chain::start_chain(fakechain_port, true, recv_kill_in_start_chain, false).await?;
let version = version.parse()?;
let anvil_process = 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
Loading

0 comments on commit 1c1d42e

Please sign in to comment.