dart-decimate 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (188) hide show
  1. package/Cargo.lock +572 -0
  2. package/Cargo.toml +41 -0
  3. package/LICENSE +21 -0
  4. package/README.md +617 -0
  5. package/assets/dart-decimate-banner.png +0 -0
  6. package/npm/bin/dart-decimate-mcp.js +48 -0
  7. package/npm/bin/dart-decimate.js +48 -0
  8. package/npm/scripts/postinstall.js +46 -0
  9. package/npm/scripts/test-npx-local.js +54 -0
  10. package/npm/scripts/test-npx-mcp-local.js +56 -0
  11. package/package.json +45 -0
  12. package/src/baseline.rs +643 -0
  13. package/src/bin/dart-decimate-mcp.rs +11 -0
  14. package/src/boundaries/tests.rs +60 -0
  15. package/src/boundaries.rs +317 -0
  16. package/src/changed_scope.rs +117 -0
  17. package/src/ci_template.rs +399 -0
  18. package/src/cli/analyze.rs +462 -0
  19. package/src/cli/analyzer_options.rs +82 -0
  20. package/src/cli/audit_run.rs +327 -0
  21. package/src/cli/boundary_args.rs +44 -0
  22. package/src/cli/ci_template_run.rs +196 -0
  23. package/src/cli/command_name.rs +20 -0
  24. package/src/cli/common_args.rs +167 -0
  25. package/src/cli/config_run.rs +78 -0
  26. package/src/cli/coverage_run.rs +393 -0
  27. package/src/cli/decision_surface_run.rs +99 -0
  28. package/src/cli/default_command.rs +69 -0
  29. package/src/cli/dupes_args.rs +150 -0
  30. package/src/cli/entry_points.rs +121 -0
  31. package/src/cli/error.rs +149 -0
  32. package/src/cli/error_output.rs +68 -0
  33. package/src/cli/explain_run.rs +72 -0
  34. package/src/cli/fix_run.rs +162 -0
  35. package/src/cli/flags_args.rs +23 -0
  36. package/src/cli/health_args.rs +200 -0
  37. package/src/cli/hooks_run.rs +191 -0
  38. package/src/cli/impact_run.rs +95 -0
  39. package/src/cli/init_run.rs +49 -0
  40. package/src/cli/inspect_args.rs +20 -0
  41. package/src/cli/inspect_run.rs +66 -0
  42. package/src/cli/issue_filter_args.rs +240 -0
  43. package/src/cli/list_run.rs +311 -0
  44. package/src/cli/mode_args.rs +72 -0
  45. package/src/cli/output_format.rs +62 -0
  46. package/src/cli/regression_args.rs +156 -0
  47. package/src/cli/request_args.rs +175 -0
  48. package/src/cli/schema_commands.rs +72 -0
  49. package/src/cli/scope_args.rs +174 -0
  50. package/src/cli/security_args.rs +175 -0
  51. package/src/cli/security_gate_run.rs +66 -0
  52. package/src/cli/security_summary_run.rs +43 -0
  53. package/src/cli/summary_args.rs +12 -0
  54. package/src/cli/tests.rs +516 -0
  55. package/src/cli/trace_args.rs +67 -0
  56. package/src/cli/trace_run.rs +131 -0
  57. package/src/cli/trace_tests.rs +235 -0
  58. package/src/cli/unsupported_run.rs +116 -0
  59. package/src/cli/watch_run.rs +153 -0
  60. package/src/cli.rs +603 -0
  61. package/src/config/boundary_config.rs +235 -0
  62. package/src/config/cache.rs +25 -0
  63. package/src/config/dependencies.rs +114 -0
  64. package/src/config/health.rs +237 -0
  65. package/src/config/jsonc.rs +66 -0
  66. package/src/config/policy.rs +91 -0
  67. package/src/config/rule_aliases.rs +487 -0
  68. package/src/config/rules.rs +339 -0
  69. package/src/config.rs +635 -0
  70. package/src/coverage.rs +469 -0
  71. package/src/dart_parser.rs +673 -0
  72. package/src/decision_surface.rs +405 -0
  73. package/src/dependencies/analyze.rs +218 -0
  74. package/src/dependencies/codegen.rs +118 -0
  75. package/src/dependencies/discovery.rs +60 -0
  76. package/src/dependencies/overrides.rs +76 -0
  77. package/src/dependencies/private_src_imports.rs +32 -0
  78. package/src/dependencies/pubspec_document.rs +86 -0
  79. package/src/dependencies/pubspec_entry.rs +96 -0
  80. package/src/dependencies/tests.rs +544 -0
  81. package/src/dependencies/usage.rs +33 -0
  82. package/src/dependencies.rs +553 -0
  83. package/src/dependency_scripts.rs +91 -0
  84. package/src/dupes/lex.rs +236 -0
  85. package/src/dupes/tests.rs +119 -0
  86. package/src/dupes.rs +625 -0
  87. package/src/explain.rs +692 -0
  88. package/src/extract/directives.rs +252 -0
  89. package/src/extract/error_tests.rs +25 -0
  90. package/src/extract/members.rs +210 -0
  91. package/src/extract/primary_constructors.rs +421 -0
  92. package/src/extract/references.rs +151 -0
  93. package/src/extract/routes.rs +691 -0
  94. package/src/extract/signatures.rs +266 -0
  95. package/src/extract/string_tests.rs +18 -0
  96. package/src/extract/strings.rs +107 -0
  97. package/src/extract/tests.rs +697 -0
  98. package/src/extract.rs +680 -0
  99. package/src/feature_flags/tests.rs +76 -0
  100. package/src/feature_flags.rs +453 -0
  101. package/src/finding_identity.rs +60 -0
  102. package/src/fix.rs +551 -0
  103. package/src/generated.rs +53 -0
  104. package/src/graph/augment_tests.rs +212 -0
  105. package/src/graph/conditionals.rs +74 -0
  106. package/src/graph/package_config_tests.rs +100 -0
  107. package/src/graph/parts.rs +248 -0
  108. package/src/graph/pubspec_tests.rs +119 -0
  109. package/src/graph/tests.rs +837 -0
  110. package/src/graph/uri_tests.rs +102 -0
  111. package/src/graph.rs +644 -0
  112. package/src/health/coverage.rs +309 -0
  113. package/src/health/ownership.rs +175 -0
  114. package/src/health/runtime_coverage.rs +616 -0
  115. package/src/health/runtime_intelligence.rs +511 -0
  116. package/src/health/scores.rs +296 -0
  117. package/src/health/tests.rs +240 -0
  118. package/src/health/threshold_types.rs +163 -0
  119. package/src/health/thresholds.rs +229 -0
  120. package/src/health/types.rs +636 -0
  121. package/src/health.rs +597 -0
  122. package/src/hooks/agent_settings.rs +175 -0
  123. package/src/hooks/templates.rs +51 -0
  124. package/src/hooks.rs +569 -0
  125. package/src/impact.rs +287 -0
  126. package/src/init.rs +237 -0
  127. package/src/inspect.rs +142 -0
  128. package/src/intelligence/tests.rs +232 -0
  129. package/src/intelligence.rs +305 -0
  130. package/src/lib.rs +198 -0
  131. package/src/main.rs +5 -0
  132. package/src/manifest/mcp.rs +565 -0
  133. package/src/manifest.rs +552 -0
  134. package/src/mcp/cli_args/value_args.rs +175 -0
  135. package/src/mcp/cli_args.rs +658 -0
  136. package/src/mcp/cli_args_tests.rs +522 -0
  137. package/src/mcp/code_execute.rs +671 -0
  138. package/src/mcp/tools.rs +660 -0
  139. package/src/mcp.rs +325 -0
  140. package/src/output/audit_risk.rs +135 -0
  141. package/src/output/dependency_findings.rs +327 -0
  142. package/src/output/duplication_findings.rs +114 -0
  143. package/src/output/feature_flag_findings.rs +122 -0
  144. package/src/output/format.rs +31 -0
  145. package/src/output/graph_findings.rs +461 -0
  146. package/src/output/health_findings.rs +612 -0
  147. package/src/output/human.rs +27 -0
  148. package/src/output/next_steps.rs +92 -0
  149. package/src/output/route_findings.rs +76 -0
  150. package/src/output/runtime_coverage.rs +515 -0
  151. package/src/output/scope.rs +183 -0
  152. package/src/output/security_findings.rs +390 -0
  153. package/src/output/security_sarif.rs +150 -0
  154. package/src/output/suppressions.rs +323 -0
  155. package/src/output/symbol_findings.rs +257 -0
  156. package/src/output/tests.rs +114 -0
  157. package/src/output/types.rs +604 -0
  158. package/src/output/widget_findings.rs +213 -0
  159. package/src/output.rs +682 -0
  160. package/src/package_map.rs +495 -0
  161. package/src/policy/packs.rs +232 -0
  162. package/src/policy/tests.rs +134 -0
  163. package/src/policy.rs +640 -0
  164. package/src/project_list.rs +376 -0
  165. package/src/report_schema/inventory.rs +418 -0
  166. package/src/report_schema.rs +488 -0
  167. package/src/routes.rs +354 -0
  168. package/src/scan/tests.rs +250 -0
  169. package/src/scan.rs +275 -0
  170. package/src/security/detect.rs +639 -0
  171. package/src/security/tests.rs +178 -0
  172. package/src/security.rs +373 -0
  173. package/src/security_gate.rs +555 -0
  174. package/src/symbols/path_filters.rs +27 -0
  175. package/src/symbols/private_type_leaks.rs +125 -0
  176. package/src/symbols/riverpod.rs +113 -0
  177. package/src/symbols/tests.rs +692 -0
  178. package/src/symbols.rs +693 -0
  179. package/src/trace/tests.rs +225 -0
  180. package/src/trace.rs +690 -0
  181. package/src/unsupported.rs +47 -0
  182. package/src/widgets/lifecycle.rs +484 -0
  183. package/src/widgets/params.rs +236 -0
  184. package/src/widgets/tests.rs +430 -0
  185. package/src/widgets/top_level.rs +110 -0
  186. package/src/widgets/unrendered.rs +374 -0
  187. package/src/widgets.rs +590 -0
  188. package/src/workspace_scope.rs +358 -0
