Skip to content

Commit f639c75

Browse files
feat: unify configuration
fix ast-grep#1557
1 parent 0523bf7 commit f639c75

File tree

9 files changed

+50
-62
lines changed

9 files changed

+50
-62
lines changed

crates/cli/src/config.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,6 @@ pub struct ProjectConfig {
6060
}
6161

6262
impl ProjectConfig {
63-
pub fn by_config_path_must(config_path: Option<PathBuf>) -> Result<Self> {
64-
Self::discover_project(config_path, None)?.ok_or_else(|| anyhow::anyhow!(EC::ProjectNotExist))
65-
}
66-
pub fn by_config_path(config_path: Option<PathBuf>) -> Result<Option<Self>> {
67-
Self::discover_project(config_path, None)
68-
}
6963
// return None if config file does not exist
7064
fn discover_project(config_path: Option<PathBuf>, base: Option<&Path>) -> Result<Option<Self>> {
7165
let config_path =
@@ -94,17 +88,17 @@ impl ProjectConfig {
9488
let global_rules = find_util_rules(self)?;
9589
read_directory_yaml(self, global_rules, rule_overwrite)
9690
}
91+
// do not report error if no sgconfig.yml is found
92+
pub fn setup(config_path: Option<PathBuf>) -> Result<Option<Self>> {
93+
let Some(config) = Self::discover_project(config_path, None)? else {
94+
return Ok(None);
95+
};
96+
register_custom_language(&config.project_dir, config.sg_config.clone())?;
97+
Ok(Some(config))
98+
}
9799
}
98100

99-
pub fn register_custom_language(project_config: Option<ProjectConfig>) -> Result<()> {
100-
// do not report error if no sgconfig.yml is found
101-
let Some(project_config) = project_config else {
102-
return Ok(());
103-
};
104-
let ProjectConfig {
105-
project_dir,
106-
sg_config,
107-
} = project_config;
101+
fn register_custom_language(project_dir: &Path, sg_config: AstGrepConfig) -> Result<()> {
108102
if let Some(custom_langs) = sg_config.custom_languages {
109103
SgLang::register_custom_language(project_dir, custom_langs);
110104
}

crates/cli/src/lang/custom_lang.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ pub struct CustomLang {
1616
}
1717

1818
impl CustomLang {
19-
pub fn register(base: PathBuf, langs: HashMap<String, CustomLang>) {
19+
pub fn register(base: &Path, langs: HashMap<String, CustomLang>) {
2020
let registrations = langs
2121
.into_iter()
22-
.map(|(name, custom)| to_registration(name, custom, &base))
22+
.map(|(name, custom)| to_registration(name, custom, base))
2323
.collect();
2424
// TODO, add error handling
2525
unsafe { DynamicLang::register(registrations).expect("TODO") }

crates/cli/src/lang/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use serde::{Deserialize, Serialize};
1515
use std::borrow::Cow;
1616
use std::collections::HashMap;
1717
use std::fmt::{Debug, Display, Formatter};
18-
use std::path::{Path, PathBuf};
18+
use std::path::Path;
1919
use std::str::FromStr;
2020

2121
pub use custom_lang::CustomLang;
@@ -40,7 +40,7 @@ impl SgLang {
4040
}
4141

4242
// register_globs must be called after register_custom_language
43-
pub fn register_custom_language(base: PathBuf, langs: HashMap<String, CustomLang>) {
43+
pub fn register_custom_language(base: &Path, langs: HashMap<String, CustomLang>) {
4444
CustomLang::register(base, langs)
4545
}
4646

crates/cli/src/lsp.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::config::{register_custom_language, ProjectConfig};
1+
use crate::config::ProjectConfig;
22
use crate::utils::ErrorContext as EC;
33
use anyhow::{Context, Result};
44
use ast_grep_lsp::{Backend, LspService, Server};
@@ -15,8 +15,9 @@ pub struct LspArg {
1515
async fn run_language_server_impl(arg: LspArg) -> Result<()> {
1616
// env_logger::init();
1717
// TODO: move this error to client
18-
let project_config = ProjectConfig::by_config_path_must(arg.config.clone())?;
19-
register_custom_language(Some(project_config.clone()))?;
18+
let Some(project_config) = ProjectConfig::setup(arg.config.clone())? else {
19+
return Err(anyhow::anyhow!(EC::ProjectNotExist));
20+
};
2021
let stdin = tokio::io::stdin();
2122
let stdout = tokio::io::stdout();
2223
let config_result = project_config.find_rules(Default::default());

crates/cli/src/new.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::config::{register_custom_language, AstGrepConfig, ProjectConfig, TestConfig};
1+
use crate::config::{AstGrepConfig, ProjectConfig, TestConfig};
22
use crate::lang::SgLang;
33
use crate::utils::ErrorContext as EC;
44

@@ -46,9 +46,6 @@ fn create_dir(project_dir: &Path, dir: &str) -> Result<PathBuf> {
4646
}
4747

4848
impl NewArg {
49-
fn find_project_config(&self) -> Result<Option<ProjectConfig>> {
50-
ProjectConfig::by_config_path(self.config.clone())
51-
}
5249
fn ask_dir(&self, prompt: &str, default: &str) -> Result<String> {
5350
let dir = if self.yes {
5451
default.to_owned()
@@ -152,18 +149,17 @@ impl Display for Entity {
152149
}
153150

154151
pub fn run_create_new(mut arg: NewArg) -> Result<()> {
155-
let project_config = arg.find_project_config()?;
156-
register_custom_language(project_config)?;
152+
let project_config = ProjectConfig::setup(arg.config.clone())?;
157153
if let Some(entity) = arg.entity.take() {
158-
run_create_entity(entity, arg)
154+
run_create_entity(entity, arg, project_config)
159155
} else {
160-
ask_entity_type(arg)
156+
ask_entity_type(arg, project_config)
161157
}
162158
}
163159

164-
fn run_create_entity(entity: Entity, arg: NewArg) -> Result<()> {
160+
fn run_create_entity(entity: Entity, arg: NewArg, project: Option<ProjectConfig>) -> Result<()> {
165161
// check if we are under a project dir
166-
if let Some(found) = arg.find_project_config()? {
162+
if let Some(found) = project {
167163
return do_create_entity(entity, found, arg);
168164
}
169165
// check if we creating a project
@@ -185,9 +181,9 @@ fn do_create_entity(entity: Entity, found: ProjectConfig, arg: NewArg) -> Result
185181
}
186182
}
187183

188-
fn ask_entity_type(arg: NewArg) -> Result<()> {
184+
fn ask_entity_type(arg: NewArg, project: Option<ProjectConfig>) -> Result<()> {
189185
// 1. check if we are under a sgconfig.yml
190-
if let Some(found) = arg.find_project_config()? {
186+
if let Some(found) = project {
191187
// 2. ask users what to create if yes
192188
let entity = arg.ask_entity_type()?;
193189
do_create_entity(entity, found, arg)

crates/cli/src/run.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use ast_grep_language::Language;
77
use clap::{builder::PossibleValue, Parser, ValueEnum};
88
use ignore::WalkParallel;
99

10-
use crate::config::register_custom_language;
10+
use crate::config::ProjectConfig;
1111
use crate::lang::SgLang;
1212
use crate::print::{ColoredPrinter, Diff, Heading, InteractivePrinter, JSONPrinter, Printer};
1313
use crate::utils::ErrorContext as EC;
@@ -22,7 +22,7 @@ pub fn register_custom_language_if_is_run(args: &[String]) -> Result<()> {
2222
return Ok(());
2323
};
2424
if arg.starts_with('-') || arg == "run" {
25-
register_custom_language(None)?;
25+
ProjectConfig::setup(None)?;
2626
}
2727
Ok(())
2828
}

crates/cli/src/scan.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use ast_grep_core::{NodeMatch, StrDoc};
99
use clap::Args;
1010
use ignore::WalkParallel;
1111

12-
use crate::config::{read_rule_file, register_custom_language, ProjectConfig};
12+
use crate::config::{read_rule_file, ProjectConfig};
1313
use crate::lang::SgLang;
1414
use crate::print::{
1515
CloudPrinter, ColoredPrinter, Diff, InteractivePrinter, JSONPrinter, Platform, Printer,
@@ -68,8 +68,6 @@ pub struct ScanArg {
6868
}
6969

7070
pub fn run_with_config(arg: ScanArg) -> Result<()> {
71-
let project_config = ProjectConfig::by_config_path(arg.config.clone())?;
72-
register_custom_language(project_config)?;
7371
let context = arg.context.get();
7472
if let Some(_format) = &arg.format {
7573
let printer = CloudPrinter::stdout();
@@ -93,12 +91,13 @@ pub fn run_with_config(arg: ScanArg) -> Result<()> {
9391
}
9492

9593
fn run_scan<P: Printer + 'static>(arg: ScanArg, printer: P) -> Result<()> {
94+
let project_config = ProjectConfig::setup(arg.config.clone())?;
9695
if arg.input.stdin {
9796
let worker = ScanWithRule::try_new(arg, printer)?;
9897
// TODO: report a soft error if rules have different languages
9998
worker.run_std_in()
10099
} else {
101-
let worker = ScanWithConfig::try_new(arg, printer)?;
100+
let worker = ScanWithConfig::try_new(arg, printer, project_config)?;
102101
worker.run_path()
103102
}
104103
}
@@ -110,7 +109,7 @@ struct ScanWithConfig<Printer> {
110109
trace: ScanTrace,
111110
}
112111
impl<P: Printer> ScanWithConfig<P> {
113-
fn try_new(arg: ScanArg, printer: P) -> Result<Self> {
112+
fn try_new(arg: ScanArg, printer: P, project: Option<ProjectConfig>) -> Result<Self> {
114113
let mut rule_trace = RuleTrace::default();
115114
let configs = if let Some(path) = &arg.rule {
116115
let rules = read_rule_file(path, None)?;
@@ -120,7 +119,7 @@ impl<P: Printer> ScanWithConfig<P> {
120119
.with_context(|| EC::ParseRule("INLINE_RULES".into()))?;
121120
RuleCollection::try_new(rules).context(EC::GlobPattern)?
122121
} else {
123-
let project_config = ProjectConfig::by_config_path_must(arg.config.clone())?;
122+
let project_config = project.ok_or_else(|| anyhow::anyhow!(EC::ProjectNotExist))?;
124123
let overwrite = RuleOverwrite::new(&arg.overwrite)?;
125124
let (configs, r_stats) = project_config.find_rules(overwrite)?;
126125
rule_trace = r_stats;

crates/cli/src/verify.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ mod reporter;
44
mod snapshot;
55
mod test_case;
66

7-
use crate::config::{register_custom_language, ProjectConfig};
7+
use crate::config::ProjectConfig;
88
use crate::lang::SgLang;
99
use crate::utils::ErrorContext;
1010
use anyhow::{anyhow, Result};
@@ -56,9 +56,12 @@ where
5656
})
5757
}
5858

59-
fn run_test_rule_impl<R: Reporter + Send>(arg: TestArg, reporter: R) -> Result<()> {
60-
let project_config = ProjectConfig::by_config_path_must(arg.config.clone())?;
61-
let collections = &project_config.find_rules(Default::default())?.0;
59+
fn run_test_rule_impl<R: Reporter + Send>(
60+
arg: TestArg,
61+
reporter: R,
62+
project: ProjectConfig,
63+
) -> Result<()> {
64+
let collections = &project.find_rules(Default::default())?.0;
6265
let TestHarness {
6366
test_cases,
6467
snapshots,
@@ -67,7 +70,7 @@ fn run_test_rule_impl<R: Reporter + Send>(arg: TestArg, reporter: R) -> Result<(
6770
let snapshot_dirname = arg.snapshot_dir.as_deref();
6871
TestHarness::from_dir(&test_dirname, snapshot_dirname, arg.filter.as_ref())?
6972
} else {
70-
TestHarness::from_config(arg.config, arg.filter.as_ref())?
73+
TestHarness::from_config(project, arg.filter.as_ref())?
7174
};
7275
let snapshots = (!arg.skip_snapshot_tests).then_some(snapshots);
7376
let reporter = &Arc::new(Mutex::new(reporter));
@@ -184,20 +187,20 @@ pub struct TestArg {
184187
}
185188

186189
pub fn run_test_rule(arg: TestArg) -> Result<()> {
187-
let project_config = ProjectConfig::by_config_path(arg.config.clone())?;
188-
register_custom_language(project_config)?;
190+
let project = ProjectConfig::setup(arg.config.clone())?
191+
.ok_or_else(|| anyhow!(ErrorContext::ProjectNotExist))?;
189192
if arg.interactive {
190193
let reporter = InteractiveReporter {
191194
output: std::io::stdout(),
192195
should_accept_all: false,
193196
};
194-
run_test_rule_impl(arg, reporter)
197+
run_test_rule_impl(arg, reporter, project)
195198
} else {
196199
let reporter = DefaultReporter {
197200
output: std::io::stdout(),
198201
update_all: arg.update_all,
199202
};
200-
run_test_rule_impl(arg, reporter)
203+
run_test_rule_impl(arg, reporter, project)
201204
}
202205
}
203206

@@ -302,13 +305,8 @@ rule:
302305
assert!(ret.is_none());
303306
}
304307

305-
use codespan_reporting::term::termcolor::Buffer;
306308
#[test]
307309
fn test_run_verify_error() {
308-
let reporter = DefaultReporter {
309-
output: Buffer::no_color(),
310-
update_all: false,
311-
};
312310
let arg = TestArg {
313311
config: None,
314312
interactive: false,
@@ -318,7 +316,7 @@ rule:
318316
update_all: false,
319317
filter: None,
320318
};
321-
assert!(run_test_rule_impl(arg, reporter).is_err());
319+
assert!(run_test_rule(arg).is_err());
322320
}
323321
const TRANSFORM_TEXT: &str = "
324322
transform:

crates/cli/src/verify/find_file.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ pub struct TestHarness {
2323
}
2424

2525
impl TestHarness {
26-
pub fn from_config(config_path: Option<PathBuf>, regex_filter: Option<&Regex>) -> Result<Self> {
27-
find_tests(config_path, regex_filter)
26+
pub fn from_config(project_config: ProjectConfig, regex_filter: Option<&Regex>) -> Result<Self> {
27+
find_tests(project_config, regex_filter)
2828
}
2929

3030
pub fn from_dir(
@@ -87,13 +87,13 @@ impl<'a> HarnessBuilder<'a> {
8787
}
8888

8989
pub fn find_tests(
90-
config_path: Option<PathBuf>,
90+
project_config: ProjectConfig,
9191
regex_filter: Option<&Regex>,
9292
) -> Result<TestHarness> {
9393
let ProjectConfig {
94-
project_dir,
9594
sg_config,
96-
} = ProjectConfig::by_config_path_must(config_path)?;
95+
project_dir,
96+
} = project_config;
9797
let test_configs = sg_config.test_configs.unwrap_or_default();
9898
let mut builder = HarnessBuilder {
9999
base_dir: project_dir,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy