build: integrate webpack building into build.rs

This commit is contained in:
liushuyu 2019-06-25 14:01:11 +08:00
parent 6c19b8b0d1
commit 27d0a05ade
3 changed files with 39 additions and 88 deletions

View File

@ -11,8 +11,8 @@ matrix:
- os: osx - os: osx
language: rust language: rust
osx_image: xcode10 osx_image: xcode10
script: cargo build script: brew install yarn && cargo build
- os: windows - os: windows
language: rust language: rust
script: cargo build script: choco install nodejs yarn && cargo build

View File

@ -1,7 +1,15 @@
#!/usr/bin/env bash #!/usr/bin/env bash
cd /liftinstall || exit 1 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 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 cargo build

113
build.rs
View File

@ -1,5 +1,3 @@
extern crate walkdir;
#[cfg(windows)] #[cfg(windows)]
extern crate winres; extern crate winres;
@ -11,24 +9,17 @@ extern crate serde;
extern crate serde_derive; extern crate serde_derive;
extern crate toml; extern crate toml;
use walkdir::WalkDir;
use std::env; use std::env;
use std::path::PathBuf; use std::path::PathBuf;
use std::fs::copy; use std::fs::copy;
use std::fs::create_dir_all;
use std::fs::File; use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
use std::io::Read; use std::io::Read;
use std::io::Write; use std::process::Command;
use std::env::consts::OS; use std::env::consts::OS;
const FILES_TO_PREPROCESS: &'static [&'static str] = &["helpers.js", "views.js"];
/// Describes the application itself. /// Describes the application itself.
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct BaseAttributes { pub struct BaseAttributes {
@ -62,6 +53,8 @@ fn handle_binary(_config: &BaseAttributes) {}
fn main() { fn main() {
let output_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); 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(); let os = OS.to_lowercase();
@ -92,80 +85,30 @@ fn main() {
// Copy for the main build // Copy for the main build
copy(&target_config, output_dir.join("bootstrap.toml")).expect("Unable to copy config file"); copy(&target_config, output_dir.join("bootstrap.toml")).expect("Unable to copy config file");
// Copy files from static/ to build dir // Build and deploy frontend files
for entry in WalkDir::new("static") { Command::new("yarn")
let entry = entry.expect("Unable to read output directory"); .arg("--version")
.spawn()
let output_file = output_dir.join(entry.path()); .expect("Please install Yarn");
Command::new("yarn")
if entry.path().is_dir() { .arg("--cwd")
create_dir_all(output_file).expect("Unable to create dir"); .arg(ui_dir.to_str().expect("Unable to covert path"))
} else { .spawn()
let filename = entry .unwrap()
.path() .wait().expect("Unable to install Node.JS dependencies using Yarn");
.file_name() Command::new("yarn")
.expect("Unable to parse filename") .args(&[
"--cwd",
ui_dir.to_str().expect("Unable to covert path"),
"run",
"build",
"--dest",
output_dir
.join("static")
.to_str() .to_str()
.expect("Unable to convert to string"); .expect("Unable to convert path"),
])
if FILES_TO_PREPROCESS.contains(&filename) { .spawn()
// Do basic preprocessing - transcribe template string .unwrap()
let source = BufReader::new(File::open(entry.path()).expect("Unable to copy file")); .wait().expect("Unable to build frontend assets using Webpack");
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");
}
}
}
} }