Skip to main content

flowey_hvlite/pipelines_shared/
gh_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::pipeline::prelude::FlowArch;
7use flowey::pipeline::prelude::FlowPlatform;
8
9const COMMON_TEMPLATE: &str = include_str!("gh_flowey_bootstrap_template.yml");
10const LINUX_TEMPLATE: &str = include_str!("gh_flowey_bootstrap_template_linux.yml");
11const WINDOWS_TEMPLATE: &str = include_str!("gh_flowey_bootstrap_template_windows.yml");
12
13/// Get a bootstrap template with only the installer matching the job's platform
14/// and architecture.
15///
16/// Use with [`Pipeline::gh_set_flowey_bootstrap_template_fn`].
17///
18/// [`Pipeline::gh_set_flowey_bootstrap_template_fn`]:
19///     flowey::pipeline::prelude::Pipeline::gh_set_flowey_bootstrap_template_fn
20pub fn get_template_for_platform(platform: FlowPlatform, arch: FlowArch) -> anyhow::Result<String> {
21    let installer = match (platform, arch) {
22        (FlowPlatform::Windows, FlowArch::X86_64) => windows_template("x86_64", "X64"),
23        (FlowPlatform::Windows, FlowArch::Aarch64) => windows_template("aarch64", "ARM64"),
24        (FlowPlatform::Linux(_), FlowArch::X86_64 | FlowArch::Aarch64) => LINUX_TEMPLATE.into(),
25        (platform, arch) => {
26            anyhow::bail!("unsupported bootstrap platform {platform} / arch {arch}")
27        }
28    };
29
30    Ok(with_toolchain(format!("{installer}\n{COMMON_TEMPLATE}")))
31}
32
33fn windows_template(host_arch: &str, runner_arch: &str) -> String {
34    WINDOWS_TEMPLATE
35        .replace("{{RUSTUP_HOST_ARCH}}", host_arch)
36        .replace("{{RUNNER_ARCH}}", runner_arch)
37}
38
39fn with_toolchain(template: String) -> String {
40    template.replace(
41        "{{RUSTUP_TOOLCHAIN}}",
42        flowey_lib_hvlite::cfg_rustup_version::RUSTUP_TOOLCHAIN,
43    )
44}