rust/xtask/src/dist.rs

166 lines
5.0 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, pushenv, read_file, rm_rf, write_file};
2020-03-04 11:36:16 -06:00
use crate::{date_iso, flags, project_root};
2020-03-04 11:36:16 -06:00
impl flags::Dist {
pub(crate) fn run(self) -> Result<()> {
let stable =
std::env::var("GITHUB_REF").unwrap_or_default().as_str() == "refs/heads/release";
2020-07-24 08:59:01 -05:00
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(patch_version) = self.client_patch_version {
let version = if stable {
format!("0.2.{}", patch_version)
} else {
// A hack to make VS Code prefer nightly over stable.
format!("0.3.{}", patch_version)
};
let release_tag = if stable { date_iso()? } else { "nightly".to_string() };
2020-07-24 08:59:01 -05:00
dist_client(&version, &release_tag)?;
}
let release_channel = if stable { "stable" } else { "nightly" };
dist_server(release_channel)?;
2020-07-24 08:59:01 -05:00
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))
.replace(r#""$generated-start": false,"#, "")
2021-04-24 02:31:58 -05:00
.replace(",\n \"$generated-end\": false", "");
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(release_channel: &str) -> Result<()> {
let _e = pushenv("RUST_ANALYZER_CHANNEL", release_channel);
let target = get_target();
if target.contains("-linux-gnu") || target.contains("-linux-musl") {
env::set_var("CC", "clang");
2020-03-04 11:36:16 -06:00
}
cmd!("cargo build --manifest-path ./crates/rust-analyzer/Cargo.toml --bin rust-analyzer --target {target} --release").run()?;
let suffix = exe_suffix(&target);
let src =
Path::new("target").join(&target).join("release").join(format!("rust-analyzer{}", suffix));
let dst = Path::new("dist").join(format!("rust-analyzer-{}{}", target, suffix));
gzip(&src, &dst.with_extension("gz"))?;
// FIXME: the old names are temporarily kept for client compatibility, but they should be removed
// Remove this block after a couple of releases
match target.as_ref() {
"x86_64-unknown-linux-gnu" => {
cp(&src, "dist/rust-analyzer-linux")?;
gzip(&src, Path::new("dist/rust-analyzer-linux.gz"))?;
}
"x86_64-pc-windows-msvc" => {
cp(&src, "dist/rust-analyzer-windows.exe")?;
gzip(&src, Path::new("dist/rust-analyzer-windows.gz"))?;
}
"x86_64-apple-darwin" => {
cp(&src, "dist/rust-analyzer-mac")?;
gzip(&src, Path::new("dist/rust-analyzer-mac.gz"))?;
}
_ => {}
}
Ok(())
}
2020-03-04 11:36:16 -06:00
fn get_target() -> String {
match env::var("RA_TARGET") {
Ok(target) => target,
_ => {
if cfg!(target_os = "linux") {
"x86_64-unknown-linux-gnu".to_string()
} else if cfg!(target_os = "windows") {
"x86_64-pc-windows-msvc".to_string()
} else if cfg!(target_os = "macos") {
"x86_64-apple-darwin".to_string()
} else {
panic!("Unsupported OS, maybe try setting RA_TARGET")
}
}
}
}
fn exe_suffix(target: &str) -> String {
if target.contains("-windows-") {
".exe".into()
} else {
"".into()
}
}
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
}
}