]> git.lizzy.rs Git - rust.git/blob - xtask/src/dist.rs
lsp-types 0.74
[rust.git] / xtask / src / dist.rs
1 use std::path::PathBuf;
2
3 use anyhow::Result;
4
5 use crate::{
6     not_bash::{date_iso, fs2, pushd, rm_rf, run},
7     project_root,
8 };
9
10 pub fn run_dist(nightly: bool, client_version: Option<String>) -> Result<()> {
11     let dist = project_root().join("dist");
12     rm_rf(&dist)?;
13     fs2::create_dir_all(&dist)?;
14
15     if let Some(version) = client_version {
16         let release_tag = if nightly { "nightly".to_string() } else { date_iso()? };
17         dist_client(&version, &release_tag)?;
18     }
19     dist_server(nightly)?;
20     Ok(())
21 }
22
23 fn dist_client(version: &str, release_tag: &str) -> Result<()> {
24     let _d = pushd("./editors/code");
25     let nightly = release_tag == "nightly";
26
27     let mut patch = Patch::new("./package.json")?;
28
29     patch
30         .replace(r#""version": "0.4.0-dev""#, &format!(r#""version": "{}""#, version))
31         .replace(r#""releaseTag": null"#, &format!(r#""releaseTag": "{}""#, release_tag));
32
33     if nightly {
34         patch.replace(
35             r#""displayName": "rust-analyzer""#,
36             r#""displayName": "rust-analyzer (nightly)""#,
37         );
38     }
39     if !nightly {
40         patch.replace(r#""enableProposedApi": true,"#, r#""#);
41     }
42     patch.commit()?;
43
44     run!("npm ci")?;
45     run!("npx vsce package -o ../../dist/rust-analyzer.vsix")?;
46     Ok(())
47 }
48
49 fn dist_server(nightly: bool) -> Result<()> {
50     if cfg!(target_os = "linux") {
51         std::env::set_var("CC", "clang");
52         run!(
53             "cargo build --manifest-path ./crates/rust-analyzer/Cargo.toml --bin rust-analyzer --release
54              --target x86_64-unknown-linux-musl
55             "
56             // We'd want to add, but that requires setting the right linker somehow
57             // --features=jemalloc
58         )?;
59         if !nightly {
60             run!("strip ./target/x86_64-unknown-linux-musl/release/rust-analyzer")?;
61         }
62     } else {
63         run!("cargo build --manifest-path ./crates/rust-analyzer/Cargo.toml --bin rust-analyzer --release")?;
64     }
65
66     let (src, dst) = if cfg!(target_os = "linux") {
67         ("./target/x86_64-unknown-linux-musl/release/rust-analyzer", "./dist/rust-analyzer-linux")
68     } else if cfg!(target_os = "windows") {
69         ("./target/release/rust-analyzer.exe", "./dist/rust-analyzer-windows.exe")
70     } else if cfg!(target_os = "macos") {
71         ("./target/release/rust-analyzer", "./dist/rust-analyzer-mac")
72     } else {
73         panic!("Unsupported OS")
74     };
75
76     fs2::copy(src, dst)?;
77
78     Ok(())
79 }
80
81 struct Patch {
82     path: PathBuf,
83     original_contents: String,
84     contents: String,
85 }
86
87 impl Patch {
88     fn new(path: impl Into<PathBuf>) -> Result<Patch> {
89         let path = path.into();
90         let contents = fs2::read_to_string(&path)?;
91         Ok(Patch { path, original_contents: contents.clone(), contents })
92     }
93
94     fn replace(&mut self, from: &str, to: &str) -> &mut Patch {
95         assert!(self.contents.contains(from));
96         self.contents = self.contents.replace(from, to);
97         self
98     }
99
100     fn commit(&self) -> Result<()> {
101         fs2::write(&self.path, &self.contents)
102     }
103 }
104
105 impl Drop for Patch {
106     fn drop(&mut self) {
107         fs2::write(&self.path, &self.original_contents).unwrap();
108     }
109 }