tinymist_project/
lock.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#![allow(missing_docs)]

use std::cmp::Ordering;
use std::io::{Read, Seek, SeekFrom, Write};
use std::{path::Path, sync::Arc};

use ecow::{eco_vec, EcoVec};
use tinymist_std::error::prelude::*;
use tinymist_std::path::unix_slash;
use tinymist_std::{bail, ImmutPath};
use typst::diag::EcoString;
use typst::World;

use crate::model::{ApplyProjectTask, Id, ProjectInput, ProjectRoute, ResourcePath};
use crate::{LockFile, LockFileCompat, LspWorld, ProjectPathMaterial, LOCK_VERSION};

pub const LOCK_FILENAME: &str = "tinymist.lock";

impl LockFile {
    pub fn get_document(&self, id: &Id) -> Option<&ProjectInput> {
        self.document.iter().find(|i| &i.id == id)
    }

    pub fn get_task(&self, id: &Id) -> Option<&ApplyProjectTask> {
        self.task.iter().find(|i| &i.id == id)
    }

    pub fn replace_document(&mut self, input: ProjectInput) {
        let id = input.id.clone();
        let index = self.document.iter().position(|i| i.id == id);
        if let Some(index) = index {
            self.document[index] = input;
        } else {
            self.document.push(input);
        }
    }

    pub fn replace_task(&mut self, task: ApplyProjectTask) {
        let id = task.id().clone();
        let index = self.task.iter().position(|i| *i.id() == id);
        if let Some(index) = index {
            self.task[index] = task;
        } else {
            self.task.push(task);
        }
    }

    pub fn replace_route(&mut self, route: ProjectRoute) {
        let id = route.id.clone();

        self.route.retain(|i| i.id != id);
        self.route.push(route);
    }

    pub fn sort(&mut self) {
        self.document.sort_by(|a, b| a.id.cmp(&b.id));
        self.task
            .sort_by(|a, b| a.doc_id().cmp(b.doc_id()).then_with(|| a.id().cmp(b.id())));
        // the route's order is important, so we don't sort them.
    }

    pub fn serialize_resolve(&self) -> String {
        let content = toml::Table::try_from(self).unwrap();

        let mut out = String::new();

        // At the start of the file we notify the reader that the file is generated.
        // Specifically Phabricator ignores files containing "@generated", so we use
        // that.
        let marker_line = "# This file is automatically @generated by tinymist.";
        let extra_line = "# It is not intended for manual editing.";

        out.push_str(marker_line);
        out.push('\n');
        out.push_str(extra_line);
        out.push('\n');

        out.push_str(&format!("version = {LOCK_VERSION:?}\n"));

        let document = content.get("document");
        if let Some(document) = document {
            for document in document.as_array().unwrap() {
                out.push('\n');
                out.push_str("[[document]]\n");
                emit_document(document, &mut out);
            }
        }

        let route = content.get("route");
        if let Some(route) = route {
            for route in route.as_array().unwrap() {
                out.push('\n');
                out.push_str("[[route]]\n");
                emit_route(route, &mut out);
            }
        }

        let task = content.get("task");
        if let Some(task) = task {
            for task in task.as_array().unwrap() {
                out.push('\n');
                out.push_str("[[task]]\n");
                emit_output(task, &mut out);
            }
        }

        return out;

        fn emit_document(input: &toml::Value, out: &mut String) {
            let table = input.as_table().unwrap();
            out.push_str(&table.to_string());
        }

        fn emit_output(output: &toml::Value, out: &mut String) {
            let mut table = output.clone();
            let table = table.as_table_mut().unwrap();
            // replace transform with task.transforms
            if let Some(transform) = table.remove("transform") {
                let mut task_table = toml::Table::new();
                task_table.insert("transform".to_string(), transform);

                table.insert("task".to_string(), task_table.into());
            }

            out.push_str(&table.to_string());
        }

        fn emit_route(route: &toml::Value, out: &mut String) {
            let table = route.as_table().unwrap();
            out.push_str(&table.to_string());
        }
    }

    pub fn update(cwd: &Path, f: impl FnOnce(&mut Self) -> Result<()>) -> Result<()> {
        let fs = tinymist_std::fs::flock::Filesystem::new(cwd.to_owned());

        let mut lock_file = fs
            .open_rw_exclusive_create(LOCK_FILENAME, "project commands")
            .context("tinymist.lock")?;

        let mut data = vec![];
        lock_file.read_to_end(&mut data).context("read lock")?;

        let old_data =
            std::str::from_utf8(&data).context("tinymist.lock file is not valid utf-8")?;

        let mut state = if old_data.trim().is_empty() {
            LockFile {
                document: vec![],
                task: vec![],
                route: eco_vec![],
            }
        } else {
            let old_state = toml::from_str::<LockFileCompat>(old_data)
                .context_ut("tinymist.lock file is not a valid TOML file")?;

            let version = old_state.version()?;
            match Version(version).partial_cmp(&Version(LOCK_VERSION)) {
                Some(Ordering::Equal | Ordering::Less) => {}
                Some(Ordering::Greater) => {
                    bail!(
                    "trying to update lock file having a future version, current tinymist-cli supports {LOCK_VERSION}, the lock file is {version}",
                );
                }
                None => {
                    bail!(
                    "cannot compare version, are version strings in right format? current tinymist-cli supports {LOCK_VERSION}, the lock file is {version}",
                );
                }
            }

            old_state.migrate()?
        };

        f(&mut state)?;

        // todo: for read only operations, we don't have to compare it.
        state.sort();
        let new_data = state.serialize_resolve();

        // If the lock file contents haven't changed so don't rewrite it. This is
        // helpful on read-only filesystems.
        if old_data == new_data {
            return Ok(());
        }

        // todo: even if cargo, they don't update the lock file atomically. This
        // indicates that we may get data corruption if the process is killed
        // while writing the lock file. This is sensible because `Cargo.lock` is
        // only a "resolved result" of the `Cargo.toml`. Thus, we should inform
        // users that don't only persist configuration in the lock file.
        lock_file.file().set_len(0).context(LOCK_FILENAME)?;
        lock_file.seek(SeekFrom::Start(0)).context(LOCK_FILENAME)?;
        lock_file
            .write_all(new_data.as_bytes())
            .context(LOCK_FILENAME)?;

        Ok(())
    }

