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