assemble_core/defaults/
plugins.rs

1use crate::defaults::tasks::{Help, TaskReport, WrapperTask};
2use crate::dependencies::project_dependency::ProjectDependencyPlugin;
3use crate::plugins::{Plugin, PluginAware};
4use crate::project::error::ProjectResult;
5
6use crate::project::GetProjectId;
7use crate::Project;
8
9/// The base plugin is applied to every project and supplies only needed tasks.
10///
11/// # Provided Tasks
12/// - `tasks`: lists the available tasks in this project
13#[derive(Default)]
14pub struct BasePlugin;
15
16/// The name of the task that reports all tasks in a project.
17pub const TASKS_REPORT_TASK_NAME: &str = "tasks";
18/// The name of the task that provides help information for the project
19pub const HELP_TASK_NAME: &str = "help";
20/// The name of the task that can create a wrapper for running assemble projects. Only present in the
21/// root project
22pub const WRAPPER_TASK_NAME: &str = "wrapper";
23/// The assemble group are tasks that are important for the operation of an assemble project
24pub const ASSEMBLE_GROUP: &str = "assemble";
25
26impl Plugin<Project> for BasePlugin {
27    fn apply_to(&self, project: &mut Project) -> ProjectResult {
28        trace!("applying the base plugin to {}", project);
29        project
30            .task_container_mut()
31            .register_task_with::<TaskReport, _>(TASKS_REPORT_TASK_NAME, |tasks, _| {
32                tasks.set_group(ASSEMBLE_GROUP);
33                Ok(())
34            })?;
35        let mut help = project
36            .task_container_mut()
37            .register_task::<Help>(HELP_TASK_NAME)?;
38        help.configure_with(|task, _| {
39            task.set_group(ASSEMBLE_GROUP);
40            Ok(())
41        })?;
42        project.set_default_tasks([help.id().clone()]);
43
44        if project.is_root() {
45            project
46                .task_container_mut()
47                .register_task_with::<WrapperTask, _>(WRAPPER_TASK_NAME, |task, _| {
48                    task.set_group(ASSEMBLE_GROUP);
49                    Ok(())
50                })?;
51        }
52
53        project.apply_plugin::<ProjectDependencyPlugin>()?;
54        Ok(())
55    }
56}
57
58#[cfg(test)]
59mod tests {
60
61    use crate::defaults::plugins::TASKS_REPORT_TASK_NAME;
62    use crate::defaults::tasks::TaskReport;
63    use crate::identifier::TaskId;
64    use crate::project::finder::TaskFinder;
65    use crate::Project;
66
67    #[test]
68    fn base_always_applied() {
69        let project = Project::temp(None);
70        let handle = project.find_task(TASKS_REPORT_TASK_NAME);
71        assert!(
72            handle.is_ok(),
73            "{} was not added to project",
74            TASKS_REPORT_TASK_NAME
75        );
76        let handle = handle.unwrap();
77        let task_report = handle.as_type::<TaskReport>();
78        assert!(
79            task_report.is_some(),
80            "could not get {} as TaskReport",
81            TASKS_REPORT_TASK_NAME
82        );
83    }
84}