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

generate cache scaffolding #6

Merged
merged 9 commits into from
May 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,13 @@
![Audit](https://github.com/alexevanczuk/pks/actions/workflows/audit.yml/badge.svg)

WIP: Rust implementation of packs for ruby

# Initial Milestone

- [ ] `pks generate_cache`, which can be used to update `tmp/packwerk/cache` for faster `packwerk` output. It should produce the exact same `json` that `packwerk` produces today. Challenges include:
- [ ] Parsing ERB
- [ ] Parsing Rails associations and rewriting them as constant references using a pluralizer. Initially, non-standard inflections will likely not be supported (although I may support it through hard-coded map in `packwerk.yml`)
- [ ] Replicating packwerk's behavior with respect to not recording "local definitions"
- [ ] `pks check`, which can be used as a drop-in replacement to the VSCode
- [ ] `pks update`, which can be used to update `deprecated_references.yml`
- [ ] `pks lsp`, to launch an LSP-server to provide faster feedback
7 changes: 3 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
pub mod cli;
pub mod packs;
use cli::cli;
use packs::cli;

pub mod packs;
fn main() {
cli()
cli::run()
}
4 changes: 3 additions & 1 deletion src/packs.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use glob::glob;
use std::path::PathBuf;

pub(crate) mod cli;
pub mod parser;
mod string_helpers;

pub fn greet() {
println!("Hello! This CLI is under construction.")
Expand Down Expand Up @@ -62,7 +64,7 @@ mod tests {
#[test]
fn test_all() {
let mut expected_packs: Vec<Pack> = Vec::new();
let absolute_root: PathBuf = PathBuf::from("tests/fixtures/simple_dependency_violation");
let absolute_root: PathBuf = PathBuf::from("tests/fixtures/simple_app");

let foo_yml = absolute_root.join(PathBuf::from("packs/foo/package.yml"));
let root_yml = absolute_root.join(PathBuf::from("package.yml"));
Expand Down
19 changes: 17 additions & 2 deletions src/cli.rs → src/packs/cli.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
use crate::packs;
use crate::packs::parser;
use crate::packs::{self, string_helpers};

// Make this import work
// I'm getting this error:
// unresolved import `crate::string_helpers`
// no `string_helpers` in the root
// use crate::string_helpers;

use clap::{Parser, Subcommand};
use std::path::PathBuf;

Expand All @@ -8,6 +15,10 @@ enum Command {
Greet,
ListPacks,
Check,
GenerateCache {
#[clap(required = true)]
files: Vec<String>,
},
}

/// A CLI to interact with packs
Expand All @@ -32,7 +43,7 @@ impl Args {
// }
}

pub fn cli() {
pub fn run() {
let args = Args::parse();
let absolute_root = args.absolute_project_root().expect("Issue getting absolute_project_root!");
match args.command {
Expand All @@ -43,5 +54,9 @@ pub fn cli() {
Command::Check => {
parser::get_references(absolute_root);
}
Command::GenerateCache { files } => {
let file_string = string_helpers::to_sentence(files);
println!("Cache was generated for files {}", file_string);
}
}
}
4 changes: 4 additions & 0 deletions src/packs/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,3 +463,7 @@ mod tests {
);
}
}

pub fn generate_cache(_absolute_root: PathBuf) {
todo!()
}
18 changes: 18 additions & 0 deletions src/packs/string_helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
pub(crate) fn to_sentence(list: Vec<String>) -> String {
let mut file_string = String::new();
// Extract this to src/string_helpers.rs to_sentence function and import it into this
// and invoke it.
for (i, file) in list.iter().enumerate() {
if i == 0 {
file_string.push_str(file);
} else if i == list.len() - 1 {
file_string.push_str(", and ");
file_string.push_str(file);
} else {
file_string.push_str(", ");
file_string.push_str(file);
}
}

file_string
}
20 changes: 20 additions & 0 deletions tests/generate_cache.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use assert_cmd::prelude::*;
use predicates::prelude::*;
use std::{error::Error, process::Command};

#[test]
fn test_generate_cache() -> Result<(), Box<dyn Error>> {
Command::cargo_bin("pks")?
.arg("--project-root")
.arg("tests/fixtures/simple_app")
.arg("generate-cache")
.arg("file1.rb")
.arg("file2.rb")
.arg("file3.rb")
.assert()
.success()
.stdout(predicate::str::contains(
"Cache was generated for files file1.rb, file2.rb, and file3.rb\n",
));
Ok(())
}
2 changes: 1 addition & 1 deletion tests/list_packs_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{error::Error, process::Command};
fn lint_packs() -> Result<(), Box<dyn Error>> {
Command::cargo_bin("pks")?
.arg("--project-root")
.arg("tests/fixtures/simple_dependency_violation")
.arg("tests/fixtures/simple_app")
.arg("list-packs")
.assert()
.success()
Expand Down
2 changes: 1 addition & 1 deletion tests/simple_dependency_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{error::Error, process::Command};
fn test_check() -> Result<(), Box<dyn Error>> {
Command::cargo_bin("packs")?
.arg("--project-root")
.arg("tests/fixtures/simple_dependency_violation")
.arg("tests/fixtures/simple_app")
.arg("check")
.assert()
.failure()
Expand Down