9 use flate2::{write::GzEncoder, Compression};
10 use xshell::{cmd, cp, mkdir_p, pushd, pushenv, read_file, rm_rf, write_file};
12 use crate::{date_iso, flags, project_root};
15 pub(crate) fn run(self) -> Result<()> {
17 std::env::var("GITHUB_REF").unwrap_or_default().as_str() == "refs/heads/release";
19 let dist = project_root().join("dist");
23 if let Some(patch_version) = self.client_patch_version {
24 let version = if stable {
25 format!("0.2.{}", patch_version)
27 // A hack to make VS Code prefer nightly over stable.
28 format!("0.3.{}", patch_version)
30 let release_tag = if stable { date_iso()? } else { "nightly".to_string() };
31 dist_client(&version, &release_tag)?;
33 let release_channel = if stable { "stable" } else { "nightly" };
34 dist_server(release_channel)?;
39 fn dist_client(version: &str, release_tag: &str) -> Result<()> {
40 let _d = pushd("./editors/code")?;
41 let nightly = release_tag == "nightly";
43 let mut patch = Patch::new("./package.json")?;
46 .replace(r#""version": "0.4.0-dev""#, &format!(r#""version": "{}""#, version))
47 .replace(r#""releaseTag": null"#, &format!(r#""releaseTag": "{}""#, release_tag))
48 .replace(r#""$generated-start": false,"#, "")
49 .replace(",\n \"$generated-end\": false", "");
53 r#""displayName": "rust-analyzer""#,
54 r#""displayName": "rust-analyzer (nightly)""#,
58 patch.replace(r#""enableProposedApi": true,"#, r#""#);
62 cmd!("npm ci").run()?;
63 cmd!("npx vsce package -o ../../dist/rust-analyzer.vsix").run()?;
67 fn dist_server(release_channel: &str) -> Result<()> {
68 let _e = pushenv("RUST_ANALYZER_CHANNEL", release_channel);
69 let target = get_target();
70 if target.contains("-linux-gnu") || target.contains("-linux-musl") {
71 env::set_var("CC", "clang");
74 cmd!("cargo build --manifest-path ./crates/rust-analyzer/Cargo.toml --bin rust-analyzer --target {target} --release").run()?;
76 let suffix = exe_suffix(&target);
78 Path::new("target").join(&target).join("release").join(format!("rust-analyzer{}", suffix));
79 let dst = Path::new("dist").join(format!("rust-analyzer-{}{}", target, suffix));
80 gzip(&src, &dst.with_extension("gz"))?;
82 // FIXME: the old names are temporarily kept for client compatibility, but they should be removed
83 // Remove this block after a couple of releases
84 match target.as_ref() {
85 "x86_64-unknown-linux-gnu" => {
86 cp(&src, "dist/rust-analyzer-linux")?;
87 gzip(&src, Path::new("dist/rust-analyzer-linux.gz"))?;
89 "x86_64-pc-windows-msvc" => {
90 cp(&src, "dist/rust-analyzer-windows.exe")?;
91 gzip(&src, Path::new("dist/rust-analyzer-windows.gz"))?;
93 "x86_64-apple-darwin" => {
94 cp(&src, "dist/rust-analyzer-mac")?;
95 gzip(&src, Path::new("dist/rust-analyzer-mac.gz"))?;
103 fn get_target() -> String {
104 match env::var("RA_TARGET") {
105 Ok(target) => target,
107 if cfg!(target_os = "linux") {
108 "x86_64-unknown-linux-gnu".to_string()
109 } else if cfg!(target_os = "windows") {
110 "x86_64-pc-windows-msvc".to_string()
111 } else if cfg!(target_os = "macos") {
112 "x86_64-apple-darwin".to_string()
114 panic!("Unsupported OS, maybe try setting RA_TARGET")
120 fn exe_suffix(target: &str) -> String {
121 if target.contains("-windows-") {
128 fn gzip(src_path: &Path, dest_path: &Path) -> Result<()> {
129 let mut encoder = GzEncoder::new(File::create(dest_path)?, Compression::best());
130 let mut input = io::BufReader::new(File::open(src_path)?);
131 io::copy(&mut input, &mut encoder)?;
138 original_contents: String,
143 fn new(path: impl Into<PathBuf>) -> Result<Patch> {
144 let path = path.into();
145 let contents = read_file(&path)?;
146 Ok(Patch { path, original_contents: contents.clone(), contents })
149 fn replace(&mut self, from: &str, to: &str) -> &mut Patch {
150 assert!(self.contents.contains(from));
151 self.contents = self.contents.replace(from, to);
155 fn commit(&self) -> Result<()> {
156 write_file(&self.path, &self.contents)?;
161 impl Drop for Patch {
163 write_file(&self.path, &self.original_contents).unwrap();