|
| 1 | +use crate::util::errors::LingoError; |
| 2 | +use crate::util::execute_command_to_build_result; |
| 3 | +use std::error::Error; |
| 4 | +use std::fs; |
| 5 | +use std::path::{Path, PathBuf}; |
| 6 | +use std::process::Command; |
| 7 | + |
| 8 | +pub struct Npm; |
| 9 | + |
| 10 | +pub struct TypeScriptToolCommands { |
| 11 | + pub binary_name: &'static str, |
| 12 | + pub install_command: &'static str, |
| 13 | + pub release_build_argument: &'static str, |
| 14 | +} |
| 15 | + |
| 16 | +use crate::backends::{ |
| 17 | + BatchBackend, BatchBuildResults, BuildCommandOptions, BuildProfile, CommandSpec, |
| 18 | +}; |
| 19 | + |
| 20 | +pub fn do_typescript_build( |
| 21 | + results: &mut BatchBuildResults, |
| 22 | + options: &BuildCommandOptions, |
| 23 | + commands: TypeScriptToolCommands, |
| 24 | +) { |
| 25 | + super::lfc::LFC::do_parallel_lfc_codegen(options, results, false); |
| 26 | + if !options.compile_target_code { |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + let extract_name = |path: &PathBuf| -> Result<String, Box<dyn Error + Send + Sync>> { |
| 31 | + match Path::new(&path).file_stem() { |
| 32 | + Some(value) => value |
| 33 | + .to_str() |
| 34 | + .map(String::from) |
| 35 | + .ok_or(Box::new(LingoError::InvalidMainReactor)), |
| 36 | + None => Err(Box::new(LingoError::InvalidMainReactor)), |
| 37 | + } |
| 38 | + }; |
| 39 | + |
| 40 | + results |
| 41 | + .map(|app| { |
| 42 | + let file_name = extract_name(&app.main_reactor)?; |
| 43 | + let path = app.output_root.join("src-gen").join(file_name); |
| 44 | + |
| 45 | + let mut npm_install = Command::new(commands.binary_name); |
| 46 | + npm_install.current_dir(path); |
| 47 | + npm_install.arg(commands.install_command); |
| 48 | + if options.profile == BuildProfile::Release { |
| 49 | + npm_install.arg(commands.release_build_argument); |
| 50 | + } |
| 51 | + execute_command_to_build_result(npm_install)?; |
| 52 | + Ok(()) |
| 53 | + }) |
| 54 | + .map(|app| { |
| 55 | + let file_name = extract_name(&app.main_reactor)?; |
| 56 | + let path = app.output_root.join("src-gen").join(file_name); |
| 57 | + |
| 58 | + let mut npm_build = Command::new(commands.binary_name); |
| 59 | + npm_build.current_dir(path); |
| 60 | + npm_build.arg("run"); |
| 61 | + npm_build.arg("build"); |
| 62 | + |
| 63 | + if options.profile == BuildProfile::Release { |
| 64 | + npm_build.arg(commands.release_build_argument); |
| 65 | + } |
| 66 | + execute_command_to_build_result(npm_build)?; |
| 67 | + |
| 68 | + Ok(()) |
| 69 | + }) |
| 70 | + .map(|app| { |
| 71 | + fs::create_dir_all(app.output_root.join("bin"))?; |
| 72 | + |
| 73 | + let file_name = extract_name(&app.main_reactor)?; |
| 74 | + let path = app |
| 75 | + .output_root |
| 76 | + .join("src-gen") |
| 77 | + .join(&file_name) |
| 78 | + .join("dist") |
| 79 | + .join(file_name + ".js"); |
| 80 | + |
| 81 | + // cleanup: rename executable to match the app name |
| 82 | + fs::rename(path, app.executable_path())?; |
| 83 | + Ok(()) |
| 84 | + }); |
| 85 | +} |
| 86 | + |
| 87 | +impl BatchBackend for Npm { |
| 88 | + fn execute_command(&mut self, command: &CommandSpec, results: &mut BatchBuildResults) { |
| 89 | + match command { |
| 90 | + CommandSpec::Build(options) => do_typescript_build( |
| 91 | + results, |
| 92 | + options, |
| 93 | + TypeScriptToolCommands { |
| 94 | + binary_name: "npm", |
| 95 | + install_command: "install", |
| 96 | + release_build_argument: "--production", |
| 97 | + }, |
| 98 | + ), |
| 99 | + CommandSpec::Clean => { |
| 100 | + results.par_map(|app| { |
| 101 | + crate::util::default_build_clean(&app.output_root)?; |
| 102 | + crate::util::delete_subdirs(&app.output_root, &["node_modules", "dist"])?; |
| 103 | + Ok(()) |
| 104 | + }); |
| 105 | + } |
| 106 | + _ => todo!(), |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments