Skip to main content

nerdbank_gitversioning/cloud_build_services/
appveyor.rs

1// Copyright (c) .NET Foundation and Contributors. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4//! [AppVeyor](https://www.appveyor.com/docs/environment-variables/) support.
5
6use std::io::{self, ErrorKind, Write};
7use std::process::Command;
8
9use crate::cloud::{CloudBuild, EnvironmentVariables, empty_variables, env, is_nonempty};
10
11/// AppVeyor cloud-build integration.
12pub struct AppVeyor;
13
14impl AppVeyor {
15    fn run(arguments: &[&str], stderr: &mut dyn Write) -> io::Result<()> {
16        if is_nonempty(env("_NBGV_UnitTest").as_deref()) {
17            return Ok(());
18        }
19
20        match Command::new("appveyor").args(arguments).status() {
21            Ok(_) => Ok(()),
22            Err(error) if error.kind() == ErrorKind::NotFound => {
23                writeln!(
24                    stderr,
25                    "Could not find appveyor tool to set cloud build variable."
26                )
27            }
28            Err(error) => Err(error),
29        }
30    }
31}
32
33impl CloudBuild for AppVeyor {
34    fn name(&self) -> &'static str {
35        "AppVeyor"
36    }
37
38    fn is_applicable(&self) -> bool {
39        is_nonempty(env("APPVEYOR").as_deref())
40    }
41
42    fn is_pull_request(&self) -> bool {
43        is_nonempty(env("APPVEYOR_PULL_REQUEST_NUMBER").as_deref())
44    }
45
46    fn building_branch(&self) -> Option<String> {
47        (!self.is_pull_request())
48            .then(|| env("APPVEYOR_REPO_BRANCH"))
49            .flatten()
50            .filter(|branch| !branch.is_empty())
51            .map(|branch| format!("refs/heads/{branch}"))
52    }
53
54    fn building_tag(&self) -> Option<String> {
55        env("APPVEYOR_REPO_TAG")
56            .is_some_and(|value| value.eq_ignore_ascii_case("true"))
57            .then(|| {
58                format!(
59                    "refs/tags/{}",
60                    env("APPVEYOR_REPO_TAG_NAME").unwrap_or_default()
61                )
62            })
63    }
64
65    fn git_commit_id(&self) -> Option<String> {
66        None
67    }
68
69    fn set_cloud_build_number(
70        &self,
71        build_number: &str,
72        _stdout: &mut dyn Write,
73        stderr: &mut dyn Write,
74    ) -> io::Result<EnvironmentVariables> {
75        Self::run(&["UpdateBuild", "-Version", build_number], stderr)?;
76        Ok(empty_variables())
77    }
78
79    fn set_cloud_build_variable(
80        &self,
81        name: &str,
82        value: &str,
83        _stdout: &mut dyn Write,
84        stderr: &mut dyn Write,
85    ) -> io::Result<EnvironmentVariables> {
86        Self::run(&["SetVariable", "-Name", name, "-Value", value], stderr)?;
87        Ok(empty_variables())
88    }
89}