pavex_cli_client/client.rs
1use std::{path::PathBuf, process::Command};
2
3use pavex::Blueprint;
4
5use crate::commands::generate::GenerateBuilder;
6use crate::commands::new::NewBuilder;
7use crate::config::Color;
8
9/// A fluent API for configuring and executing `pavex`'s CLI commands.
10#[derive(Clone, Debug)]
11pub struct Client {
12 pavex_cli_path: Option<PathBuf>,
13 color: Color,
14 debug: bool,
15 log: bool,
16 log_filter: Option<String>,
17 perf_profile: bool,
18}
19
20impl Default for Client {
21 fn default() -> Self {
22 Self {
23 pavex_cli_path: None,
24 color: Color::Auto,
25 debug: false,
26 log: false,
27 log_filter: None,
28 perf_profile: false,
29 }
30 }
31}
32
33impl Client {
34 /// Create a new `Client` with the default configuration.
35 pub fn new() -> Self {
36 Self::default()
37 }
38
39 /// Convert this `Client` into a `std::process::Command` that will run `pavex`
40 /// with the chosen configuration.
41 fn command(self) -> Command {
42 let pavex_path = self.pavex_cli_path.unwrap_or_else(|| "pavex".into());
43 let mut cmd = Command::new(pavex_path);
44
45 match self.color {
46 Color::Auto => {}
47 Color::Always => {
48 cmd.arg("--color").arg("always");
49 }
50 Color::Never => {
51 cmd.arg("--color").arg("never");
52 }
53 }
54
55 if self.debug {
56 cmd.arg("--debug");
57 }
58
59 if self.log {
60 cmd.arg("--log");
61 }
62
63 if let Some(filter) = self.log_filter {
64 cmd.arg("--log-filter").arg(filter);
65 }
66
67 if self.perf_profile {
68 cmd.arg("--perf-profile");
69 }
70
71 cmd
72 }
73
74 /// Start building the configuration for the code-generator.
75 ///
76 /// You must specify:
77 ///
78 /// - The `Blueprint` for the application that you want to generate;
79 /// - The directory where the generated code should be written.
80 pub fn generate(self, blueprint: Blueprint, output_directory: PathBuf) -> GenerateBuilder {
81 let cmd = self.command();
82 GenerateBuilder::new(cmd, blueprint, output_directory)
83 }
84}
85
86/// Setters for optional configuration knobs on `Client`.
87impl Client {
88 /// Set the path to the `pavex` executable.
89 ///
90 /// If this is not set, we will assume that `pavex` is in the `PATH`.
91 pub fn pavex_cli_path(mut self, path: PathBuf) -> Self {
92 self.pavex_cli_path = Some(path);
93 self
94 }
95
96 /// Set whether to use colors in the output of Pavex's code generator.
97 ///
98 /// If this is not set, Pavex will automatically determine whether to use colors or not.
99 pub fn color(mut self, color: Color) -> Self {
100 self.color = color;
101 self
102 }
103
104 /// Enable debug mode.
105 ///
106 /// This will print additional debug information when running `pavex` commands.
107 pub fn debug(mut self) -> Self {
108 self.debug = true;
109 self
110 }
111
112 /// Disable debug mode.
113 ///
114 /// `pavex` will not print additional debug information when running commands.
115 /// This is the default behaviour.
116 pub fn no_debug(mut self) -> Self {
117 self.debug = false;
118 self
119 }
120
121 /// Start building the configuration for the `new` command.
122 ///
123 /// You must specify the path where the new project should be created.
124 pub fn new_command(self, path: PathBuf) -> NewBuilder {
125 let cmd = self.command();
126 NewBuilder::new(cmd, path)
127 }
128
129 /// Enable logging.
130 ///
131 /// `pavex` will emit internal log messages to the console.
132 pub fn log(mut self) -> Self {
133 self.log = true;
134 self
135 }
136
137 /// Disable logging.
138 ///
139 /// `pavex` will not emit internal log messages to the console.
140 /// This is the default behaviour.
141 pub fn no_log(mut self) -> Self {
142 self.log = false;
143 self
144 }
145
146 /// Set the log filter.
147 ///
148 /// Control which logs are emitted if `--log` or `--perf-profile` are enabled.
149 /// If no filter is specified, Pavex will default to `info,pavex=trace`.
150 pub fn log_filter(mut self, filter: String) -> Self {
151 self.log_filter = Some(filter);
152 self
153 }
154
155 /// Enable performance profiling.
156 ///
157 /// `pavex` will serialize to disk tracing information to profile command execution.
158 pub fn perf_profile(mut self) -> Self {
159 self.perf_profile = true;
160 self
161 }
162
163 /// Disable performance profiling.
164 ///
165 /// `pavex` will not serialize to disk tracing information to profile command execution.
166 /// This is the default behaviour.
167 pub fn no_perf_profile(mut self) -> Self {
168 self.perf_profile = false;
169 self
170 }
171}