Skip to main content

flowey_hvlite/pipelines_shared/
ado_flowey_bootstrap_template.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! See [`get_template_for_platform`]
5
6use flowey::node::prelude::AdoResourcesRepositoryId;
7use flowey::pipeline::prelude::FlowArch;
8use flowey::pipeline::prelude::FlowPlatform;
9
10const COMMON_TEMPLATE: &str = include_str!("ado_flowey_bootstrap_template.yml");
11const LINUX_TEMPLATE: &str = include_str!("ado_flowey_bootstrap_template_linux.yml");
12const WINDOWS_TEMPLATE: &str = include_str!("ado_flowey_bootstrap_template_windows.yml");
13
14/// Get a bootstrap template with only the installer matching the job's platform
15/// and architecture.
16///
17/// Use with [`Pipeline::ado_set_flowey_bootstrap_template_fn`].
18///
19/// [`Pipeline::ado_set_flowey_bootstrap_template_fn`]:
20///     flowey::pipeline::prelude::Pipeline::ado_set_flowey_bootstrap_template_fn
21pub fn get_template_for_platform(
22    platform: FlowPlatform,
23    arch: FlowArch,
24    ado_hvlite_repo_resource_id: &AdoResourcesRepositoryId,
25) -> anyhow::Result<String> {
26    let installer = match (platform, arch) {
27        (FlowPlatform::Windows, FlowArch::X86_64) => windows_template("x86_64", "X64"),
28        (FlowPlatform::Windows, FlowArch::Aarch64) => windows_template("aarch64", "ARM64"),
29        (FlowPlatform::Linux(_), FlowArch::X86_64 | FlowArch::Aarch64) => LINUX_TEMPLATE.into(),
30        (platform, arch) => {
31            anyhow::bail!("unsupported bootstrap platform {platform} / arch {arch}")
32        }
33    };
34
35    Ok(with_repo_and_toolchain(
36        format!("{installer}\n{COMMON_TEMPLATE}"),
37        ado_hvlite_repo_resource_id,
38    ))
39}
40
41fn windows_template(host_arch: &str, runner_arch: &str) -> String {
42    WINDOWS_TEMPLATE
43        .replace("{{RUSTUP_HOST_ARCH}}", host_arch)
44        .replace("{{RUNNER_ARCH}}", runner_arch)
45}
46
47fn with_repo_and_toolchain(
48    template: String,
49    ado_hvlite_repo_resource_id: &AdoResourcesRepositoryId,
50) -> String {
51    // to be clear: these replaces are totally custom to this particular
52    // bootstrap template. flowey knows nothing of these replacements.
53    template
54        // not actually used today, but will be used the moment pipelines live
55        // in a repo that doesn't also contain all flowey source code as well.
56        .replace(
57            "{{FLOWEY_ADO_REPO_ID}}",
58            ado_hvlite_repo_resource_id.dangerous_get_raw_id(),
59        )
60        .replace(
61            "{{RUSTUP_TOOLCHAIN}}",
62            flowey_lib_hvlite::cfg_rustup_version::RUSTUP_TOOLCHAIN,
63        )
64}