rust/xtask/src/dist.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

223 lines
7.4 KiB
Rust
Raw Normal View History

use std::{
env,
fs::File,
2023-01-01 10:52:47 -06:00
io::{self, BufWriter},
path::{Path, PathBuf},
};
2020-03-04 11:36:16 -06:00
2020-10-16 12:46:03 -05:00
use flate2::{write::GzEncoder, Compression};
2023-02-01 11:06:09 -06:00
use time::OffsetDateTime;
2022-03-13 16:20:51 -05:00
use xshell::{cmd, Shell};
2023-01-01 10:52:47 -06:00
use zip::{write::FileOptions, DateTime, ZipWriter};
2020-03-04 11:36:16 -06:00
use crate::{
date_iso,
flags::{self, Malloc},
project_root,
};
2020-03-04 11:36:16 -06:00
2022-05-17 09:02:07 -05:00
const VERSION_STABLE: &str = "0.3";
const VERSION_NIGHTLY: &str = "0.4";
const VERSION_DEV: &str = "0.5"; // keep this one in sync with `package.json`
impl flags::Dist {
2022-03-13 16:20:51 -05:00
pub(crate) fn run(self, sh: &Shell) -> anyhow::Result<()> {
let stable = sh.var("GITHUB_REF").unwrap_or_default().as_str() == "refs/heads/release";
2021-12-17 12:59:02 -06:00
let project_root = project_root();
let target = Target::get(&project_root);
let allocator = self.allocator();
2021-12-17 12:59:02 -06:00
let dist = project_root.join("dist");
2022-03-13 16:20:51 -05:00
sh.remove_path(&dist)?;
sh.create_dir(&dist)?;
2020-07-24 08:59:01 -05:00
if let Some(patch_version) = self.client_patch_version {
let version = if stable {
format!("{VERSION_STABLE}.{patch_version}")
} else {
// A hack to make VS Code prefer nightly over stable.
format!("{VERSION_NIGHTLY}.{patch_version}")
};
dist_server(sh, &format!("{version}-standalone"), &target, allocator)?;
let release_tag = if stable { date_iso(sh)? } else { "nightly".to_owned() };
2022-03-13 16:20:51 -05:00
dist_client(sh, &version, &release_tag, &target)?;
} else {
dist_server(sh, "0.0.0-standalone", &target, allocator)?;
2020-07-24 08:59:01 -05:00
}
Ok(())
2020-03-04 11:36:16 -06:00
}
}
2022-03-13 16:20:51 -05:00
fn dist_client(
sh: &Shell,
version: &str,
release_tag: &str,
target: &Target,
) -> anyhow::Result<()> {
2021-12-17 12:59:02 -06:00
let bundle_path = Path::new("editors").join("code").join("server");
2022-03-13 16:20:51 -05:00
sh.create_dir(&bundle_path)?;
sh.copy_file(&target.server_path, &bundle_path)?;
2021-12-17 12:59:02 -06:00
if let Some(symbols_path) = &target.symbols_path {
2022-03-13 16:20:51 -05:00
sh.copy_file(symbols_path, &bundle_path)?;
2021-12-17 12:59:02 -06:00
}
2022-03-13 16:20:51 -05:00
let _d = sh.push_dir("./editors/code");
2020-03-04 11:36:16 -06:00
2022-03-13 16:20:51 -05:00
let mut patch = Patch::new(sh, "./package.json")?;
2020-03-19 03:32:57 -05:00
patch
2022-05-17 09:02:07 -05:00
.replace(
&format!(r#""version": "{VERSION_DEV}.0-dev""#),
&format!(r#""version": "{version}""#),
2022-05-17 09:02:07 -05:00
)
.replace(r#""releaseTag": null"#, &format!(r#""releaseTag": "{release_tag}""#))
.replace(r#""$generated-start": {},"#, "")
.replace(",\n \"$generated-end\": {}", "")
2022-03-08 00:34:03 -06:00
.replace(r#""enabledApiProposals": [],"#, r#""#);
2022-03-13 16:20:51 -05:00
patch.commit(sh)?;
2020-03-04 11:36:16 -06:00
Ok(())
}
fn dist_server(
sh: &Shell,
release: &str,
target: &Target,
allocator: Malloc,
) -> anyhow::Result<()> {
let _e = sh.push_env("CFG_RELEASE", release);
2022-03-13 16:20:51 -05:00
let _e = sh.push_env("CARGO_PROFILE_RELEASE_LTO", "thin");
2021-05-27 03:53:33 -05:00
// Uncomment to enable debug info for releases. Note that:
// * debug info is split on windows and macs, so it does nothing for those platforms,
// * on Linux, this blows up the binary size from 8MB to 43MB, which is unreasonable.
2022-03-13 16:20:51 -05:00
// let _e = sh.push_env("CARGO_PROFILE_RELEASE_DEBUG", "1");
2021-12-17 12:59:02 -06:00
if target.name.contains("-linux-") {
env::set_var("CC", "clang");
2020-03-04 11:36:16 -06:00
}
2021-12-17 12:59:02 -06:00
let target_name = &target.name;
let features = allocator.to_features();
cmd!(sh, "cargo build --manifest-path ./crates/rust-analyzer/Cargo.toml --bin rust-analyzer --target {target_name} {features...} --release").run()?;
2021-12-17 12:59:02 -06:00
let dst = Path::new("dist").join(&target.artifact_name);
gzip(&target.server_path, &dst.with_extension("gz"))?;
2023-01-01 10:52:47 -06:00
if target_name.contains("-windows-") {
zip(&target.server_path, target.symbols_path.as_ref(), &dst.with_extension("zip"))?;
}
Ok(())
}
2020-03-04 11:36:16 -06:00
2022-03-13 16:20:51 -05:00
fn gzip(src_path: &Path, dest_path: &Path) -> anyhow::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(())
}
2023-01-01 10:52:47 -06:00
fn zip(src_path: &Path, symbols_path: Option<&PathBuf>, dest_path: &Path) -> anyhow::Result<()> {
let file = File::create(dest_path)?;
let mut writer = ZipWriter::new(BufWriter::new(file));
writer.start_file(
src_path.file_name().unwrap().to_str().unwrap(),
FileOptions::default()
.last_modified_time(
2023-02-01 11:06:09 -06:00
DateTime::try_from(OffsetDateTime::from(std::fs::metadata(src_path)?.modified()?))
.unwrap(),
2023-01-01 10:52:47 -06:00
)
.unix_permissions(0o755)
.compression_method(zip::CompressionMethod::Deflated)
.compression_level(Some(9)),
)?;
let mut input = io::BufReader::new(File::open(src_path)?);
io::copy(&mut input, &mut writer)?;
if let Some(symbols_path) = symbols_path {
writer.start_file(
symbols_path.file_name().unwrap().to_str().unwrap(),
FileOptions::default()
.last_modified_time(
2023-02-01 11:06:09 -06:00
DateTime::try_from(OffsetDateTime::from(
std::fs::metadata(src_path)?.modified()?,
))
.unwrap(),
2023-01-01 10:52:47 -06:00
)
.compression_method(zip::CompressionMethod::Deflated)
.compression_level(Some(9)),
)?;
let mut input = io::BufReader::new(File::open(symbols_path)?);
io::copy(&mut input, &mut writer)?;
}
writer.finish()?;
Ok(())
}
2021-12-17 12:59:02 -06:00
struct Target {
name: String,
server_path: PathBuf,
symbols_path: Option<PathBuf>,
artifact_name: String,
}
impl Target {
fn get(project_root: &Path) -> Self {
let name = match env::var("RA_TARGET") {
Ok(target) => target,
_ => {
if cfg!(target_os = "linux") {
"x86_64-unknown-linux-gnu".to_owned()
2021-12-17 12:59:02 -06:00
} else if cfg!(target_os = "windows") {
"x86_64-pc-windows-msvc".to_owned()
2021-12-17 12:59:02 -06:00
} else if cfg!(target_os = "macos") {
"x86_64-apple-darwin".to_owned()
2021-12-17 12:59:02 -06:00
} else {
panic!("Unsupported OS, maybe try setting RA_TARGET")
}
}
};
let out_path = project_root.join("target").join(&name).join("release");
let (exe_suffix, symbols_path) = if name.contains("-windows-") {
(".exe".into(), Some(out_path.join("rust_analyzer.pdb")))
} else {
(String::new(), None)
};
let server_path = out_path.join(format!("rust-analyzer{exe_suffix}"));
let artifact_name = format!("rust-analyzer-{name}{exe_suffix}");
2021-12-17 12:59:02 -06:00
Self { name, server_path, symbols_path, artifact_name }
}
}
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 {
2022-03-13 16:20:51 -05:00
fn new(sh: &Shell, path: impl Into<PathBuf>) -> anyhow::Result<Patch> {
2020-03-19 03:32:57 -05:00
let path = path.into();
2022-03-13 16:20:51 -05:00
let contents = sh.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
}
2022-03-13 16:20:51 -05:00
fn commit(&self, sh: &Shell) -> anyhow::Result<()> {
sh.write_file(&self.path, &self.contents)?;
2020-10-16 12:46:03 -05:00
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) {
2021-12-17 12:59:02 -06:00
// FIXME: find a way to bring this back
let _ = &self.original_contents;
// write_file(&self.path, &self.original_contents).unwrap();
2020-03-04 11:36:16 -06:00
}
}