1pub fn dh_invoke_add_with(line: &str, with_argument: &str) -> String {
5 if line.contains(with_argument) {
6 return line.to_owned();
7 }
8 if !line.contains(" --with") {
9 return format!("{} --with={}", line, with_argument);
10 }
11
12 lazy_regex::regex_replace!(
13 r"([ \t])--with([ =])([^ \t]+)",
14 line,
15 |_, head, _with, tail| format!("{}--with={},{}", head, with_argument, tail)
16 )
17 .to_string()
18}
19
20pub fn dh_invoke_get_with(line: &str) -> Vec<String> {
22 let mut ret = Vec::new();
23 for cap in lazy_regex::regex!("[ \t]--with[ =]([^ \t]+)").captures_iter(line) {
24 if let Some(m) = cap.get(1) {
25 ret.extend(m.as_str().split(',').map(|s| s.to_owned()));
26 }
27 }
28 ret
29}
30
31pub fn dh_invoke_drop_with(line: &str, with_argument: &str) -> String {
53 if !line.contains(with_argument) {
54 return line.to_owned();
55 }
56
57 let mut result = line.to_owned();
58 let escaped = regex::escape(with_argument);
59
60 if let Ok(re) = regex::Regex::new(&format!(r"[ \t]--with[ =]{}( .+|)$", escaped)) {
62 result = re.replace(&result, "$1").to_string();
63 }
64
65 if let Ok(re) = regex::Regex::new(&format!(r"([ \t])--with([ =]){},", escaped)) {
67 result = re.replace(&result, "${1}--with${2}").to_string();
68 }
69
70 if let Ok(re) = regex::Regex::new(&format!(r"([ \t])--with([ =])(.+),{}([ ,])", escaped)) {
72 result = re.replace(&result, "${1}--with${2}${3}${4}").to_string();
73 }
74
75 if let Ok(re) = regex::Regex::new(&format!(r"([ \t])--with([ =])(.+),{}$", escaped)) {
77 result = re.replace(&result, "${1}--with${2}${3}").to_string();
78 }
79
80 result
81}
82
83pub fn dh_invoke_drop_argument(line: &str, argument: &str) -> String {
101 if !line.contains(argument) {
102 return line.to_owned();
103 }
104
105 let mut result = line.to_owned();
106 let escaped = regex::escape(argument);
107
108 if let Ok(re) = regex::Regex::new(&format!(r"[ \t]+{}$", escaped)) {
110 result = re.replace(&result, "").to_string();
111 }
112
113 if let Ok(re) = regex::Regex::new(&format!(r"([ \t]){}[ \t]", escaped)) {
115 result = re.replace(&result, "$1").to_string();
116 }
117
118 result
119}
120
121pub fn dh_invoke_replace_argument(line: &str, old: &str, new: &str) -> String {
140 if !line.contains(old) {
141 return line.to_owned();
142 }
143
144 let mut result = line.to_owned();
145 let escaped = regex::escape(old);
146
147 if let Ok(re) = regex::Regex::new(&format!(r"([ \t]){}$", escaped)) {
149 result = re.replace(&result, format!("$1{}", new)).to_string();
150 }
151
152 if let Ok(re) = regex::Regex::new(&format!(r"([ \t]){}([ \t])", escaped)) {
154 result = re.replace(&result, format!("$1{}$2", new)).to_string();
155 }
156
157 result
158}
159
160pub fn check_cdbs(path: &std::path::Path) -> bool {
175 let Ok(contents) = std::fs::read(path) else {
176 return false;
177 };
178
179 for line in contents.split(|&b| b == b'\n') {
180 let trimmed = line.strip_prefix(b"-").unwrap_or(line);
181 if trimmed.starts_with(b"include /usr/share/cdbs/") {
182 return true;
183 }
184 }
185 false
186}
187
188#[cfg(test)]
189mod tests {
190 use super::*;
191
192 #[test]
193 fn test_dh_invoke_add_with() {
194 assert_eq!(dh_invoke_add_with("dh", "blah"), "dh --with=blah");
195 assert_eq!(
196 dh_invoke_add_with("dh --with=foo", "blah"),
197 "dh --with=blah,foo"
198 );
199 assert_eq!(
200 dh_invoke_add_with("dh --with=foo --other", "blah"),
201 "dh --with=blah,foo --other"
202 );
203 }
204
205 #[test]
206 fn test_dh_invoke_get_with() {
207 assert_eq!(dh_invoke_get_with("dh --with=blah --foo"), vec!["blah"]);
208 assert_eq!(dh_invoke_get_with("dh --with=blah"), vec!["blah"]);
209 assert_eq!(
210 dh_invoke_get_with("dh --with=blah,blie"),
211 vec!["blah", "blie"]
212 );
213 }
214
215 #[test]
216 fn test_dh_invoke_drop_with() {
217 assert_eq!(dh_invoke_drop_with("dh --with=blah", "blah"), "dh");
218 assert_eq!(
219 dh_invoke_drop_with("dh --with=blah,foo", "blah"),
220 "dh --with=foo"
221 );
222 assert_eq!(
223 dh_invoke_drop_with("dh --with=blah,foo --other", "blah"),
224 "dh --with=foo --other"
225 );
226 assert_eq!(dh_invoke_drop_with("dh --with=blah", "blah"), "dh");
227 assert_eq!(
228 dh_invoke_drop_with("dh --with=foo,blah", "blah"),
229 "dh --with=foo"
230 );
231 assert_eq!(
232 dh_invoke_drop_with(
233 "dh $@ --verbose --with autoreconf,systemd,cme-upgrade",
234 "systemd"
235 ),
236 "dh $@ --verbose --with autoreconf,cme-upgrade"
237 );
238 assert_eq!(
239 dh_invoke_drop_with(
240 "dh $@ --with gir,python3,sphinxdoc,systemd --without autoreconf --buildsystem=cmake",
241 "systemd"
242 ),
243 "dh $@ --with gir,python3,sphinxdoc --without autoreconf --buildsystem=cmake"
244 );
245 assert_eq!(
246 dh_invoke_drop_with("dh $@ --with systemd", "systemd"),
247 "dh $@"
248 );
249 }
250
251 #[test]
252 fn test_dh_invoke_drop_argument() {
253 assert_eq!(
254 dh_invoke_drop_argument("dh $@ --foo --bar", "--foo"),
255 "dh $@ --bar"
256 );
257 assert_eq!(
258 dh_invoke_drop_argument("dh $@ --foo --bar", "--bar"),
259 "dh $@ --foo"
260 );
261 assert_eq!(dh_invoke_drop_argument("dh $@ --foo", "--foo"), "dh $@");
262 }
263
264 #[test]
265 fn test_dh_invoke_replace_argument() {
266 assert_eq!(
267 dh_invoke_replace_argument("dh $@ --foo", "--foo", "--bar"),
268 "dh $@ --bar"
269 );
270 assert_eq!(
271 dh_invoke_replace_argument("dh $@ --foo --baz", "--foo", "--bar"),
272 "dh $@ --bar --baz"
273 );
274 }
275}