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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
// Copyright 2021 Peter Williams <peter@newton.cx> and collaborators
// Licensed under the MIT License.
//! Visual Studio C# projects.
//!
//! We currently "manually" update `Properties/AssemblyInfo.cs`.
use anyhow::bail;
use log::warn;
use quick_xml::{events::Event, Reader};
use std::{
collections::HashMap,
fs::File,
io::{BufRead, BufReader, Write},
};
use crate::{
a_ok_or,
app::{AppBuilder, AppSession},
atry,
errors::Result,
project::{DepRequirement, DependencyTarget, ProjectId},
repository::{ChangeList, RepoPath, RepoPathBuf, Repository},
rewriters::Rewriter,
version::Version,
write_crlf,
};
/// Framework for auto-loading Visual Studio C# projects from the repository
/// contents.
#[derive(Debug, Default)]
pub struct CsProjLoader {
dirs_of_interest: HashMap<RepoPathBuf, DirData>,
vdproj_files: Vec<RepoPathBuf>,
}
#[derive(Debug, Default)]
struct DirData {
csproj: Option<RepoPathBuf>,
assembly_info: Option<RepoPathBuf>,
}
impl CsProjLoader {
pub fn process_index_item(
&mut self,
_repo: &Repository,
repopath: &RepoPath,
dirname: &RepoPath,
basename: &RepoPath,
) -> Result<()> {
if basename.ends_with(b".csproj") {
let dir = dirname.to_owned();
let mut e = self.dirs_of_interest.entry(dir).or_default();
e.csproj = Some(repopath.to_owned());
} else if basename.as_ref() == b"AssemblyInfo.cs" {
// Hardcode the assumption that we should walk up one directory:
let (dir, _base) = dirname.pop_sep().split_basename();
let dir = dir.to_owned();
let mut e = self.dirs_of_interest.entry(dir).or_default();
e.assembly_info = Some(repopath.to_owned());
} else if basename.ends_with(b".vdproj") {
self.vdproj_files.push(repopath.to_owned());
}
Ok(())
}
/// Finalize autoloading any CsProj projects. Consumes this object.
pub fn finalize(mut self, app: &mut AppBuilder) -> Result<()> {
// Scan any vdproj files that might be associated with projects.
let mut guid_to_vdproj: HashMap<String, Vec<RepoPathBuf>> = HashMap::new();
for vdproj in self.vdproj_files.drain(..) {
let p = app.repo.resolve_workdir(&vdproj);
let f = atry!(
File::open(&p);
["failed to open file `{}`", p.display()]
);
let reader = BufReader::new(f);
let mut guid = None;
let mut ignore = false;
for line in reader.lines() {
let line = atry!(
line;
["error reading data from file `{}`", p.display()]
);
if line.contains("OutputProjectGuid") {
// lines look like: ` "OutputProjectGuid" = "8:{733C84E7-58F2-4E09-AC82-58AFD7E7BDD3}"`
// Our GUIDs include the curly braces and are always lowercased since the casing isn't
// always consistent in different files.
let mut this_guid = extract_braced_text(&line)?.to_owned();
this_guid.make_ascii_lowercase();
if let Some(prev_guid) = guid.as_ref() {
if &this_guid != prev_guid {
warn!(
"ignoring setup project `{}` that seems to reference multiple key project GUIDs",
p.display()
);
ignore = true;
}
} else {
guid = Some(this_guid);
}
}
}
if ignore {
continue;
}
// If no GUID found, silently ignore.
if let Some(guid) = guid {
guid_to_vdproj.entry(guid).or_default().push(vdproj);
}
}
// Build up the table of projects and their deps.
struct Info {
ident: ProjectId,
name: String,
/// (guid, literal-req, resolved-req)
deps: Vec<(String, String, DepRequirement)>,
}
let mut guid_to_info = HashMap::new();
let mut gave_dep_warning_help = false;
for (repodir, data) in &self.dirs_of_interest {
// Basic checking that we got both a csproj and an assemblyinfo.
let csproj = match data.csproj {
Some(ref d) => d,
None => {
// This warning could get super annoying, but in my first use case
// it shouldn't happen.
warn!(
"ignoring directory `{}` that has an AssemblyInfo.cs but no .csproj file",
repodir.escaped()
);
continue;
}
};
let assembly_info = match data.assembly_info {
Some(ref d) => d,
None => {
warn!(
"ignoring directory `{}` that has a .csproj file but no Properties/AssemblyInfo.cs",
repodir.escaped()
);
continue;
}
};
// Parse the .csproj XML
let p = app.repo.resolve_workdir(csproj);
let mut xml = atry!(
Reader::from_file(&p);
["unable to open `{}` for reading", p.display()]
);
let mut buf = Vec::new();
let mut guid = None;
let mut name = None;
let mut dep_guids = Vec::new();
let mut dep_reqs = HashMap::new();
let mut state = State::Scanning;
enum State {
Scanning,
GuidText,
NameText,
DepGuidText,
DepReqText,
}
loop {
match xml.read_event(&mut buf) {
Ok(Event::Start(ref e)) => {
state = match e.name() {
b"ProjectGuid" => State::GuidText,
b"AssemblyName" => State::NameText,
b"Project" => {
// Make sure to ignore the toplevel <Project> element!
if guid.is_some() {
State::DepGuidText
} else {
State::Scanning
}
}
b"CrankoInternalDepVersion" => State::DepReqText,
_ => State::Scanning,
};
}
Ok(Event::Text(ref t)) => match state {
State::GuidText => {
let mut g = atry!(
t.unescape_and_decode_without_bom(&xml);
["unable to decode XML text in ProjectGuid of `{}`", p.display()]
);
g.make_ascii_lowercase();
guid = Some(g);
state = State::Scanning;
}
State::NameText => {
name = Some(atry!(
t.unescape_and_decode_without_bom(&xml);
["unable to decode XML text in AssemblyName of `{}`", p.display()]
));
state = State::Scanning;
}
State::DepGuidText => {
let mut g = atry!(
t.unescape_and_decode_without_bom(&xml);
["unable to decode XML text in <Project> of `{}`", p.display()]
);
g.make_ascii_lowercase();
dep_guids.push(g);
state = State::Scanning;
}
State::DepReqText => {
let r = atry!(
t.unescape_and_decode_without_bom(&xml);
["unable to decode XML text in CrankoInternalDepVersion of `{}`", p.display()]
);
let (guid, reqtext) = a_ok_or!(
r.split_once('=');
["malformatted CrankoInternalDepVersion `{}` in `{}`", r, p.display()]
);
let mut guid = guid.to_owned();
guid.make_ascii_lowercase();
dep_reqs.insert(guid, reqtext.to_owned());
state = State::Scanning;
}
State::Scanning => {}
},
Ok(Event::End(_)) => state = State::Scanning,
Ok(Event::Eof) => break,
Err(e) => {
return Err(anyhow::Error::new(e)
.context(format!("error parsing `{}` as XML", p.display())))
}
_ => {}
}
}
let guid = match guid {
Some(g) => g,
None => {
warn!(
"ignoring .csproj file `{}`: cannot find its ProjectGuid",
p.display()
);
continue;
}
};
let name = match name {
Some(n) => n,
None => {
warn!(
"ignoring .csproj file `{}`: cannot find its ProjectGuid",
p.display()
);
continue;
}
};
// Process the internal dep requirements.
let mut resolved_reqs = Vec::new();
for guid in dep_guids.drain(..) {
let (req, text) = if let Some(text) = dep_reqs.get(&guid) {
match app
.repo
.parse_history_ref(text)
.and_then(|cref| app.repo.resolve_history_ref(&cref, csproj))
{
Ok(r) => (r, text.clone()),
Err(e) => {
warn!(
"invalid <CrankoInternalDepVersion> entry `{}` in `{}`: {}",
text,
p.display(),
e
);
(DepRequirement::Unavailable, text.clone())
}
}
} else {
(DepRequirement::Unavailable, "UNDEFINED".to_owned())
};
if req == DepRequirement::Unavailable {
warn!("cannot find version requirement for dependency of `{}` on the project with GUID `{}`", &name, &guid);
if !gave_dep_warning_help {
warn!("... you likely need to add <CrankoInternalDepVersion>{{guid}}={{req}}</CrankoInternalDepVersion> to `{}`", p.display());
warn!("... Cranko needs this information to know the oldest compatible version of the dependency");
gave_dep_warning_help = true;
}
}
resolved_reqs.push((guid, text, req));
}
// Now parse the assembly info ...
let mut version = None;
let p = app.repo.resolve_workdir(assembly_info);
{
let f = atry!(
File::open(&p);
["failed to open file `{}`", p.display()]
);
let reader = BufReader::new(f);
for line in reader.lines() {
let line = atry!(
line;
["error reading data from file `{}`", p.display()]
);
if line.starts_with("[assembly: AssemblyVersion") {
let l1 = a_ok_or!(
line.find('"');
["error parsing AssemblyVersion line in file `{}`", p.display()]
);
let l2 = a_ok_or!(
line.rfind('"');
["error parsing AssemblyVersion line in file `{}`", p.display()]
);
let v = atry!(
line[l1 + 1..l2].parse();
["error parsing AssemblyVersion line in file `{}`", p.display()]
);
version = Some(Version::DotNet(v));
}
}
}
let version = match version {
Some(v) => v,
None => {
warn!(
"ignoring project in `{}`: cannot find its version in `{}`",
repodir.escaped(),
p.display()
);
continue;
}
};
// Finally we can register this project.
let ident = app.graph.add_project();
let mut proj = app.graph.lookup_mut(ident);
proj.qnames = vec![name.to_owned(), "csproj".to_owned()];
proj.prefix = Some(repodir.to_owned());
proj.version = Some(version);
// Auto-register a rewriter to update this package's `AssemblyInfo.cs`.
let rewrite = AssemblyInfoCsRewriter::new(ident, assembly_info.to_owned());
proj.rewriters.push(Box::new(rewrite));
// Any vdproj rewriters?
if let Some(mut vdprojs) = guid_to_vdproj.remove(&guid) {
for vdproj in vdprojs.drain(..) {
let rewrite = VdprojRewriter::new(ident, vdproj);
proj.rewriters.push(Box::new(rewrite));
}
}
// Save the info for dep-linking.
guid_to_info.insert(
guid,
Info {
ident,
name: name.to_owned(),
deps: resolved_reqs,
},
);
}
// Now that we've registered them all, we can populate the interdependencies.
for info in guid_to_info.values() {
for (guid, literal, req) in &info.deps {
let dep_ident = match guid_to_info.get(guid) {
Some(dep_info) => dep_info.ident,
None => bail!(
"C# project `{}` depends on a project with GUID {} but I cannot locate it",
info.name,
guid
),
};
app.graph.add_dependency(
info.ident,
DependencyTarget::Ident(dep_ident),
literal.to_owned(),
req.clone(),
);
}
}
Ok(())
}
}
/// Rewrite `AssemblyInfo.cs` to include real version numbers.
#[derive(Debug)]
pub struct AssemblyInfoCsRewriter {
proj_id: ProjectId,
cs_path: RepoPathBuf,
}
impl AssemblyInfoCsRewriter {
/// Create a new `AssemblyInfo.cs` rewriter.
pub fn new(proj_id: ProjectId, cs_path: RepoPathBuf) -> Self {
AssemblyInfoCsRewriter { proj_id, cs_path }
}
}
impl Rewriter for AssemblyInfoCsRewriter {
fn rewrite(&self, app: &AppSession, changes: &mut ChangeList) -> Result<()> {
let mut did_anything = false;
let file_path = app.repo.resolve_workdir(&self.cs_path);
let cur_f = atry!(
File::open(&file_path);
["failed to open file `{}` for reading", file_path.display()]
);
let cur_reader = BufReader::new(cur_f);
let new_af = atomicwrites::AtomicFile::new(
&file_path,
atomicwrites::OverwriteBehavior::AllowOverwrite,
);
let proj = app.graph().lookup(self.proj_id);
let r = new_af.write(|new_f| {
for line in cur_reader.lines() {
let line = atry!(
line;
["error reading data from file `{}`", file_path.display()]
);
let line = if line.starts_with("[assembly: AssemblyVersion") || line.starts_with("[assembly: AssemblyFileVersion") {
did_anything = true;
atry!(
crate::pypa::simple_py_parse::replace_text_in_string_literal(&line, &proj.version.to_string());
["couldn't rewrite version-string source line `{}`", line]
)
} else {
line
};
atry!(
write_crlf!(new_f, "{}", line);
["error writing data to `{}`", new_af.path().display()]
);
}
Ok(())
});
match r {
Err(atomicwrites::Error::Internal(e)) => Err(e.into()),
Err(atomicwrites::Error::User(e)) => Err(e),
Ok(()) => {
if !did_anything {
warn!(
"rewriter for C# assembly info file `{}` didn't make any modifications",
file_path.display()
);
}
changes.add_path(&self.cs_path);
Ok(())
}
}
}
}
/// Rewrite a vdproj (setup installer) to include real version numbers.
#[derive(Debug)]
pub struct VdprojRewriter {
proj_id: ProjectId,
vdproj_path: RepoPathBuf,
}
impl VdprojRewriter {
/// Create a new `AssemblyInfo.cs` rewriter.
pub fn new(proj_id: ProjectId, vdproj_path: RepoPathBuf) -> Self {
VdprojRewriter {
proj_id,
vdproj_path,
}
}
}
impl Rewriter for VdprojRewriter {
fn rewrite(&self, app: &AppSession, changes: &mut ChangeList) -> Result<()> {
let mut did_anything = false;
let file_path = app.repo.resolve_workdir(&self.vdproj_path);
let cur_f = atry!(
File::open(&file_path);
["failed to open file `{}` for reading", file_path.display()]
);
let cur_reader = BufReader::new(cur_f);
let new_af = atomicwrites::AtomicFile::new(
&file_path,
atomicwrites::OverwriteBehavior::AllowOverwrite,
);
let proj = app.graph().lookup(self.proj_id);
let r = new_af.write(|new_f| {
for line in cur_reader.lines() {
let line = atry!(
line;
["error reading data from file `{}`", file_path.display()]
);
let line = if line.contains("\"ProductVersion\" =") {
// ProductVersion must have the form `X.Y.Z`; the "revision"
// component must be stripped.
let prod_vers = proj.version.to_string();
let pieces: Vec<_> = prod_vers.split('.').collect();
let prod_vers = &pieces[..3].join(".");
did_anything = true;
atry!(
replace_vdproj_text(&line, prod_vers);
["couldn't rewrite version-string source line `{}`", line]
)
} else {
line
};
atry!(
write_crlf!(new_f, "{}", line);
["error writing data to `{}`", new_af.path().display()]
);
}
Ok(())
});
match r {
Err(atomicwrites::Error::Internal(e)) => Err(e.into()),
Err(atomicwrites::Error::User(e)) => Err(e),
Ok(()) => {
if !did_anything {
warn!(
"rewriter for vdproj file `{}` didn't make any modifications",
file_path.display()
);
}
changes.add_path(&self.vdproj_path);
Ok(())
}
}
}
}
pub fn extract_braced_text(line: &str) -> Result<&str> {
let lc_loc = line.find('{');
let rc_loc = line.rfind('}');
match (lc_loc, rc_loc) {
(Some(lc_idx), Some(rc_idx)) => {
if lc_idx < rc_idx {
Ok(&line[lc_idx..=rc_idx])
} else {
bail!(
"expected braced text in line `{}`, but the braces were confusing",
line
);
}
}
_ => {
bail!(
"expected braced text in line `{}`, but didn't find a matched pair",
line
);
}
}
}
/// This function works on vdproj ProductVersion lines that look like:
///
/// ```
/// "ProductVersion" = "8:6.0.13"
/// ```
///
/// Our goal is to replace the `6.0.13` in this example.
pub fn replace_vdproj_text(line: &str, new_val: &str) -> Result<String> {
let left_loc = line.rfind(':');
let right_loc = line.rfind('"');
let (left_idx, right_idx) = match (left_loc, right_loc) {
(Some(li), Some(ri)) => (li, ri),
_ => {
bail!(
"expected a vdproj string in line `{}`, but I couldn't understand the syntax",
line
);
}
};
let mut replaced = line[..=left_idx].to_owned();
replaced.push_str(new_val);
replaced.push_str(&line[right_idx..]);
Ok(replaced)
}