dalbit_core/
injector.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use std::{
    borrow::Cow,
    collections::HashSet,
    path::{Path, PathBuf},
};

use anyhow::{anyhow, Result};
use full_moon::{
    tokenizer::{Token, TokenType},
    visitors::Visitor,
    LuaVersion,
};
use path_slash::PathBufExt;
use pathdiff::diff_paths;
use tokio::fs;

#[inline]
fn make_relative(path: &PathBuf) -> Cow<Path> {
    if path.starts_with(".") | path.starts_with("..") {
        Cow::Borrowed(path.as_path())
    } else {
        Cow::Owned(Path::new(".").join(path))
    }
}

#[derive(Debug)]
struct CollectUsedLibraries {
    libraries: HashSet<String>,
    used_libraries: HashSet<String>,
}

impl CollectUsedLibraries {
    fn new(libraries: HashSet<String>) -> Self {
        Self {
            libraries,
            used_libraries: HashSet::new(),
        }
    }
}

impl Visitor for CollectUsedLibraries {
    fn visit_identifier(&mut self, identifier: &Token) {
        if let TokenType::Identifier { identifier } = identifier.token_type() {
            let identifier = identifier.to_string();
            if self.libraries.contains(&identifier) {
                self.used_libraries.insert(identifier);
            }
        }
    }
}

/// Injector that injects module's export which is a table constructor.
pub struct Injector {
    module_path: PathBuf,
    exports: HashSet<String>,
    removes: Option<Vec<String>>,
    lua_version: LuaVersion,
}

impl Injector {
    pub fn new(
        module_path: PathBuf,
        exports: HashSet<String>,
        lua_version: LuaVersion,
        removes: Option<Vec<String>>,
    ) -> Self {
        Self {
            module_path,
            exports,
            removes,
            lua_version,
        }
    }

    pub fn module_path(&self) -> &PathBuf {
        &self.module_path
    }

    pub fn removes(&self) -> &Option<Vec<String>> {
        &self.removes
    }

    pub async fn inject(&self, source_path: &PathBuf) -> Result<()> {
        let parent = source_path
            .parent()
            .ok_or(anyhow!("File path must have parent path"))?;
        let require_path = diff_paths(self.module_path(), parent)
            .ok_or(anyhow!("Couldn't resolve the require path"))?
            .with_extension("");
        let require_path = make_relative(&require_path).to_path_buf();

        let code = fs::read_to_string(source_path).await?;

        let mut lines: Vec<String> = code.lines().map(String::from).collect();
        let mut libraries_texts: Vec<String> = Vec::new();

        let ast = full_moon::parse_fallible(code.as_str(), self.lua_version)
            .into_result()
            .map_err(|errors| anyhow!("{:?}", errors))?;

        let mut collect_used_libs = CollectUsedLibraries::new(self.exports.clone());
        collect_used_libs.visit_ast(&ast);

        for lib in collect_used_libs.used_libraries {
            log::debug!("used library: {}", lib);
            libraries_texts.push(format!(
                "local {}=require'{}'.{} ",
                lib,
                require_path.to_slash_lossy(),
                lib
            ));
        }

        if let Some(removes) = self.removes() {
            for lib in removes {
                libraries_texts.push(format!("local {}=nil ", lib));
            }
        }

        let libraries_text = libraries_texts.join("");
        if let Some(first_line) = lines.get_mut(0) {
            first_line.insert_str(0, &libraries_text);
        } else {
            lines.push(libraries_text);
        }

        let new_content = lines.join("\n");

        log::debug!("injected source path: {:?}", source_path);

        fs::write(source_path, new_content).await?;

        Ok(())
    }
}