]> git.lizzy.rs Git - rust.git/blob - src/tools/rust-analyzer/xtask/src/dist.rs
Rollup merge of #105459 - jyn514:proc-macro-default, r=Mark-Simulacrum
[rust.git] / src / tools / rust-analyzer / xtask / src / dist.rs
1 use std::{
2     env,
3     fs::File,
4     io,
5     path::{Path, PathBuf},
6 };
7
8 use flate2::{write::GzEncoder, Compression};
9 use xshell::{cmd, Shell};
10
11 use crate::{date_iso, flags, project_root};
12
13 const VERSION_STABLE: &str = "0.3";
14 const VERSION_NIGHTLY: &str = "0.4";
15 const VERSION_DEV: &str = "0.5"; // keep this one in sync with `package.json`
16
17 impl flags::Dist {
18     pub(crate) fn run(self, sh: &Shell) -> anyhow::Result<()> {
19         let stable = sh.var("GITHUB_REF").unwrap_or_default().as_str() == "refs/heads/release";
20
21         let project_root = project_root();
22         let target = Target::get(&project_root);
23         let dist = project_root.join("dist");
24         sh.remove_path(&dist)?;
25         sh.create_dir(&dist)?;
26
27         if let Some(patch_version) = self.client_patch_version {
28             let version = if stable {
29                 format!("{}.{}", VERSION_STABLE, patch_version)
30             } else {
31                 // A hack to make VS Code prefer nightly over stable.
32                 format!("{}.{}", VERSION_NIGHTLY, patch_version)
33             };
34             dist_server(sh, &format!("{version}-standalone"), &target)?;
35             let release_tag = if stable { date_iso(sh)? } else { "nightly".to_string() };
36             dist_client(sh, &version, &release_tag, &target)?;
37         } else {
38             dist_server(sh, "0.0.0-standalone", &target)?;
39         }
40         Ok(())
41     }
42 }
43
44 fn dist_client(
45     sh: &Shell,
46     version: &str,
47     release_tag: &str,
48     target: &Target,
49 ) -> anyhow::Result<()> {
50     let bundle_path = Path::new("editors").join("code").join("server");
51     sh.create_dir(&bundle_path)?;
52     sh.copy_file(&target.server_path, &bundle_path)?;
53     if let Some(symbols_path) = &target.symbols_path {
54         sh.copy_file(symbols_path, &bundle_path)?;
55     }
56
57     let _d = sh.push_dir("./editors/code");
58
59     let mut patch = Patch::new(sh, "./package.json")?;
60     patch
61         .replace(
62             &format!(r#""version": "{}.0-dev""#, VERSION_DEV),
63             &format!(r#""version": "{}""#, version),
64         )
65         .replace(r#""releaseTag": null"#, &format!(r#""releaseTag": "{}""#, release_tag))
66         .replace(r#""$generated-start": {},"#, "")
67         .replace(",\n                \"$generated-end\": {}", "")
68         .replace(r#""enabledApiProposals": [],"#, r#""#);
69     patch.commit(sh)?;
70
71     Ok(())
72 }
73
74 fn dist_server(sh: &Shell, release: &str, target: &Target) -> anyhow::Result<()> {
75     let _e = sh.push_env("CFG_RELEASE", release);
76     let _e = sh.push_env("CARGO_PROFILE_RELEASE_LTO", "thin");
77
78     // Uncomment to enable debug info for releases. Note that:
79     //   * debug info is split on windows and macs, so it does nothing for those platforms,
80     //   * on Linux, this blows up the binary size from 8MB to 43MB, which is unreasonable.
81     // let _e = sh.push_env("CARGO_PROFILE_RELEASE_DEBUG", "1");
82
83     if target.name.contains("-linux-") {
84         env::set_var("CC", "clang");
85     }
86
87     let target_name = &target.name;
88     cmd!(sh, "cargo build --manifest-path ./crates/rust-analyzer/Cargo.toml --bin rust-analyzer --target {target_name} --release").run()?;
89
90     let dst = Path::new("dist").join(&target.artifact_name);
91     gzip(&target.server_path, &dst.with_extension("gz"))?;
92
93     Ok(())
94 }
95
96 fn gzip(src_path: &Path, dest_path: &Path) -> anyhow::Result<()> {
97     let mut encoder = GzEncoder::new(File::create(dest_path)?, Compression::best());
98     let mut input = io::BufReader::new(File::open(src_path)?);
99     io::copy(&mut input, &mut encoder)?;
100     encoder.finish()?;
101     Ok(())
102 }
103
104 struct Target {
105     name: String,
106     server_path: PathBuf,
107     symbols_path: Option<PathBuf>,
108     artifact_name: String,
109 }
110
111 impl Target {
112     fn get(project_root: &Path) -> Self {
113         let name = match env::var("RA_TARGET") {
114             Ok(target) => target,
115             _ => {
116                 if cfg!(target_os = "linux") {
117                     "x86_64-unknown-linux-gnu".to_string()
118                 } else if cfg!(target_os = "windows") {
119                     "x86_64-pc-windows-msvc".to_string()
120                 } else if cfg!(target_os = "macos") {
121                     "x86_64-apple-darwin".to_string()
122                 } else {
123                     panic!("Unsupported OS, maybe try setting RA_TARGET")
124                 }
125             }
126         };
127         let out_path = project_root.join("target").join(&name).join("release");
128         let (exe_suffix, symbols_path) = if name.contains("-windows-") {
129             (".exe".into(), Some(out_path.join("rust_analyzer.pdb")))
130         } else {
131             (String::new(), None)
132         };
133         let server_path = out_path.join(format!("rust-analyzer{}", exe_suffix));
134         let artifact_name = format!("rust-analyzer-{}{}", name, exe_suffix);
135         Self { name, server_path, symbols_path, artifact_name }
136     }
137 }
138
139 struct Patch {
140     path: PathBuf,
141     original_contents: String,
142     contents: String,
143 }
144
145 impl Patch {
146     fn new(sh: &Shell, path: impl Into<PathBuf>) -> anyhow::Result<Patch> {
147         let path = path.into();
148         let contents = sh.read_file(&path)?;
149         Ok(Patch { path, original_contents: contents.clone(), contents })
150     }
151
152     fn replace(&mut self, from: &str, to: &str) -> &mut Patch {
153         assert!(self.contents.contains(from));
154         self.contents = self.contents.replace(from, to);
155         self
156     }
157
158     fn commit(&self, sh: &Shell) -> anyhow::Result<()> {
159         sh.write_file(&self.path, &self.contents)?;
160         Ok(())
161     }
162 }
163
164 impl Drop for Patch {
165     fn drop(&mut self) {
166         // FIXME: find a way to bring this back
167         let _ = &self.original_contents;
168         // write_file(&self.path, &self.original_contents).unwrap();
169     }
170 }