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
76 changes: 53 additions & 23 deletions crates/fs/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,30 +296,60 @@ mod tests {
}

#[tokio::test]
async fn test_replace_in_place() -> Result<()> {
// Create an in-memory filesystem
let fs = Fs::mem();

// Create a test file with content to replace
let test_path = "config.txt";
let original_content =
"server_url=localhost:8080\napi_version=v1\nserver_url=example.com:9090";
fs.write_to_file(test_path, original_content).await?;

// Create a regex pattern to replace all server_url values
let pattern = Regex::new(r"server_url=([^\n]+)")?;
let replacement = "server_url=production.example.com:443";

// Apply the replacement
fs.replace_in_place(&pattern, replacement, test_path)
.await?;

// Read the modified content
let modified_content = fs.read_to_string(test_path).await?;
async fn test_replace_in_place_table() -> Result<()> {
// Define test cases as a table
struct TestCase {
name: &'static str,
original_content: &'static str,
pattern: &'static str,
replacement: &'static str,
expected_content: &'static str,
}

// Verify the replacements were made
let expected_content = "server_url=production.example.com:443\napi_version=v1\nserver_url=production.example.com:443";
assert_eq!(modified_content, expected_content);
let test_cases = vec![
TestCase {
name: "replace server_url values",
original_content: "server_url=localhost:8080\napi_version=v1\nserver_url=example.com:9090",
pattern: r"server_url=([^\n]+)",
replacement: "server_url=production.example.com:443",
expected_content: "server_url=production.example.com:443\napi_version=v1\nserver_url=production.example.com:443",
},
TestCase {
name: "replace e3-compute-provider line",
original_content: "[dependencies]\nsome-crate = \"1.0\"\ne3-compute-provider = { git = \"https://github.com/example/repo\" }\nother-crate = \"2.0\"",
pattern: r"(?m)^e3-compute-provider = \{ git =.*\n?",
replacement: "YO LATER DUDE!\n",
expected_content: "[dependencies]\nsome-crate = \"1.0\"\nYO LATER DUDE!\nother-crate = \"2.0\"",
},
Comment thread
ryardley marked this conversation as resolved.
];

// Run each test case
for test_case in test_cases {
// Create an in-memory filesystem for each test
let fs = Fs::mem();
let test_path = format!("{}.txt", test_case.name.replace(" ", "_"));

// Write the original content
fs.write_to_file(&test_path, test_case.original_content)
.await?;

// Create the regex pattern
let pattern = Regex::new(test_case.pattern)?;

// Apply the replacement
fs.replace_in_place(&pattern, test_case.replacement, &test_path)
.await?;

// Read the modified content
let modified_content = fs.read_to_string(&test_path).await?;

// Verify the replacement was made
assert_eq!(
modified_content, test_case.expected_content,
"Test case '{}' failed",
test_case.name
);
}

Ok(())
}
Expand Down
8 changes: 1 addition & 7 deletions crates/init/src/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,9 @@ where
fs.cp(&src_path, &dest_path).await?;

for filter in filters {
// We need to prefix the glob pattern or it will be from the root of the filesystem
let prefixed_glob_pattern = format!(
"{}/{}",
dest_path.as_ref().to_string_lossy(),
&filter.glob_pattern
);
info!("Running filter: {:?}", filter);

let file_paths = fs.find_files(&dest_path, &prefixed_glob_pattern).await?;
let file_paths = fs.find_files(&dest_path, &filter.glob_pattern).await?;
info!("pattern:{} found {:?}", filter.glob_pattern, file_paths);

for file_path in file_paths {
Expand Down
22 changes: 22 additions & 0 deletions crates/init/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,28 @@ pub async fn add_submodule(
Ok(())
}

pub async fn get_commit_hash(path: impl AsRef<Path>) -> Result<String> {
let path = path.as_ref();

let output = Command::new("git")
.args(&["rev-parse", "HEAD"])
.current_dir(path)
.output()
.await
.with_context(|| format!("Failed to get commit hash in directory: {}", path.display()))?;

if !output.status.success() {
return Err(anyhow::anyhow!(
"Git rev-parse failed with exit code: {}",
output.status.code().unwrap_or(-1)
));
}

let hash = String::from_utf8(output.stdout)?.trim().to_string();

Ok(hash)
}

#[derive(Debug)]
pub struct GitReference {
pub base_url: String,
Expand Down
26 changes: 25 additions & 1 deletion crates/init/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,16 @@ async fn install_enclave(cwd: &PathBuf, template: Option<String>, verbose: bool)
})
.await?;

spinner.complete_task("Template downloaded\n");
let commit_hash = spinner
.run("Getting commit hash...", || async {
git::get_commit_hash(TEMP_DIR).await
})
.await?;

spinner.complete_task(&format!(
"Template downloaded with commit hash '{}'\n",
commit_hash
));

spinner.update("Configuring template...".to_string()).await;

Expand Down Expand Up @@ -108,6 +117,21 @@ async fn install_enclave(cwd: &PathBuf, template: Option<String>, verbose: bool)
r#""@enclave-e3/sdk":\s*"[^"]*""#,
&format!(r#""@enclave-e3/sdk": "{}""#, sdk_version),
),
Filter::new(
"**/Cargo.toml",
r"(?m)^e3-program-server =.*\n?",
&format!("e3-program-server = {{ git = \"https://github.com/gnosisguild/enclave\", rev = \"{}\" }}\n",commit_hash),
),
Filter::new(
"**/Cargo.toml",
r"(?m)^e3-bfv-helpers =.*\n?",
&format!("e3-bfv-helpers = {{ git = \"https://github.com/gnosisguild/enclave\", rev = \"{}\" }}\n",commit_hash),
),
Filter::new(
"**/Cargo.toml",
r"(?m)^e3-compute-provider =.*\n?",
&format!("e3-compute-provider = {{ git = \"https://github.com/gnosisguild/enclave\", rev = \"{}\" }}\n",commit_hash),
),
Comment thread
ryardley marked this conversation as resolved.
],
)
.await
Expand Down
Loading
Loading