Skip to content

Commit

Permalink
feat: move pattern files to the main patterns array (#398)
Browse files Browse the repository at this point in the history
  • Loading branch information
morgante committed Jul 4, 2024
1 parent 4165037 commit f850d3a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 17 deletions.
5 changes: 2 additions & 3 deletions crates/gritmodule/fixtures/pattern_files/.grit/grit.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
version: 0.0.1
patterns:
- file: ../docs/guides/version_5_upgrade.md
- file: ../docs/guides/something.md
- name: remove_console_error
level: error
body: |
engine marzano(0.1)
language js
`console.error($_)` => .
pattern_files:
- docs/guides/version_5_upgrade.md
- docs/guides/something.md
20 changes: 17 additions & 3 deletions crates/gritmodule/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,31 @@ pub struct GritGitHubConfig {
pub reviewers: Vec<String>,
}

/// Represents a reference to an external pattern file
#[derive(Debug, Deserialize)]
pub struct GritPatternFile {
pub file: PathBuf,
}

/// Pure in-memory representation of the grit config
#[derive(Debug)]
pub struct GritConfig {
pub patterns: Vec<GritDefinitionConfig>,
pub pattern_files: Option<Vec<String>>,
pub pattern_files: Option<Vec<GritPatternFile>>,
pub github: Option<GritGitHubConfig>,
}

#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum GritPatternConfig {
File(GritPatternFile),
Pattern(GritSerializedDefinitionConfig),
}

/// Compacted / serialized version of the GritConfig
#[derive(Debug, Deserialize)]
pub struct SerializedGritConfig {
pub patterns: Vec<GritSerializedDefinitionConfig>,
pub pattern_files: Option<Vec<String>>,
pub patterns: Vec<GritPatternConfig>,
pub github: Option<GritGitHubConfig>,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ expression: resolved_patterns
samples: ~
path: ".grit/grit.yaml"
position:
line: 3
line: 5
column: 11
raw: ~
module:
Expand Down Expand Up @@ -136,7 +136,7 @@ expression: resolved_patterns
startByte: 1220
endByte: 1262
output_range: ~
path: fixtures/pattern_files/docs/guides/something.md
path: ".grit/../docs/guides/something.md"
position:
line: 10
column: 1
Expand Down Expand Up @@ -264,7 +264,7 @@ expression: resolved_patterns
startByte: 1220
endByte: 1262
output_range: ~
path: fixtures/pattern_files/docs/guides/version_5_upgrade.md
path: ".grit/../docs/guides/version_5_upgrade.md"
position:
line: 10
column: 1
Expand Down
36 changes: 28 additions & 8 deletions crates/gritmodule/src/yaml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,31 @@ pub fn get_grit_config(source: &str, source_path: &str) -> Result<GritConfig> {
}
};

let mut patterns = Vec::new();
let mut pattern_files = Vec::new();

for pattern in serialized.patterns.into_iter() {
match pattern {
crate::config::GritPatternConfig::File(file) => {
pattern_files.push(file);
}
crate::config::GritPatternConfig::Pattern(p) => {
patterns.push(GritDefinitionConfig::from_serialized(
p,
source_path.to_string(),
));
}
}
}

let new_config = GritConfig {
github: serialized.github,
pattern_files: serialized.pattern_files,
patterns: serialized
.patterns
.into_iter()
.map(|p| GritDefinitionConfig::from_serialized(p, source_path.to_string()))
.collect(),
pattern_files: if pattern_files.is_empty() {
None
} else {
Some(pattern_files)
},
patterns,
};

Ok(new_config)
Expand All @@ -47,7 +64,8 @@ pub async fn get_patterns_from_yaml(
root: &Option<String>,
repo_dir: &str,
) -> Result<Vec<ModuleGritPattern>> {
let mut config = get_grit_config(&file.content, &extract_relative_file_path(file, root))?;
let grit_path = extract_relative_file_path(file, root);
let mut config = get_grit_config(&file.content, &grit_path)?;

for pattern in config.patterns.iter_mut() {
pattern.kind = Some(DefinitionKind::Pattern);
Expand All @@ -69,7 +87,9 @@ pub async fn get_patterns_from_yaml(
let mut file_readers = Vec::new();

for pattern_file in config.pattern_files.unwrap() {
let pattern_file = PathBuf::from(repo_dir).join(&pattern_file);
let pattern_file = PathBuf::from(repo_dir)
.join(REPO_CONFIG_DIR_NAME)
.join(&pattern_file.file);
let extension = PatternFileExt::from_path(&pattern_file);
if extension.is_none() {
continue;
Expand Down

0 comments on commit f850d3a

Please sign in to comment.