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
// Copyright 2020 Peter Williams <peter@newton.cx> and collaborators
// Licensed under the MIT License.
//! State for the Cranko CLI application.
use log::{error, info, warn};
use std::collections::HashMap;
use crate::{
errors::{Error, Result},
graph::{ProjectGraph, RepoHistories},
project::{ProjectId, ResolvedRequirement},
repository::{
ChangeList, CommitAvailability, PathMatcher, RcCommitInfo, RcProjectInfo,
ReleaseCommitInfo, Repository,
},
version::Version,
};
/// The main Cranko CLI application state structure.
pub struct AppSession {
/// The backing repository.
pub repo: Repository,
/// The graph of projects contained within the repo.
graph: ProjectGraph,
/// Information about the CI environment that we may be running in.
ci_info: ci_info::types::CiInfo,
}
impl AppSession {
/// Initialize a new application session.
///
/// Initialization may fail if the environment doesn't associate the process
/// with a proper Git repository with a work tree.
pub fn initialize() -> Result<AppSession> {
let repo = Repository::open_from_env()?;
let graph = ProjectGraph::default();
let ci_info = ci_info::get();
Ok(AppSession {
graph,
repo,
ci_info,
})
}
/// Characterize the repository environment in which this process is
/// running.
pub fn execution_environment(&self) -> Result<ExecutionEnvironment> {
if !self.ci_info.ci {
Ok(ExecutionEnvironment::NotCi)
} else {
let maybe_pr = self.ci_info.pr;
let maybe_ci_branch = self.ci_info.branch_name.as_ref().map(|s| s.as_ref());
let rc_name = self.repo.upstream_rc_name();
let release_name = self.repo.upstream_release_name();
if maybe_ci_branch.is_none() {
warn!("cannot determine the triggering branch name in this CI environment");
warn!("... this will affect many workflow safety checks")
}
if let Some(true) = maybe_pr {
if maybe_ci_branch == Some(rc_name) {
warn!("cranko seems to be running in a pull request to the `{}` branch; this is not recommended", rc_name);
warn!("... treating as a non-CI environment for safety");
return Ok(ExecutionEnvironment::NotCi);
}
if maybe_ci_branch == Some(release_name) {
warn!("cranko seems to be running in a pull request to the `{}` branch; this is not recommended", release_name);
warn!("... treating as a non-CI environment for safety");
return Ok(ExecutionEnvironment::NotCi);
}
}
if maybe_ci_branch == Some(release_name) {
warn!("cranko seems to be running in an update to the `{}` branch; this is not recommended", release_name);
warn!("... treating as a non-CI environment for safety");
return Ok(ExecutionEnvironment::NotCi);
}
// Gather some useful parameters ... Note: on Azure Pipelines, the
// initial checkout is in detached-HEAD state, so on pushes to the
// `rc` branch we can't determine `current_branch`. It would be kind
// of tedious to force all Azure users to manually check out the RC
// branch, so if we can parse out the RC info, let's assume that's
// what's going on.
let is_rc_update = maybe_ci_branch == Some(rc_name);
let current_is_release = self
.repo
.current_branch_name()?
.as_ref()
.map(|s| s.as_ref())
== Some(release_name);
// If the current branch is called `release`, we insist that we can
// parse release info from HEAD. We must be in dev mode (due to PR,
// or dev branch update) unless we have been triggered by an update to
// the `rc` branch.
if current_is_release {
let rel_info = self.repo.parse_release_info_from_head()?;
let dev_mode = !is_rc_update;
return Ok(ExecutionEnvironment::CiReleaseMode(dev_mode, rel_info));
}
// Otherwise, we must be in RC mode. If we're an update to the `rc`
// branch, we are *not* in dev mode and we insist that we can parse
// actual RC info from HEAD. Otherwise, we are in dev mode and we
// fake an RC request for all projects.
let (dev_mode, rc_info) = if is_rc_update {
(false, self.repo.parse_rc_info_from_head()?)
} else {
(true, self.default_dev_rc_info())
};
Ok(ExecutionEnvironment::CiRcMode(dev_mode, rc_info))
}
}
/// Check that the current process is running *outside* of a CI environment.
pub fn ensure_not_ci(&self, force: bool) -> Result<()> {
match self.execution_environment()? {
ExecutionEnvironment::NotCi => Ok(()),
_ => {
warn!("CI environment detected; this is unexpected for this command");
if force {
Ok(())
} else {
Err(Error::Environment(
"refusing to proceed (use \"force\" mode to override)".to_owned(),
))
}
}
}
}
/// Check that the current process is running in the "release mode" CI
/// environment, returning the latest release information. Any other
/// circumstance results in an error.
///
/// The returned boolean is true if in a "development"-like mode, false if
/// in the intended `rc` mode.
pub fn ensure_ci_release_mode(&self) -> Result<(bool, ReleaseCommitInfo)> {
match self.execution_environment()? {
ExecutionEnvironment::NotCi => {
error!("no CI environment detected; this is unexpected for this command");
Err(Error::Environment(
"don't know how to obtain release information -- cannot proceed".to_owned(),
))
}
ExecutionEnvironment::CiReleaseMode(dev, ri) => Ok((dev, ri)),
_ => {
error!("unexpected CI environment detected");
error!("... this command should only be run after switching to a local `release`-type branch");
Err(Error::Environment(
"don't know how to obtain release information -- cannot proceed".to_owned(),
))
}
}
}
/// Check that the current process is running and "RC"-like CI mode.
///
/// The returned boolean is true if in a "development"-like mode, false if
/// in the intended `rc` mode.
pub fn ensure_ci_rc_mode(&self, force: bool) -> Result<(bool, RcCommitInfo)> {
match self.execution_environment()? {
ExecutionEnvironment::CiRcMode(dev, rci) => Ok((dev, rci)),
ExecutionEnvironment::NotCi => {
warn!("no CI environment detected; this is unexpected for this command");
if force {
Ok((true, self.default_dev_rc_info()))
} else {
Err(Error::Environment(
"refusing to proceed (use \"force\" mode to override)".to_owned(),
))
}
}
_ => {
warn!("unexpected CI environment detected");
warn!("... this command should only be run in `rc` contexts");
if force {
Ok((true, self.default_dev_rc_info()))
} else {
Err(Error::Environment(
"refusing to proceed (use \"force\" mode to override)".to_owned(),
))
}
}
}
}
/// Check that the working tree is completely clean. We allow untracked and
/// ignored files but otherwise don't want any modifications, etc. Returns Ok
/// if clean, Err if not.
pub fn ensure_fully_clean(&self) -> Result<()> {
if let Some(changed_path) = self.repo.check_if_dirty(&[])? {
Err(Error::DirtyRepository(changed_path))
} else {
Ok(())
}
}
/// Check that the working tree is clean, excepting modifications to any
/// files interpreted as changelogs. Returns Ok if clean, Err if not.
pub fn ensure_changelog_clean(&self) -> Result<()> {
let mut matchers: Vec<Result<PathMatcher>> = self
.graph
.projects()
.map(|p| p.changelog.create_path_matcher(p))
.collect();
let matchers: Result<Vec<PathMatcher>> = matchers.drain(..).collect();
let matchers = matchers?;
if let Some(changed_path) = self.repo.check_if_dirty(&matchers[..])? {
Err(Error::DirtyRepository(changed_path))
} else {
Ok(())
}
}
/// Get the graph of projects inside this app session.
pub fn graph(&self) -> &ProjectGraph {
&self.graph
}
/// Get the graph of projects inside this app session, mutably.
pub fn graph_mut(&mut self) -> &mut ProjectGraph {
&mut self.graph
}
/// Get the graph of projects inside this app session.
///
/// If the graph has not yet been loaded, this triggers processing of the
/// config file and repository to fill in the graph information, hence the
/// fallibility.
pub fn populated_graph(&mut self) -> Result<&ProjectGraph> {
if self.graph.len() == 0 {
self.populate_graph()?;
}
Ok(&self.graph)
}
fn populate_graph(&mut self) -> Result<()> {
// Start by auto-detecting everything in the repo index.
let mut cargo = crate::cargo::CargoLoader::default();
self.repo.scan_paths(|p| {
let (dirname, basename) = p.split_basename();
cargo.process_index_item(dirname, basename);
})?;
cargo.finalize(self)?;
self.graph.complete_loading()?;
Ok(())
}
/// Apply version numbers given the current repository state and bump
/// specifications.
///
/// This also involves solving the version requirements for internal
/// dependencies.
pub fn apply_versions(&mut self, rc_info: &RcCommitInfo) -> Result<()> {
let mut new_versions: HashMap<ProjectId, Version> = HashMap::new();
let latest_info = self.repo.get_latest_release_info()?;
for ident in self.graph.toposort_idents()? {
// First, make sure that we can satisfy this project's internal
// dependencies. By definition of the toposort, any of its
// dependencies will have already been visited.
let deps = self.graph.resolve_direct_dependencies(&self.repo, ident)?;
let proj = self.graph.lookup_mut(ident);
for dep in &deps[..] {
let min_version = match dep.availability {
CommitAvailability::NotAvailable => {
return Err(Error::UnsatisfiedInternalRequirement(
proj.user_facing_name.to_string(),
))
}
CommitAvailability::ExistingRelease(ref v) => v.clone(),
CommitAvailability::NewRelease => {
if let Some(v) = new_versions.get(&dep.ident) {
v.clone()
} else {
return Err(Error::UnsatisfiedInternalRequirement(
proj.user_facing_name.to_string(),
));
}
}
};
proj.internal_reqs.push(ResolvedRequirement {
ident: dep.ident,
min_version,
});
}
// Now, set the baseline version to the last release.
let latest_release = latest_info.lookup_project(proj);
proj.version = if let Some(info) = latest_release {
proj.version.parse_like(&info.version)?
} else {
proj.version.zero_like()
};
let baseline_version = proj.version.clone();
// If there's a bump, apply it.
if let Some(rc) = rc_info.lookup_project(proj) {
let scheme = proj.version.parse_bump_scheme(&rc.bump_spec)?;
scheme.apply(&mut proj.version)?;
new_versions.insert(proj.ident(), proj.version.clone());
info!(
"{}: {} => {}",
proj.user_facing_name, baseline_version, proj.version
);
} else {
info!(
"{}: unchanged from {}",
proj.user_facing_name, baseline_version
);
}
}
Ok(())
}
/// Rewrite everyone's metadata to match our internal state.
pub fn rewrite(&self) -> Result<ChangeList> {
let mut changes = ChangeList::default();
for ident in self.graph.toposort_idents()? {
let proj = self.graph.lookup(ident);
for rw in &proj.rewriters {
rw.rewrite(self, &mut changes)?;
}
}
Ok(changes)
}
pub fn make_release_commit(&mut self) -> Result<()> {
self.repo.make_release_commit(&self.graph)
}
pub fn make_rc_commit(
&mut self,
rcinfo: Vec<RcProjectInfo>,
changes: &ChangeList,
) -> Result<()> {
self.repo.make_rc_commit(rcinfo, &changes)?;
Ok(())
}
pub fn analyze_histories(&self) -> Result<RepoHistories> {
self.graph.analyze_histories(&self.repo)
}
pub fn default_dev_rc_info(&self) -> RcCommitInfo {
let mut rcinfo = RcCommitInfo::default();
for proj in self.graph.projects() {
rcinfo.projects.push(RcProjectInfo {
qnames: proj.qualified_names().to_owned(),
bump_spec: "dev-datecode".to_owned(),
})
}
rcinfo
}
// Rewrite the changelogs of packages staged for release to include their
// final version numbers and other release information.
pub fn apply_changelogs(&self, rcinfo: &RcCommitInfo, changes: &mut ChangeList) -> Result<()> {
// This step could plausibly be implemented in the "rewriter" framework,
// probably? I dodn't have a great reason for doing otherwise, other
// than that it seemed easier at the time.
for proj in self.graph.toposort()? {
if let Some(_) = rcinfo.lookup_project(proj) {
proj.changelog
.finalize_changelog(proj, &self.repo, changes)?;
}
}
Ok(())
}
/// Create version control tags for new releases.
pub fn create_tags(&mut self, rel_info: &ReleaseCommitInfo) -> Result<()> {
self.populate_graph()?;
for proj in self.graph.toposort_mut()? {
if let Some(rel) = rel_info.lookup_if_released(proj) {
self.repo.tag_project_at_head(proj, rel)?;
}
}
Ok(())
}
}
/// Different categorizations of the environment in which the program is
/// running.
pub enum ExecutionEnvironment {
/// The program is running in a CI environment, in "release request" mode
/// where we have not yet created a "release commit" with final version
/// number information. If the boolean is true, we are in a development mode
/// where version numbers are temporary and release artifacts will not be
/// deployed.
CiRcMode(bool, RcCommitInfo),
/// The program is running in a CI environment, in a "release deployment"
/// mode where HEAD is a Cranko release commit. If the boolean is true, we
/// are in a development mode where version numbers are temporary and
/// release artifacts will not be deployed (but this mode still can be
/// useful for creating artifacts and so on).
CiReleaseMode(bool, ReleaseCommitInfo),
/// The program does not appear to be running in a CI environment. We infer
/// that we're running in an individual development environment.
NotCi,
}