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