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

feat: use cargo generate instead of hardcoding examples source code #888

Merged
merged 14 commits into from
Jun 9, 2023
Merged
Prev Previous commit
feat: create a .gitignore file when copying an example
closes #867
  • Loading branch information
paulotten committed Jun 8, 2023
commit c381487e12beaa75399d34ecbc51ebcd96fb46be
38 changes: 34 additions & 4 deletions cargo-shuttle/src/init.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::{
fs::read_to_string,
fs::{read_to_string, OpenOptions},
io::{ErrorKind, Write},
path::{Path, PathBuf},
};

use anyhow::{Context, Result};
use cargo_generate::{GenerateArgs, TemplatePath};
use anyhow::{anyhow, Context, Result};
use cargo_generate::{GenerateArgs, TemplatePath, Vcs};
use indoc::indoc;
use shuttle_common::project::ProjectName;
use toml_edit::{value, Document};

Expand Down Expand Up @@ -209,14 +211,15 @@ pub fn cargo_generate(path: PathBuf, name: &ProjectName, framework: Template) ->
},
name: Some(name.to_string()), // appears to do nothing...
destination: Some(path.clone()),
vcs: Some(Vcs::Git),
..Default::default()
};
cargo_generate::generate(generate_args)
.with_context(|| "Failed to initialize with cargo generate.")?;

set_crate_name(&path, name.as_str()).with_context(|| "Failed to set crate name.")?;

remove_shuttle_toml(&path);
create_gitignore_file(&path).with_context(|| "Failed to create .gitignore file.")?;

Ok(())
}
Expand Down Expand Up @@ -252,3 +255,30 @@ fn remove_shuttle_toml(path: &Path) {
// this file only exists for some of the examples, it's fine if we don't find it
_ = std::fs::remove_file(path);
}

fn create_gitignore_file(path: &Path) -> Result<()> {
let mut path = path.to_path_buf();
path.push(".gitignore");

let mut file = match OpenOptions::new().create_new(true).write(true).open(path) {
Ok(f) => f,
Err(e) => {
match e.kind() {
ErrorKind::AlreadyExists => {
// if the example already has a .gitignore file, just use that
return Ok(());
}
_ => {
return Err(anyhow!(e));
}
}
}
};

file.write_all(indoc! {b"
/target
Secrets*.toml
"})?;

Ok(())
}