From 27d0a05aded88da39841c7de8169c4199563d2ba Mon Sep 17 00:00:00 2001 From: liushuyu Date: Tue, 25 Jun 2019 14:01:11 +0800 Subject: [PATCH] build: integrate webpack building into build.rs --- .travis.yml | 4 +- .travis/build.sh | 10 ++++- build.rs | 113 ++++++++++++----------------------------------- 3 files changed, 39 insertions(+), 88 deletions(-) diff --git a/.travis.yml b/.travis.yml index c291ad6..3b0c45c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,8 +11,8 @@ matrix: - os: osx language: rust osx_image: xcode10 - script: cargo build + script: brew install yarn && cargo build - os: windows language: rust - script: cargo build + script: choco install nodejs yarn && cargo build diff --git a/.travis/build.sh b/.travis/build.sh index fce6adf..92bc3d2 100644 --- a/.travis/build.sh +++ b/.travis/build.sh @@ -1,7 +1,15 @@ #!/usr/bin/env bash cd /liftinstall || exit 1 +# setup NodeJS +curl -sL https://deb.nodesource.com/setup_12.x | bash - +# setup Yarn +curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - +echo "deb https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list + apt-get update -apt-get install -y libwebkit2gtk-4.0-dev libssl-dev +apt-get install -y libwebkit2gtk-4.0-dev libssl-dev nodejs yarn + +yarn --cwd ui cargo build diff --git a/build.rs b/build.rs index 4ba6eec..5dc0817 100644 --- a/build.rs +++ b/build.rs @@ -1,5 +1,3 @@ -extern crate walkdir; - #[cfg(windows)] extern crate winres; @@ -11,24 +9,17 @@ extern crate serde; extern crate serde_derive; extern crate toml; -use walkdir::WalkDir; - use std::env; use std::path::PathBuf; use std::fs::copy; -use std::fs::create_dir_all; use std::fs::File; -use std::io::BufRead; -use std::io::BufReader; use std::io::Read; -use std::io::Write; +use std::process::Command; use std::env::consts::OS; -const FILES_TO_PREPROCESS: &'static [&'static str] = &["helpers.js", "views.js"]; - /// Describes the application itself. #[derive(Debug, Deserialize)] pub struct BaseAttributes { @@ -62,6 +53,8 @@ fn handle_binary(_config: &BaseAttributes) {} fn main() { let output_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); + let current_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); + let ui_dir = current_dir.join("ui"); let os = OS.to_lowercase(); @@ -92,80 +85,30 @@ fn main() { // Copy for the main build copy(&target_config, output_dir.join("bootstrap.toml")).expect("Unable to copy config file"); - // Copy files from static/ to build dir - for entry in WalkDir::new("static") { - let entry = entry.expect("Unable to read output directory"); - - let output_file = output_dir.join(entry.path()); - - if entry.path().is_dir() { - create_dir_all(output_file).expect("Unable to create dir"); - } else { - let filename = entry - .path() - .file_name() - .expect("Unable to parse filename") + // Build and deploy frontend files + Command::new("yarn") + .arg("--version") + .spawn() + .expect("Please install Yarn"); + Command::new("yarn") + .arg("--cwd") + .arg(ui_dir.to_str().expect("Unable to covert path")) + .spawn() + .unwrap() + .wait().expect("Unable to install Node.JS dependencies using Yarn"); + Command::new("yarn") + .args(&[ + "--cwd", + ui_dir.to_str().expect("Unable to covert path"), + "run", + "build", + "--dest", + output_dir + .join("static") .to_str() - .expect("Unable to convert to string"); - - if FILES_TO_PREPROCESS.contains(&filename) { - // Do basic preprocessing - transcribe template string - let source = BufReader::new(File::open(entry.path()).expect("Unable to copy file")); - let mut target = File::create(output_file).expect("Unable to copy file"); - - let mut is_template_string = false; - - for line in source.lines() { - let line = line.expect("Unable to read line from JS file"); - - let mut is_break = false; - let mut is_quote = false; - - let mut output_line = String::new(); - - if is_template_string { - output_line += "\""; - } - - for c in line.chars() { - if c == '\\' { - is_break = true; - output_line.push('\\'); - continue; - } - - if (c == '\"' || c == '\'') && !is_break && !is_template_string { - is_quote = !is_quote; - } - - if c == '`' && !is_break && !is_quote { - output_line += "\""; - is_template_string = !is_template_string; - continue; - } - - if c == '"' && !is_break && is_template_string { - output_line += "\\\""; - continue; - } - - is_break = false; - output_line.push(c); - } - - if is_template_string { - output_line += "\" +"; - } - - output_line.push('\n'); - - target - .write(output_line.as_bytes()) - .expect("Unable to write line"); - } - } else { - copy(entry.path(), output_file).expect("Unable to copy file"); - } - } - } + .expect("Unable to convert path"), + ]) + .spawn() + .unwrap() + .wait().expect("Unable to build frontend assets using Webpack"); }