msbuild 0.2.1

Allows builds to run msbuild for visual studio projects
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
//! # The msbuild crate
//! This crates provides the functionality of finding
//! the msbuild binary on the system.
//!
//! # Environment Variables
//! - The `VS_WHERE_PATH` environment variable can be used in order
//!   overwrite the default path where the crate tries to locate
//!   the `vswhere.exe` binary.
//!
//! - The `VS_INSTALLATION_PATH` environment variable can be used in order
//!   to overwrite specify a path to Visual Studio
//!   Note! The path must still lead to a binary the fulfills the version
//!   requirements otherwise the crate will try to probe the system
//!   for a suitable version.
//!
//! - The `WIN_SDK_PATH` environment variable can be used in order to
//!   to overwrite in what location the library will search for
//!   WinSDK installations.
use lenient_semver::Version;
use serde_json::Value;
use std::{
    convert::TryFrom,
    io::{Error, ErrorKind},
    path::{Path, PathBuf},
};

pub mod vs_where;
pub mod win_sdk;

pub use vs_where::VsWhere;

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
pub struct InstallationVersion<'a>(Version<'a>);

impl<'a> InstallationVersion<'a> {
    pub fn parse(value: &'a str) -> std::io::Result<InstallationVersion<'a>> {
        Version::parse(value).map_or_else(
            |e| {
                Err(Error::new(
                    ErrorKind::InvalidData,
                    format!("Failed to parse &str as a InstallationVersion: {}", e),
                ))
            },
            |v| Ok(InstallationVersion(v)),
        )
    }
}

/// Enum holding the product line versions.
pub enum ProductLineVersion {
    Vs2026,
    Vs2022,
    Vs2019,
    Vs2017,
}

impl ProductLineVersion {
    /// The non inclusive max installation version for a
    /// specific product line version.
    pub fn installation_version_max(&self) -> InstallationVersion<'_> {
        // Constant values that are always safe to parse.
        match self {
            Self::Vs2026 => InstallationVersion::parse("19.0.0.0").unwrap(),
            Self::Vs2022 => InstallationVersion::parse("18.0.0.0").unwrap(),
            Self::Vs2019 => InstallationVersion::parse("17.0.0.0").unwrap(),
            Self::Vs2017 => InstallationVersion::parse("16.0.0.0").unwrap(),
        }
    }

    /// The inclusive min installation version for a
    /// specific product line version.
    pub fn installation_version_min(&self) -> InstallationVersion<'_> {
        match self {
            Self::Vs2026 => InstallationVersion::parse("18.0.0.0").unwrap(),
            Self::Vs2022 => InstallationVersion::parse("17.0.0.0").unwrap(),
            Self::Vs2019 => InstallationVersion::parse("16.0.0.0").unwrap(),
            Self::Vs2017 => InstallationVersion::parse("15.0.0.0").unwrap(),
        }
    }
}

impl TryFrom<&str> for ProductLineVersion {
    type Error = Error;

    fn try_from(s: &str) -> std::io::Result<Self> {
        match s {
            "2017" => Ok(ProductLineVersion::Vs2017),
            "2019" => Ok(ProductLineVersion::Vs2019),
            "2022" => Ok(ProductLineVersion::Vs2022),
            "2026" => Ok(ProductLineVersion::Vs2026),
            _ => Err(Error::new(
                ErrorKind::InvalidData,
                format!("Product line version {} did not match any known values.", s),
            )),
        }
    }
}

/// Type for finding and interactive with
/// the msbuild executable.
pub struct MsBuild {
    path: PathBuf,
}

impl MsBuild {
    const ENV_KEY: &'static str = "VS_INSTALLATION_PATH";
    /// Finds the msbuild executable that is associated with provided product line version
    /// if no version is provided then the first installation of msbuild that is found
    /// will be selected.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// let product_line_version: Option<&str> = Some("2017");
    /// let msbuild = msbuild::MsBuild::find_msbuild(product_line_version);
    /// ```
    pub fn find_msbuild(product_line_version: Option<&str>) -> std::io::Result<Self> {
        product_line_version
            .map(ProductLineVersion::try_from)
            .transpose()
            .and_then(|potential_plv| {
                let max = potential_plv
                    .as_ref()
                    .map(|plv| plv.installation_version_max());
                let min = potential_plv
                    .as_ref()
                    .map(|plv| plv.installation_version_min());
                MsBuild::find_msbuild_in_range(max, min)
            })
    }

