-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacks.rs
More file actions
362 lines (322 loc) · 10.3 KB
/
packs.rs
File metadata and controls
362 lines (322 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// Currently there are no supported library APIs for packs. The public API is the CLI.
// This may change in the future! Please file an issue if you have a use case for a library API.
pub mod cli;
// Module declarations
pub(crate) mod bin_locater;
pub(crate) mod caching;
pub(crate) mod checker;
pub(crate) mod checker_configuration;
pub(crate) mod configuration;
pub(crate) mod constant_resolver;
pub(crate) mod creator;
pub(crate) mod csv;
pub(crate) mod dependencies;
pub(crate) mod ignored;
pub(crate) mod monkey_patch_detection;
pub(crate) mod pack;
pub(crate) mod parsing;
pub(crate) mod raw_configuration;
pub mod walk_directory;
mod constant_dependencies;
mod file_utils;
mod logger;
mod pack_set;
mod package_todo;
mod reference_extractor;
use crate::packs;
use crate::packs::pack::write_pack_to_disk;
// Internal imports
pub(crate) use self::checker::Violation;
use self::creator::CreateResult;
pub(crate) use self::pack_set::PackSet;
pub(crate) use self::parsing::process_files_with_cache;
pub(crate) use self::parsing::ruby::experimental::get_experimental_constant_resolver;
pub(crate) use self::parsing::ruby::zeitwerk::get_zeitwerk_constant_resolver;
pub(crate) use self::parsing::ParsedDefinition;
pub(crate) use self::parsing::UnresolvedReference;
use anyhow::bail;
use cli::OutputFormat;
pub(crate) use configuration::Configuration;
pub(crate) use package_todo::PackageTodo;
// External imports
use anyhow::Context;
use serde::Deserialize;
use serde::Serialize;
use std::path::PathBuf;
pub fn greet() {
println!("👋 Hello! Welcome to packs 📦 🔥 🎉 🌈. This tool is under construction.")
}
pub fn create(
configuration: &Configuration,
name: String,
) -> anyhow::Result<()> {
match creator::create(configuration, &name)? {
CreateResult::AlreadyExists => {
println!("`{}` already exists!", &name);
}
CreateResult::Success => {
println!("Successfully created `{}`!", &name);
}
}
Ok(())
}
pub fn check(
configuration: &Configuration,
output_format: OutputFormat,
files: Vec<String>,
) -> anyhow::Result<()> {
let result = checker::check_all(configuration, files)
.context("Failed to check files")?;
match output_format {
OutputFormat::Packwerk => {
println!("{}", result);
if result.has_violations() {
bail!("Violations found!")
}
}
OutputFormat::CSV => {
csv::write_csv(&result, std::io::stdout())?;
}
}
Ok(())
}
pub fn update(configuration: &Configuration) -> anyhow::Result<()> {
checker::update(configuration)
}
pub fn add_dependency(
configuration: &Configuration,
from: String,
to: String,
) -> anyhow::Result<()> {
let pack_set = &configuration.pack_set;
let from_pack = pack_set
.for_pack(&from)
.context(format!("`{}` not found", from))?;
let to_pack = pack_set
.for_pack(&to)
.context(format!("`{}` not found", to))?;
// Print a warning if the dependency already exists
if from_pack.dependencies.contains(&to_pack.name) {
println!(
"`{}` already depends on `{}`!",
from_pack.name, to_pack.name
);
return Ok(());
}
let new_from_pack = from_pack.add_dependency(to_pack);
write_pack_to_disk(&new_from_pack)?;
// Note: Ideally we wouldn't have to refetch the configuration and could instead
// either update the existing one OR modify the existing one and return a new one
// (which takes ownership over the previous one).
// For now, we simply refetch the entire configuration for simplicity,
// since we don't mind the slowdown for this CLI command.
let new_configuration = configuration::get(&configuration.absolute_root)?;
let validation_result = packs::validate(&new_configuration);
if validation_result.is_err() {
println!("Added `{}` as a dependency to `{}`!", to, from);
println!("Warning: This creates a cycle!");
} else {
println!("Successfully added `{}` as a dependency to `{}`!", to, from);
}
Ok(())
}
pub fn list_included_files(configuration: Configuration) -> anyhow::Result<()> {
configuration
.included_files
.iter()
.for_each(|f| println!("{}", f.display()));
Ok(())
}
pub fn validate(configuration: &Configuration) -> anyhow::Result<()> {
checker::validate_all(configuration)
}
pub fn configuration(project_root: PathBuf) -> anyhow::Result<Configuration> {
let absolute_root = project_root.canonicalize()?;
configuration::get(&absolute_root)
}
pub fn check_unnecessary_dependencies(
configuration: &Configuration,
auto_correct: bool,
) -> anyhow::Result<()> {
if auto_correct {
checker::remove_unnecessary_dependencies(configuration)
} else {
checker::check_unnecessary_dependencies(configuration)
}
}
pub fn update_dependencies_for_constant(
configuration: &Configuration,
constant_name: &str,
) -> anyhow::Result<()> {
match constant_dependencies::update_dependencies_for_constant(
configuration,
constant_name,
) {
Ok(num_updated) => {
match num_updated {
0 => println!(
"No dependencies to update for constant '{}'",
constant_name
),
1 => println!(
"Successfully updated 1 dependency for constant '{}'",
constant_name
),
_ => println!(
"Successfully updated {} dependencies for constant '{}'",
num_updated, constant_name
),
}
Ok(())
}
Err(err) => Err(anyhow::anyhow!(err)),
}
}
pub fn list(configuration: Configuration) {
for pack in configuration.pack_set.packs {
println!("{}", pack.yml.display())
}
}
pub fn lint_package_yml_files(
configuration: &Configuration,
) -> anyhow::Result<()> {
for pack in &configuration.pack_set.packs {
write_pack_to_disk(pack)?
}
Ok(())
}
pub fn delete_cache(configuration: Configuration) {
let absolute_cache_dir = configuration.cache_directory;
if let Err(err) = std::fs::remove_dir_all(&absolute_cache_dir) {
eprintln!(
"Failed to remove {}: {}",
&absolute_cache_dir.display(),
err
);
}
}
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
pub struct ProcessedFile {
pub absolute_path: PathBuf,
pub unresolved_references: Vec<UnresolvedReference>,
pub definitions: Vec<ParsedDefinition>,
}
#[derive(Debug, PartialEq, Serialize, Deserialize, Default, Eq, Clone)]
pub struct SourceLocation {
line: usize,
column: usize,
}
pub(crate) fn list_definitions(
configuration: &Configuration,
ambiguous: bool,
) -> anyhow::Result<()> {
let constant_resolver = if configuration.experimental_parser {
let processed_files: Vec<ProcessedFile> = process_files_with_cache(
&configuration.included_files,
configuration.get_cache(),
configuration,
)?;
get_experimental_constant_resolver(
&configuration.absolute_root,
&processed_files,
&configuration.ignored_definitions,
)
} else {
if ambiguous {
bail!("Ambiguous mode is not supported for the Zeitwerk parser");
}
get_zeitwerk_constant_resolver(
&configuration.pack_set,
&configuration.constant_resolver_configuration(),
)
};
let constant_definition_map = constant_resolver
.fully_qualified_constant_name_to_constant_definition_map();
for (name, definitions) in constant_definition_map {
if ambiguous && definitions.len() == 1 {
continue;
}
for definition in definitions {
let relative_path = definition
.absolute_path_of_definition
.strip_prefix(&configuration.absolute_root)?;
println!("{:?} is defined at {:?}", name, relative_path);
}
}
Ok(())
}
fn expose_monkey_patches(
configuration: &Configuration,
rubydir: &PathBuf,
gemdir: &PathBuf,
) -> anyhow::Result<()> {
println!(
"{}",
monkey_patch_detection::expose_monkey_patches(
configuration,
rubydir,
gemdir,
)?
);
Ok(())
}
fn list_dependencies(
configuration: &Configuration,
pack_name: String,
) -> anyhow::Result<()> {
println!("Pack dependencies for {}\n", pack_name);
let dependencies =
dependencies::find_dependencies(configuration, &pack_name)?;
println!("Explicit ({}):", dependencies.explicit.len());
if dependencies.explicit.is_empty() {
println!("- None");
} else {
for dependency in dependencies.explicit {
println!("- {}", dependency);
}
}
println!("\nImplicit (violations) ({}):", dependencies.implicit.len());
if dependencies.implicit.is_empty() {
println!("- None");
} else {
let mut dependent_packs_with_violations =
dependencies.implicit.keys().collect::<Vec<_>>();
dependent_packs_with_violations.sort();
for dependent in dependent_packs_with_violations {
println!("- {}", dependent);
for (violation_type, count) in &dependencies.implicit[dependent] {
println!(" - {}: {}", violation_type, count);
}
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
#[test]
fn test_for_file() {
let configuration = configuration::get(
PathBuf::from("tests/fixtures/simple_app")
.canonicalize()
.expect("Could not canonicalize path")
.as_path(),
)
.unwrap();
let absolute_file_path = configuration
.absolute_root
.join("packs/foo/app/services/foo.rb")
.canonicalize()
.expect("Could not canonicalize path");
assert_eq!(
String::from("packs/foo"),
configuration
.pack_set
.for_file(&absolute_file_path)
.unwrap()
.unwrap()
.name
)
}
}