From fd586e58d97e4ac2d2448426cf6c4937b48c5660 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 4 Mar 2020 17:58:22 +0100 Subject: cargo xtask dist This builds the typescript extension --- xtask/src/install.rs | 6 +----- xtask/src/lib.rs | 41 ++++++++++++++++++++++++++++++++++++++++- xtask/src/main.rs | 14 ++++++++++++-- xtask/src/not_bash.rs | 17 +++++++++++++++-- 4 files changed, 68 insertions(+), 10 deletions(-) (limited to 'xtask') diff --git a/xtask/src/install.rs b/xtask/src/install.rs index f76467cac..d0d745b05 100644 --- a/xtask/src/install.rs +++ b/xtask/src/install.rs @@ -4,10 +4,7 @@ use std::{env, path::PathBuf, str}; use anyhow::{bail, format_err, Context, Result}; -use crate::{ - not_bash::{pushd, run}, - project_root, -}; +use crate::not_bash::{pushd, run}; // Latest stable, feel free to send a PR if this lags behind. const REQUIRED_RUST_VERSION: u32 = 41; @@ -27,7 +24,6 @@ pub struct ServerOpt { impl InstallCmd { pub fn run(self) -> Result<()> { - let _dir = pushd(project_root()); let both = self.server.is_some() && self.client.is_some(); if cfg!(target_os = "macos") { fix_path_for_mac().context("Fix path for mac")? diff --git a/xtask/src/lib.rs b/xtask/src/lib.rs index f48045d17..adbee10ee 100644 --- a/xtask/src/lib.rs +++ b/xtask/src/lib.rs @@ -19,7 +19,7 @@ use std::{ use crate::{ codegen::Mode, - not_bash::{fs2, pushd, rm_rf, run}, + not_bash::{fs2, pushd, pwd, rm_rf, run}, }; pub use anyhow::Result; @@ -206,3 +206,42 @@ Release: release:{}[] fn is_release_tag(tag: &str) -> bool { tag.len() == "2020-02-24".len() && tag.starts_with(|c: char| c.is_ascii_digit()) } + +pub fn run_dist(nightly: bool) -> Result<()> { + let dist = project_root().join("dist"); + rm_rf(&dist)?; + fs2::create_dir_all(&dist)?; + + let _d = pushd("./editors/code"); + + let package_json_path = pwd().join("package.json"); + let original_package_json = fs2::read_to_string(&package_json_path)?; + let _restore = + Restore { path: package_json_path.clone(), contents: original_package_json.clone() }; + + let mut package_json = original_package_json.replace(r#""enableProposedApi": true,"#, r#""#); + + if nightly { + package_json = package_json + .replace(r#""name": "rust-analyzer""#, r#""name": "rust-analyzer-nightly""#) + .replace( + r#""displayName": "rust-analyzer""#, + r#""displayName": "rust-analyzer nightly""#, + ); + } + fs2::write(package_json_path, package_json)?; + + run!("npx vsce package -o {}/rust-analyzer.vsix", dist.display())?; + Ok(()) +} + +struct Restore { + path: PathBuf, + contents: String, +} + +impl Drop for Restore { + fn drop(&mut self) { + fs2::write(&self.path, &self.contents).unwrap(); + } +} diff --git a/xtask/src/main.rs b/xtask/src/main.rs index a7dffe2cc..17a2f1c68 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -14,7 +14,9 @@ use pico_args::Arguments; use xtask::{ codegen::{self, Mode}, install::{ClientOpt, InstallCmd, ServerOpt}, - pre_commit, run_clippy, run_fuzzer, run_pre_cache, run_release, run_rustfmt, Result, + not_bash::pushd, + pre_commit, project_root, run_clippy, run_dist, run_fuzzer, run_pre_cache, run_release, + run_rustfmt, Result, }; fn main() -> Result<()> { @@ -22,6 +24,8 @@ fn main() -> Result<()> { return pre_commit::run_hook(); } + let _d = pushd(project_root()); + let mut args = Arguments::from_env(); let subcommand = args.subcommand()?.unwrap_or_default(); @@ -97,6 +101,11 @@ FLAGS: args.finish()?; run_release(dry_run) } + "dist" => { + let nightly = args.contains("--nightly"); + args.finish()?; + run_dist(nightly) + } _ => { eprintln!( "\ @@ -112,7 +121,8 @@ SUBCOMMANDS: fuzz-tests codegen install - lint" + lint + dist" ); Ok(()) } diff --git a/xtask/src/not_bash.rs b/xtask/src/not_bash.rs index 40f706d9f..1697b7fcd 100644 --- a/xtask/src/not_bash.rs +++ b/xtask/src/not_bash.rs @@ -19,6 +19,11 @@ pub mod fs2 { fs::read_dir(path).with_context(|| format!("Failed to read {}", path.display())) } + pub fn read_to_string>(path: P) -> Result { + let path = path.as_ref(); + fs::read_to_string(path).with_context(|| format!("Failed to read {}", path.display())) + } + pub fn write, C: AsRef<[u8]>>(path: P, contents: C) -> Result<()> { let path = path.as_ref(); fs::write(path, contents).with_context(|| format!("Failed to write {}", path.display())) @@ -40,6 +45,11 @@ pub mod fs2 { let path = path.as_ref(); fs::remove_dir_all(path).with_context(|| format!("Failed to remove dir {}", path.display())) } + + pub fn create_dir_all>(path: P) -> Result<()> { + let path = path.as_ref(); + fs::create_dir_all(path).with_context(|| format!("Failed to create dir {}", path.display())) + } } macro_rules! _run { @@ -61,6 +71,10 @@ pub fn pushd(path: impl Into) -> Pushd { Pushd { _p: () } } +pub fn pwd() -> PathBuf { + Env::with(|env| env.cwd()) +} + impl Drop for Pushd { fn drop(&mut self) { Env::with(|env| env.popd()) @@ -85,7 +99,6 @@ pub fn run_process(cmd: String, echo: bool) -> Result { } fn run_process_inner(cmd: &str, echo: bool) -> Result { - let cwd = Env::with(|env| env.cwd()); let mut args = shelx(cmd); let binary = args.remove(0); @@ -95,7 +108,7 @@ fn run_process_inner(cmd: &str, echo: bool) -> Result { let output = Command::new(binary) .args(args) - .current_dir(cwd) + .current_dir(pwd()) .stdin(Stdio::null()) .stderr(Stdio::inherit()) .output()?; -- cgit v1.2.3