assemble-core 0.2.0

The core crate of the assemble-rs package
Documentation
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! Handles standard invoking and monitoring builds

use crate::logging::{ConsoleMode, LoggingArgs};
use crate::plugins::PluginManager;
use crate::prelude::listeners::TaskExecutionGraphListener;
use crate::prelude::{PluginAware, SettingsAware};
use std::backtrace::Backtrace;

use crate::project::ProjectResult;
use crate::startup::execution_graph::ExecutionGraph;
use crate::startup::listeners::{BuildListener, Listener, TaskExecutionListener};
use crate::version::{version, Version};

use itertools::Itertools;
use log::Level;
use once_cell::sync::OnceCell;
use parking_lot::RwLock;
use std::collections::HashMap;
use std::env::current_dir;
use std::fmt::Debug;
use std::ops::{Deref, DerefMut};
use std::path::{Path, PathBuf};
use std::sync::Arc;

/// Provides a wrapper around the assemble instance that's running this build.
#[derive(Debug)]
pub struct Assemble {
    plugins: PluginManager<Assemble>,
    task_listeners: Vec<Box<dyn TaskExecutionListener>>,
    task_graph_listeners: Vec<Box<dyn TaskExecutionGraphListener>>,
    build_listeners: Vec<Box<dyn BuildListener>>,
    version: Version,
    start_parameter: StartParameter,
    graph: RwLock<OnceCell<ExecutionGraph>>,
}

impl Assemble {
    /// Create a new assemble instance
    pub fn new(start: StartParameter) -> Self {
        Self {
            plugins: PluginManager::new(),
            task_listeners: vec![],
            task_graph_listeners: vec![],
            build_listeners: vec![],
            version: version(),
            start_parameter: start,
            graph: Default::default(),
        }
    }

    /// Makes the execution graph available
    pub fn set_execution_graph(&mut self, graph: &ExecutionGraph) -> ProjectResult {
        self.graph
            .write()
            .set(graph.clone())
            .expect("execution graph already set");
        for listener in &mut self.task_graph_listeners {
            listener.graph_ready(graph)?;
        }
        Ok(())
    }

    /// Add a listener to the inner freight
    pub fn add_listener<T: Listener<Listened = Self>>(&mut self, listener: T) -> ProjectResult {
        listener.add_listener(self)
    }

    pub fn add_task_execution_listener<T: TaskExecutionListener + 'static>(
        &mut self,
        listener: T,
    ) -> ProjectResult {
        self.task_listeners.push(Box::new(listener));
        Ok(())
    }

    pub fn add_task_execution_graph_listener<T: TaskExecutionGraphListener + 'static>(
        &mut self,
        mut listener: T,
    ) -> ProjectResult {
        if let Some(graph) = self.graph.read().get() {
            listener.graph_ready(graph)
        } else {
            self.task_graph_listeners.push(Box::new(listener));
            Ok(())
        }
    }

    pub fn add_build_listener<T: BuildListener + 'static>(&mut self, listener: T) -> ProjectResult {
        self.build_listeners.push(Box::new(listener));
        Ok(())
    }

    pub fn settings_evaluated<S: SettingsAware>(&mut self, settings: S) -> ProjectResult {
        trace!("running settings evaluated method in build listeners");
        settings.with_settings(|settings| {
            self.build_listeners
                .iter_mut()
                .map(|b| b.settings_evaluated(&settings))
                .collect::<ProjectResult>()
        })
    }

    /// Gets the current version of assemble
    pub fn assemble_version(&self) -> &Version {
        &self.version
    }

    /// Gets the start parameters used to start this build
    pub fn start_parameter(&self) -> &StartParameter {
        &self.start_parameter
    }
    pub fn current_dir(&self) -> &Path {
        self.start_parameter().current_dir()
    }

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

    pub fn properties(&self) -> &HashMap<String, Option<String>> {
        &self.start_parameter.properties
    }
}

impl PluginAware for Assemble {
    fn plugin_manager(&self) -> &PluginManager<Self> {
        &self.plugins
    }

    fn plugin_manager_mut(&mut self) -> &mut PluginManager<Self> {
        &mut self.plugins
    }
}

impl Default for Assemble {
    fn default() -> Self {
        Assemble::new(StartParameter::new())
    }
}

/// A type that's aware it's part of an assemble build
pub trait AssembleAware {
    /// Get the assemble instance this value is aware of.
    fn with_assemble<F, R>(&self, func: F) -> R
    where
        F: FnOnce(&Assemble) -> R;

    /// Get the assemble instance this value is aware of as a mutable reference
    fn with_assemble_mut<F, R>(&mut self, func: F) -> R
    where
        F: FnOnce(&mut Assemble) -> R;

    fn start_parameter(&self) -> StartParameter {
        self.with_assemble(|asm| asm.start_parameter.clone())
    }
}

impl AssembleAware for Assemble {
    /// Gets this [`Assemble`](Assemble) instance.
    fn with_assemble<F, R>(&self, func: F) -> R
    where
        F: FnOnce(&Assemble) -> R,
    {
        (func)(self)
    }

    fn with_assemble_mut<F, R>(&mut self, func: F) -> R
    where
        F: FnOnce(&mut Assemble) -> R,
    {
        (func)(self)
    }
}

impl AssembleAware for Arc<RwLock<Assemble>> {
    fn with_assemble<F, R>(&self, func: F) -> R
    where
        F: FnOnce(&Assemble) -> R,
    {
        (func)(self.read().deref())
    }

    fn with_assemble_mut<F, R>(&mut self, func: F) -> R
    where
        F: FnOnce(&mut Assemble) -> R,
    {
        (func)(self.write().deref_mut())
    }
}

