Skip to main content

luaur_reduce_cli/methods/
reducer_write_temp_script.rs

1use crate::records::reducer::Reducer;
2use alloc::string::String;
3use alloc::vec::Vec;
4use core::ffi::{c_char, c_void};
5use luaur_ast::functions::pretty_print_with_types_pretty_printer::pretty_print_with_types_ast_stat_block_cst_node_map;
6use luaur_ast::records::ast_stat_block::AstStatBlock;
7use std::fs::File;
8use std::io::Write;
9use std::process;
10
11impl Reducer {
12    pub fn write_temp_script(&mut self, minify: bool) {
13        let mut source = pretty_print_with_types_ast_stat_block_cst_node_map(
14            unsafe { &mut *self.root },
15            self.cst_node_map.clone(),
16        );
17
18        if minify {
19            let mut pos = 0;
20            loop {
21                if let Some(found_pos) = source[pos..].find("\n\n") {
22                    source.remove(pos + found_pos);
23                    pos += found_pos;
24                } else {
25                    break;
26                }
27            }
28        }
29
30        let file = File::create(&self.script_name);
31        let mut f = match file {
32            Ok(f) => f,
33            Err(_) => {
34                println!("Unable to open temp script to {}", self.script_name);
35                process::exit(2);
36            }
37        };
38
39        for comment in &self.parse_result.hotcomments {
40            if let Err(_) = writeln!(f, "--!{}", comment.content) {
41                println!("Unable to write to temp script {}", self.script_name);
42                process::exit(3);
43            }
44        }
45
46        if let Err(_) = f.write_all(source.as_bytes()) {
47            println!("Unable to write to temp script {}", self.script_name);
48            process::exit(3);
49        }
50    }
51}