Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use esbuild to reliably identify changes to the full/relevant import tree in eventhandler files #8

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
cff5e86
fix: hash using esbuild cmd
DenhamPreen Mar 19, 2024
c33d268
tests: unit tests hash string directory and esbuild on js & ts
DenhamPreen Apr 2, 2024
2981e39
feat: use esbuild from inside generated folder
DenhamPreen Apr 4, 2024
cbd7fb2
test: update hash
DenhamPreen Apr 4, 2024
4ce80e7
feat: decouple persisted state flow
DenhamPreen Apr 9, 2024
f091941
Add additional fields to chainmetadata table for UI
MJYoung114 Mar 26, 2024
2b49d12
Bump undici from 5.28.3 to 5.28.4 in /scenarios/ploffen/contracts
dependabot[bot] Apr 4, 2024
5f32293
Bump undici from 5.28.3 to 5.28.4 in /scenarios/erc20/contracts
dependabot[bot] Apr 4, 2024
6a7444b
Bump undici from 5.28.3 to 5.28.4 in /scenarios/greeter/contracts
dependabot[bot] Apr 4, 2024
1969cb0
Bump undici from 5.28.3 to 5.28.4 in /scenarios/nft-factory/contracts
dependabot[bot] Apr 4, 2024
427fb8d
Bump whoami from 1.4.1 to 1.5.1 in /codegenerator/integration_tests
dependabot[bot] Apr 5, 2024
60bd12d
Bump whoami from 1.4.1 to 1.5.1 in /codegenerator/cli
dependabot[bot] Apr 5, 2024
9c17595
Bump whoami from 1.4.1 to 1.5.1 in /codegenerator
dependabot[bot] Apr 5, 2024
fe80c6e
Bump h2 from 0.3.24 to 0.3.26 in /codegenerator/integration_tests
dependabot[bot] Apr 5, 2024
1555660
Bump h2 from 0.3.24 to 0.3.26 in /codegenerator/cli
dependabot[bot] Apr 5, 2024
e032a08
Bump h2 from 0.3.24 to 0.3.26 in /codegenerator
dependabot[bot] Apr 5, 2024
1ab8cfb
Bump express in /codegenerator/cli/templates/static/codegen
dependabot[bot] Mar 26, 2024
0927d33
Bump mio from 0.8.8 to 0.8.11 in /codegenerator/cli
dependabot[bot] Apr 8, 2024
58cbd98
Ensure dynamic contract lookup is checksummed
JonoPrest Apr 8, 2024
138c043
Updated hypersync client bindings to use check summed address and rem…
JonoPrest Apr 8, 2024
b2b19fc
Make hypersync client the default event decoder
JonoPrest Apr 8, 2024
6bbbba9
Revert "Make hypersync client the default event decoder"
JonoPrest Apr 9, 2024
799d816
feat: use esbuild from inside generated folder
DenhamPreen Apr 4, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions codegenerator/Cargo.lock

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

43 changes: 31 additions & 12 deletions codegenerator/cli/Cargo.lock

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

