Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion codegenerator/cli/npm/envio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@envio-dev/hypersync-client": "0.6.5",
"rescript": "11.1.3",
"rescript-schema": "9.3.0",
"viem": "2.21.0"
"viem": "2.21.0",
"bignumber.js": "9.1.2"
}
}
3 changes: 2 additions & 1 deletion codegenerator/cli/npm/envio/package.json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"@envio-dev/hypersync-client": "0.6.5",
"rescript": "11.1.3",
"rescript-schema": "9.3.0",
"viem": "2.21.0"
"viem": "2.21.0",
"bignumber.js": "9.1.2"
},
"files": [
"bin.js",
Expand Down
38 changes: 16 additions & 22 deletions codegenerator/cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ pub mod rescript {
use anyhow::Result;
use std::path::Path;

pub async fn clean(path: &Path) -> Result<std::process::ExitStatus> {
let args = vec!["rescript", "clean"];
execute_command("pnpm", args, path).await
}

pub async fn build(path: &Path) -> Result<std::process::ExitStatus> {
let args = vec!["rescript"];
execute_command("pnpm", args, path).await
Expand Down Expand Up @@ -94,15 +89,22 @@ pub mod codegen {
Ok(())
}

pub async fn pnpm_install(
project_paths: &ParsedProjectPaths,
) -> Result<std::process::ExitStatus> {
async fn pnpm_install(project_paths: &ParsedProjectPaths) -> Result<std::process::ExitStatus> {
println!("Checking for pnpm package...");
let current_dir = &project_paths.project_root;
check_and_install_pnpm(current_dir).await?;

let args = vec!["install", "--no-frozen-lockfile", "--prefer-offline"];
execute_command("pnpm", args, current_dir).await
check_and_install_pnpm(&project_paths.generated).await?;

execute_command(
"pnpm",
vec!["install", "--no-lockfile", "--prefer-offline"],
&project_paths.generated,
)
.await?;
execute_command(
"pnpm",
vec!["install", "--prefer-offline"],
&project_paths.project_root,
)
.await
}

async fn run_post_codegen_command_sequence(
Expand All @@ -114,15 +116,7 @@ pub mod codegen {
return Ok(exit1);
}

println!("Clean build directory");
let exit2 = rescript::clean(&project_paths.generated)
.await
.context("Failed running rescript clean")?;
if !exit2.success() {
return Ok(exit2);
}
Comment on lines -117 to -123
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't remember what case we were worried about stale build 🤔, I assume it's when a file gets removed from generated (but still exists from a stale generated version)


println!("Building code");
println!("Generating HyperIndex code...");
let exit3 = rescript::build(&project_paths.generated)
.await
.context("Failed running rescript build")?;
Expand Down
66 changes: 65 additions & 1 deletion codegenerator/cli/src/config_parsing/system_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ use anyhow::{anyhow, Context, Result};
use dotenvy::{EnvLoader, EnvMap, EnvSequence};
use ethers::abi::{ethabi::Event as EthAbiEvent, EventExt, EventParam, HumanReadableParser};
use itertools::Itertools;
use regex::Regex;
use std::{
collections::{HashMap, HashSet},
fs,
env, fs,
path::{Path, PathBuf},
};

Expand Down Expand Up @@ -370,6 +371,34 @@ DefaultForMissingAndEmpty with empty env and empty default: ""
}
}

//Validates version name (3 digits separated by period ".")
//Returns false if there are any additional chars as this should imply
//it is a dev release version or an unstable release
fn is_valid_release_version_number(version: &str) -> bool {
let re_version_pattern = Regex::new(r"^\d+\.\d+\.\d+(-rc\.\d+)?$")
.expect("version regex pattern should be valid regex");
re_version_pattern.is_match(version) || version.contains("-main-")
}

pub fn get_envio_version() -> Result<String> {
let crate_version = env!("CARGO_PKG_VERSION");
if is_valid_release_version_number(crate_version) {
// Check that crate version is not a dev release. In which case the
// version should be installable from npm
Ok(crate_version.to_string())
} else {
// Else install the local version for development and testing
match env::current_exe() {
// This should be something like "file:~/envio/hyperindex/codegenerator/target/debug/envio" or "file:.../target/debug/integration_tests"
Ok(exe_path) => Ok(format!(
"file:{}/../../../cli/npm/envio",
exe_path.to_string_lossy()
)),
Err(e) => Err(anyhow!("failed to get current exe path: {e}")),
}
}
}

#[derive(Debug)]
pub struct SystemConfig {
pub name: String,
Expand Down Expand Up @@ -1941,4 +1970,39 @@ mod test {
}
);
}

#[test]
fn test_valid_version_numbers() {
let valid_version_numbers = vec![
"0.0.0",
"999.999.999",
"0.0.1",
"10.2.3",
"2.0.0-rc.1",
"0.0.0-main-20241001144237-a236a894",
];

for vn in valid_version_numbers {
assert!(super::is_valid_release_version_number(vn));
}
}

#[test]
fn test_invalid_version_numbers() {
let invalid_version_numbers = vec![
"v10.1.0",
"0.1",
"0.0.1-dev",
"0.1.*",
"^0.1.2",
"0.0.1.2",
"1..1",
"1.1.",
".1.1",
"1.1.1.",
];
for vn in invalid_version_numbers {
assert!(!super::is_valid_release_version_number(vn));
}
}
}
76 changes: 7 additions & 69 deletions codegenerator/cli/src/executor/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ use crate::{
},
commands,
config_parsing::{
entity_parsing::Schema, graph_migration::generate_config_from_subgraph_id,
human_config::HumanConfig, system_config::SystemConfig,
entity_parsing::Schema,
graph_migration::generate_config_from_subgraph_id,
human_config::HumanConfig,
system_config::{get_envio_version, SystemConfig},
},
hbs_templating::{
contract_import_templates, hbs_dir_generator::HandleBarsDirGenerator,
Expand All @@ -18,17 +20,8 @@ use crate::{
utils::file_system,
};
use anyhow::{anyhow, Context, Result};
use regex::Regex;
use std::{env, path::PathBuf};

//Validates version name (3 digits separated by period ".")
//Returns false if there are any additional chars as this should imply
//it is a dev release version or an unstable release
fn is_valid_release_version_number(version: &str) -> bool {
let re_version_pattern = Regex::new(r"^\d+\.\d+\.\d+(-rc\.\d+)?$")
.expect("version regex pattern should be valid regex");
re_version_pattern.is_match(version) || version.contains("-main-")
}

use std::path::PathBuf;

pub async fn run_init_args(init_args: InitArgs, project_paths: &ProjectPaths) -> Result<()> {
let template_dirs = TemplateDirs::new();
Expand Down Expand Up @@ -236,23 +229,7 @@ pub async fn run_init_args(init_args: InitArgs, project_paths: &ProjectPaths) ->
}
}

let envio_version = {
let crate_version = env!("CARGO_PKG_VERSION");
if is_valid_release_version_number(crate_version) {
// Check that crate version is not a dev release. In which case the
// version should be installable from npm
crate_version.to_string()
} else {
// Else install the local version for development and testing
match env::current_exe() {
// This should be something like "file:~/envio/hyperindex/codegenerator/target/debug/envio" or "file:.../target/debug/integration_tests"
Ok(exe_path) => {
format!("file:{}/../../../cli/npm/envio", exe_path.to_string_lossy())
}
Err(e) => return Err(anyhow!("failed to get current exe path: {e}")),
}
}
};
let envio_version = get_envio_version()?;

let hbs_template = InitTemplates::new(
init_config.name.clone(),
Expand Down Expand Up @@ -298,42 +275,3 @@ pub async fn run_init_args(init_args: InitArgs, project_paths: &ProjectPaths) ->

Ok(())
}

#[cfg(test)]
mod test {

#[test]
fn test_valid_version_numbers() {
let valid_version_numbers = vec![
"0.0.0",
"999.999.999",
"0.0.1",
"10.2.3",
"2.0.0-rc.1",
"0.0.0-main-20241001144237-a236a894",
];

for vn in valid_version_numbers {
assert!(super::is_valid_release_version_number(vn));
}
}

#[test]
fn test_invalid_version_numbers() {
let invalid_version_numbers = vec![
"v10.1.0",
"0.1",
"0.0.1-dev",
"0.1.*",
"^0.1.2",
"0.0.1.2",
"1..1",
"1.1.",
".1.1",
"1.1.1.",
];
for vn in invalid_version_numbers {
assert!(!super::is_valid_release_version_number(vn));
}
}
}
8 changes: 5 additions & 3 deletions codegenerator/cli/src/hbs_templating/codegen_templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use crate::{
human_config::evm::{For, Rpc, RpcSyncConfig},
postgres_types,
system_config::{
self, Abi, Ecosystem, EventKind, FuelEventKind, MainEvmDataSource, SelectedField,
SystemConfig,
self, get_envio_version, Abi, Ecosystem, EventKind, FuelEventKind, MainEvmDataSource,
SelectedField, SystemConfig,
},
},
persisted_state::{PersistedState, PersistedStateJsonString},
Expand Down Expand Up @@ -838,7 +838,7 @@ let eventSignatures = [{}]
))?;

