rune/cli/
loader.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
use std::ffi::OsStr;
use std::fs;
use std::io;
use std::path::PathBuf;
use std::{path::Path, sync::Arc};

use anyhow::{anyhow, Context as _, Result};

use crate::alloc::{Vec, VecDeque};
use crate::cli::{visitor, Io, SharedFlags};
use crate::compile::FileSourceLoader;
use crate::{Context, Diagnostics, Hash, ItemBuf, Options, Source, Sources, Unit};

pub(super) struct Load {
    pub(super) unit: Arc<Unit>,
    pub(super) sources: Sources,
    pub(super) functions: Vec<(Hash, ItemBuf)>,
}

/// Load context and code for a given path
pub(super) fn load(
    io: &mut Io<'_>,
    context: &Context,
    shared: &SharedFlags,
    options: &Options,
    path: &Path,
    attribute: visitor::Attribute,
) -> Result<Load> {
    let bytecode_path = path.with_extension("rnc");

    let source =
        Source::from_path(path).with_context(|| anyhow!("cannot read file: {}", path.display()))?;

    let mut sources = Sources::new();
    sources.insert(source)?;

    let use_cache = options.bytecode && should_cache_be_used(path, &bytecode_path)?;

    // TODO: how do we deal with tests discovery for bytecode loading
    let maybe_unit = if use_cache {
        let f = fs::File::open(&bytecode_path)?;

        match bincode::deserialize_from::<_, Unit>(f) {
            Ok(unit) => {
                tracing::trace!("Using cache: {}", bytecode_path.display());
                Some(Arc::new(unit))
            }
            Err(_error) => {
                tracing::error!(
                    "Failed to deserialize: {}: {}",
                    bytecode_path.display(),
                    _error
                );
                None
            }
        }
    } else {
        None
    };

    let (unit, functions) = match maybe_unit {
        Some(unit) => (unit, Default::default()),
        None => {
            tracing::trace!("building file: {}", path.display());

            let mut diagnostics = if shared.warnings {
                Diagnostics::new()
            } else {
                Diagnostics::without_warnings()
            };

            let mut functions = visitor::FunctionVisitor::new(attribute);
            let mut source_loader = FileSourceLoader::new();

            let result = crate::prepare(&mut sources)
                .with_context(context)
                .with_diagnostics(&mut diagnostics)
                .with_options(options)
                .with_visitor(&mut functions)?
                .with_source_loader(&mut source_loader)
                .build();

            diagnostics.emit(io.stdout, &sources)?;
            let unit = result?;

            if options.bytecode {
                tracing::trace!("serializing cache: {}", bytecode_path.display());
                let f = fs::File::create(&bytecode_path)?;
                bincode::serialize_into(f, &unit)?;
            }

            (Arc::new(unit), functions.into_functions())
        }
    };

    Ok(Load {
        unit,
        sources,
        functions,
    })
}

/// Test if path `a` is newer than path `b`.
fn should_cache_be_used(source: &Path, cached: &Path) -> io::Result<bool> {
    let source = fs::metadata(source)?;

    let cached = match fs::metadata(cached) {
        Ok(cached) => cached,
        Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(false),
        Err(error) => return Err(error),
    };

    Ok(source.modified()? < cached.modified()?)
}

pub(super) fn recurse_paths(
    recursive: bool,
    first: PathBuf,
) -> impl Iterator<Item = Result<PathBuf>> {
    let mut queue = VecDeque::new();
    let mut first = Some(first);

    std::iter::from_fn(move || loop {
        let path = first.take().or_else(|| queue.pop_front())?;

        if !recursive {
            return Some(Ok(path));
        }

        if path.is_file() {
            if path.extension() == Some(OsStr::new("rn")) {
                return Some(Ok(path));
            }

            continue;
        }

        let d = match fs::read_dir(path) {
            Ok(d) => d,
            Err(error) => return Some(Err(anyhow::Error::from(error))),
        };

        for e in d {
            let e = match e {
                Ok(e) => e,
                Err(error) => return Some(Err(anyhow::Error::from(error))),
            };

            if let Err(error) = queue.try_push_back(e.path()) {
                return Some(Err(anyhow::Error::from(error)));
            }
        }
    })
}