Skip to content

Commit c745cf2

Browse files
committed
fix: 🐛 fixed recursive extraction and excluded subcrates from workspaces
1 parent 335ff5d commit c745cf2

File tree

6 files changed

+46
-8
lines changed

6 files changed

+46
-8
lines changed

bonnie.toml

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ dev.subcommands.serve.cmd = [
1010
"bonnie serve %server"
1111
]
1212
dev.subcommands.serve.args = [ "server" ]
13-
# TODO fix this with an example
1413
dev.subcommands.cli = [
1514
"cd packages/perseus-cli",
1615
"cargo run -- %%"

examples/basic/.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
.perseus/
1+
2+
.perseus/

packages/perseus-cli/src/bin/main.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use lib::errors::*;
88
fn main() {
99
// In development, we'll test in the `basic` example
1010
if cfg!(debug_assertions) {
11-
env::set_current_dir("../../examples/cli").unwrap();
11+
env::set_current_dir("../../examples/basic").unwrap();
1212
}
1313
let exit_code = real_main();
1414
std::process::exit(exit_code)
@@ -77,6 +77,10 @@ fn core(dir: PathBuf) -> Result<i32> {
7777
prepare(dir.clone())?;
7878
let exit_code = serve(dir, &prog_args)?;
7979
Ok(exit_code)
80+
} else if prog_args[0] == "prep" {
81+
// Set up the '.perseus/' directory if needed
82+
prepare(dir.clone())?;
83+
Ok(0)
8084
} else if prog_args[0] == "clean" {
8185
// Just delete the '.perseus/' directory directly, as we'd do in a corruption
8286
delete_bad_dir(dir)?;
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// This file contains a temporary fix for the issues with recursive extraction in `include_dir`
2+
// Tracking issue is https://github.com/Michael-F-Bryan/include_dir/issues/59
3+
4+
use std::path::Path;
5+
use include_dir::Dir;
6+
use std::io::Write;
7+
8+
/// Extracts a directory included with `include_dir!` until issue #59 is fixed on that module (recursive extraction support).
9+
pub fn extract_dir<S: AsRef<Path>>(dir: Dir, path: S) -> std::io::Result<()> {
10+
let path = path.as_ref();
11+
12+
// Create all the subdirectories in here (but not their files yet)
13+
for dir in dir.dirs() {
14+
std::fs::create_dir_all(path.join(dir.path()))?;
15+
// Recurse for this directory
16+
extract_dir(*dir, path)?;
17+
}
18+
19+
// Write all the files at the root of this directory
20+
for file in dir.files() {
21+
let mut fsf = std::fs::OpenOptions::new()
22+
.write(true)
23+
.create_new(true)
24+
.open(path.join(file.path()))?;
25+
fsf.write_all(file.contents())?;
26+
fsf.sync_all()?;
27+
}
28+
29+
Ok(())
30+
}

packages/perseus-cli/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ mod build;
55
mod serve;
66
mod cmd;
77

8+
mod extraction;
9+
810
use errors::*;
911
use std::fs;
1012
use std::path::PathBuf;

packages/perseus-cli/src/prepare.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ use std::process::Command;
88
use cargo_toml::Manifest;
99
use crate::errors::*;
1010
use crate::PERSEUS_VERSION;
11+
use crate::extraction::extract_dir;
1112

1213
/// This literally includes the entire subcrate in the program, allowing more efficient development.
1314
const SUBCRATES: Dir = include_dir!("../../examples/cli/.perseus");
14-
// const SUBCRATES: Dir = include_dir!("./test");
1515

16-
// BUG: `include_dir` currently doesn't support recursive extraction, tracking issue is https://github.com/Michael-F-Bryan/include_dir/issues/59
1716
/// Prepares the user's project by copying in the `.perseus/` subcrates. We use these subcrates to do all the building/serving, we just
1817
/// have to execute the right commands in the CLI. We can essentially treat the subcrates themselves as a blackbox of just a folder.
1918
pub fn prepare(dir: PathBuf) -> Result<()> {
@@ -32,7 +31,7 @@ pub fn prepare(dir: PathBuf) -> Result<()> {
3231
bail!(ErrorKind::ExtractionFailed(target.to_str().map(|s| s.to_string()), err.to_string()))
3332
}
3433
// Notably, this function will not do anything or tell us if the directory already exists...
35-
if let Err(err) = SUBCRATES.extract(&target) {
34+
if let Err(err) = extract_dir(SUBCRATES, &target) {
3635
bail!(ErrorKind::ExtractionFailed(target.to_str().map(|s| s.to_string()), err.to_string()))
3736
}
3837
// Use the current version of this crate (and thus all Perseus crates) to replace the relative imports
@@ -56,12 +55,15 @@ pub fn prepare(dir: PathBuf) -> Result<()> {
5655
};
5756
// Replace the relative path references to Perseus packages
5857
// Also update the name of the user's crate (Cargo needs more than just a path and an alias)
58+
// Also add an empty `[workspace]` key so we exclude from any of the user's workspace settings
5959
let updated_root_manifest = root_manifest_contents
6060
.replace("{ path = \"../../../packages/perseus\" }", &format!("\"{}\"", PERSEUS_VERSION))
61-
.replace("perseus-example-cli", &user_crate_name);
61+
.replace("perseus-example-cli", &user_crate_name)
62+
+ "\n[workspace]";
6263
let updated_server_manifest = server_manifest_contents
6364
.replace("{ path = \"../../../../packages/perseus-actix-web\" }", &format!("\"{}\"", PERSEUS_VERSION))
64-
.replace("perseus-example-cli", &user_crate_name);
65+
.replace("perseus-example-cli", &user_crate_name)
66+
+ "\n[workspace]";
6567
// Write the updated manifests back
6668
if let Err(err) = fs::write(&root_manifest, updated_root_manifest) {
6769
bail!(ErrorKind::ManifestUpdateFailed(root_manifest.to_str().map(|s| s.to_string()), err.to_string()))

0 commit comments

Comments
 (0)