175 changes: 170 additions & 5 deletions codegenerator/cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@ pub mod rescript {
pub mod codegen {
use super::{execute_command, rescript};
use crate::{
config_parsing::system_config::SystemConfig, hbs_templating, template_dirs::TemplateDirs,
config_parsing::system_config::SystemConfig, constants::project_paths::ESBUILD_PATH,
hbs_templating, persisted_state::PersistedState, project_paths::path_utils,
template_dirs::TemplateDirs,
};
use anyhow::{self, Context, Result};
use anyhow::{anyhow, Context, Result};
use pathdiff::diff_paths;
use std::path::PathBuf;

use crate::project_paths::ParsedProjectPaths;
Expand Down Expand Up @@ -109,15 +112,88 @@ pub mod codegen {
execute_command("pnpm", args, current_dir).await
}

// eg: pnpm esbuild --platform=node --bundle --minify --outdir=./esbuild-handlers --external:./* ../src/EventHandlers.ts ../src/another-file.ts
pub async fn generate_esbuild_out(
project_paths: &ParsedProjectPaths,
all_handler_paths: Vec<PathBuf>,
) -> Result<std::process::ExitStatus> {
let current_dir = &project_paths.generated;

let config_directory = project_paths
.config
.parent()
.ok_or_else(|| anyhow!("Unexpected config file should have a parent directory"))?;

// let all_handler_paths_parent = all_handler_paths.clone();
let all_handler_paths_string = all_handler_paths
.clone()
.into_iter()
.map(|path| {
let handler_path_joined = &config_directory.join(path);
let absolute_path = path_utils::normalize_path(&handler_path_joined);

let binding = diff_paths(absolute_path.clone(), &project_paths.generated)
.ok_or_else(|| anyhow!("could not find handler path relative to generated"))?;

let relative_to_generated = binding
.to_str()
.ok_or_else(|| anyhow!("Handler path should be unicode"))?;

Ok(relative_to_generated.to_string())
})
.collect::<Result<Vec<_>>>()
.context("failed to get relative paths for handlers")?;

let out_dir_arg = format!("--outdir={}", ESBUILD_PATH);

let esbuild_cmd: Vec<String> = vec![
"esbuild",
"--platform=node",
"--bundle",
"--minify",
&out_dir_arg,
"--external:./*",
"--log-level=warning",
]
.iter()
.map(|s| s.to_string())
.collect();

let esbuild_cmd_with_paths = esbuild_cmd
.iter()
.chain(all_handler_paths_string.iter())
.into_iter()
.map(|s| s.as_str())
.collect::<Vec<&str>>();

execute_command("pnpm", esbuild_cmd_with_paths, &current_dir).await
}

pub async fn run_post_codegen_command_sequence(
project_paths: &ParsedProjectPaths,
config: &SystemConfig,
) -> anyhow::Result<std::process::ExitStatus> {
println!("installing packages... ");
let exit1 = pnpm_install(project_paths).await?;
if !exit1.success() {
return Ok(exit1);
}

// create persisted state
let persisted_state = PersistedState::get_current_state(config)
.await
.context("Failed creating default persisted state")?;

let persisted_state_path = project_paths.generated.join("persisted_state.envio.json");
// write persisted state to a file called persisted_state.json
fs::write(
persisted_state_path,
serde_json::to_string(&persisted_state)
.context("failed to serialize persisted state")?,
)
.await
.context("Failed writing persisted state to file")?; // todo : check what write behaviour

println!("clean build directory");
let exit2 = rescript::clean(&project_paths.generated)
.await
Expand Down Expand Up @@ -152,16 +228,14 @@ pub mod codegen {
) -> anyhow::Result<()> {
let template_dirs = TemplateDirs::new();
fs::create_dir_all(&project_paths.generated).await?;

let template =
hbs_templating::codegen_templates::ProjectTemplate::from_config(config, project_paths)
.await
.context("Failed creating project template")?;

template_dirs
.get_codegen_static_dir()?
.extract(&project_paths.generated)
.context("Failed extracting static codegen files")?;

template
.generate_templates(project_paths)
.context("Failed generating dynamic codegen files")?;
Expand Down Expand Up @@ -273,3 +347,94 @@ pub mod db_migrate {
Ok(())
}
}

#[cfg(test)]
mod test {

use crate::{
config_parsing::{entity_parsing::Schema, human_config, system_config::SystemConfig},
project_paths::ParsedProjectPaths,
};
use anyhow::Context;
use std::path::PathBuf;

#[tokio::test]

async fn commands_execute() {
let cmd = "echo";
let args = vec!["hello"];
let current_dir = std::env::current_dir().unwrap();
let exit = super::execute_command(cmd, args, &current_dir)
.await
.unwrap();
assert!(exit.success());
}

#[tokio::test]
#[ignore = "Needs esbuild to be installed globally"]
async fn generate_esbuild_out_commands_execute() {
println!("Needs esbuild to be installed globally");
let root = format!("{}/test", env!("CARGO_MANIFEST_DIR"));
let path = format!("{}/configs/config.js.yaml", &root);
let config_path = PathBuf::from(path);

let human_cfg = human_config::deserialize_config_from_yaml(&config_path)
.context("human cfg")
.expect("result from human config");
let system_cfg = SystemConfig::parse_from_human_cfg_with_schema(
&human_cfg,
Schema::empty(),
&ParsedProjectPaths::new(&root, "generated", "config.js.yaml")
.expect("parsed project paths"),
)
.context("system_cfg")
.expect("result from system config");

let all_handler_paths = system_cfg
.get_all_paths_to_handlers()
.context("Failed getting handler paths")
.expect("handlers from the config");

let exit = super::codegen::generate_esbuild_out(
&system_cfg.parsed_project_paths,
all_handler_paths,
)
.await
.unwrap();
assert!(exit.success());
}

#[tokio::test]
#[ignore = "Needs esbuild to be installed globally"]
async fn generate_esbuild_out_commands_execute_ts() {
println!("Needs esbuild to be installed globally");
let root = format!("{}/test", env!("CARGO_MANIFEST_DIR"));
let path = format!("{}/configs/config.ts.yaml", &root);
let config_path = PathBuf::from(path);

let human_cfg = human_config::deserialize_config_from_yaml(&config_path)
.context("human cfg")
.expect("result from human config");
let system_cfg = SystemConfig::parse_from_human_cfg_with_schema(
&human_cfg,
Schema::empty(),
&ParsedProjectPaths::new(&root, "generated", "config.ts.yaml")
.expect("parsed project paths"),
)
.context("system_cfg")
.expect("result from system config");

let all_handler_paths = system_cfg
.get_all_paths_to_handlers()
.context("Failed getting handler paths")
.expect("handlers from the config");

let exit = super::codegen::generate_esbuild_out(
&system_cfg.parsed_project_paths,
all_handler_paths,
)
.await
.unwrap();
assert!(exit.success());
}
}
Loading