Skip to content

Commit

Permalink
create rails dirs and rspec dirs if in Gemfile (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
perryqh authored Jul 31, 2024
1 parent a95a858 commit 51c4ba7
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[package]
name = "pks"
version = "0.2.17"
version = "0.2.18"
edition = "2021"
description = "Welcome! Please see https://github.com/rubyatscale/pks for more information!"
license = "MIT"
Expand Down
33 changes: 31 additions & 2 deletions src/packs/creator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,22 @@ pub fn create(
)?;

write_pack_to_disk(&new_pack)?;
std::fs::create_dir_all(new_pack_path.join("app/public/"))
.context("failed to create app/public")?;
let pack_name =
&name.split('/').last().context("unable to find pack name")?;
std::fs::create_dir_all(new_pack_path.join("app/public/").join(pack_name))
.context(format!("failed to create app/public/{}", &name))?;
std::fs::create_dir_all(
new_pack_path.join("app/services/").join(pack_name),
)
.context(format!("failed to create app/services/{}", &name))?;
if is_rails(configuration) {
std::fs::create_dir_all(new_pack_path.join("app/controllers/"))
.context("failed to create app/controllers")?;
}
if is_rspec(configuration) {
std::fs::create_dir_all(new_pack_path.join("spec"))
.context("failed to create spec")?;
}

let readme = readme(name);
let readme_path = &new_pack_path.join("README.md");
Expand Down Expand Up @@ -65,3 +79,18 @@ See https://github.com/rubyatscale/pks#readme for more info!",
pack_name
)
}

fn is_rails(configuration: &Configuration) -> bool {
gemfile_contains(configuration, "rails")
}

fn is_rspec(configuration: &Configuration) -> bool {
gemfile_contains(configuration, "rspec")
}

fn gemfile_contains(configuration: &Configuration, val: &str) -> bool {
match std::fs::read_to_string(configuration.absolute_root.join("Gemfile")) {
Ok(as_string) => as_string.contains(val),
_ => false,
}
}
12 changes: 9 additions & 3 deletions tests/create_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@ fn test_create() -> Result<(), Box<dyn Error>> {
assert!(actual.contains("enforce_dependencies: true"));
assert!(actual.contains("enforce_privacy: true"));
assert!(actual.contains("enforce_layers: true"));
assert!(
Path::new("tests/fixtures/simple_app/packs/foobar/app/public").exists()
);
assert!(Path::new(
"tests/fixtures/simple_app/packs/foobar/app/public/foobar"
)
.exists());
assert!(Path::new(
"tests/fixtures/simple_app/packs/foobar/app/services/foobar"
)
.exists());
assert!(Path::new("tests/fixtures/simple_app/packs/foobar/spec").exists());

let expected_readme = String::from("\
Welcome to `packs/foobar`!
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/simple_app/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
gem 'rails', '7.1.3.4', github: 'rails', branch: '7-1-stable'

gem 'rspec'

0 comments on commit 51c4ba7

Please sign in to comment.