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
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

View File

@ -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

113
build.rs
View File

@ -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");
}