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
|
use std::{
env,
fs::File,
io,
path::{Path, PathBuf},
};
use anyhow::Result;
use flate2::{write::GzEncoder, Compression};
use xshell::{cmd, mkdir_p, pushd, pushenv, read_file, rm_rf, write_file};
use crate::{date_iso, flags, project_root};
impl flags::Dist {
pub(crate) fn run(self) -> Result<()> {
let stable =
std::env::var("GITHUB_REF").unwrap_or_default().as_str() == "refs/heads/release";
let dist = project_root().join("dist");
rm_rf(&dist)?;
mkdir_p(&dist)?;
if let Some(patch_version) = self.client_patch_version {
let version = if stable {
format!("0.2.{}", patch_version)
} else {
// A hack to make VS Code prefer nightly over stable.
format!("0.3.{}", patch_version)
};
let release_tag = if stable { date_iso()? } else { "nightly".to_string() };
dist_client(&version, &release_tag)?;
}
let release_channel = if stable { "stable" } else { "nightly" };
dist_server(release_channel)?;
Ok(())
}
}
fn dist_client(version: &str, release_tag: &str) -> Result<()> {
let _d = pushd("./editors/code")?;
let nightly = release_tag == "nightly";
let mut patch = Patch::new("./package.json")?;
patch
.replace(r#""version": "0.4.0-dev""#, &format!(r#""version": "{}""#, version))
.replace(r#""releaseTag": null"#, &format!(r#""releaseTag": "{}""#, release_tag))
.replace(r#""$generated-start": false,"#, "")
.replace(",\n \"$generated-end\": false", "");
if nightly {
patch.replace(
r#""displayName": "rust-analyzer""#,
r#""displayName": "rust-analyzer (nightly)""#,
);
}
if !nightly {
patch.replace(r#""enableProposedApi": true,"#, r#""#);
}
patch.commit()?;
cmd!("npm ci").run()?;
cmd!("npx vsce package -o ../../dist/rust-analyzer.vsix").run()?;
Ok(())
}
fn dist_server(release_channel: &str) -> Result<()> {
let _e = pushenv("RUST_ANALYZER_CHANNEL", release_channel);
let _e = pushenv("CARGO_PROFILE_RELEASE_LTO", "thin");
let _e = pushenv("CARGO_PROFILE_RELEASE_DEBUG", "1");
let target = get_target();
if target.contains("-linux-gnu") || target.contains("-linux-musl") {
env::set_var("CC", "clang");
}
cmd!("cargo build --manifest-path ./crates/rust-analyzer/Cargo.toml --bin rust-analyzer --target {target} --release").run()?;
let suffix = exe_suffix(&target);
let src =
Path::new("target").join(&target).join("release").join(format!("rust-analyzer{}", suffix));
let dst = Path::new("dist").join(format!("rust-analyzer-{}{}", target, suffix));
gzip(&src, &dst.with_extension("gz"))?;
Ok(())
}
fn get_target() -> String {
match env::var("RA_TARGET") {
Ok(target) => target,
_ => {
if cfg!(target_os = "linux") {
"x86_64-unknown-linux-gnu".to_string()
} else if cfg!(target_os = "windows") {
"x86_64-pc-windows-msvc".to_string()
} else if cfg!(target_os = "macos") {
"x86_64-apple-darwin".to_string()
} else {
panic!("Unsupported OS, maybe try setting RA_TARGET")
}
}
}
}
fn exe_suffix(target: &str) -> String {
if target.contains("-windows-") {
".exe".into()
} else {
"".into()
}
}
fn gzip(src_path: &Path, dest_path: &Path) -> Result<()> {
let mut encoder = GzEncoder::new(File::create(dest_path)?, Compression::best());
let mut input = io::BufReader::new(File::open(src_path)?);
io::copy(&mut input, &mut encoder)?;
encoder.finish()?;
Ok(())
}
struct Patch {
path: PathBuf,
original_contents: String,
contents: String,
}
impl Patch {
fn new(path: impl Into<PathBuf>) -> Result<Patch> {
let path = path.into();
let contents = read_file(&path)?;
Ok(Patch { path, original_contents: contents.clone(), contents })
}
fn replace(&mut self, from: &str, to: &str) -> &mut Patch {
assert!(self.contents.contains(from));
self.contents = self.contents.replace(from, to);
self
}
fn commit(&self) -> Result<()> {
write_file(&self.path, &self.contents)?;
Ok(())
}
}
impl Drop for Patch {
fn drop(&mut self) {
write_file(&self.path, &self.original_contents).unwrap();
}
}
|