Skip to content

Commit

Permalink
🐛 fix template extraction on scaffold
Browse files Browse the repository at this point in the history
  • Loading branch information
Odonno committed Apr 19, 2023
1 parent da46c1f commit 208f9d8
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 15 deletions.
22 changes: 21 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "surrealdb-migrations"
version = "0.9.1"
version = "0.9.2"
description = "An awesome CLI for SurrealDB migrations (provides commands to scaffold, create and apply migrations)."
authors = ["David Bottiau"]
repository = "https://github.com/Odonno/surrealdb-migrations/"
Expand All @@ -20,6 +20,7 @@ clap = { version = "4.1.8", features = ["derive"] }
cli-table = "0.4.7"
diffy = "0.3.0"
fs_extra = "1.3.0"
include_dir = "0.7.3"
regex = "1.7.1"
rust-ini = "0.18"
serde_json = "1.0"
Expand Down
50 changes: 37 additions & 13 deletions src/scaffold.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use fs_extra::dir::CopyOptions;
use include_dir::{include_dir, Dir};
use std::{
io::Write,
path::{Path, PathBuf},
process,
};
Expand Down Expand Up @@ -48,24 +49,17 @@ fn fails_if_folder_already_exists(dir_path: &PathBuf, dir_name: &str) {
}

fn copy_template_files_to_current_dir(template: ScaffoldTemplate, folder_path: Option<String>) {
let template_dir_name = get_template_dir_name(template);
const TEMPLATES_DIR: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/templates");

let template_dir_name = get_template_name(template);
let from = TEMPLATES_DIR.get_dir(template_dir_name).unwrap();

let to = match folder_path.to_owned() {
Some(folder_path) => folder_path,
None => ".".to_owned(),
};

fs_extra::dir::copy(
template_dir_name,
to,
&CopyOptions::new().content_only(true),
)
.unwrap();
}

fn get_template_dir_name(template: ScaffoldTemplate) -> String {
let template_dir_name = get_template_name(template);
format!("templates/{}", template_dir_name)
extract(from, to).unwrap();
}

fn get_template_name(template: ScaffoldTemplate) -> &'static str {
Expand All @@ -76,6 +70,36 @@ fn get_template_name(template: ScaffoldTemplate) -> &'static str {
}
}

// 💡 Function extract customized because it is not implemented in the "include_dir" crate.
// cf. https://github.com/Michael-F-Bryan/include_dir/pull/60
pub fn extract<S: AsRef<Path>>(dir: &Dir<'_>, path: S) -> std::io::Result<()> {
fn extract_dir<S: AsRef<Path>>(dir: Dir<'_>, path: S) -> std::io::Result<()> {
let path = path.as_ref();

for dir in dir.dirs() {
let dir_path = dir.path().components().skip(1).collect::<PathBuf>();

std::fs::create_dir_all(path.join(dir_path))?;
extract_dir(dir.clone(), path)?;
}

for file in dir.files() {
let file_path = file.path().components().skip(1).collect::<PathBuf>();

let mut fsf = std::fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(path.join(file_path))?;
fsf.write_all(file.contents())?;
fsf.sync_all()?;
}

Ok(())
}

extract_dir(dir.clone(), path)
}

fn ensures_folder_exists(dir_path: &PathBuf) {
if !dir_path.exists() {
fs_extra::dir::create(&dir_path, false).unwrap();
Expand Down

0 comments on commit 208f9d8

Please sign in to comment.