|
| 1 | +use std::process::Command; |
| 2 | +use std::path::PathBuf; |
| 3 | + |
| 4 | +use clap::ArgMatches; |
| 5 | + |
| 6 | + |
| 7 | +pub fn run(sub_matches: &ArgMatches) -> Result<(), String> { |
| 8 | + let mut src_path = std::env::current_dir().map_err(|e| format!("{}", e))?; |
| 9 | + let build_path = src_path.as_path().join("build"); |
| 10 | + let htldoc_version = crate::utils::htldoc_version(); |
| 11 | + |
| 12 | + // create the build dir |
| 13 | + std::fs::create_dir_all(build_path.as_path()); |
| 14 | + |
| 15 | + |
| 16 | + // copy template files to there |
| 17 | + let template_path_output = Command::new("nix") |
| 18 | + .arg("eval") |
| 19 | + .arg(format!("{}#self.outPath", htldoc_version)) |
| 20 | + .output().expect("failed to get #self.outPath of the htldocVersion in the htldoc.nix") |
| 21 | + ; |
| 22 | + let mut template_path_string = String::from_utf8(template_path_output.stdout).expect("not utf8"); |
| 23 | + if !template_path_output.status.success() { |
| 24 | + println!("{}", String::from_utf8(template_path_output.stderr).unwrap()); |
| 25 | + return Err("failed to get the template_path_output".to_owned()); |
| 26 | + } |
| 27 | + template_path_string.pop(); // remove \n |
| 28 | + template_path_string.pop(); // remove the " at the end |
| 29 | + template_path_string.remove(0); // remove the " at the begining |
| 30 | + let template_path = PathBuf::from(template_path_string); |
| 31 | + println!("src_path: {}", src_path.display()); |
| 32 | + println!("template_path: {}", template_path.display()); |
| 33 | + println!("build_path: {}", build_path.display()); |
| 34 | + |
| 35 | + |
| 36 | + // generate settings.tex from config.nix |
| 37 | + let settings_tex_output = Command::new("nix") |
| 38 | + .arg("eval") |
| 39 | + .arg("--impure") |
| 40 | + .arg("--expr") |
| 41 | + .arg(format!(r#" |
| 42 | + let |
| 43 | + htldocFlake = builtins.getFlake {htldoc_version}; |
| 44 | + pkgs = import htldocFlake.inputs.nixpkgs {{}}; |
| 45 | + lib = pkgs.lib; |
| 46 | + defaultConfig = import {}/diplomarbeit/default-config.nix {{ }}; |
| 47 | + userConfig = import {}/htldoc.nix {{ }}; |
| 48 | + config = defaultConfig // userConfig; |
| 49 | + in import {}/diplomarbeit/latex_template_htlinn/template/settings-tex.nix {{ inherit config lib; }} |
| 50 | + "#, template_path.display(), src_path.display(), template_path.display())) |
| 51 | + .output().expect("failed to eval the $template/diplomarbeit/latex_template_htlinn/template/settings-tex.nix") |
| 52 | + ; |
| 53 | + if !settings_tex_output.status.success() { |
| 54 | + println!("{}", String::from_utf8(settings_tex_output.stderr).unwrap()); |
| 55 | + return Err("failed to eval the $template/diplomarbeit/latex_template_htlinn/template/settings-tex.nix".to_owned()); |
| 56 | + } |
| 57 | + let settings_tex = String::from_utf8(settings_tex_output.stdout).expect("not utf8"); |
| 58 | + println!("settings_tex: {}", settings_tex); |
| 59 | + println!("template_path: {}", template_path.display()); |
| 60 | + println!("htldocVersion: {}", htldoc_version); |
| 61 | + println!(r#" |
| 62 | + let |
| 63 | + defaultConfig = import {}/diplomarbeit/default-config.nix {{ }}; |
| 64 | + userConfig = import {}/htldoc.nix {{ }}; |
| 65 | + config = defaultConfig // userConfig; |
| 66 | + in import {}/diplomarbeit/latex_template_htlinn/template/settings-tex.nix config |
| 67 | + "#, template_path.display(), src_path.display(), template_path.display()); |
| 68 | + std::fs::write(build_path.as_path().join("template").join("settings.tex"), settings_tex).map_err(|e| format!("from IO err: {}", e))?; |
| 69 | + |
| 70 | + |
| 71 | + // run latex build commands |
| 72 | + /* |
| 73 | + let settings_tex = Command::new("nix") |
| 74 | + .arg("eval") |
| 75 | + .arg("--impure") |
| 76 | + .arg("--expr") |
| 77 | + .arg(format!(r#" |
| 78 | + let |
| 79 | + defaultConfig = import {template_path}/diplomarbeit/default-config.nix {}; |
| 80 | + userConfig = import {src_path}/htldoc.nix {}; |
| 81 | + config = defaultConfig // userConfig; |
| 82 | + in import {template_path}/diplomarbeit/latex_template_htlinn/template/settings-tex.nix config |
| 83 | + "#,)) |
| 84 | + .output().expect("failed to eval the $template/diplomarbeit/latex_template_htlinn/template/settings-tex.nix") |
| 85 | + */ |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + Ok(()) |
| 90 | +} |
| 91 | + |
| 92 | + |
| 93 | + |
0 commit comments