rust-analyzer/xtask/src/dist.rs

110 lines
3.1 KiB
Rust
Raw Normal View History

2020-03-04 17:36:16 +00:00
use std::path::PathBuf;
use anyhow::Result;
use crate::{
2020-04-08 09:47:40 +00:00
not_bash::{date_iso, fs2, pushd, rm_rf, run},
2020-03-04 17:36:16 +00:00
project_root,
};
2020-04-08 09:47:40 +00:00
pub fn run_dist(nightly: bool, client_version: Option<String>) -> Result<()> {
2020-03-04 17:36:16 +00:00
let dist = project_root().join("dist");
rm_rf(&dist)?;
fs2::create_dir_all(&dist)?;
2020-04-08 09:47:40 +00:00
if let Some(version) = client_version {
let release_tag = if nightly { "nightly".to_string() } else { date_iso()? };
2020-03-23 13:33:44 +00:00
dist_client(&version, &release_tag)?;
2020-03-04 17:36:16 +00:00
}
2020-04-08 09:47:40 +00:00
dist_server(nightly)?;
2020-03-04 17:36:16 +00:00
Ok(())
}
2020-03-19 08:32:57 +00:00
fn dist_client(version: &str, release_tag: &str) -> Result<()> {
2020-03-04 17:36:16 +00:00
let _d = pushd("./editors/code");
2020-03-19 08:32:57 +00:00
let nightly = release_tag == "nightly";
2020-03-04 17:36:16 +00:00
2020-03-19 08:32:57 +00:00
let mut patch = Patch::new("./package.json")?;
2020-03-04 17:36:16 +00:00
2020-03-19 08:32:57 +00:00
patch
.replace(r#""version": "0.4.0-dev""#, &format!(r#""version": "{}""#, version))
.replace(r#""releaseTag": null"#, &format!(r#""releaseTag": "{}""#, release_tag));
2020-03-04 17:36:16 +00:00
if nightly {
2020-03-18 12:23:44 +00:00
patch.replace(
r#""displayName": "rust-analyzer""#,
2020-03-18 12:23:44 +00:00
r#""displayName": "rust-analyzer (nightly)""#,
);
2020-03-04 17:36:16 +00:00
}
2020-03-18 12:23:44 +00:00
if !nightly {
patch.replace(r#""enableProposedApi": true,"#, r#""#);
}
patch.commit()?;
2020-03-04 17:36:16 +00:00
2020-03-05 10:11:47 +00:00
run!("npm ci")?;
2020-03-04 17:36:16 +00:00
run!("npx vsce package -o ../../dist/rust-analyzer.vsix")?;
Ok(())
}
2020-04-08 09:47:40 +00:00
fn dist_server(nightly: bool) -> Result<()> {
2020-03-04 17:36:16 +00:00
if cfg!(target_os = "linux") {
std::env::set_var("CC", "clang");
2020-03-09 11:35:31 +00:00
run!(
"cargo build --manifest-path ./crates/rust-analyzer/Cargo.toml --bin rust-analyzer --release
--target x86_64-unknown-linux-musl
"
// We'd want to add, but that requires setting the right linker somehow
// --features=jemalloc
2020-03-09 11:35:31 +00:00
)?;
2020-04-08 09:47:40 +00:00
if !nightly {
run!("strip ./target/x86_64-unknown-linux-musl/release/rust-analyzer")?;
}
2020-03-04 17:36:16 +00:00
} else {
2020-03-09 11:35:31 +00:00
run!("cargo build --manifest-path ./crates/rust-analyzer/Cargo.toml --bin rust-analyzer --release")?;
2020-03-04 17:36:16 +00:00
}
let (src, dst) = if cfg!(target_os = "linux") {
("./target/x86_64-unknown-linux-musl/release/rust-analyzer", "./dist/rust-analyzer-linux")
} else if cfg!(target_os = "windows") {
2020-03-05 10:55:34 +00:00
("./target/release/rust-analyzer.exe", "./dist/rust-analyzer-windows.exe")
2020-03-04 17:36:16 +00:00
} else if cfg!(target_os = "macos") {
2020-03-05 10:55:34 +00:00
("./target/release/rust-analyzer", "./dist/rust-analyzer-mac")
2020-03-04 17:36:16 +00:00
} else {
panic!("Unsupported OS")
};
fs2::copy(src, dst)?;
Ok(())
}
2020-03-18 12:23:44 +00:00
struct Patch {
2020-03-04 17:36:16 +00:00
path: PathBuf,
2020-03-18 12:23:44 +00:00
original_contents: String,
2020-03-04 17:36:16 +00:00
contents: String,
}
2020-03-18 12:23:44 +00:00
impl Patch {
2020-03-19 08:32:57 +00:00
fn new(path: impl Into<PathBuf>) -> Result<Patch> {
let path = path.into();
2020-03-18 12:23:44 +00:00
let contents = fs2::read_to_string(&path)?;
Ok(Patch { path, original_contents: contents.clone(), contents })
}
fn replace(&mut self, from: &str, to: &str) -> &mut Patch {
assert!(self.contents.contains(from));
self.contents = self.contents.replace(from, to);
self
}
fn commit(&self) -> Result<()> {
fs2::write(&self.path, &self.contents)
}
}
impl Drop for Patch {
2020-03-04 17:36:16 +00:00
fn drop(&mut self) {
2020-03-18 12:23:44 +00:00
fs2::write(&self.path, &self.original_contents).unwrap();
2020-03-04 17:36:16 +00:00
}
}