Skip to main content

vertigo_cli/build/
build_run.rs

1use std::path::PathBuf;
2
3use crate::commons::{ErrorCode, models::IndexModel};
4
5use super::{
6    build_opts::BuildOpts,
7    cargo_build::run_cargo_build,
8    cargo_workspace::{Workspace, get_workspace},
9    check_env::check_env,
10    find_target::{find_package_rlib_in_target, find_wasm_in_target, profile_name},
11    wasm_opt::run_wasm_opt,
12    wasm_path::WasmPath,
13};
14
15pub fn run(opts: BuildOpts) -> Result<(), ErrorCode> {
16    let ws = match get_workspace() {
17        Ok(ws) => ws,
18        Err(err) => {
19            log::error!("Can't read workspace");
20            return Err(err);
21        }
22    };
23
24    run_with_ws(opts, &ws, false)
25}
26
27pub fn run_with_ws(opts: BuildOpts, ws: &Workspace, allow_error: bool) -> Result<(), ErrorCode> {
28    let package_name = match opts.inner.package_name.as_deref() {
29        Some(name) => name.to_string(),
30        None => match ws.infer_package_name() {
31            Some(name) => {
32                log::info!("Inferred package name = {name}");
33                name
34            }
35            None => {
36                log::error!(
37                    "Can't find vertigo project in {} (no cdylib member)",
38                    ws.get_root_dir()
39                );
40                return Err(ErrorCode::CantFindCdylibMember);
41            }
42        },
43    };
44
45    check_env()?;
46
47    let release = opts.inner.release_mode.unwrap_or(true);
48    let profile = profile_name(release);
49
50    let dest_dir = WasmPath::new(PathBuf::from(&opts.common.dest_dir));
51
52    // Clean destination
53
54    dest_dir.remove_dir_all();
55    dest_dir.create_dir_all();
56
57    // Delete rlibs to re-generate static files
58
59    find_package_rlib_in_target(&package_name, profile).remove_file()?;
60
61    // Run build
62
63    let target_path = match run_cargo_build(
64        &package_name,
65        &opts.get_public_path(),
66        ws,
67        allow_error,
68        release,
69        &opts.inner.cargo_opts,
70    )? {
71        Ok(path) => path,
72        Err(_) => return Err(ErrorCode::BuildFailed),
73    };
74
75    // Get wasm_run.js and index.template.html from vertigo build
76
77    let vertigo_statics_dir = target_path.join("static");
78
79    let mut run_script_content = match std::fs::read(vertigo_statics_dir.join("wasm_run.js")) {
80        Ok(content) => content,
81        Err(err) => {
82            log::error!("Can't read wasm_run from statics directory: {err}");
83            return Err(ErrorCode::CantReadWasmRunFromStatics);
84        }
85    };
86
87    if !opts.inner.wasm_run_source_map {
88        erase_last_two_lines(&mut run_script_content);
89    }
90
91    let run_script_hash_name = opts
92        .new_path_in_static_make(&["wasm_run.js"])
93        .save_with_hash(&run_script_content)?;
94
95    if opts.inner.wasm_run_source_map {
96        let run_script_sourcemap_content =
97            match std::fs::read_to_string(vertigo_statics_dir.join("wasm_run.js.map")) {
98                Ok(content) => {
99                    // Replace original script filename in sourcemap with the hashed one
100                    content.replace("wasm_run.js", &run_script_hash_name)
101                }
102                Err(err) => {
103                    log::error!("Can't read wasm_run sourcemap from statics directory: {err}");
104                    return Err(ErrorCode::CantReadWasmRunFromStatics);
105                }
106            };
107
108        opts.new_path_in_static_make(&["wasm_run.js.map"])
109            .save(&run_script_sourcemap_content.into_bytes())?;
110    }
111
112    // Copy .wasm to destination
113
114    let wasm_path_target = find_wasm_in_target(&package_name, profile);
115    let wasm_path = opts.new_path_in_static_from(&wasm_path_target);
116
117    // Optimize .wasm
118
119    let wasm_path_hash =
120        if opts.inner.wasm_opt.unwrap_or(true) && run_wasm_opt(&wasm_path_target, &wasm_path) {
121            // optimized
122            let wasm_path_hash = wasm_path.save_with_hash(wasm_path.read()?.as_slice())?;
123            wasm_path.remove_file()?;
124            wasm_path_hash
125        } else {
126            // copy without optimization
127            let wasm_content = wasm_path_target.read()?;
128            wasm_path.save_with_hash(wasm_content.as_slice())?
129        };
130
131    // Generate index.json in destination
132
133    let index = IndexModel {
134        run_js: opts.public_path_to(run_script_hash_name),
135        wasm: opts.public_path_to(wasm_path_hash),
136    };
137
138    let index_content = serde_json::to_string_pretty(&index).map_err(|err| {
139        log::error!("Can't serialize index.json: {err}");
140        ErrorCode::CantWriteOrRemoveFile
141    })?;
142    opts.new_path_in_static_make(&["index.json"])
143        .save(index_content.as_bytes())?;
144
145    // Copy statics generated by dom macro invocations
146
147    if let Ok(dir) = std::fs::read_dir(vertigo_statics_dir.join("included")) {
148        for entry in dir {
149            match entry {
150                Ok(entry) => {
151                    let src_file_path = WasmPath::new(entry.path());
152                    let content = src_file_path.read()?;
153                    let dest_file_path = opts.new_path_in_static_from(&src_file_path);
154                    dest_file_path.save(&content)?;
155                }
156                Err(err) => {
157                    log::warn!("Can't read entry: {err}");
158                    return Err(ErrorCode::CantReadStaticFile);
159                }
160            }
161        }
162    }
163
164    Ok(())
165}
166
167fn erase_last_two_lines(content: &mut Vec<u8>) {
168    // Find the positions of the last two newline characters
169    let mut last_newline_pos = None;
170    let mut second_last_newline_pos = None;
171
172    for (i, &byte) in content.iter().enumerate() {
173        if byte == b'\n' {
174            second_last_newline_pos = last_newline_pos; // Update second last
175            last_newline_pos = Some(i); // Update last
176        }
177    }
178
179    // Truncate the Vec<u8> to the position of the second last newline
180    if let Some(second_last) = second_last_newline_pos {
181        content.truncate(second_last);
182    }
183}