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
//! github ci config
use cargo_dist_schema::{
ContainerConfig, GithubRunner, GithubRunnerConfig, GithubRunnerConfigInput, StringLikeOr,
TripleName,
};
use crate::platform::{github_runners::target_for_github_runner, targets};
use super::*;
/// github ci config (raw from file)
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct GithubCiLayer {
/// Common options
#[serde(flatten)]
pub common: CommonCiLayer,
/// Custom GitHub runners, mapped by triple target
#[serde(skip_serializing_if = "Option::is_none")]
pub runners: Option<SortedMap<TripleName, StringLikeOr<GithubRunner, GithubRunnerConfigInput>>>,
/// Custom permissions for jobs
#[serde(skip_serializing_if = "Option::is_none")]
pub permissions: Option<SortedMap<String, GithubPermissionMap>>,
/// Custom permissions for jobs
#[serde(skip_serializing_if = "Option::is_none")]
pub build_setup: Option<String>,
/// Use these commits for actions
#[serde(skip_serializing_if = "Option::is_none")]
pub action_commits: Option<SortedMap<String, String>>,
}
/// github ci config (final)
#[derive(Debug, Default, Clone)]
pub struct GithubCiConfig {
/// Common options
pub common: CommonCiConfig,
/// Custom GitHub runners, mapped by triple target
pub runners: SortedMap<TripleName, GithubRunnerConfig>,
/// Custom permissions for jobs
pub permissions: SortedMap<String, GithubPermissionMap>,
/// Custom permissions for jobs
pub build_setup: Option<String>,
/// Use these commits for github actions
pub action_commits: SortedMap<String, String>,
}
impl GithubCiConfig {
/// Get defaults for the given package
pub fn defaults_for_workspace(_workspaces: &WorkspaceGraph, common: &CommonCiConfig) -> Self {
Self {
common: common.clone(),
runners: Default::default(),
permissions: Default::default(),
action_commits: Default::default(),
build_setup: None,
}
}
}
impl ApplyLayer for GithubCiConfig {
type Layer = GithubCiLayer;
fn apply_layer(
&mut self,
Self::Layer {
common,
runners,
permissions,
build_setup,
action_commits,
}: Self::Layer,
) {
self.common.apply_layer(common);
let mk_default_github_runner = || GithubRunner::new("ubuntu-22.04".to_owned());
self.runners.apply_val(runners.map(|runners| {
runners
.into_iter()
.map(|(target_triple, runner)| {
(
target_triple.clone(),
match runner {
StringLikeOr::StringLike(runner) => {
let host = target_for_github_runner(&runner)
.map(|t| t.to_owned())
.unwrap_or_else(|| target_triple.clone());
GithubRunnerConfig {
host,
runner,
container: None,
}
}
StringLikeOr::Val(runner_config) => {
let runner = runner_config
.runner
.unwrap_or_else(mk_default_github_runner);
let host = runner_config
.host
.or_else(|| {
target_for_github_runner(&runner).map(|t| t.to_owned())
})
.unwrap_or_else(|| {
// if not specified, then assume the custom github runner is
// the right platform (host == target)
target_triple.clone()
});
let container =
runner_config.container.map(|container| match container {
StringLikeOr::StringLike(image_name) => {
ContainerConfig {
image: image_name,
// assume x86_64-unknown-linux-musl if not specified
host: targets::TARGET_X64_LINUX_MUSL.to_owned(),
package_manager: None,
}
}
StringLikeOr::Val(container_config) => ContainerConfig {
image: container_config.image,
host: container_config.host.unwrap_or_else(|| {
targets::TARGET_X64_LINUX_MUSL.to_owned()
}),
package_manager: container_config.package_manager,
},
});
GithubRunnerConfig {
runner,
host,
container,
}
}
},
)
})
.collect()
}));
self.permissions.apply_val(permissions);
self.build_setup.apply_opt(build_setup);
self.action_commits.apply_val(action_commits);
}
}
impl ApplyLayer for GithubCiLayer {
type Layer = GithubCiLayer;
fn apply_layer(
&mut self,
Self::Layer {
common,
runners,
permissions,
build_setup,
action_commits,
}: Self::Layer,
) {
self.common.apply_layer(common);
self.runners.apply_opt(runners);
self.permissions.apply_opt(permissions);
self.build_setup.apply_opt(build_setup);
self.action_commits.apply_opt(action_commits);
}
}
impl std::ops::Deref for GithubCiConfig {
type Target = CommonCiConfig;
fn deref(&self) -> &Self::Target {
&self.common
}
}