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
// This is free and unencumbered software released into the public domain.
#![allow(unused_imports)]
use clientele::{
StandardOptions,
SysexitsError::{self, *},
crates::camino::Utf8PathBuf,
crates::clap::{Parser, Subcommand},
};
use readmer::{
Context, DirContext, Engine, RenderError, Workspace,
model::{LoadError, Package, Project},
};
use std::{default, path::PathBuf};
use thiserror::Error;
use tracing::error;
/// Readmer composes README.md files from Jinja2 or Liquid templates.
#[derive(Debug, Parser)]
#[command(name = "Readmer", long_about)]
#[command(arg_required_else_help = true)]
struct Options {
#[clap(flatten)]
flags: StandardOptions,
#[command(subcommand)]
command: Option<Command>,
}
#[derive(Debug, Subcommand)]
enum Command {
/// TODO
#[cfg(feature = "unstable")]
#[clap(aliases = ["i", "in", "ini", "install"])]
Init {},
/// Build ./README.md from templates in $WORKSPACE/.config/readmer/.
#[cfg(feature = "unstable")]
#[clap(aliases = ["b", "bu", "bui", "buidl"])]
Build {
/// The output files to build [default: ./README.md].
outputs: Vec<Utf8PathBuf>,
},
/// Describe the current project's metadata in JSON format.
#[clap(aliases = ["d", "de", "des", "desc"])]
Describe {
/// The project directory to use [default: $PWD].
project: Option<Utf8PathBuf>,
/// The project property to output [default: all properties].
property: Option<String>,
/// The workspace directory to use [default: $WORKSPACE].
#[clap(short = 'W', long)]
workspace: Option<Workspace>,
/// The output format to use.
#[clap(short, long, default_value = "json")]
output: String,
/// Define a variable and value to pass to the templating engine.
#[clap(short = 'D', long = "define")]
defines: Vec<String>,
},
/// Render a template file to standard output.
#[clap(aliases = ["r", "re", "ren"])]
Render {
/// The template files to render [default: $WORKSPACE/.config/readmer/.../README.md.liquid].
inputs: Vec<Utf8PathBuf>,
/// The workspace directory to use [default: $WORKSPACE].
#[clap(short = 'W', long)]
workspace: Option<Workspace>,
/// The templating engine to use [default: auto].
#[clap(short, long)]
engine: Option<String>,
/// Define a variable and value to pass to the templating engine.
#[clap(short = 'D', long = "define")]
defines: Vec<String>,
},
}
impl Default for Command {
fn default() -> Self {
Self::Render {
inputs: Vec::new(),
workspace: None,
engine: None,
defines: Vec::new(),
}
}
}
#[derive(Debug, Error)]
pub enum ProgramError {
#[error("unknown --engine name: {0}")]
UnknownEngineName(String),
#[error("unknown --output format: {0}")]
UnknownOutputFormat(String),
#[error("invalid --define format: {0}")]
InvalidDefineFormat(String),
#[error(transparent)]
RenderError(#[from] RenderError),
#[error(transparent)]
LoadError(#[from] LoadError),
#[error(transparent)]
Io(#[from] std::io::Error),
#[error(transparent)]
Exit(#[from] SysexitsError),
#[error(transparent)]
Other(#[from] Box<dyn core::error::Error>),
}
impl From<ProgramError> for SysexitsError {
fn from(error: ProgramError) -> Self {
use ProgramError::*;
match error {
Exit(code) => code,
_ => EX_SOFTWARE,
}
}
}
pub fn main() -> SysexitsError {
use ProgramError::*;
match run() {
Ok(()) => EX_OK,
Err(Exit(exit)) => exit,
Err(error) => {
// TODO: color coding
error!("{}: error: {}", env!("CARGO_PKG_NAME"), error);
error.into()
},
}
}
pub fn run() -> Result<(), ProgramError> {
use ProgramError::*;
// Load environment variables from `.env`:
clientele::dotenv().ok();
// Expand wildcards and @argfiles:
let args = clientele::args_os()?;
// Parse command-line options:
let options = Options::parse_from(args);
// Print the program version, if requested:
if options.flags.version {
println!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
return Ok(());
}
// Print the program license, if requested:
if options.flags.license {
print!("{}", include_str!("../UNLICENSE"));
return Ok(());
}
// Configure debug output:
if options.flags.debug {
tracing_subscriber::fmt().init();
} else {
tracing_subscriber::fmt()
.with_writer(std::io::stderr)
.compact()
.without_time()
.with_target(false)
.with_level(false)
.with_thread_ids(false)
.with_thread_names(false)
.with_file(false)
.with_line_number(false)
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) // respects RUST_LOG
.init();
}
let mut result = Ok(());
match options.command.unwrap_or_default() {
#[cfg(feature = "unstable")]
Command::Init {} => {
// TODO: implement `readmer init`
},
#[cfg(feature = "unstable")]
Command::Build { outputs } => {
let _outputs = if outputs.is_empty() {
vec!["README.md".into()]
} else {
outputs
};
// TODO: implement `readmer build`
},
Command::Describe {
project: _, // FIXME: reimplement `readmer describe --project`
property,
workspace,
output,
defines,
} => {
let workspace = workspace.map(Ok).unwrap_or_else(|| Workspace::locate())?;
let mut context = DirContext { workspace }.load()?;
for define in defines {
let (k, v) = define
.split_once('=')
.ok_or_else(|| InvalidDefineFormat(define.clone()))?;
context.define(k, v);
}
match output.as_str() {
"json" => {
let mut json = context.into_json();
if let Some(property) = property {
json = json.get(property).cloned().unwrap_or_default();
}
println!(
"{}",
serde_json::to_string_pretty(&json).map_err(|e| Other(e.into()))?
);
},
_ => {
return Err(UnknownOutputFormat(output));
},
}
},
Command::Render {
mut inputs,
workspace,
engine,
defines,
} => {
let workspace = workspace.map(Ok).unwrap_or_else(|| Workspace::locate())?;
let mut context = DirContext {
workspace: workspace.clone(),
}
.load()?;
for define in defines {
let (k, v) = define
.split_once('=')
.ok_or_else(|| InvalidDefineFormat(define.clone()))?;
context.define(k, v);
}
if inputs.is_empty() {
// TODO: find an actual existing template, if any
inputs.push("README.md.liquid".into());
}
let inputs: Vec<(String, Utf8PathBuf)> = inputs
.into_iter()
.map(|input_path| {
if input_path.has_root()
|| input_path.starts_with(".")
|| input_path.starts_with("..")
|| input_path.starts_with(".config")
{
// Qualified paths are used as-is w/o further resolution,
// but we do try to derive a sensible template name:
let input_name = input_path.to_string();
let input_name = input_name
.split(".config/readmer/")
.last()
.map(ToString::to_string)
.unwrap_or_else(|| input_name);
(input_name, input_path)
} else {
// Unqualified paths are interpreted relative to the
// workspace's prefixed configuration directory
// (`$WORKSPACE/.config/readmer/$PREFIX/`), where the
// prefix is the relative path to the workspace root:
let input_name = workspace.as_ref().down.join(input_path);
let input_path = workspace.config_path().join(&input_name);
(input_name.into_string(), input_path)
}
})
.collect();
for (template_name, template_path) in inputs {
let engine_name = engine
.as_deref()
.or_else(|| template_path.extension())
.unwrap_or("liquid");
let mut engine: Box<dyn Engine> = match engine_name {
"liquid" => Box::new(readmer::LiquidEngine::new()),
"minijinja" | "jinja" | "jinja2" | "j2" => {
Box::new(readmer::MinijinjaEngine::new())
},
_ => return Err(UnknownEngineName(engine_name.into())),
};
match engine.load_template(template_name.clone(), template_path.clone()) {
Ok(_) => {},
Err(error) => {
error!(
"{}: failed to load template `{}`: {} for `{}`",
env!("CARGO_PKG_NAME"),
&template_name,
error,
&template_path
);
result = Err(Exit(EX_DATAERR));
continue;
},
};
let output = engine.render(template_name, Box::new(context.clone()))?;
print!("{}", output);
}
},
};
result
}