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