]> git.lizzy.rs Git - rust.git/blob - xtask/src/dist.rs
12a7fea1e1ddcd9c8dfe1c5113994ff2fd10a72a
[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 target = get_target();
70     if target.contains("-linux-gnu") || target.contains("-linux-musl") {
71         env::set_var("CC", "clang");
72     }
73
74     cmd!("cargo build --manifest-path ./crates/rust-analyzer/Cargo.toml --bin rust-analyzer --target {target} --release").run()?;
75
76     let suffix = exe_suffix(&target);
77     let src =
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"))?;
81
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"))?;
88         }
89         "x86_64-pc-windows-msvc" => {
90             cp(&src, "dist/rust-analyzer-windows.exe")?;
91             gzip(&src, Path::new("dist/rust-analyzer-windows.gz"))?;
92         }
93         "x86_64-apple-darwin" => {
94             cp(&src, "dist/rust-analyzer-mac")?;
95             gzip(&src, Path::new("dist/rust-analyzer-mac.gz"))?;
96         }
97         _ => {}
98     }
99
100     Ok(())
101 }
102
103 fn get_target() -> String {
104     match env::var("RA_TARGET") {
105         Ok(target) => target,
106         _ => {
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()
113             } else {
114                 panic!("Unsupported OS, maybe try setting RA_TARGET")
115             }
116         }
117     }
118 }
119
120 fn exe_suffix(target: &str) -> String {
121     if target.contains("-windows-") {
122         ".exe".into()
123     } else {
124         "".into()
125     }
126 }
127
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)?;
132     encoder.finish()?;
133     Ok(())
134 }
135
136 struct Patch {
137     path: PathBuf,
138     original_contents: String,
139     contents: String,
140 }
141
142 impl Patch {
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 })
147     }
148
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);
152         self
153     }
154
155     fn commit(&self) -> Result<()> {
156         write_file(&self.path, &self.contents)?;
157         Ok(())
158     }
159 }
160
161 impl Drop for Patch {
162     fn drop(&mut self) {
163         write_file(&self.path, &self.original_contents).unwrap();
164     }
165 }