2018-08-03 12:21:34 +00:00
#[ cfg(windows) ]
extern crate winres ;
2018-08-06 10:51:59 +00:00
#[ cfg(windows) ]
extern crate cc ;
2018-08-08 11:19:46 +00:00
extern crate serde ;
#[ macro_use ]
extern crate serde_derive ;
extern crate toml ;
2019-06-26 13:43:24 +00:00
extern crate which ;
2018-08-07 10:17:01 +00:00
use std ::env ;
2022-03-30 07:38:46 +00:00
use std ::io ::Write ;
2018-08-07 10:17:01 +00:00
use std ::path ::PathBuf ;
use std ::fs ::copy ;
use std ::fs ::File ;
2018-08-08 11:19:46 +00:00
use std ::io ::Read ;
2019-06-25 06:01:11 +00:00
use std ::process ::Command ;
2018-08-07 10:17:01 +00:00
2018-08-08 02:47:32 +00:00
use std ::env ::consts ::OS ;
2022-03-30 07:38:46 +00:00
use image ::imageops ::FilterType ;
2018-08-08 11:19:46 +00:00
/// Describes the application itself.
#[ derive(Debug, Deserialize) ]
pub struct BaseAttributes {
pub name : String ,
pub target_url : String ,
}
2018-08-03 12:21:34 +00:00
#[ cfg(windows) ]
2018-08-08 11:19:46 +00:00
fn handle_binary ( config : & BaseAttributes ) {
2018-08-03 12:21:34 +00:00
let mut res = winres ::WindowsResource ::new ( ) ;
2019-06-26 13:43:24 +00:00
res . set_icon ( " ui/public/favicon.ico " ) ;
2018-08-08 11:19:46 +00:00
res . set (
" FileDescription " ,
& format! ( " Interactive installer for {} " , config . name ) ,
) ;
res . set ( " ProductName " , & format! ( " {} installer " , config . name ) ) ;
res . set (
" OriginalFilename " ,
& format! ( " {} _installer.exe " , config . name ) ,
) ;
2018-08-04 07:03:32 +00:00
res . compile ( ) . expect ( " Failed to generate metadata " ) ;
2018-08-06 10:51:59 +00:00
cc ::Build ::new ( )
. cpp ( true )
2020-03-09 18:58:57 +00:00
. define ( " _WIN32_WINNT " , Some ( " 0x0600 " ) )
. define ( " WINVER " , Some ( " 0x0600 " ) )
2018-08-06 10:51:59 +00:00
. file ( " src/native/interop.cpp " )
. compile ( " interop " ) ;
2018-08-03 12:21:34 +00:00
}
#[ cfg(not(windows)) ]
2018-08-09 10:03:26 +00:00
fn handle_binary ( _config : & BaseAttributes ) { }
2018-08-07 10:17:01 +00:00
fn main ( ) {
let output_dir = PathBuf ::from ( env ::var ( " OUT_DIR " ) . unwrap ( ) ) ;
2019-06-25 06:01:11 +00:00
let current_dir = PathBuf ::from ( env ::var ( " CARGO_MANIFEST_DIR " ) . unwrap ( ) ) ;
let ui_dir = current_dir . join ( " ui " ) ;
2018-08-07 10:17:01 +00:00
2018-08-08 02:47:32 +00:00
let os = OS . to_lowercase ( ) ;
2021-10-16 00:47:45 +00:00
#[ cfg(windows) ]
{
if std ::fs ::metadata ( " MicrosoftEdgeWebview2Setup.exe " ) . is_err ( ) {
panic! ( " Please download MicrosoftEdgeWebview2Setup.exe from https://go.microsoft.com/fwlink/p/?LinkId=2124703 and put the file at the workspace root! " ) ;
}
}
2018-08-08 02:47:32 +00:00
// Find target config
2018-09-20 03:37:44 +00:00
let target_config = PathBuf ::from ( format! ( " bootstrap. {} .toml " , os ) ) ;
2018-08-08 02:47:32 +00:00
if ! target_config . exists ( ) {
panic! (
" There is no config file specified for the platform: {:?}. \
2018-09-20 03:37:44 +00:00
Create a file named \ " bootstrap.{}.toml \" in the root directory. " ,
2018-08-08 02:47:32 +00:00
os , os
) ;
}
2018-08-08 11:19:46 +00:00
// Read in the config for our own purposes
let file_contents = {
let mut file = File ::open ( & target_config ) . expect ( " Unable to open config file " ) ;
let mut buf = Vec ::new ( ) ;
file . read_to_end ( & mut buf )
. expect ( " Unable to read config file contents " ) ;
buf
} ;
let config : BaseAttributes =
toml ::from_slice ( & file_contents ) . expect ( " Unable to parse config file " ) ;
handle_binary ( & config ) ;
// Copy for the main build
2018-09-20 03:37:44 +00:00
copy ( & target_config , output_dir . join ( " bootstrap.toml " ) ) . expect ( " Unable to copy config file " ) ;
2018-08-08 02:47:32 +00:00
2019-09-05 17:19:56 +00:00
let yarn_binary =
which ::which ( " yarn " ) . expect ( " Failed to find yarn - please go ahead and install it! " ) ;
2019-06-26 13:43:24 +00:00
2022-03-30 07:38:46 +00:00
// bundle the icon
let mut f = File ::create ( output_dir . join ( " icon-data.bin " ) ) . unwrap ( ) ;
let icon_file = image ::open ( " ui/public/favicon.ico " ) . expect ( " Unable to read the icon file " ) ;
let icon_data = icon_file . resize_exact ( 48 , 48 , FilterType ::Triangle ) . to_rgba8 ( ) ;
f . write_all ( & icon_data . into_vec ( ) ) . unwrap ( ) ;
2019-06-25 06:01:11 +00:00
// Build and deploy frontend files
2019-06-26 13:43:24 +00:00
Command ::new ( & yarn_binary )
2019-06-25 06:01:11 +00:00
. arg ( " --version " )
. spawn ( )
2019-06-26 13:43:24 +00:00
. expect ( " Yarn could not be launched " ) ;
Command ::new ( & yarn_binary )
2019-06-25 06:01:11 +00:00
. arg ( " --cwd " )
. arg ( ui_dir . to_str ( ) . expect ( " Unable to covert path " ) )
. spawn ( )
. unwrap ( )
2019-09-05 17:19:56 +00:00
. wait ( )
. expect ( " Unable to install Node.JS dependencies using Yarn " ) ;
2019-07-19 06:26:06 +00:00
let return_code = Command ::new ( & yarn_binary )
2019-06-25 06:01:11 +00:00
. args ( & [
" --cwd " ,
ui_dir . to_str ( ) . expect ( " Unable to covert path " ) ,
" run " ,
" build " ,
" --dest " ,
output_dir
. join ( " static " )
2018-08-07 10:17:01 +00:00
. to_str ( )
2019-06-25 06:01:11 +00:00
. expect ( " Unable to convert path " ) ,
] )
2019-07-19 06:26:06 +00:00
. status ( )
. expect ( " Unable to build frontend assets using Webpack " ) ;
assert! ( return_code . success ( ) ) ;
2018-08-07 10:17:01 +00:00
}