    pub fn read(dir: &Path) -> Result<Self> {
        let fs = tinymist_std::fs::flock::Filesystem::new(dir.to_owned());

        let mut lock_file = fs
            .open_ro_shared(LOCK_FILENAME, "project commands")
            .context(LOCK_FILENAME)?;

        let mut data = vec![];
        lock_file.read_to_end(&mut data).context(LOCK_FILENAME)?;

        let data = std::str::from_utf8(&data).context("tinymist.lock file is not valid utf-8")?;

        let state = toml::from_str::<LockFileCompat>(data)
            .context_ut("tinymist.lock file is not a valid TOML file")?;

        state.migrate()
    }
}

/// Make a new project lock updater.
pub fn update_lock(root: ImmutPath) -> LockFileUpdate {
    LockFileUpdate {
        root,
        updates: vec![],
    }
}

enum LockUpdate {
    Input(ProjectInput),
    Task(ApplyProjectTask),
    Material(ProjectPathMaterial),
    Route(ProjectRoute),
}

pub struct LockFileUpdate {
    root: Arc<Path>,
    updates: Vec<LockUpdate>,
}

impl LockFileUpdate {
    pub fn compiled(&mut self, world: &LspWorld) -> Option<Id> {
        let id = Id::from_world(world)?;

        let root = ResourcePath::from_user_sys(Path::new("."));
        let main = ResourcePath::from_user_sys(world.path_for_id(world.main()).ok()?.as_path());

        let font_resolver = &world.font_resolver;
        let font_paths = font_resolver
            .font_paths()
            .iter()
            .map(|p| ResourcePath::from_user_sys(p))
            .collect::<Vec<_>>();

        // let system_font = font_resolver.system_font();

        let registry = &world.registry;
        let package_path = registry
            .package_path()
            .map(|p| ResourcePath::from_user_sys(p));
        let package_cache_path = registry
            .package_cache_path()
            .map(|p| ResourcePath::from_user_sys(p));

        // todo: freeze the package paths
        let _ = package_cache_path;
        let _ = package_path;

        // todo: freeze the sys.inputs

        let input = ProjectInput {
            id: id.clone(),
            root: Some(root),
            main,
            inputs: vec![],
            font_paths,
            system_fonts: true, // !args.font.ignore_system_fonts,
            package_path: None,
            package_cache_path: None,
        };

        self.updates.push(LockUpdate::Input(input));

        Some(id)
    }

    pub fn task(&mut self, task: ApplyProjectTask) {
        self.updates.push(LockUpdate::Task(task));
    }

    pub fn update_materials(&mut self, doc_id: Id, files: EcoVec<ImmutPath>) {
        self.updates
            .push(LockUpdate::Material(ProjectPathMaterial::from_deps(
                doc_id, files,
            )));
    }

    pub fn route(&mut self, doc_id: Id, priority: u32) {
        self.updates.push(LockUpdate::Route(ProjectRoute {
            id: doc_id,
            priority,
        }));
    }

    pub fn commit(self) {
        super::LockFile::update(&self.root, |l| {
            let root: EcoString = unix_slash(&self.root).into();
            let root_hash = tinymist_std::hash::hash128(&root);
            for update in self.updates {
                match update {
                    LockUpdate::Input(input) => {
                        l.replace_document(input);
                    }
                    LockUpdate::Task(task) => {
                        l.replace_task(task);
                    }
                    LockUpdate::Material(mut mat) => {
                        let root: EcoString = unix_slash(&self.root).into();
                        mat.root = root.clone();
                        let cache_dir = dirs::cache_dir();
                        if let Some(cache_dir) = cache_dir {
                            let id = tinymist_std::hash::hash128(&mat.id);
                            let root_lo = root_hash & 0xfff;
                            let root_hi = root_hash >> 12;
                            let id_lo = id & 0xfff;
                            let id_hi = id >> 12;

                            let hash_str =
                                format!("{root_lo:03x}/{root_hi:013x}/{id_lo:03x}/{id_hi:016x}");

                            let cache_dir = cache_dir.join("tinymist/projects").join(hash_str);
                            let _ = std::fs::create_dir_all(&cache_dir);

                            let data = serde_json::to_string(&mat).unwrap();
                            let path = cache_dir.join("path-material.json");
                            tinymist_std::fs::paths::write_atomic(path, data)
                                .log_error("ProjectCompiler: write material error");

                            // todo: clean up old cache
                        }
                        // l.replace_material(mat);
                    }
                    LockUpdate::Route(route) => {
                        l.replace_route(route);
                    }
                }
            }

            Ok(())
        })
        .log_error("ProjectCompiler: lock file error");
    }
}

struct Version<'a>(&'a str);

impl PartialEq for Version<'_> {
    fn eq(&self, other: &Self) -> bool {
        semver::Version::parse(self.0)
            .ok()
            .and_then(|a| semver::Version::parse(other.0).ok().map(|b| a == b))
            .unwrap_or(false)
    }
}

impl PartialOrd for Version<'_> {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        let lhs = semver::Version::parse(self.0).ok()?;
        let rhs = semver::Version::parse(other.0).ok()?;
        Some(lhs.cmp(&rhs))
    }
}