9 use flate2::{write::GzEncoder, Compression};
10 use xshell::{cmd, cp, mkdir_p, pushd, pushenv, read_file, rm_rf, write_file};
12 use crate::{date_iso, flags, project_root};
15 pub(crate) fn run(self) -> Result<()> {
17 std::env::var("GITHUB_REF").unwrap_or_default().as_str() == "refs/heads/release";
19 let dist = project_root().join("dist");
23 if let Some(patch_version) = self.client_patch_version {
24 let version = if stable {
25 format!("0.2.{}", patch_version)
27 // A hack to make VS Code prefer nightly over stable.
28 format!("0.3.{}", patch_version)
30 let release_tag = if stable { date_iso()? } else { "nightly".to_string() };
31 dist_client(&version, &release_tag)?;
33 let release_channel = if stable { "stable" } else { "nightly" };
34 dist_server(release_channel)?;
39 fn dist_client(version: &str, release_tag: &str) -> Result<()> {
40 let _d = pushd("./editors/code")?;
41 let nightly = release_tag == "nightly";
43 let mut patch = Patch::new("./package.json")?;
46 .replace(r#""version": "0.4.0-dev""#, &format!(r#""version": "{}""#, version))
47 .replace(r#""releaseTag": null"#, &format!(r#""releaseTag": "{}""#, release_tag))
48 .replace(r#""$generated-start": false,"#, "")
49 .replace(",\n \"$generated-end\": false", "");
53 r#""displayName": "rust-analyzer""#,
54 r#""displayName": "rust-analyzer (nightly)""#,
58 patch.replace(r#""enableProposedApi": true,"#, r#""#);
62 cmd!("npm ci").run()?;
63 cmd!("npx vsce package -o ../../dist/rust-analyzer.vsix").run()?;
67 fn dist_server(release_channel: &str) -> Result<()> {
68 let _e = pushenv("RUST_ANALYZER_CHANNEL", release_channel);
69 let _e = pushenv("CARGO_PROFILE_RELEASE_LTO", "true");
70 let _e = pushenv("CARGO_PROFILE_RELEASE_DEBUG", "1");
71 let target = get_target();
72 if target.contains("-linux-gnu") || target.contains("-linux-musl") {
73 env::set_var("CC", "clang");
76 cmd!("cargo build --manifest-path ./crates/rust-analyzer/Cargo.toml --bin rust-analyzer --target {target} --release").run()?;
78 let suffix = exe_suffix(&target);
80 Path::new("target").join(&target).join("release").join(format!("rust-analyzer{}", suffix));
81 let dst = Path::new("dist").join(format!("rust-analyzer-{}{}", target, suffix));
82 gzip(&src, &dst.with_extension("gz"))?;
84 // FIXME: the old names are temporarily kept for client compatibility, but they should be removed
85 // Remove this block after a couple of releases
86 match target.as_ref() {
87 "x86_64-unknown-linux-gnu" => {
88 cp(&src, "dist/rust-analyzer-linux")?;
89 gzip(&src, Path::new("dist/rust-analyzer-linux.gz"))?;
91 "x86_64-pc-windows-msvc" => {
92 cp(&src, "dist/rust-analyzer-windows.exe")?;
93 gzip(&src, Path::new("dist/rust-analyzer-windows.gz"))?;
95 "x86_64-apple-darwin" => {
96 cp(&src, "dist/rust-analyzer-mac")?;
97 gzip(&src, Path::new("dist/rust-analyzer-mac.gz"))?;
105 fn get_target() -> String {
106 match env::var("RA_TARGET") {
107 Ok(target) => target,
109 if cfg!(target_os = "linux") {
110 "x86_64-unknown-linux-gnu".to_string()
111 } else if cfg!(target_os = "windows") {
112 "x86_64-pc-windows-msvc".to_string()
113 } else if cfg!(target_os = "macos") {
114 "x86_64-apple-darwin".to_string()
116 panic!("Unsupported OS, maybe try setting RA_TARGET")
122 fn exe_suffix(target: &str) -> String {
123 if target.contains("-windows-") {
130 fn gzip(src_path: &Path, dest_path: &Path) -> Result<()> {
131 let mut encoder = GzEncoder::new(File::create(dest_path)?, Compression::best());
132 let mut input = io::BufReader::new(File::open(src_path)?);
133 io::copy(&mut input, &mut encoder)?;
140 original_contents: String,
145 fn new(path: impl Into<PathBuf>) -> Result<Patch> {
146 let path = path.into();
147 let contents = read_file(&path)?;
148 Ok(Patch { path, original_contents: contents.clone(), contents })
151 fn replace(&mut self, from: &str, to: &str) -> &mut Patch {
152 assert!(self.contents.contains(from));
153 self.contents = self.contents.replace(from, to);
157 fn commit(&self) -> Result<()> {
158 write_file(&self.path, &self.contents)?;
163 impl Drop for Patch {
165 write_file(&self.path, &self.original_contents).unwrap();