forked from bytecodealliance/wasm-tools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
publish.rs
383 lines (351 loc) · 12.8 KB
/
publish.rs
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
//! Helper script to manage versions in this repository for various crates.
//!
//! Three subcommands:
//!
//! * `./publish diff wasmparser` - shows a git diff for the wasmparser
//! crate from the last tagged version to now. Useful for figuring out if a
//! major version bump is needed or not.
//!
//! * `./publish bump crate1:major crate2:minor ...` - performs a major or minor
//! version bump of the crates specified. All crates not mentioned here
//! which transitively depend on these crates are minor-bumped.
//!
//! * `./publish publish` - attempts to publish all crates. Only publishes if
//! their current version isn't already published. Will add wasmtime
//! publication group automatically. A git tag is created for all published
//! crates.
use std::collections::HashMap;
use std::env;
use std::fs;
use std::os::unix::prelude::*;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::thread;
use std::time::Duration;
// Crates we care about publishing sorted topologically.
const CRATES_TO_PUBLISH: &[&str] = &[
"wasmparser",
"wasm-encoder",
"wasmprinter",
"wast",
"wat",
"wasmparser-dump",
"wasm-smith",
"wasm-mutate",
"wasm-shrink",
"wasm-tools",
"wasm-compose",
];
#[derive(Clone)]
struct Crate {
manifest: PathBuf,
name: String,
version: String,
// Only set by `bump_version` if the crate was actually updated to get a new
// version.
new_version: Option<String>,
publish: bool,
}
fn main() {
let mut crates = Vec::new();
crates.push(read_crate("./Cargo.toml".as_ref()));
find_crates("crates".as_ref(), &mut crates);
let pos = CRATES_TO_PUBLISH
.iter()
.enumerate()
.map(|(i, c)| (*c, i))
.collect::<HashMap<_, _>>();
crates.sort_by_key(|krate| pos.get(&krate.name[..]));
match &env::args().nth(1).expect("must have one argument")[..] {
"bump" => {
let bumps = env::args()
.skip(2)
.map(|s| {
if let Some(s) = s.strip_suffix(":major") {
(s.to_string(), true)
} else if let Some(s) = s.strip_suffix(":minor") {
(s.to_string(), false)
} else {
panic!("unknown: {}", s);
}
})
.collect::<Vec<_>>();
for (i, mut krate) in crates.clone().into_iter().enumerate() {
bump_version(&mut krate, &mut crates, &bumps);
crates[i] = krate;
}
// update the lock file
assert!(Command::new("cargo")
.arg("fetch")
.status()
.unwrap()
.success());
}
"publish" => {
// We have so many crates to publish we're frequently either
// rate-limited or we run into issues where crates can't publish
// successfully because they're waiting on the index entries of
// previously-published crates to propagate. This means we try to
// publish in a loop and we remove crates once they're successfully
// published. Failed-to-publish crates get enqueued for another try
// later on.
for _ in 0..5 {
crates.retain(|krate| !publish(krate));
if crates.is_empty() {
break;
}
println!(
"{} crates failed to publish, waiting for a bit to retry",
crates.len(),
);
thread::sleep(Duration::from_secs(20));
}
assert!(crates.is_empty(), "failed to publish all crates");
println!("");
println!("===================================================================");
println!("");
println!("Don't forget to push tags for this release!");
println!("");
println!(" $ git push --tags");
}
"diff" => {
let krate = env::args().nth(2).unwrap();
let krate = crates.iter().find(|c| c.name == krate).unwrap();
Command::new("git")
.arg("diff")
.arg(format!("{}-{}..HEAD", krate.name, krate.version))
.arg("--")
.arg(krate.manifest.parent().unwrap())
.exec();
}
s => panic!("unknown command: {}", s),
}
}
// Recursively looks for `Cargo.toml` in `dir`
fn find_crates(dir: &Path, dst: &mut Vec<Crate>) {
if dir.join("Cargo.toml").exists() {
let krate = read_crate(&dir.join("Cargo.toml"));
if !krate.publish || CRATES_TO_PUBLISH.iter().any(|c| krate.name == *c) {
dst.push(krate);
} else {
panic!("failed to find {:?} in whitelist or blacklist", krate.name);
}
}
for entry in dir.read_dir().unwrap() {
let entry = entry.unwrap();
if entry.file_type().unwrap().is_dir() {
find_crates(&entry.path(), dst);
}
}
}
fn read_crate(manifest: &Path) -> Crate {
let mut name = None;
let mut version = None;
let mut publish = true;
for line in fs::read_to_string(manifest).unwrap().lines() {
if name.is_none() && line.starts_with("name = \"") {
name = Some(
line.replace("name = \"", "")
.replace("\"", "")
.trim()
.to_string(),
);
}
if version.is_none() && line.starts_with("version = \"") {
version = Some(
line.replace("version = \"", "")
.replace("\"", "")
.trim()
.to_string(),
);
}
if line.starts_with("publish = false") {
publish = false;
}
}
let name = name.unwrap();
let version = version.unwrap();
Crate {
manifest: manifest.to_path_buf(),
name,
version,
publish,
new_version: None,
}
}
fn bump_version(krate: &mut Crate, crates: &mut [Crate], bumps: &[(String, bool)]) {
let contents = fs::read_to_string(&krate.manifest).unwrap();
let next_version = |target: &Crate| -> String {
// If this crate is publishable then if it's explicitly requested on the
// command line we bump the version. Otherwise we also force a version
// bump if it's the same as this `krate` requested originally for this
// function. This forced bump will be thrown away if no other
// dependencies get updated.
if CRATES_TO_PUBLISH.contains(&&target.name[..]) {
if let Some((_, major)) = bumps.iter().find(|(s, _)| *s == target.name) {
return bump(&target.version, !*major);
}
if target.name == krate.name {
return bump(&target.version, true);
}
}
// Prefer the `new_version`, if set, over the old version. The new
// version will be updated by this point since crates are sorted
// topologically.
target.new_version.clone().unwrap_or(target.version.clone())
};
let mut new_manifest = String::new();
let mut is_deps = false;
let mut updated_deps = false;
for line in contents.lines() {
let mut rewritten = false;
if !is_deps && line.starts_with("version =") {
if CRATES_TO_PUBLISH.contains(&&krate.name[..]) {
new_manifest.push_str(&line.replace(&krate.version, &next_version(krate)));
rewritten = true;
}
}
is_deps = if line.starts_with("[") {
line.contains("dependencies")
} else {
is_deps
};
for other in crates.iter() {
// If `other` isn't a published crate then it's not going to get a
// bumped version so we don't need to update anything in the
// manifest.
if !other.publish {
continue;
}
if !is_deps || !line.starts_with(&format!("{} ", other.name)) {
continue;
}
if !line.contains(&other.version) {
if !line.contains("version =") || !krate.publish {
continue;
}
panic!(
"{:?} has a dep on {} but doesn't list version {}",
krate.manifest, other.name, other.version
);
}
let next = next_version(other);
if next != other.version {
rewritten = true;
updated_deps = true;
new_manifest.push_str(&line.replace(&other.version, &next));
}
break;
}
if !rewritten {
new_manifest.push_str(line);
}
new_manifest.push_str("\n");
}
// Only actually rewrite the manifest if this crate was explicitly requested
// to get bumped or one of its dependencies changed, otherwise nothing
// changed about it.
if updated_deps || bumps.iter().any(|(s, _)| *s == krate.name) {
let new = next_version(krate);
println!("bump `{}` {} => {}", krate.name, krate.version, new,);
fs::write(&krate.manifest, new_manifest).unwrap();
krate.new_version = Some(new);
}
}
/// Performs a major version bump increment on the semver version `version`.
///
/// This function will perform a semver-major-version bump on the `version`
/// specified. This is used to calculate the next version of a crate in this
/// repository since we're currently making major version bumps for all our
/// releases. This may end up getting tweaked as we stabilize crates and start
/// doing more minor/patch releases, but for now this should do the trick.
fn bump(version: &str, patch_bump: bool) -> String {
let mut iter = version.split('.').map(|s| s.parse::<u32>().unwrap());
let major = iter.next().expect("major version");
let minor = iter.next().expect("minor version");
let patch = iter.next().expect("patch version");
if patch_bump {
return format!("{}.{}.{}", major, minor, patch + 1);
}
if major != 0 {
format!("{}.0.0", major + 1)
} else if minor != 0 {
format!("0.{}.0", minor + 1)
} else {
format!("0.0.{}", patch + 1)
}
}
fn publish(krate: &Crate) -> bool {
if !CRATES_TO_PUBLISH.iter().any(|s| *s == krate.name) {
return true;
}
// First make sure the crate isn't already published at this version. This
// script may be re-run and there's no need to re-attempt previous work.
let output = Command::new("curl")
.arg(&format!("https://crates.io/api/v1/crates/{}", krate.name))
.output()
.expect("failed to invoke `curl`");
if output.status.success()
&& String::from_utf8_lossy(&output.stdout)
.contains(&format!("\"newest_version\":\"{}\"", krate.version))
{
println!(
"skip publish {} because {} is latest version",
krate.name, krate.version,
);
return true;
}
let status = Command::new("cargo")
.arg("publish")
.current_dir(krate.manifest.parent().unwrap())
.status()
.expect("failed to run cargo");
if !status.success() {
println!("FAIL: failed to publish `{}`: {}", krate.name, status);
return false;
}
let status = Command::new("git")
.arg("tag")
.arg(format!("{}-{}", krate.name, krate.version))
.status()
.expect("failed to run git");
if !status.success() {
panic!("FAIL: failed to tag: {}", status);
}
// After we've published then make sure that the `wasmtime-publish` group is
// added to this crate for future publications. If it's already present
// though we can skip the `cargo owner` modification.
let output = Command::new("curl")
.arg(&format!(
"https://crates.io/api/v1/crates/{}/owners",
krate.name
))
.output()
.expect("failed to invoke `curl`");
if output.status.success()
&& String::from_utf8_lossy(&output.stdout).contains("wasmtime-publish")
{
println!(
"wasmtime-publish already listed as an owner of {}",
krate.name
);
return true;
}
// Note that the status is ignored here. This fails most of the time because
// the owner is already set and present, so we only want to add this to
// crates which haven't previously been published.
let status = Command::new("cargo")
.arg("owner")
.arg("-a")
.arg("github:bytecodealliance:wasmtime-publish")
.arg(&krate.name)
.status()
.expect("failed to run cargo");
if !status.success() {
panic!(
"FAIL: failed to add wasmtime-publish as owner `{}`: {}",
krate.name, status
);
}
true
}