Skip to content

Commit a95a858

Browse files
authored
create packs (#3)
* extracting packs creator * adding all violations to package.yml
1 parent ea87ed9 commit a95a858

File tree

3 files changed

+88
-57
lines changed

3 files changed

+88
-57
lines changed

src/packs.rs

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub(crate) mod checker;
99
pub(crate) mod checker_configuration;
1010
pub(crate) mod configuration;
1111
pub(crate) mod constant_resolver;
12+
pub(crate) mod creator;
1213
pub(crate) mod dependencies;
1314
pub(crate) mod ignored;
1415
pub(crate) mod monkey_patch_detection;
@@ -26,10 +27,10 @@ mod reference_extractor;
2627

2728
use crate::packs;
2829
use crate::packs::pack::write_pack_to_disk;
29-
use crate::packs::pack::Pack;
3030

3131
// Internal imports
3232
pub(crate) use self::checker::Violation;
33+
use self::creator::CreateResult;
3334
pub(crate) use self::pack_set::PackSet;
3435
pub(crate) use self::parsing::process_files_with_cache;
3536
pub(crate) use self::parsing::ruby::experimental::get_experimental_constant_resolver;
@@ -50,47 +51,18 @@ pub fn greet() {
5051
println!("👋 Hello! Welcome to packs 📦 🔥 🎉 🌈. This tool is under construction.")
5152
}
5253

53-
fn create(configuration: &Configuration, name: String) -> anyhow::Result<()> {
54-
let existing_pack = configuration.pack_set.for_pack(&name);
55-
if existing_pack.is_ok() {
56-
println!("`{}` already exists!", &name);
57-
return Ok(());
54+
pub fn create(
55+
configuration: &Configuration,
56+
name: String,
57+
) -> anyhow::Result<()> {
58+
match creator::create(configuration, &name)? {
59+
CreateResult::AlreadyExists => {
60+
println!("`{}` already exists!", &name);
61+
}
62+
CreateResult::Success => {
63+
println!("Successfully created `{}`!", &name);
64+
}
5865
}
59-
let new_pack_path =
60-
configuration.absolute_root.join(&name).join("package.yml");
61-
62-
let new_pack = Pack::from_contents(
63-
&new_pack_path,
64-
&configuration.absolute_root,
65-
"enforce_dependencies: true",
66-
PackageTodo::default(),
67-
)?;
68-
69-
write_pack_to_disk(&new_pack)?;
70-
71-
let readme = format!(
72-
"Welcome to `{}`!
73-
74-
If you're the author, please consider replacing this file with a README.md, which may contain:
75-
- What your pack is and does
76-
- How you expect people to use your pack
77-
- Example usage of your pack's public API and where to find it
78-
- Limitations, risks, and important considerations of usage
79-
- How to get in touch with eng and other stakeholders for questions or issues pertaining to this pack
80-
- What SLAs/SLOs (service level agreements/objectives), if any, your package provides
81-
- When in doubt, keep it simple
82-
- Anything else you may want to include!
83-
84-
README.md should change as your public API changes.
85-
86-
See https://github.com/rubyatscale/packs#readme for more info!",
87-
new_pack.name
88-
);
89-
90-
let readme_path = configuration.absolute_root.join(&name).join("README.md");
91-
std::fs::write(readme_path, readme).context("Failed to write README.md")?;
92-
93-
println!("Successfully created `{}`!", name);
9466
Ok(())
9567
}
9668

src/packs/creator.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
use anyhow::Context;
2+
3+
use crate::packs::{
4+
pack::{write_pack_to_disk, Pack},
5+
PackageTodo,
6+
};
7+
8+
use super::Configuration;
9+
10+
pub enum CreateResult {
11+
Success,
12+
AlreadyExists,
13+
}
14+
15+
pub const NEW_PACKAGE_YML_CONTENTS: &str = "enforce_dependencies: true
16+
enforce_privacy: true
17+
enforce_layers: true";
18+
19+
pub fn create(
20+
configuration: &Configuration,
21+
name: &str,
22+
) -> anyhow::Result<CreateResult> {
23+
let existing_pack = configuration.pack_set.for_pack(name);
24+
if existing_pack.is_ok() {
25+
return Ok(CreateResult::AlreadyExists);
26+
}
27+
28+
let new_pack_path = configuration.absolute_root.join(name);
29+
30+
let new_pack = Pack::from_contents(
31+
&new_pack_path.join("package.yml"),
32+
&configuration.absolute_root,
33+
NEW_PACKAGE_YML_CONTENTS,
34+
PackageTodo::default(),
35+
)?;
36+
37+
write_pack_to_disk(&new_pack)?;
38+
std::fs::create_dir_all(new_pack_path.join("app/public/"))
39+
.context("failed to create app/public")?;
40+
41+
let readme = readme(name);
42+
let readme_path = &new_pack_path.join("README.md");
43+
std::fs::write(readme_path, readme).context("Failed to write README.md")?;
44+
45+
Ok(CreateResult::Success)
46+
}
47+
48+
fn readme(pack_name: &str) -> String {
49+
format!(
50+
"Welcome to `{}`!
51+
52+
If you're the author, please consider replacing this file with a README.md, which may contain:
53+
- What your pack is and does
54+
- How you expect people to use your pack
55+
- Example usage of your pack's public API and where to find it
56+
- Limitations, risks, and important considerations of usage
57+
- How to get in touch with eng and other stakeholders for questions or issues pertaining to this pack
58+
- What SLAs/SLOs (service level agreements/objectives), if any, your package provides
59+
- When in doubt, keep it simple
60+
- Anything else you may want to include!
61+
62+
README.md should change as your public API changes.
63+
64+
See https://github.com/rubyatscale/pks#readme for more info!",
65+
pack_name
66+
)
67+
}

tests/create_test.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use assert_cmd::Command;
22
use predicates::prelude::*;
33
use pretty_assertions::assert_eq;
4-
use std::{error::Error, fs};
4+
use std::{error::Error, fs, path::Path};
55

66
mod common;
77

@@ -18,11 +18,15 @@ fn test_create() -> Result<(), Box<dyn Error>> {
1818
"Successfully created `packs/foobar`!",
1919
));
2020

21-
let expected = "enforce_dependencies: true\n";
2221
let actual = fs::read_to_string(
2322
"tests/fixtures/simple_app/packs/foobar/package.yml",
2423
).unwrap_or_else(|_| panic!("Could not read file tests/fixtures/simple_app/packs/foobar/package.yml"));
25-
assert_eq!(expected, actual);
24+
assert!(actual.contains("enforce_dependencies: true"));
25+
assert!(actual.contains("enforce_privacy: true"));
26+
assert!(actual.contains("enforce_layers: true"));
27+
assert!(
28+
Path::new("tests/fixtures/simple_app/packs/foobar/app/public").exists()
29+
);
2630

2731
let expected_readme = String::from("\
2832
Welcome to `packs/foobar`!
@@ -39,7 +43,7 @@ If you're the author, please consider replacing this file with a README.md, whic
3943
4044
README.md should change as your public API changes.
4145
42-
See https://github.com/rubyatscale/packs#readme for more info!");
46+
See https://github.com/rubyatscale/pks#readme for more info!");
4347

4448
let actual_readme =
4549
fs::read_to_string("tests/fixtures/simple_app/packs/foobar/README.md").unwrap_or_else(|e| {
@@ -65,18 +69,6 @@ fn test_create_already_exists() -> Result<(), Box<dyn Error>> {
6569
.success()
6670
.stdout(predicate::str::contains("`packs/foo` already exists!"));
6771

68-
let expected = String::from(
69-
"\
70-
enforce_dependencies: true
71-
enforce_privacy: true
72-
dependencies:
73-
- packs/baz
74-
",
75-
);
76-
77-
let actual =
78-
fs::read_to_string("tests/fixtures/simple_app/packs/foo/package.yml")?;
79-
assert_eq!(expected, actual);
8072
common::teardown();
8173
Ok(())
8274
}

0 commit comments

Comments
 (0)