]> git.lizzy.rs Git - rust.git/blob - xtask/src/dist.rs
internal: try enabling debug info for releases
[rust.git] / xtask / src / dist.rs
1 use std::{
2     env,
3     fs::File,
4     io,
5     path::{Path, PathBuf},
6 };
7
8 use anyhow::Result;
9 use flate2::{write::GzEncoder, Compression};
10 use xshell::{cmd, cp, mkdir_p, pushd, pushenv, read_file, rm_rf, write_file};
11
12 use crate::{date_iso, flags, project_root};
13
14 impl flags::Dist {
15     pub(crate) fn run(self) -> Result<()> {
16         let stable =
17             std::env::var("GITHUB_REF").unwrap_or_default().as_str() == "refs/heads/release";
18
19         let dist = project_root().join("dist");
20         rm_rf(&dist)?;
21         mkdir_p(&dist)?;
22
23         if let Some(patch_version) = self.client_patch_version {
24             let version = if stable {
25                 format!("0.2.{}", patch_version)
26             } else {
27                 // A hack to make VS Code prefer nightly over stable.
28                 format!("0.3.{}", patch_version)
29             };
30             let release_tag = if stable { date_iso()? } else { "nightly".to_string() };
31             dist_client(&version, &release_tag)?;
32         }
33         let release_channel = if stable { "stable" } else { "nightly" };
34         dist_server(release_channel)?;
35         Ok(())
36     }
37 }
38
39 fn dist_client(version: &str, release_tag: &str) -> Result<()> {
40     let _d = pushd("./editors/code")?;
41     let nightly = release_tag == "nightly";
42
43     let mut patch = Patch::new("./package.json")?;
44
45     patch
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", "");
50
51     if nightly {
52         patch.replace(
53             r#""displayName": "rust-analyzer""#,
54             r#""displayName": "rust-analyzer (nightly)""#,
55         );
56     }
57     if !nightly {
58         patch.replace(r#""enableProposedApi": true,"#, r#""#);
59     }
60     patch.commit()?;
61
62     cmd!("npm ci").run()?;
63     cmd!("npx vsce package -o ../../dist/rust-analyzer.vsix").run()?;
64     Ok(())
65 }
66
67 fn dist_server(release_channel: &str) -> Result<()> {
68     let _e = pushenv("RUST_ANALYZER_CHANNEL", release_channel);
69     let _e = pushenv("CARGO_PROFILE_RELEASE_LTO", "true");
70     let _e = pushenv("CARGO_PROFILE_RELEASE_DEBUG", "1");
71     let target = get_target();
72     if target.contains("-linux-gnu") || target.contains("-linux-musl") {
73         env::set_var("CC", "clang");
74     }
75
76     cmd!("cargo build --manifest-path ./crates/rust-analyzer/Cargo.toml --bin rust-analyzer --target {target} --release").run()?;
77
78     let suffix = exe_suffix(&target);
79     let src =
80         Path::new("target").join(&target).join("release").join(format!("rust-analyzer{}", suffix));
81     let dst = Path::new("dist").join(format!("rust-analyzer-{}{}", target, suffix));
82     gzip(&src, &dst.with_extension("gz"))?;
83
84     // FIXME: the old names are temporarily kept for client compatibility, but they should be removed
85     // Remove this block after a couple of releases
86     match target.as_ref() {
87         "x86_64-unknown-linux-gnu" => {
88             cp(&src, "dist/rust-analyzer-linux")?;
89             gzip(&src, Path::new("dist/rust-analyzer-linux.gz"))?;
90         }
91         "x86_64-pc-windows-msvc" => {
92             cp(&src, "dist/rust-analyzer-windows.exe")?;
93             gzip(&src, Path::new("dist/rust-analyzer-windows.gz"))?;
94         }
95         "x86_64-apple-darwin" => {
96             cp(&src, "dist/rust-analyzer-mac")?;
97             gzip(&src, Path::new("dist/rust-analyzer-mac.gz"))?;
98         }
99         _ => {}
100     }
101
102     Ok(())
103 }
104
105 fn get_target() -> String {
106     match env::var("RA_TARGET") {
107         Ok(target) => target,
108         _ => {
109             if cfg!(target_os = "linux") {
110                 "x86_64-unknown-linux-gnu".to_string()
111             } else if cfg!(target_os = "windows") {
112                 "x86_64-pc-windows-msvc".to_string()
113             } else if cfg!(target_os = "macos") {
114                 "x86_64-apple-darwin".to_string()
115             } else {
116                 panic!("Unsupported OS, maybe try setting RA_TARGET")
117             }
118         }
119     }
120 }
121
122 fn exe_suffix(target: &str) -> String {
123     if target.contains("-windows-") {
124         ".exe".into()
125     } else {
126         "".into()
127     }
128 }
129
130 fn gzip(src_path: &Path, dest_path: &Path) -> Result<()> {
131     let mut encoder = GzEncoder::new(File::create(dest_path)?, Compression::best());
132     let mut input = io::BufReader::new(File::open(src_path)?);
133     io::copy(&mut input, &mut encoder)?;
134     encoder.finish()?;
135     Ok(())
136 }
137
138 struct Patch {
139     path: PathBuf,
140     original_contents: String,
141     contents: String,
142 }
143
144 impl Patch {
145     fn new(path: impl Into<PathBuf>) -> Result<Patch> {
146         let path = path.into();
147         let contents = read_file(&path)?;
148         Ok(Patch { path, original_contents: contents.clone(), contents })
149     }
150
151     fn replace(&mut self, from: &str, to: &str) -> &mut Patch {
152         assert!(self.contents.contains(from));
153         self.contents = self.contents.replace(from, to);
154         self
155     }
156
157     fn commit(&self) -> Result<()> {
158         write_file(&self.path, &self.contents)?;
159         Ok(())
160     }
161 }
162
163 impl Drop for Patch {
164     fn drop(&mut self) {
165         write_file(&self.path, &self.original_contents).unwrap();
166     }
167 }