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
use std::collections::BTreeMap;
use std::path::Path;
use crate::config::Replace;
use crate::error::CargoResult;
pub static NOW: once_cell::sync::Lazy<String> = once_cell::sync::Lazy::new(|| {
time::OffsetDateTime::now_utc()
.format(time::macros::format_description!("[year]-[month]-[day]"))
.unwrap()
});
#[derive(Clone, Default, Debug)]
pub struct Template<'a> {
pub prev_version: Option<&'a str>,
pub prev_metadata: Option<&'a str>,
pub version: Option<&'a str>,
pub metadata: Option<&'a str>,
pub crate_name: Option<&'a str>,
pub date: Option<&'a str>,
pub prefix: Option<&'a str>,
pub tag_name: Option<&'a str>,
}
impl<'a> Template<'a> {
pub fn render(&self, input: &str) -> String {
let mut s = input.to_string();
const PREV_VERSION: &str = "{{prev_version}}";
s = render_var(s, PREV_VERSION, self.prev_version);
const PREV_METADATA: &str = "{{prev_metadata}}";
s = render_var(s, PREV_METADATA, self.prev_metadata);
const VERSION: &str = "{{version}}";
s = render_var(s, VERSION, self.version);
const METADATA: &str = "{{metadata}}";
s = render_var(s, METADATA, self.metadata);
const CRATE_NAME: &str = "{{crate_name}}";
s = render_var(s, CRATE_NAME, self.crate_name);
const DATE: &str = "{{date}}";
s = render_var(s, DATE, self.date);
const PREFIX: &str = "{{prefix}}";
s = render_var(s, PREFIX, self.prefix);
const TAG_NAME: &str = "{{tag_name}}";
s = render_var(s, TAG_NAME, self.tag_name);
s
}
}
fn render_var(mut template: String, var_name: &str, var_value: Option<&str>) -> String {
if let Some(var_value) = var_value {
template = template.replace(var_name, var_value);
} else if template.contains(var_name) {
log::debug!("Unrendered {} present in template {:?}", var_name, template);
}
template
}
pub fn do_file_replacements(
replace_config: &[Replace],
template: &Template<'_>,
cwd: &Path,
prerelease: bool,
noisy: bool,
dry_run: bool,
) -> CargoResult<bool> {
let mut by_file = BTreeMap::new();
for replace in replace_config {
let file = replace.file.clone();
by_file.entry(file).or_insert_with(Vec::new).push(replace);
}
for (path, replaces) in by_file.into_iter() {
let file = cwd.join(&path);
log::debug!("processing replacements for file {}", file.display());
if !file.exists() {
anyhow::bail!("unable to find file {} to perform replace", file.display());
}
let data = std::fs::read_to_string(&file)?;
let mut replaced = data.clone();
for replace in replaces {
if prerelease && !replace.prerelease {
log::debug!("pre-release, not replacing {}", replace.search);
continue;
}
let pattern = replace.search.as_str();
let r = regex::RegexBuilder::new(pattern).multi_line(true).build()?;
let min = replace.min.or(replace.exactly).unwrap_or(1);
let max = replace.max.or(replace.exactly).unwrap_or(std::usize::MAX);
let actual = r.find_iter(&replaced).count();
if actual < min {
anyhow::bail!(
"for `{}` in '{}', at least {} replacements expected, found {}",
pattern,
path.display(),
min,
actual
);
} else if max < actual {
anyhow::bail!(
"for `{}` in '{}', at most {} replacements expected, found {}",
pattern,
path.display(),
max,
actual
);
}
let to_replace = replace.replace.as_str();
let replacer = template.render(to_replace);
replaced = r.replace_all(&replaced, replacer.as_str()).into_owned();
}
if data != replaced {
if dry_run {
let display_path = path.display().to_string();
let data_lines: Vec<_> = data.lines().map(|s| format!("{}\n", s)).collect();
let replaced_lines: Vec<_> = replaced.lines().map(|s| format!("{}\n", s)).collect();
let diff = difflib::unified_diff(
&data_lines,
&replaced_lines,
display_path.as_str(),
display_path.as_str(),
"original",
"replaced",
0,
);
if noisy {
let _ = crate::ops::shell::status(
"Replacing",
format!(
"in {}\n{}",
path.display(),
itertools::join(diff.into_iter(), "")
),
);
} else {
let _ =
crate::ops::shell::status("Replacing", format!("in {}", path.display()));
}
} else {
std::fs::write(&file, replaced)?;
}
} else {
log::trace!("{} is unchanged", file.display());
}
}
Ok(true)
}