Skip to content

Commit 6161fe5

Browse files
Make README template customizable (#17)
* Make README template customizable * update tests to use different fixtures * formatting and updating rust * bump version to 0.3.0 --------- Co-authored-by: Perry Hertler <[email protected]>
1 parent 11c8648 commit 6161fe5

File tree

24 files changed

+188
-12
lines changed

24 files changed

+188
-12
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[package]
44
name = "pks"
5-
version = "0.2.23"
5+
version = "0.3.0"
66
edition = "2021"
77
description = "Welcome! Please see https://github.com/rubyatscale/pks for more information!"
88
license = "MIT"

rust-toolchain.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[toolchain]
2-
channel = "1.77.2"
2+
channel = "1.83.0"
33
components = ["clippy", "rustfmt"]
44
targets = ["x86_64-apple-darwin", "aarch64-apple-darwin", "x86_64-unknown-linux-gnu"]

src/packs/caching/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ pub enum CacheResult {
1212

1313
#[derive(Debug, Default)]
1414
pub struct EmptyCacheEntry {
15+
#[allow(dead_code)]
1516
pub filepath: PathBuf,
1617
pub file_contents_digest: String,
18+
#[allow(dead_code)]
1719
pub file_name_digest: String,
1820
pub cache_file_path: PathBuf,
1921
}

src/packs/configuration.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub struct Configuration {
3232
pub ignored_definitions: HashMap<String, HashSet<PathBuf>>,
3333
pub autoload_roots: HashMap<PathBuf, String>,
3434
pub inflections_path: PathBuf,
35+
pub readme_template_path: PathBuf,
3536
pub custom_associations: Vec<String>,
3637
pub stdin_file_path: Option<PathBuf>,
3738
// Note that it'd probably be better to use the logger library, `tracing` (see logger.rs)
@@ -139,6 +140,12 @@ pub(crate) fn from_raw(
139140
.unwrap_or(PathBuf::from("config/initializers/inflections.rb")),
140141
);
141142

143+
let readme_template_path = absolute_root.join(
144+
raw_config
145+
.readme_template_path
146+
.unwrap_or(PathBuf::from("README_TEMPLATE.md")),
147+
);
148+
142149
let custom_associations = raw_config
143150
.custom_associations
144151
.iter()
@@ -162,6 +169,7 @@ pub(crate) fn from_raw(
162169
ignored_definitions,
163170
autoload_roots,
164171
inflections_path,
172+
readme_template_path,
165173
custom_associations,
166174
stdin_file_path: None,
167175
print_files: false,
@@ -370,7 +378,11 @@ mod tests {
370378

371379
assert_eq!(expected_packs, actual.pack_set.packs);
372380

373-
assert!(!actual.cache_enabled)
381+
assert!(!actual.cache_enabled);
382+
383+
let expected_readme_template_path =
384+
absolute_root.join("README_TEMPLATE.md");
385+
assert_eq!(actual.readme_template_path, expected_readme_template_path);
374386
}
375387

376388
#[test]
@@ -456,4 +468,16 @@ mod tests {
456468

457469
assert_eq!(actual_associations, expected_paths);
458470
}
471+
472+
#[test]
473+
fn with_readme_template_path() {
474+
let absolute_root =
475+
PathBuf::from("tests/fixtures/app_with_custom_readme");
476+
let actual = configuration::get(&absolute_root).unwrap();
477+
478+
let actual_readme_template_path = actual.readme_template_path;
479+
let expected_readme_template_path =
480+
absolute_root.join("config/packs/README_EXAMPLE.md");
481+
assert_eq!(actual_readme_template_path, expected_readme_template_path);
482+
}
459483
}

src/packs/creator.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,20 @@ pub fn create(
5252
.context("failed to create spec")?;
5353
}
5454

55-
let readme = readme(name);
55+
let readme = readme(configuration, name);
5656
let readme_path = &new_pack_path.join("README.md");
5757
std::fs::write(readme_path, readme).context("Failed to write README.md")?;
5858

5959
Ok(CreateResult::Success)
6060
}
6161

62-
fn readme(pack_name: &str) -> String {
63-
format!(
62+
fn readme(configuration: &Configuration, pack_name: &str) -> String {
63+
let readme_template_path = configuration.readme_template_path.clone();
64+
65+
if readme_template_path.exists() {
66+
std::fs::read_to_string(readme_template_path).unwrap()
67+
} else {
68+
format!(
6469
"Welcome to `{}`!
6570
6671
If you're the author, please consider replacing this file with a README.md, which may contain:
@@ -76,8 +81,9 @@ If you're the author, please consider replacing this file with a README.md, whic
7681
README.md should change as your public API changes.
7782
7883
See https://github.com/rubyatscale/pks#readme for more info!",
79-
pack_name
80-
)
84+
pack_name
85+
)
86+
}
8187
}
8288

8389
fn is_rails(configuration: &Configuration) -> bool {

src/packs/pack_set.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ impl PackSet {
108108
pub fn all_pack_dependencies<'a>(
109109
&'a self,
110110
configuration: &'a Configuration,
111-
) -> Result<Vec<PackDependency>> {
112-
let mut pack_refs: Vec<PackDependency> = Vec::new();
111+
) -> Result<Vec<PackDependency<'a>>> {
112+
let mut pack_refs: Vec<PackDependency<'a>> = Vec::new();
113113
for from_pack in &configuration.pack_set.packs {
114114
for dependency_pack_name in &from_pack.dependencies {
115115
match configuration.pack_set.for_pack(dependency_pack_name) {

src/packs/parsing/ruby/experimental/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ struct ReferenceCollector<'a> {
2525
pub custom_associations: Vec<String>,
2626
}
2727

28-
impl<'a> Visitor for ReferenceCollector<'a> {
28+
impl Visitor for ReferenceCollector<'_> {
2929
fn on_class(&mut self, node: &nodes::Class) {
3030
// We're not collecting definitions, so no need to visit the class definitioname);
3131
let namespace_result = fetch_const_name(&node.name);

src/packs/parsing/ruby/packwerk/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct ReferenceCollector<'a> {
3636
pub custom_associations: Vec<String>,
3737
}
3838

39-
impl<'a> Visitor for ReferenceCollector<'a> {
39+
impl Visitor for ReferenceCollector<'_> {
4040
fn on_class(&mut self, node: &nodes::Class) {
4141
// We're not collecting definitions, so no need to visit the class definitioname);
4242
let namespace_result = fetch_const_name(&node.name);

src/packs/raw_configuration.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ pub(crate) struct RawConfiguration {
5252
#[serde(default)]
5353
pub layers: Vec<String>,
5454

55+
// Path to the README template
56+
#[serde(default)]
57+
pub readme_template_path: Option<PathBuf>,
58+
5559
// Experimental parser
5660
#[serde(default)]
5761
pub experimental_parser: bool,

tests/common/mod.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,30 @@ pub fn delete_foobar() {
3131
}
3232
}
3333

34+
#[allow(dead_code)]
35+
pub fn delete_foobaz() {
36+
let directory =
37+
PathBuf::from("tests/fixtures/simple_packs_first_app/packs/foobaz");
38+
if let Err(err) = fs::remove_dir_all(directory) {
39+
eprintln!(
40+
"Failed to remove tests/fixtures/simple_packs_first_app/packs/foobaz during test teardown: {}",
41+
err
42+
);
43+
}
44+
}
45+
46+
#[allow(dead_code)]
47+
pub fn delete_foobar_app_with_custom_readme() {
48+
let directory =
49+
PathBuf::from("tests/fixtures/app_with_custom_readme/packs/foobar");
50+
if let Err(err) = fs::remove_dir_all(directory) {
51+
eprintln!(
52+
"Failed to remove tests/fixtures/app_with_custom_readme/packs/foobar during test teardown: {}",
53+
err
54+
);
55+
}
56+
}
57+
3458
// In case we want our tests to call `update` or otherwise mutate the file system
3559
#[allow(dead_code)]
3660
pub fn set_up_fixtures() {

0 commit comments

Comments
 (0)