rust/xtask/src/dist.rs

123 lines
3.2 KiB
Rust
Raw Normal View History

use std::{
env,
fs::File,
io,
path::{Path, PathBuf},
};
2020-03-04 11:36:16 -06:00
use anyhow::Result;
2020-10-16 12:46:03 -05:00
use flate2::{write::GzEncoder, Compression};
use xshell::{cmd, cp, mkdir_p, pushd, read_file, rm_rf, write_file};
2020-03-04 11:36:16 -06:00
2020-10-16 12:46:03 -05:00
use crate::{date_iso, project_root};
2020-03-04 11:36:16 -06:00
2020-07-24 08:59:01 -05:00
pub struct DistCmd {
pub nightly: bool,
pub client_version: Option<String>,
}
impl DistCmd {
pub fn run(self) -> Result<()> {
let dist = project_root().join("dist");
rm_rf(&dist)?;
2020-10-16 12:46:03 -05:00
mkdir_p(&dist)?;
2020-07-24 08:59:01 -05:00
if let Some(version) = self.client_version {
let release_tag = if self.nightly { "nightly".to_string() } else { date_iso()? };
dist_client(&version, &release_tag)?;
}
dist_server()?;
Ok(())
2020-03-04 11:36:16 -06:00
}
}
2020-03-19 03:32:57 -05:00
fn dist_client(version: &str, release_tag: &str) -> Result<()> {
2020-10-16 12:46:03 -05:00
let _d = pushd("./editors/code")?;
2020-03-19 03:32:57 -05:00
let nightly = release_tag == "nightly";
2020-03-04 11:36:16 -06:00
2020-03-19 03:32:57 -05:00
let mut patch = Patch::new("./package.json")?;
2020-03-04 11:36:16 -06:00
2020-03-19 03:32:57 -05: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 11:36:16 -06:00
if nightly {
2020-03-18 07:23:44 -05:00
patch.replace(
r#""displayName": "rust-analyzer""#,
2020-03-18 07:23:44 -05:00
r#""displayName": "rust-analyzer (nightly)""#,
);
2020-03-04 11:36:16 -06:00
}
2020-03-18 07:23:44 -05:00
if !nightly {
patch.replace(r#""enableProposedApi": true,"#, r#""#);
}
patch.commit()?;
2020-03-04 11:36:16 -06:00
2020-10-16 12:46:03 -05:00
cmd!("npm ci").run()?;
cmd!("npx vsce package -o ../../dist/rust-analyzer.vsix").run()?;
2020-03-04 11:36:16 -06:00
Ok(())
}
fn dist_server() -> Result<()> {
2020-03-04 11:36:16 -06:00
if cfg!(target_os = "linux") {
env::set_var("CC", "clang");
2020-03-04 11:36:16 -06:00
}
2020-10-16 12:46:03 -05:00
cmd!("cargo build --manifest-path ./crates/rust-analyzer/Cargo.toml --bin rust-analyzer --release").run()?;
2020-03-04 11:36:16 -06:00
let (src, dst) = if cfg!(target_os = "linux") {
("./target/release/rust-analyzer", "./dist/rust-analyzer-linux")
2020-03-04 11:36:16 -06:00
} else if cfg!(target_os = "windows") {
2020-03-05 04:55:34 -06:00
("./target/release/rust-analyzer.exe", "./dist/rust-analyzer-windows.exe")
2020-03-04 11:36:16 -06:00
} else if cfg!(target_os = "macos") {
2020-03-05 04:55:34 -06:00
("./target/release/rust-analyzer", "./dist/rust-analyzer-mac")
2020-03-04 11:36:16 -06:00
} else {
panic!("Unsupported OS")
};
let src = Path::new(src);
let dst = Path::new(dst);
2020-10-16 12:46:03 -05:00
cp(&src, &dst)?;
gzip(&src, &dst.with_extension("gz"))?;
Ok(())
}
2020-03-04 11:36:16 -06:00
fn gzip(src_path: &Path, dest_path: &Path) -> Result<()> {
let mut encoder = GzEncoder::new(File::create(dest_path)?, Compression::best());
let mut input = io::BufReader::new(File::open(src_path)?);
io::copy(&mut input, &mut encoder)?;
encoder.finish()?;
2020-03-04 11:36:16 -06:00
Ok(())
}
2020-03-18 07:23:44 -05:00
struct Patch {
2020-03-04 11:36:16 -06:00
path: PathBuf,
2020-03-18 07:23:44 -05:00
original_contents: String,
2020-03-04 11:36:16 -06:00
contents: String,
}
2020-03-18 07:23:44 -05:00
impl Patch {
2020-03-19 03:32:57 -05:00
fn new(path: impl Into<PathBuf>) -> Result<Patch> {
let path = path.into();
2020-10-16 12:46:03 -05:00
let contents = read_file(&path)?;
2020-03-18 07:23:44 -05:00
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
}
2020-10-16 12:46:03 -05:00
fn commit(&self) -> Result<()> {
write_file(&self.path, &self.contents)?;
Ok(())
2020-03-18 07:23:44 -05:00
}
}
impl Drop for Patch {
2020-03-04 11:36:16 -06:00
fn drop(&mut self) {
2020-10-16 12:46:03 -05:00
write_file(&self.path, &self.original_contents).unwrap();
2020-03-04 11:36:16 -06:00
}
}