Skip to content

Commit 3e032b5

Browse files
committed
[bfops/fix-cli-rebuild]: simplify CLI build.rs
1 parent 500a870 commit 3e032b5

1 file changed

Lines changed: 5 additions & 70 deletions

File tree

crates/cli/build.rs

Lines changed: 5 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ fn discover_templates(templates_dir: &Path) -> Vec<TemplateInfo> {
202202
if !metadata_path.exists() {
203203
continue;
204204
}
205+
println!("cargo:rerun-if-changed={}", metadata_path.display());
205206

206207
let metadata_content = match fs::read_to_string(&metadata_path) {
207208
Ok(content) => content,
@@ -261,32 +262,7 @@ fn generate_template_entry(code: &mut String, template_path: &Path, source: &str
261262
panic!("Template '{}' has no git-tracked files! Check that the directory exists and contains files tracked by git.", source);
262263
}
263264

264-
// Example: /Users/user/SpacetimeDB
265265
let repo_root = get_repo_root();
266-
let repo_root_canonical = std::fs::canonicalize(&repo_root).unwrap();
267-
// Example: /Users/user/SpacetimeDB/crates/cli
268-
let manifest_canonical = Path::new(manifest_dir).canonicalize().unwrap();
269-
// Example: crates/cli
270-
let manifest_rel = manifest_canonical.strip_prefix(&repo_root_canonical).unwrap();
271-
272-
// Example for inside crate: /Users/user/SpacetimeDB/crates/cli/templates/basic-rs/server
273-
// Example for outside crate: /Users/user/SpacetimeDB/modules/chat-console-rs
274-
let resolved_canonical = repo_root.join(&resolved_base).canonicalize().unwrap();
275-
276-
// If the files are outside of the cli crate we need to copy them to the crate directory,
277-
// so they're included properly even when the crate is published
278-
let local_copy_dir = if resolved_canonical.strip_prefix(&manifest_canonical).is_err() {
279-
// Example source: "../../modules/quickstart-chat"
280-
// Sanitized: "parent_parent_modules_quickstart-chat"
281-
let sanitized_source = source.replace("/", "_").replace("\\", "_").replace("..", "parent");
282-
// Example: /Users/user/SpacetimeDB/crates/cli/.templates/parent_parent_modules_quickstart-chat
283-
let copy_dir = Path::new(manifest_dir).join(".templates").join(&sanitized_source);
284-
fs::create_dir_all(&copy_dir).expect("Failed to create .templates directory");
285-
286-
Some(copy_dir)
287-
} else {
288-
None
289-
};
290266

291267
code.push_str(" {\n");
292268
code.push_str(" let mut files = HashMap::new();\n");
@@ -313,38 +289,13 @@ fn generate_template_entry(code: &mut String, template_path: &Path, source: &str
313289
// Example: /Users/user/SpacetimeDB/modules/quickstart-chat/src/lib.rs
314290
let full_path = repo_root.join(&file_path);
315291
if full_path.exists() && full_path.is_file() {
316-
let include_path = if let Some(ref copy_dir) = local_copy_dir {
317-
// Outside crate: copy to .templates
318-
// Example dest_file: /Users/user/SpacetimeDB/crates/cli/.templates/parent_parent_modules_chat-console-rs/src/lib.rs
319-
let dest_file = copy_dir.join(relative_path);
320-
fs::create_dir_all(dest_file.parent().unwrap()).expect("Failed to create parent directory");
321-
copy_if_changed(&full_path, &dest_file)
322-
.unwrap_or_else(|_| panic!("Failed to copy file {:?} to {:?}", full_path, dest_file));
323-
324-
// Example relative_to_manifest: .templates/parent_parent_modules_chat-console-rs/src/lib.rs
325-
let relative_to_manifest = dest_file.strip_prefix(manifest_dir).unwrap();
326-
let path_str = relative_to_manifest.to_str().unwrap().replace("\\", "/");
327-
// Watch the original file for changes
328-
// Example: modules/chat-console-rs/src/lib.rs
329-
println!("cargo:rerun-if-changed={}", full_path.display());
330-
path_str
331-
} else {
332-
// Inside crate: use path relative to CARGO_MANIFEST_DIR
333-
// Example file_path: crates/cli/templates/basic-rs/server/src/lib.rs
334-
// Example manifest_rel: crates/cli
335-
// Result: templates/basic-rs/server/src/lib.rs
336-
let relative_to_manifest = file_path.strip_prefix(manifest_rel).unwrap();
337-
let path_str = relative_to_manifest.to_str().unwrap().replace("\\", "/");
338-
// Example: crates/cli/templates/basic-rs/server/src/lib.rs
339-
println!("cargo:rerun-if-changed={}", full_path.display());
340-
path_str
341-
};
292+
let include_path = file_path.to_str().unwrap().replace("\\", "/");
293+
println!("cargo:rerun-if-changed={}", full_path.display());
342294

343-
// Example include_path (inside crate): "templates/basic-rs/server/src/lib.rs"
344-
// Example include_path (outside crate): ".templates/parent_parent_modules_chat-console-rs/src/lib.rs"
295+
// Example include_path: "templates/basic-rs/src/main.rs"
345296
// Example relative_str: "src/lib.rs"
346297
code.push_str(&format!(
347-
" files.insert(\"{}\", include_str!(concat!(env!(\"CARGO_MANIFEST_DIR\"), \"/{}\")));\n",
298+
" files.insert(\"{}\", include_str!(concat!(env!(\"CARGO_MANIFEST_DIR\"), \"/../../{}\")));\n",
348299
relative_str, include_path
349300
));
350301
}
@@ -561,22 +512,6 @@ fn write_if_changed(path: &Path, contents: &[u8]) -> io::Result<()> {
561512
}
562513
}
563514

564-
fn copy_if_changed(src: &Path, dst: &Path) -> io::Result<()> {
565-
let src_bytes = fs::read(src)?;
566-
if let Ok(existing) = fs::read(dst)
567-
&& existing == src_bytes
568-
{
569-
return Ok(());
570-
}
571-
572-
if let Some(parent) = dst.parent() {
573-
fs::create_dir_all(parent)?;
574-
}
575-
576-
let mut file = fs::File::create(dst)?;
577-
file.write_all(&src_bytes)
578-
}
579-
580515
/// Discover skill directories under skills/. Each directory containing a SKILL.md
581516
/// file is considered a skill. Returns sorted skill names.
582517
fn discover_skill_names(skills_dir: &Path) -> Vec<String> {

0 commit comments

Comments
 (0)