dalbit_core/
polyfill.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
use anyhow::Result;
use anyhow::{anyhow, Context};
use auth_git2::GitAuthenticator;
use blake3;
use dirs;
use fs_err;
use git2::Repository;
use hex;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::io;
use std::path::PathBuf;
use std::str::FromStr;
use tokio::fs;
use url::Url;

use crate::manifest::WritableManifest;
use crate::{utils, TargetVersion};

pub const DEFAULT_REPO_URL: &str = "https://github.com/CavefulGames/dalbit-polyfill";
pub const DEFAULT_INJECTION_PATH: &str = "__polyfill__";

/// Cleans cache from polyfill repository url.
pub async fn clean_cache(url: &Url) -> Result<()> {
    let index_path = index_path(url)?;
    fs::remove_dir_all(index_path).await?;
    Ok(())
}

/// Cleans every caches of polyfill.
pub async fn clean_cache_all() -> Result<()> {
    let path = cache_dir()?;
    fs::remove_dir_all(path).await?;
    Ok(())
}

/// Gets cache directory path of polyfills.
pub fn cache_dir() -> Result<PathBuf> {
    Ok(dirs::cache_dir()
        .ok_or_else(|| anyhow!("could not find cache directory"))?
        .join("dalbit")
        .join("polyfills"))
}

/// Polyfill-related manifest.
#[derive(Debug, Deserialize, Serialize)]
pub struct Polyfill {
    repository: Url,
    #[serde(skip_serializing_if = "HashMap::is_empty")]
    #[serde(default)]
    globals: HashMap<String, bool>,
    #[serde(skip_serializing_if = "HashMap::is_empty")]
    #[serde(default)]
    config: HashMap<String, bool>,
    injection_path: PathBuf,
}

impl Default for Polyfill {
    fn default() -> Self {
        Self {
            repository: Url::from_str(DEFAULT_REPO_URL).unwrap(),
            globals: HashMap::new(),
            config: HashMap::new(),
            injection_path: PathBuf::from_str(DEFAULT_INJECTION_PATH).unwrap(),
        }
    }
}

impl Polyfill {
    pub fn new(repository: Url, injection_path: PathBuf) -> Self {
        Self {
            repository,
            globals: HashMap::new(),
            config: HashMap::new(),
            injection_path,
        }
    }

    /// Loads polyfill cache.
    pub async fn cache(&self) -> Result<PolyfillCache> {
        PolyfillCache::new(&self.repository).await
    }

    #[inline]
    pub fn repository(&self) -> &Url {
        &self.repository
    }

    #[inline]
    pub fn globals(&self) -> &HashMap<String, bool> {
        &self.globals
    }

    #[inline]
    pub fn config(&self) -> &HashMap<String, bool> {
        &self.config
    }

    #[inline]
    pub fn injection_path(&self) -> &PathBuf {
        &self.injection_path
    }
}

/// Polyfill's manifest (`/polyfill.toml` in a polyfill repository)
#[derive(Debug, Deserialize, Serialize)]
pub struct PolyfillManifest {
    globals: PathBuf,
    removes: Option<Vec<String>>,
    config: HashMap<String, bool>,
    lua_version: TargetVersion,
}

impl WritableManifest for PolyfillManifest {}

/// Polyfill's globals.
#[derive(Debug)]
pub struct Globals {
    path: PathBuf,
    exports: HashSet<String>,
}

/// Represents a loaded polyfill cache.
pub struct PolyfillCache {
    repository: Repository,
    path: PathBuf,
    globals: Globals,
    removes: Option<Vec<String>>,
    config: HashMap<String, bool>,
}

fn index_path(url: &Url) -> anyhow::Result<PathBuf> {
    let name = match (url.domain(), url.scheme()) {
        (Some(domain), _) => domain,
        (None, "file") => "local",
        _ => "unknown",
    };

    let hash = blake3::hash(url.to_string().as_bytes());
    let hash_hex = hex::encode(&hash.as_bytes()[..8]);
    let ident = format!("{}-{}", name, hash_hex);

    let path = cache_dir()?.join(ident);

    log::debug!("index path {:?}", path);

    Ok(path)
}

impl PolyfillCache {
    /// Creates a new polyfill from git repository.
    pub async fn new(url: &Url) -> Result<Self> {
        let path = index_path(url)?;
        let repository = match Repository::open(path.as_path()) {
            Ok(repo) => repo,
            Err(_) => {
                if let Err(err) = fs_err::remove_dir_all(path.as_path()) {
                    if err.kind() != io::ErrorKind::NotFound {
                        return Err(err.into());
                    }
                }

                fs_err::create_dir_all(path.as_path())?;
                let auth = GitAuthenticator::new();
                auth.clone_repo(url, &path.as_path())?
            }
        };

        log::info!("repository is ready");

        //let manifest = Manifest::from_file(path.join("polyfill.toml")).await?;
        let manifest_content = fs::read_to_string(path.join("polyfill.toml")).await?;
        let manifest: PolyfillManifest = toml::from_str(&manifest_content)?;

        let globals_path = path.join(&manifest.globals);
        log::debug!("globals path {:?}", globals_path);
        let globals_ast = utils::parse_file(&globals_path, &manifest.lua_version).await?;
        let exports = utils::get_exports_from_last_stmt(&utils::ParseTarget::FullMoonAst(globals_ast))
            .await?
            .ok_or_else(|| anyhow!("Invalid polyfill structure. Polyfills' globals must return at least one global in a table."))?;

        let globals = Globals {
            path: globals_path,
            exports,
        };

        log::info!("polyfill ready");

        Ok(Self {
            path,
            repository,
            globals: globals,
            removes: manifest.removes,
            config: manifest.config,
        })
    }

    /// Fetches and updates polyfill repository using git.
    pub fn fetch(&self) -> Result<()> {
        let mut remote = self.repository.find_remote("origin")?;
        let auth = GitAuthenticator::new();
        auth.fetch(&self.repository, &mut remote, &["main"], None)
            .with_context(|| format!("Could not fetch git repository"))?;

        let mut options = git2::build::CheckoutBuilder::new();
        options.force();

        let commit = self
            .repository
            .find_reference("FETCH_HEAD")?
            .peel_to_commit()?;
        self.repository
            .reset(
                &commit.into_object(),
                git2::ResetType::Hard,
                Some(&mut options),
            )
            .with_context(|| format!("Could not reset git repo to fetch_head"))?;

        Ok(())
    }

    #[inline]
    pub fn path(&self) -> &PathBuf {
        &self.path
    }

    #[inline]
    pub fn globals_path(&self) -> &PathBuf {
        &self.globals.path
    }

    #[inline]
    pub fn globals_exports(&self) -> &HashSet<String> {
        &self.globals.exports
    }

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

    #[inline]
    pub fn config(&self) -> &HashMap<String, bool> {
        &self.config
    }
}