format!(
"let abi = Fuel.transpileAbi(%raw(`require(\"../../{}\")`))\n{}\n{}\n{chain_id_type_code}",
"let abi = Fuel.transpileAbi(%raw(`require`)(`../${{Path.relativePathToRootFromGenerated}}/{}`))\n{}\n{}\n{chain_id_type_code}",
// If we decide to inline the abi, instead of using require
// we need to remember that abi might contain ` and we should escape it
abi.path_buf.to_string_lossy(),
Expand Down Expand Up @@ -1242,6 +1242,7 @@ pub struct ProjectTemplate {
aggregated_field_selection: FieldSelection,
is_evm_ecosystem: bool,
is_fuel_ecosystem: bool,
envio_version: String,
//Used for the package.json reference to handlers in generated
relative_path_to_root_from_generated: String,
}
Expand Down Expand Up @@ -1347,6 +1348,7 @@ impl ProjectTemplate {
aggregated_field_selection,
is_evm_ecosystem: cfg.get_ecosystem() == Ecosystem::Evm,
is_fuel_ecosystem: cfg.get_ecosystem() == Ecosystem::Fuel,
envio_version: get_envio_version()?,
//Used for the package.json reference to handlers in generated
relative_path_to_root_from_generated,
})
Expand Down
4 changes: 4 additions & 0 deletions codegenerator/cli/src/template_dirs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ impl<'a> RelativeDir<'a> {
self.new_child(dir).extract(base_path)?;
}
DirEntry::File(f) => {
// Skip .gitkeep files
if path.file_name().map_or(false, |n| n == ".gitkeep") {
continue;
}
fs::write(path, f.contents())?;
}
}
Expand Down
2 changes: 2 additions & 0 deletions codegenerator/cli/templates/dynamic/codegen/package.json.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.1.0",
"main": "index.js",
"types": "index.d.ts",
"private": true,
"scripts": {
"clean": "rescript clean",
"build": "rescript",
Expand Down Expand Up @@ -54,6 +55,7 @@
"rescript": "11.1.3",
"rescript-envsafe": "5.0.0",
"rescript-schema": "9.3.0",
"envio": "{{envio_version}}",
"viem": "2.21.0",
"yargs": "17.7.2"
}
Expand Down
Empty file.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@
"version": 4
},
"suffix": ".bs.js",
"bs-dependencies": ["generated", "envio"],
"pinned-dependencies": ["generated"]
"bs-dependencies": ["generated", "envio"]
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@
"version": 4
},
"suffix": ".bs.js",
"bs-dependencies": ["generated", "envio"],
"pinned-dependencies": ["generated"]
"bs-dependencies": ["generated", "envio"]
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@
"version": 4
},
"suffix": ".bs.js",
"bs-dependencies": ["generated", "envio"],
"pinned-dependencies": ["generated"]
"bs-dependencies": ["generated", "envio"]
}
Loading