@@ -0,0 +1,72 @@
1
+ use std::io::Write;
2
+
3
+ use clap::{Arg, ArgMatches, Command};
4
+ use serde::Serialize;
5
+
6
+ use super::common_args::format_arg;
7
+ use super::{CliError, OutputFormat, output_format_value};
8
+ use crate::{explain_issue, render_explain_report};
9
+
10
+ pub(super) fn explain_command() -> Command {
11
+ Command::new("explain")
12
+ .about("Explain one Dart Decimate issue type")
13
+ .arg(
14
+ Arg::new("issue-type")
15
+ .value_name("ISSUE_TYPE")
16
+ .help("Issue type such as unused-export, unused exports, or code duplication")
17
+ .required(true),
18
+ )
19
+ .arg(format_arg())
20
+ }
21
+
22
+ pub(super) fn run_explain<W: Write>(
23
+ subcommand: &ArgMatches,
24
+ mut writer: W,
25
+ ) -> Result<i32, CliError> {
26
+ let format = output_format_value(subcommand);
27
+ let issue_type = subcommand
28
+ .get_one::<String>("issue-type")
29
+ .map_or("", String::as_str);
30
+
31
+ match explain_issue(issue_type) {
32
+ Ok(report) => {
33
+ match format {
34
+ OutputFormat::Json => {
35
+ serde_json::to_writer_pretty(&mut writer, &report)?;
36
+ writeln!(writer)?;
37
+ }
38
+ OutputFormat::Human => {
39
+ writer.write_all(render_explain_report(&report).as_bytes())?;
40
+ }
41
+ }
42
+ Ok(0)
43
+ }
44
+ Err(error) => {
45
+ let message = error.to_string();
46
+ match format {
47
+ OutputFormat::Json => {
48
+ serde_json::to_writer_pretty(
49
+ &mut writer,
50
+ &ExplainErrorEnvelope {
51
+ error: true,
52
+ message,
53
+ exit_code: 2,
54
+ },
55
+ )?;
56
+ writeln!(writer)?;
57
+ }
58
+ OutputFormat::Human => {
59
+ writeln!(writer, "{message}")?;
60
+ }
61
+ }
62
+ Ok(2)
63
+ }
64
+ }
65
+ }
66
+
67
+ #[derive(Debug, Serialize)]
68
+ struct ExplainErrorEnvelope {
69
+ error: bool,
70
+ message: String,
71
+ exit_code: i32,
72
+ }
@@ -0,0 +1,162 @@
1
+ use std::collections::BTreeSet;
2
+ use std::io::Write;
3
+
4
+ use clap::{Arg, ArgAction, ArgGroup, ArgMatches, Command};
5
+
6
+ use crate::config::apply_rules_to_report;
7
+ use crate::fix::{FixMode, fix_findings, render_fix_report};
8
+ use crate::output::{ReportCommand, build_json_report};
9
+ use crate::scan::{ScanOptions, scan_project_with_options};
10
+ use crate::{
11
+ DuplicateOptions, FeatureFlagOptions, HealthOptions, RegressionTolerance, SecurityOptions,
12
+ };
13
+
14
+ use super::analyze::analyze_project;
15
+ use super::{CliError, CommandRequest, OutputFormat};
16
+
17
+ pub(super) fn fix_command() -> Command {
18
+ super::scope_args::report_command(super::scan_command(
19
+ Command::new("fix").about("Plan or apply safe auto-fixes"),
20
+ ))
21
+ .arg(
22
+ Arg::new("action")
23
+ .long("action")
24
+ .value_name("ACTION")
25
+ .help("Only plan or apply this auto-fix action")
26
+ .num_args(1)
27
+ .action(ArgAction::Append),
28
+ )
29
+ .arg(
30
+ Arg::new("dry-run")
31
+ .long("dry-run")
32
+ .help("Preview safe fixes without modifying files")
33
+ .conflicts_with_all(["apply", "yes"])
34
+ .action(ArgAction::SetTrue),
35
+ )
36
+ .arg(
37
+ Arg::new("apply")
38
+ .long("apply")
39
+ .help("Apply planned fixes")
40
+ .requires("apply-confirmation")
41
+ .action(ArgAction::SetTrue),
42
+ )
43
+ .arg(
44
+ Arg::new("yes")
45
+ .long("yes")
46
+ .help("Apply planned fixes without prompting")
47
+ .action(ArgAction::SetTrue),
48
+ )
49
+ .arg(
50
+ Arg::new("confirm")
51
+ .long("confirm")
52
+ .help("Confirm that --apply may modify files")
53
+ .action(ArgAction::SetTrue),
54
+ )
55
+ .group(
56
+ ArgGroup::new("apply-confirmation")
57
+ .args(["confirm", "yes"])
58
+ .multiple(false),
59
+ )
60
+ }
61
+
62
+ pub(super) fn run_fix<W: Write>(subcommand: &ArgMatches, mut writer: W) -> Result<i32, CliError> {
63
+ let root = super::common_args::root_path(subcommand);
64
+ let config = super::load_config(&root, subcommand)?;
65
+ if super::report_output_format(subcommand, &config) == super::ReportOutputFormat::Sarif {
66
+ return Err(CliError::UnsupportedSarifFormat { command: "fix" });
67
+ }
68
+ let format = super::output_format(subcommand, &config);
69
+ let production = super::mode_args::production(subcommand, &config);
70
+ let project = scan_project_with_options(
71
+ &root,
72
+ &ScanOptions {
73
+ ignore_patterns: config.ignore_patterns.clone(),
74
+ conditional_environment: super::common_args::conditional_environment(subcommand),
75
+ },
76
+ )?;
77
+ let request = CommandRequest {
78
+ command: ReportCommand::Check,
79
+ root,
80
+ format: match format {
81
+ OutputFormat::Human => super::ReportOutputFormat::Human,
82
+ OutputFormat::Json => super::ReportOutputFormat::Json,
83
+ },
84
+ entry_points: super::entry_points(subcommand, &config),
85
+ production,
86
+ symbol_options: super::SymbolRequestOptions {
87
+ include_entry_exports: super::mode_args::include_entry_exports(subcommand, &config),
88
+ private_type_leaks: false,
89
+ },
90
+ boundaries: config.boundaries.clone(),
91
+ boundary_coverage: false,
92
+ boundary_allow_unmatched: config.boundary_allow_unmatched.clone(),
93
+ boundary_calls: Vec::new(),
94
+ policy_packs: Vec::new(),
95
+ audit_base: None,
96
+ audit_gate: super::audit_run::AuditGate::default(),
97
+ file_paths: subcommand
98
+ .get_many::<std::path::PathBuf>("file")
99
+ .map(|values| values.cloned().collect())
100
+ .unwrap_or_default(),
101
+ workspace_patterns: subcommand
102
+ .get_many::<String>("workspace")
103
+ .map(|values| values.cloned().collect())
104
+ .unwrap_or_default(),
105
+ changed_workspaces: subcommand.get_one::<String>("changed-workspaces").cloned(),
106
+ changed_since: subcommand.get_one::<String>("changed-since").cloned(),
107
+ baseline_paths: Vec::new(),
108
+ security_sarif_file: None,
109
+ security_gate: None,
110
+ security_diff: None,
111
+ security_changed_since: None,
112
+ security_issue_mode: super::security_args::SecurityIssueMode::default(),
113
+ security_summary_mode: super::security_args::SecuritySummaryMode::default(),
114
+ save_baseline: None,
115
+ regression_baseline: None,
116
+ save_regression_baseline: None,
117
+ regression_tolerance: RegressionTolerance::default(),
118
+ fail_on_regression: false,
119
+ trace_file: None,
120
+ trace_symbol: None,
121
+ trace_dependency: None,
122
+ trace_clone: None,
123
+ duplicate_options: DuplicateOptions::default(),
124
+ health_options: HealthOptions::default(),
125
+ feature_flag_options: FeatureFlagOptions::default(),
126
+ security_options: SecurityOptions::default(),
127
+ issue_filters: super::issue_filter_args::IssueFilters::default(),
128
+ scan_options: ScanOptions {
129
+ ignore_patterns: config.ignore_patterns.clone(),
130
+ conditional_environment: super::common_args::conditional_environment(subcommand),
131
+ },
132
+ ignore_dependencies: config.ignore_dependencies.clone(),
133
+ ignore_dependency_overrides: config.ignore_dependency_overrides.clone(),
134
+ rules: config.rules.clone(),
135
+ };
136
+ let mut results = analyze_project(&project, &request)?;
137
+ super::scope_args::apply_report_scope(&project, &mut results, &request)?;
138
+ let mut report = build_json_report(&project, &results);
139
+ apply_rules_to_report(&mut report, &request.rules)?;
140
+
141
+ let actions = subcommand
142
+ .get_many::<String>("action")
143
+ .map(|values| values.cloned().collect::<BTreeSet<_>>())
144
+ .unwrap_or_default();
145
+ let mode = if subcommand.get_flag("apply") || subcommand.get_flag("yes") {
146
+ FixMode::Apply
147
+ } else {
148
+ FixMode::DryRun
149
+ };
150
+ let fix_report = fix_findings(&project.root, &report.findings, &actions, mode);
151
+ match format {
152
+ OutputFormat::Human => writer.write_all(render_fix_report(&fix_report).as_bytes())?,
153
+ OutputFormat::Json => {
154
+ serde_json::to_writer_pretty(&mut writer, &fix_report)?;
155
+ writeln!(writer)?;
156
+ }
157
+ }
158
+
159
+ Ok(i32::from(
160
+ mode == FixMode::Apply && fix_report.summary.skipped > 0,
161
+ ))
162
+ }
@@ -0,0 +1,23 @@
1
+ use clap::{Arg, ArgMatches, Command, parser::ValueSource, value_parser};
2
+
3
+ use crate::FeatureFlagOptions;
4
+
5
+ pub(super) fn flags_command(command: Command) -> Command {
6
+ command.arg(
7
+ Arg::new("top")
8
+ .long("top")
9
+ .value_name("N")
10
+ .help("Show only the N most frequently referenced feature flags")
11
+ .value_parser(value_parser!(usize)),
12
+ )
13
+ }
14
+
15
+ pub(super) fn feature_flag_options_with_defaults(
16
+ matches: &ArgMatches,
17
+ mut options: FeatureFlagOptions,
18
+ ) -> FeatureFlagOptions {
19
+ if matches.value_source("top") == Some(ValueSource::CommandLine) {
20
+ options.top = matches.get_one::<usize>("top").copied();
21
+ }
22
+ options
23
+ }
@@ -0,0 +1,200 @@
1
+ use std::path::PathBuf;
2
+
3
+ use clap::{Arg, ArgAction, ArgMatches, Command, parser::ValueSource, value_parser};
4
+
5
+ use crate::{HealthOptions, LowTrafficThreshold};
6
+
7
+ pub(super) fn health_command(command: Command) -> Command {
8
+ health_command_without_top(command).arg(
9
+ Arg::new("top")
10
+ .long("top")
11
+ .value_name("N")
12
+ .help("Show only the N highest complexity findings")
13
+ .value_parser(value_parser!(usize)),
14
+ )
15
+ }
16
+
17
+ pub(super) fn health_command_without_top(command: Command) -> Command {
18
+ let command = command
19
+ .arg(
20
+ Arg::new("max-cyclomatic")
21
+ .long("max-cyclomatic")
22
+ .value_name("N")
23
+ .help("Maximum cyclomatic complexity before reporting")
24
+ .default_value("20")
25
+ .value_parser(value_parser!(usize)),
26
+ )
27
+ .arg(
28
+ Arg::new("max-cognitive")
29
+ .long("max-cognitive")
30
+ .value_name("N")
31
+ .help("Maximum cognitive complexity before reporting")
32
+ .default_value("15")
33
+ .value_parser(value_parser!(usize)),
34
+ )
35
+ .arg(
36
+ Arg::new("complexity-breakdown")
37
+ .long("complexity-breakdown")
38
+ .help("Include per-decision-point complexity contributions")
39
+ .action(ArgAction::SetTrue),
40
+ )
41
+ .arg(
42
+ Arg::new("coverage")
43
+ .long("coverage")
44
+ .value_name("PATH")
45
+ .help("LCOV file for coverage-aware health checks")
46
+ .value_parser(value_parser!(PathBuf)),
47
+ )
48
+ .arg(
49
+ Arg::new("coverage-gaps")
50
+ .long("coverage-gaps")
51
+ .help("Report Dart files with no covered executable lines in LCOV")
52
+ .action(ArgAction::SetTrue),
53
+ )
54
+ .arg(
55
+ Arg::new("max-crap")
56
+ .long("max-crap")
57
+ .value_name("N")
58
+ .help("Maximum CRAP score before reporting")
59
+ .value_parser(value_parser!(usize)),
60
+ );
61
+ runtime_coverage_args(command, false)
62
+ .arg(
63
+ Arg::new("file-scores")
64
+ .long("file-scores")
65
+ .help("Include per-file health scores")
66
+ .action(ArgAction::SetTrue),
67
+ )
68
+ .arg(
69
+ Arg::new("hotspots")
70
+ .long("hotspots")
71
+ .help("Report low-scoring file health hotspots")
72
+ .action(ArgAction::SetTrue),
73
+ )
74
+ .arg(
75
+ Arg::new("targets")
76
+ .long("targets")
77
+ .help("Report prioritized refactoring targets")
78
+ .action(ArgAction::SetTrue),
79
+ )
80
+ .arg(
81
+ Arg::new("ownership")
82
+ .long("ownership")
83
+ .help("Attach CODEOWNERS ownership metadata to health output")
84
+ .action(ArgAction::SetTrue),
85
+ )
86
+ .arg(
87
+ Arg::new("min-score")
88
+ .long("min-score")
89
+ .value_name("N")
90
+ .help("Minimum file health score before hotspot reporting")
91
+ .value_parser(value_parser!(usize)),
92
+ )
93
+ }
94
+
95
+ pub(super) fn runtime_coverage_args(command: Command, required: bool) -> Command {
96
+ command
97
+ .arg(
98
+ Arg::new("runtime-coverage")
99
+ .long("runtime-coverage")
100
+ .value_name("PATH")
101
+ .help("V8 coverage JSON/directory or Istanbul coverage-final.json")
102
+ .required(required)
103
+ .value_parser(value_parser!(PathBuf)),
104
+ )
105
+ .arg(
106
+ Arg::new("min-invocations-hot")
107
+ .long("min-invocations-hot")
108
+ .value_name("N")
109
+ .help("Minimum runtime invocations before a file is a hot path")
110
+ .default_value("100")
111
+ .value_parser(value_parser!(usize)),
112
+ )
113
+ .arg(
114
+ Arg::new("min-observation-volume")
115
+ .long("min-observation-volume")
116
+ .value_name("N")
117
+ .help("Minimum runtime observations for high-confidence signals")
118
+ .default_value("5000")
119
+ .value_parser(value_parser!(usize)),
120
+ )
121
+ .arg(
122
+ Arg::new("low-traffic-threshold")
123
+ .long("low-traffic-threshold")
124
+ .value_name("FRACTION")
125
+ .help("Runtime traffic fraction considered low traffic")
126
+ .default_value("0.001")
127
+ .value_parser(value_parser!(f64)),
128
+ )
129
+ }
130
+
131
+ pub(super) fn health_options_with_defaults(
132
+ matches: &ArgMatches,
133
+ mut options: HealthOptions,
134
+ ) -> HealthOptions {
135
+ if is_command_line(matches, "max-cyclomatic") {
136
+ if let Some(max_cyclomatic) = matches.get_one::<usize>("max-cyclomatic") {
137
+ options.max_cyclomatic = *max_cyclomatic;
138
+ }
139
+ }
140
+ if is_command_line(matches, "max-cognitive") {
141
+ if let Some(max_cognitive) = matches.get_one::<usize>("max-cognitive") {
142
+ options.max_cognitive = *max_cognitive;
143
+ }
144
+ }
145
+ if is_command_line(matches, "top") {
146
+ options.top = matches.get_one::<usize>("top").copied();
147
+ }
148
+ if is_command_line(matches, "complexity-breakdown") {
149
+ options.complexity_breakdown = matches.get_flag("complexity-breakdown").into();
150
+ }
151
+ if is_command_line(matches, "coverage") {
152
+ options.coverage_path = matches.get_one::<PathBuf>("coverage").cloned();
153
+ }
154
+ if is_command_line(matches, "coverage-gaps") {
155
+ options.coverage_gaps = matches.get_flag("coverage-gaps").into();
156
+ }
157
+ if is_command_line(matches, "max-crap") {
158
+ options.max_crap = matches.get_one::<usize>("max-crap").copied();
159
+ }
160
+ if is_command_line(matches, "runtime-coverage") {
161
+ options.runtime_coverage_path = matches.get_one::<PathBuf>("runtime-coverage").cloned();
162
+ }
163
+ if is_command_line(matches, "min-invocations-hot") {
164
+ if let Some(min_invocations_hot) = matches.get_one::<usize>("min-invocations-hot") {
165
+ options.min_invocations_hot = (*min_invocations_hot).max(1);
166
+ }
167
+ }
168
+ if is_command_line(matches, "min-observation-volume") {
169
+ if let Some(min_observation_volume) = matches.get_one::<usize>("min-observation-volume") {
170
+ options.min_observation_volume = (*min_observation_volume).max(1);
171
+ }
172
+ }
173
+ if is_command_line(matches, "low-traffic-threshold") {
174
+ if let Some(low_traffic_threshold) = matches.get_one::<f64>("low-traffic-threshold") {
175
+ options.low_traffic_threshold = LowTrafficThreshold::from_ratio(*low_traffic_threshold);
176
+ }
177
+ }
178
+ if is_command_line(matches, "file-scores") {
179
+ options.file_scores = matches.get_flag("file-scores").into();
180
+ }
181
+ if is_command_line(matches, "hotspots") {
182
+ options.hotspots = matches.get_flag("hotspots").into();
183
+ }
184
+ if is_command_line(matches, "targets") {
185
+ options.targets = matches.get_flag("targets").into();
186
+ }
187
+ if is_command_line(matches, "ownership") {
188
+ options.ownership = matches.get_flag("ownership").into();
189
+ }
190
+ if is_command_line(matches, "min-score") {
191
+ if let Some(min_score) = matches.get_one::<usize>("min-score") {
192
+ options.min_score = (*min_score).min(100);
193
+ }
194
+ }
195
+ options
196
+ }
197
+
198
+ fn is_command_line(matches: &ArgMatches, id: &str) -> bool {
199
+ matches.value_source(id) == Some(ValueSource::CommandLine)
200
+ }
@@ -0,0 +1,191 @@
1
+ use std::io::Write;
2
+ use std::path::PathBuf;
3
+
4
+ use clap::{Arg, ArgAction, ArgMatches, Command, value_parser};
5
+
6
+ use crate::config::DartDecimateConfig;
7
+ use crate::hooks::{
8
+ HookOptions, HookTarget, hooks_status, install_hooks, render_hooks_report, uninstall_hooks,
9
+ };
10
+
11
+ use super::common_args::{format_arg, root_arg, root_flag_arg, root_path};
12
+ use super::{CliError, OutputFormat, output_format};
13
+
14
+ pub(super) fn hooks_command() -> Command {
15
+ Command::new("hooks")
16
+ .about("Inspect, install, or remove Dart Decimate-managed hooks")
17
+ .subcommand_required(true)
18
+ .arg_required_else_help(true)
19
+ .subcommand(hook_subcommand(
20
+ "status",
21
+ "Inspect Dart Decimate hook status",
22
+ ))
23
+ .subcommand(
24
+ hook_subcommand("install", "Install Dart Decimate-managed hooks").arg(force_arg()),
25
+ )
26
+ .subcommand(
27
+ hook_subcommand("uninstall", "Remove Dart Decimate-managed hooks").arg(force_arg()),
28
+ )
29
+ }
30
+
31
+ pub(super) fn setup_hooks_command() -> Command {
32
+ Command::new("setup-hooks")
33
+ .about("Install or remove Dart Decimate-managed agent hooks")
34
+ .arg(root_arg())
35
+ .arg(root_flag_arg())
36
+ .arg(format_arg())
37
+ .arg(branch_arg())
38
+ .arg(force_arg())
39
+ .arg(
40
+ Arg::new("agent")
41
+ .long("agent")
42
+ .help("Install the repository-local agent hook target")
43
+ .action(ArgAction::SetTrue),
44
+ )
45
+ .arg(
46
+ Arg::new("dry-run")
47
+ .long("dry-run")
48
+ .help("Inspect planned agent hook state without writing files")
49
+ .action(ArgAction::SetTrue),
50
+ )
51
+ .arg(
52
+ Arg::new("uninstall")
53
+ .long("uninstall")
54
+ .help("Remove Dart Decimate-managed agent hooks")
55
+ .action(ArgAction::SetTrue),
56
+ )
57
+ .arg(
58
+ Arg::new("user")
59
+ .long("user")
60
+ .help("User-level agent hooks are not supported by Dart Decimate")
61
+ .action(ArgAction::SetTrue),
62
+ )
63
+ .arg(
64
+ Arg::new("gitignore-claude")
65
+ .long("gitignore-claude")
66
+ .help("Mutating .gitignore from setup-hooks is not supported")
67
+ .action(ArgAction::SetTrue),
68
+ )
69
+ }
70
+
71
+ pub(super) fn run_hooks<W: Write>(subcommand: &ArgMatches, writer: W) -> Result<i32, CliError> {
72
+ match subcommand.subcommand() {
73
+ Some(("status", command)) => {
74
+ run_hook_report(command, writer, |root, options| hooks_status(root, options))
75
+ }
76
+ Some(("install", command)) => run_hook_report(command, writer, |root, options| {
77
+ install_hooks(root, options)
78
+ }),
79
+ Some(("uninstall", command)) => run_hook_report(command, writer, |root, options| {
80
+ uninstall_hooks(root, options)
81
+ }),
82
+ _ => unreachable!("clap requires a hooks subcommand"),
83
+ }
84
+ }
85
+
86
+ pub(super) fn run_setup_hooks<W: Write>(
87
+ subcommand: &ArgMatches,
88
+ writer: W,
89
+ ) -> Result<i32, CliError> {
90
+ if subcommand.get_flag("user") {
91
+ return Err(CliError::UnsupportedSetupHooksUser);
92
+ }
93
+ if subcommand.get_flag("gitignore-claude") {
94
+ return Err(CliError::UnsupportedSetupHooksGitignoreClaude);
95
+ }
96
+ run_hook_report(subcommand, writer, |root, options| {
97
+ let mut options = options.clone();
98
+ options.target = HookTarget::Agent;
99
+ if subcommand.get_flag("dry-run") {
100
+ hooks_status(root, &options)
101
+ } else if subcommand.get_flag("uninstall") {
102
+ uninstall_hooks(root, &options)
103
+ } else {
104
+ install_hooks(root, &options)
105
+ }
106
+ })
107
+ }
108
+
109
+ fn hook_subcommand(name: &'static str, about: &'static str) -> Command {
110
+ Command::new(name)
111
+ .about(about)
112
+ .arg(root_arg())
113
+ .arg(root_flag_arg())
114
+ .arg(format_arg())
115
+ .arg(target_arg())
116
+ .arg(branch_arg())
117
+ }
118
+
119
+ fn run_hook_report<W, F>(
120
+ subcommand: &ArgMatches,
121
+ mut writer: W,
122
+ operation: F,
123
+ ) -> Result<i32, CliError>
124
+ where
125
+ W: Write,
126
+ F: FnOnce(&PathBuf, &HookOptions) -> Result<crate::HooksReport, crate::HooksError>,
127
+ {
128
+ let root = root_path(subcommand);
129
+ let report = operation(&root, &hook_options(subcommand))?;
130
+ match output_format(subcommand, &DartDecimateConfig::default()) {
131
+ OutputFormat::Json => {
132
+ serde_json::to_writer_pretty(&mut writer, &report)?;
133
+ writeln!(writer)?;
134
+ }
135
+ OutputFormat::Human => writer.write_all(render_hooks_report(&report).as_bytes())?,
136
+ }
137
+ Ok(0)
138
+ }
139
+
140
+ fn hook_options(subcommand: &ArgMatches) -> HookOptions {
141
+ HookOptions {
142
+ target: hook_target(subcommand),
143
+ branch: subcommand
144
+ .get_one::<String>("branch")
145
+ .cloned()
146
+ .unwrap_or_else(|| "origin/main".to_owned()),
147
+ force: subcommand
148
+ .try_get_one::<bool>("force")
149
+ .ok()
150
+ .flatten()
151
+ .copied()
152
+ .unwrap_or(false),
153
+ }
154
+ }
155
+
156
+ fn hook_target(subcommand: &ArgMatches) -> HookTarget {
157
+ match subcommand
158
+ .try_get_one::<String>("target")
159
+ .ok()
160
+ .flatten()
161
+ .map_or("git", String::as_str)
162
+ {
163
+ "agent" => HookTarget::Agent,
164
+ _ => HookTarget::Git,
165
+ }
166
+ }
167
+
168
+ fn target_arg() -> Arg {
169
+ Arg::new("target")
170
+ .long("target")
171
+ .value_name("TARGET")
172
+ .help("Hook target")
173
+ .default_value("git")
174
+ .value_parser(["git", "agent"])
175
+ }
176
+
177
+ fn branch_arg() -> Arg {
178
+ Arg::new("branch")
179
+ .long("branch")
180
+ .value_name("REF")
181
+ .help("Git base ref used by installed hooks")
182
+ .default_value("origin/main")
183
+ .value_parser(value_parser!(String))
184
+ }
185
+
186
+ fn force_arg() -> Arg {
187
+ Arg::new("force")
188
+ .long("force")
189
+ .help("Overwrite or remove unmanaged hook files")
190
+ .action(ArgAction::SetTrue)
191
+ }