    /// Finds a msbuild that with the highest installation version that is in a range
    /// between max (exclusive) and min(inclusive).
    ///
    /// # Examples
    ///
    /// ```
    /// // Find the latest supported version for msbuild
    /// use msbuild::{MsBuild, ProductLineVersion};
    ///
    /// let msbuild = MsBuild::find_msbuild_in_range(
    ///     Some(ProductLineVersion::Vs2022.installation_version_max()),
    ///     Some(ProductLineVersion::Vs2017.installation_version_min()),
    /// );
    /// ```
    pub fn find_msbuild_in_range(
        max: Option<InstallationVersion>,
        min: Option<InstallationVersion>,
    ) -> std::io::Result<Self> {
        VsWhere::find_vswhere()
            .and_then(|vswhere| vswhere.run(None))
            .and_then(|output| Self::parse_from_json(&output))
            .and_then(|v: Value| {
                Self::list_instances(&v)
                    .and_then(|instances| Self::find_match(instances, max.as_ref(), min.as_ref()))
            })
            .map(|p| MsBuild {
                path: p.as_path().join("MsBuild/Current/Bin/msbuild.exe"),
            })
    }

    /// Executes msbuild using the provided project_path and
    /// the provided arguments.
    pub fn run(&self, project_path: &Path, args: &[&str]) -> std::io::Result<()> {
        if !self.path.as_path().exists() {
            return Err(Error::new(
                ErrorKind::NotFound,
                format!("Could not find [{}].", self.path.to_string_lossy()),
            ));
        }
        std::process::Command::new(self.path.as_path())
            .current_dir(project_path)
            .args(args)
            .output()
            .and_then(|out| {
                if out.status.success() {
                    Ok(())
                } else {
                    use std::io::Write;
                    std::io::stdout().write_all(&out.stdout)?;
                    let error_message = if let Some(code) = out.status.code() {
                        &format!("Failed to run msbuild: Exit code [{code}]")
                    } else {
                        "Failed to run msbuild"
                    };
                    Err(Error::other(format!(
                        "Failed to run msbuild: {error_message}"
                    )))
                }
            })
    }

    // Internal function for parsing a string as json object.
    fn parse_from_json(value: &str) -> std::io::Result<Value> {
        serde_json::from_str(value).map_err(|e| {
            Error::new(
                ErrorKind::InvalidData,
                format!("Failed to parse command output as json ({})", e),
            )
        })
    }

    // Internal function for listing the instances inthe json value.
    fn list_instances(v: &Value) -> std::io::Result<&Vec<Value>> {
        v.as_array().ok_or_else(|| {
            Error::new(
                ErrorKind::InvalidData,
                "json data did not contain any installation instances.",
            )
        })
    }

    // Internal function for finding the instances that matches the
    // version range and, if specified, the path in the environment
    // variable.
    fn find_match(
        instances_json: &[Value],
        max: Option<&InstallationVersion>,
        min: Option<&InstallationVersion>,
    ) -> std::io::Result<PathBuf> {
        let env_installation_path: Option<PathBuf> = std::env::var(MsBuild::ENV_KEY)
            .ok()
            .map(|v| PathBuf::from(&v));

        // Parse the instance json data and filter result based on version.
        let validated_instances = MsBuild::validate_instances_json(instances_json, max, min);

        if let Some(specified_installation_path) = env_installation_path {
            // Finds the specified installation path among the parsed
            // and validated instances.
            validated_instances
                .iter()
                .filter_map(|(_, p)| {
                    if specified_installation_path.starts_with(p) {
                        Some(p.to_path_buf())
                    } else {
                        None
                    }
                })
                .next()
                .ok_or(Error::new(
                    ErrorKind::NotFound,
                    "No instance found that matched requirements.",
                ))
        } else {
            // Select the latest version.
            validated_instances
                .iter()
                .max_by_key(|(v, _)| v)
                .map(|(_, p)| p.to_path_buf())
                .ok_or(Error::new(
                    ErrorKind::NotFound,
                    "No instance found that matched requirements.",
                ))
        }
    }

