8 use flate2::{write::GzEncoder, Compression};
9 use xshell::{cmd, Shell};
11 use crate::{date_iso, flags, project_root};
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`
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";
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)?;
27 if let Some(patch_version) = self.client_patch_version {
28 let version = if stable {
29 format!("{}.{}", VERSION_STABLE, patch_version)
31 // A hack to make VS Code prefer nightly over stable.
32 format!("{}.{}", VERSION_NIGHTLY, patch_version)
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)?;
38 dist_server(sh, "0.0.0-standalone", &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)?;
57 let _d = sh.push_dir("./editors/code");
59 let mut patch = Patch::new(sh, "./package.json")?;
62 &format!(r#""version": "{}.0-dev""#, VERSION_DEV),
63 &format!(r#""version": "{}""#, version),
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#""#);
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");
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");
83 if target.name.contains("-linux-") {
84 env::set_var("CC", "clang");
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()?;
90 let dst = Path::new("dist").join(&target.artifact_name);
91 gzip(&target.server_path, &dst.with_extension("gz"))?;
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)?;
106 server_path: PathBuf,
107 symbols_path: Option<PathBuf>,
108 artifact_name: String,
112 fn get(project_root: &Path) -> Self {
113 let name = match env::var("RA_TARGET") {
114 Ok(target) => target,
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()
123 panic!("Unsupported OS, maybe try setting RA_TARGET")
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")))
131 (String::new(), None)
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 }
141 original_contents: String,
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 })
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);
158 fn commit(&self, sh: &Shell) -> anyhow::Result<()> {
159 sh.write_file(&self.path, &self.contents)?;
164 impl Drop for Patch {
166 // FIXME: find a way to bring this back
167 let _ = &self.original_contents;
168 // write_file(&self.path, &self.original_contents).unwrap();