/// The start parameters define the configuration used by an assemble instance to execute a build.
///
/// Generally corresponds to the command line options for assemble.
#[derive(Debug, Clone)]
pub struct StartParameter {
    current_dir: PathBuf,
    logging: LoggingArgs,
    mode: ConsoleMode,
    project_dir: Option<PathBuf>,
    properties: HashMap<String, Option<String>>,
    task_requests: Vec<String>,
    workers: usize,
    backtrace: BacktraceEmit,
    rerun_tasks: bool,
}

/// The mechanism to emit the backtrace at
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BacktraceEmit {
    None,
    Short,
    Long,
}

impl BacktraceEmit {
    pub fn emit(&self, level: Level, backtrace: &Backtrace) {
        let lines: Vec<String> = match self {
            BacktraceEmit::None => return,
            BacktraceEmit::Short => {
                let mut bt = backtrace
                    .to_string()
                    .lines()
                    .skip_while(|line| !line.contains("assemble_core::error::PayloadError"))
                    .map(|s| s.to_string())
                    .collect::<Vec<_>>();

                if let Some(pos) = bt
                    .iter()
                    .position(|p| p.contains("assemble::main"))
                    .map(|s| s + 2)
                {
                    bt.drain(pos..);
                }

                bt.into_iter()
                    .tuples::<(_, _)>()
                    .map(|(frame, location)| {
                        if location.contains("/rustc/") {
                            vec!["\t... <hidden>".to_string()]
                        } else {
                            vec![frame, location]
                        }
                    })
                    .flatten()
                    .fold(vec![], |mut acc, line| {
                        let should_push = if line.trim().starts_with("...") {
                            if let Some(last) = acc.last() {
                                if !last.trim().starts_with("...") {
                                    true
                                } else {
                                    false
                                }
                            } else {
                                true
                            }
                        } else {
                            true
                        };
                        if should_push {
                            acc.push(line);
                        }
                        acc
                    })
            }
            BacktraceEmit::Long => backtrace
                .to_string()
                .lines()
                .map(|s| s.to_string())
                .collect(),
        };

        for line in lines {
            log!(level, "{}", line);
        }
    }
}

impl StartParameter {
    /// Creates a new instance of a start parameter with only default settings
    pub fn new() -> Self {
        Self {
            current_dir: current_dir().expect("no valid current working directory"),
            logging: LoggingArgs::default(),
            mode: ConsoleMode::Auto,
            project_dir: None,
            properties: HashMap::new(),
            task_requests: vec![],
            workers: 0,
            backtrace: BacktraceEmit::None,
            rerun_tasks: false,
        }
    }

    /// Gets the current directory of the start parameter, used to select the default project and
    /// find the settings file.
    pub fn current_dir(&self) -> &Path {
        &self.current_dir
    }

    /// The console mode to use
    pub fn mode(&self) -> ConsoleMode {
        self.mode
    }

    /// The project directory to find the default project. If not set, defaults to the same
    /// as the current dir.
    pub fn project_dir(&self) -> PathBuf {
        self.project_dir
            .as_ref()
            .unwrap_or(&self.current_dir)
            .clone()
    }

    /// The project properties set for this build
    pub fn properties(&self) -> &HashMap<String, Option<String>> {
        &self.properties
    }

    /// A mutable reference to the project properties for this build
    pub fn properties_mut(&mut self) -> &mut HashMap<String, Option<String>> {
        &mut self.properties
    }

    /// Gets whether the backtrace should be emitted
    pub fn backtrace(&self) -> BacktraceEmit {
        self.backtrace
    }

    /// the task requests used to build this project. Contains both task names
    /// and args for said tasks
    pub fn task_requests(&self) -> &[String] {
        &self.task_requests
    }

    /// the task requests used to build this project. Contains both task names
    /// and args for said tasks
    pub fn task_requests_mut(&mut self) -> &mut Vec<String> {
        &mut self.task_requests
    }

    /// Adds the task requests to this start parameter
    pub fn with_task_requests<S: AsRef<str>, I: IntoIterator<Item = S>>(mut self, iter: I) -> Self {
        self.task_requests_mut()
            .extend(iter.into_iter().map(|s| s.as_ref().to_string()));
        self
    }

    /// Whether to rerun all tasks
    pub fn is_rerun_tasks(&self) -> bool {
        self.rerun_tasks
    }

    /// Force all tasks to be re-ran
    pub fn rerun_tasks(&mut self) {
        self.rerun_tasks = true;
    }

    /// Set the current directory
    pub fn set_current_dir<P: AsRef<Path>>(&mut self, current_dir: P) {
        self.current_dir = current_dir.as_ref().to_path_buf();
    }
    /// The level filter to log
    pub fn set_logging(&mut self, log_level: LoggingArgs) {
        self.logging = log_level;
    }

    /// Sets the console mode
    pub fn set_mode(&mut self, mode: ConsoleMode) {
        self.mode = mode;
    }

    /// Sets the project directory used to find the default project
    pub fn set_project_dir<P: AsRef<Path>>(&mut self, project_dir: P) {
        self.project_dir = Some(project_dir.as_ref().to_path_buf());
    }

    pub fn set_backtrace(&mut self, backtrace: BacktraceEmit) {
        self.backtrace = backtrace;
    }

    pub fn workers(&self) -> usize {
        self.workers
    }

    pub fn set_workers(&mut self, workers: usize) {
        self.workers = workers;
    }
    pub fn logging(&self) -> &LoggingArgs {
        &self.logging
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn get_assemble_version() {
        let assemble = Assemble::default();
        println!("assemble: {:#?}", assemble);
        assert_eq!(assemble.assemble_version(), &version());
    }
}