    /// Internal function that extracts a collection of parsed
    /// installation instances with a version within the given
    /// interval.
    fn validate_instances_json<'a>(
        instances_json: &'a [Value],
        max: Option<&'a InstallationVersion>,
        min: Option<&'a InstallationVersion>,
    ) -> Vec<(InstallationVersion<'a>, &'a Path)> {
        instances_json
            .iter()
            .filter_map(|i| {
                MsBuild::parse_installation_version(i)
                    .and_then(|installation_version| {
                        if MsBuild::has_version_in_range(
                            &installation_version.0,
                            max.map(|v| &v.0),
                            min.map(|v| &v.0),
                        ) {
                            MsBuild::parse_installation_path(i).map(|installation_path| {
                                Some((installation_version, installation_path))
                            })
                        } else {
                            // Maybe log(trace) that an instance was found that was not in the range.
                            Ok(None)
                        }
                    })
                    .unwrap_or_else(|e| {
                        print!("Encounted an error during parsing of instance data: {}", e);
                        None
                    })
            })
            .collect()
    }

    fn parse_installation_path(json_value: &Value) -> std::io::Result<&Path> {
        json_value
            .get("installationPath")
            .and_then(|path_json_value: &Value| path_json_value.as_str())
            .ok_or(Error::new(
                ErrorKind::InvalidData,
                "Failed to retrieve `installationPath`.",
            ))
            .map(Path::new)
    }

    fn parse_installation_version(json_value: &Value) -> std::io::Result<InstallationVersion<'_>> {
        json_value
            .get("installationVersion")
            .and_then(|version_json_value: &Value| version_json_value.as_str())
            .and_then(|version_str: &str| Version::parse(version_str).ok())
            .map(InstallationVersion)
            .ok_or(Error::new(
                ErrorKind::InvalidData,
                "Failed to retrieve `installationVersion`.",
            ))
    }

    /// Internal function to check if a version is in the range
    /// if it has been specified.
    fn has_version_in_range(
        version: &Version,
        max: Option<&Version>,
        min: Option<&Version>,
    ) -> bool {
        let is_below_max: bool = max.is_none_or(|max_version| max_version > version);
        let is_above_min: bool = min.is_none_or(|min_version| version >= min_version);
        is_below_max && is_above_min
    }
}

// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Unit tests of the private functions and methods
// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_msbuild_has_version_in_range() {
        let max = Some(
            Version::parse("4.3.2.1")
                .expect("It should be possible to create a Version object from the string 4.3.2.1"),
        );
        let min = Some(
            Version::parse("1.2.3.4")
                .expect("It should be possible to create a Version object from the string 1.2.3.4"),
        );
        // Check with no min or max
        assert!(
            MsBuild::has_version_in_range(
                &Version::parse("0.0.0.0").expect(
                    "It should be possible to create a Version object from the string 0.0.0.0"
                ),
                None,
                None
            ),
            "The version 0.0.0.0 should be in range when no min or max values have been specified."
        );
        // Check outside of range with min value.
        assert!(
            !MsBuild::has_version_in_range(
                &Version::parse("0.0.0.0").expect(
                    "It should be possible to create a Version object from the string 0.0.0.0"
                ),
                None,
                min.as_ref()
            ),
            "The version 0.0.0.0 should not be in range when min is 1.2.3.4"
        );
        // Check inside of range with min value
        assert!(
            MsBuild::has_version_in_range(
                &Version::parse("1.2.3.300").expect(
                    "It should be possible to create a Version object from the string 1.2.3.300"
                ),
                None,
                min.as_ref()
            ),
            "The version 1.2.3.300 should be in range when min is 1.2.3.4 and no max is given."
        );
        // Check out of range with max value
        assert!(
            !MsBuild::has_version_in_range(
                &Version::parse("4.3.2.11").expect(
                    "It should be possible to create a Version object from the string 4.3.2.11"
                ),
                max.as_ref(),
                None,
            ),
            "The version 4.3.2.11 should not be in range when max is 4.3.2.1 and no min is given."
        );
        // Check in range with max value
        assert!(
            MsBuild::has_version_in_range(
                &Version::parse("4.0.2.11").expect(
                    "It should be possible to create a Version object from the string 4.0.2.11"
                ),
                max.as_ref(),
                None,
            ),
            "The version 4.3.2.11 should not be in range when max is 4.3.2.1 and no min is given."
        );
        // Check in range with min and max
        assert!(
            MsBuild::has_version_in_range(
                &Version::parse("4.0.2.11").expect(
                    "It should be possible to create a Version object from the string 4.0.2.11"
                ),
                max.as_ref(),
                min.as_ref(),
            ),
            "The version 4.3.2.11 should not be in range when max is 4.3.2.1 and no max is given."
        );
    }

    #[test]
    fn test_msbuild_parse_installation_version() {
        let version_str = "2.3.1.34";
        let json_value = serde_json::json!({
            "instanceId": "VisualStudio.14.0",
            "installationPath": "C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\",
            "installationVersion": version_str
        });
        let expected = Version::parse(version_str)
            .map(InstallationVersion)
            .expect("It should be possible to parse the `version_str` as Version object.");
        let actual = MsBuild::parse_installation_version(&json_value).expect(
            "The function should be to extract an installation version from the json_value.",
        );
        assert_eq!(expected, actual);
    }

    #[test]
    fn test_msbuild_parse_installation_path() {
        let expected = Path::new("C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\");
        let json_value = serde_json::json!({
            "instanceId": "019109ba",
            "installDate": "2023-08-26T14:05:02Z",
            "installationName": "VisualStudio/17.12.0+35506.116",
            "installationPath": expected.to_string_lossy(),
            "installationVersion": "17.12.35506.116",
            "productId": "Microsoft.VisualStudio.Product.Community",
            "productPath": "C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\Common7\\IDE\\devenv.exe",
        });
        let actual = MsBuild::parse_installation_path(&json_value)
            .expect("The function should be to extract an installation path from the json_value.");
        assert_eq!(expected, actual);
    }

    #[test]
    fn test_msbuild_validate_instances_json() {
        let json_value = serde_json::json!([
            {
                "installationPath": "C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\",
                "installationVersion": "14.0",
            },
            {
                "installationPath": "C:\\Program Files\\Microsoft Visual Studio\\2022\\Community",
                "installationVersion": "17.12.35506.116",
            },
            {
                "installationPath": "C:\\Program Files\\Microsoft Visual Studio\\2022\\Enterprise",
                "installationVersion": "17.08.35506.116",
            },
        ]);

        let values: &Vec<Value> = json_value
            .as_array()
            .expect("It should be possible to parse the json as an array of objects.");

        // Sanity check.
        assert_eq!(
            values.len(),
            3,
            "There should be 3 instances: \n {:?}",
            values
        );

        let min = Some(
            Version::parse("17.9")
                .map(InstallationVersion)
                .expect("It should be possible to parse the 17.9 as a version."),
        );
        let max = Some(
            Version::parse("18.0")
                .map(InstallationVersion)
                .expect("It should be possible to parse the 18.0 as a version."),
        );
        let validated_instances =
            MsBuild::validate_instances_json(values.as_slice(), max.as_ref(), min.as_ref());
        let expected_version = Version::parse("17.12.35506.116")
            .map(InstallationVersion)
            .expect("It should be possible to parse avlid version.");
        let expected_path =
            Path::new("C:\\Program Files\\Microsoft Visual Studio\\2022\\Community");
        assert_eq!(
            validated_instances.len(),
            1,
            "There should only be 1 element found."
        );
        let (actual_version, actual_path) = validated_instances.first().unwrap();
        assert_eq!(
            expected_version, *actual_version,
            "The returned version was not the expected one",
        );
        assert_eq!(
            expected_path, *actual_path,
            "The returned path was not the expected one."
        );
    }

    #[test]
    fn test_msbuild_find_match() {
        let json_value = serde_json::json!([
            {
                "installationPath": "C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\",
                "installationVersion": "14.0",
            },
            {
                "installationPath": "C:\\Program Files\\Microsoft Visual Studio\\2022\\Community",
                "installationVersion": "17.12.35506.116",
            },
            {
                "installationPath": "C:\\Program Files\\Microsoft Visual Studio\\2022\\Enterprise",
                "installationVersion": "17.08.35506.116",
            },
        ]);

        let values: &Vec<Value> = json_value
            .as_array()
            .expect("It should be possible to parse the json as an array of objects.");

        // Sanity check.
        assert_eq!(
            values.len(),
            3,
            "There should be 3 instances: \n {:?}",
            values
        );

        // The min and max are now chosen so that they will include
        // two possible result.
        let min = Some(
            Version::parse("17.7")
                .map(InstallationVersion)
                .expect("It should be possible to parse the 17.9 as a version."),
        );
        let max = Some(
            Version::parse("18.0")
                .map(InstallationVersion)
                .expect("It should be possible to parse the 18.0 as a version."),
        );

        // The expected values, when no environment variable have been set,
        // is the one with the latest version.
        let expected = PathBuf::from("C:\\Program Files\\Microsoft Visual Studio\\2022\\Community");

        let actual = MsBuild::find_match(values, max.as_ref(), min.as_ref())
            .expect("The function is expected to return a valid result.");

        assert_eq!(
            expected, actual,
            "The resulting path does not match the expected one."
        );
    }
}