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
use nom::combinator::complete;
use nom::InputLength;

pub use menu::*;
pub mod menu;
pub use ast::*;
pub use derive_more::*;
pub use minimo::*;
pub mod ast;

pub use parser::*;
pub mod parser;

pub use ctx::*;
pub mod ctx;

pub use runtime::*;
pub mod runtime;

pub use repository::*;
pub mod repository {
    use tokio::fs;

    use super::*;
    ///! repository
    ///! repository is a registrable git repository local / remote (github, gitlab, bitbucket) with some predefined runtimes and tasks
    ///! by adding a repository to the context, the user can access the runtimes and tasks defined in the repository
    ///! moto comes with a default repository that contains some predefined runtimes and tasks [github.com/moniverse/core]
    ///! here common runtimes like rust,dart,javascript,csharp,python,go etc are defined which can be used right out of the box by the user
    ///! the user can also add their own repository to the context

    #[derive(Debug, Display, From, Clone)]
    #[display(fmt = "{}", name)]
    pub struct Repository {
        name: String,
        url: String,
    }

    impl Repository {
        pub fn new(name: String, url: String) -> Self {
            Self { name, url }
        }

        pub fn name(&self) -> String {
            self.name.clone()
        }

        pub fn url(&self) -> String {
            self.url.clone()
        }

        pub async fn load_cells(&self) {
            //if the path is local, look at all the moto files and load all the cells into the context
            //if the path is remote, clone the repository and then load all the cells into the context

            let path = ctx::get_local_repository_path(&self.name);
            
            if !path.exists() {
                self.clone_to(&path).await
            }

             match fs::read_dir(&path).await {
                Ok(mut dir) => {
                    while let Ok(Some(entry)) = dir.next_entry().await {
                        let path = entry.path();
                        if path.extension().unwrap_or_default() == "moto" {
                            let content = fs::read_to_string(&path).await.unwrap();
                            let cells = parser::parse_cells(&content);
                             match cells {
                                Ok((_, cells)) => {
                                    for cell in cells {
                                        ctx::push_cell(cell).await;
                                    }
                                }
                                Err(e) => {
                                    println!("Error: {:?}", e);
                                }
                            }
                        }
                    }
                }
                Err(e) => {
                    println!("Error: {:?}", e);
                }
            }
        }


        pub async fn clone_to(&self, path: &std::path::Path) {
            let _ = fs::create_dir_all(path).await;
            let _ = tokio::process::Command::new("git")
                .args(&["clone", &self.url, path.to_str().unwrap()])
                .output()
                .await;
        }

        pub async fn pull(&self) {
            let path = ctx::get_local_repository_path(&self.name);
            let _ = tokio::process::Command::new("git")
                .current_dir(&path)
                .args(&["pull"])
                .output()
                .await;
        }
    }
}