xref: /DADK/src/main.rs (revision 8f388bf05339108880442da40968e6669c65ea6f)
1 //! # DADK - DragonOS Application Development Kit
2 //! # DragonOS 应用开发工具
3 //!
4 //! ## 简介
5 //!
6 //! DADK是一个用于开发DragonOS应用的工具包,设计目的是为了让开发者能够更加方便的开发DragonOS应用。
7 //!
8 //! ### DADK做什么?
9 //!
10 //! - 自动配置libc等编译用户程序所需的环境
11 //! - 自动处理软件库的依赖关系
12 //! - 自动处理软件库的编译
13 //! - 一键将软件库安装到DragonOS系统中
14 //!
15 //! ### DADK不做什么?
16 //!
17 //! - DADK不会帮助开发者编写代码
18 //! - DADK不提供任何开发DragonOS应用所需的API。这部分工作由libc等库来完成
19 //!
20 //! ## License
21 //!
22 //! DADK is licensed under the [GPLv2 License](LICENSE).
23 
24 #![feature(io_error_more)]
25 
26 #[macro_use]
27 extern crate lazy_static;
28 extern crate log;
29 extern crate serde;
30 extern crate serde_json;
31 extern crate simple_logger;
32 
33 use std::{fs, path::PathBuf, process::exit};
34 
35 use clap::Parser;
36 use log::{info, error};
37 use parser::task::{
38     BuildConfig, CodeSource, DADKTask, Dependency, GitSource, InstallConfig, TaskType,
39 };
40 use simple_logger::SimpleLogger;
41 
42 use crate::{console::Action, scheduler::Scheduler, executor::cache::cache_root_init};
43 
44 mod console;
45 mod executor;
46 mod parser;
47 mod scheduler;
48 mod utils;
49 
50 #[derive(Debug, Parser)]
51 #[command(author, version, about)]
52 struct CommandLineArgs {
53     /// DragonOS sysroot在主机上的路径
54     #[arg(short, long, value_parser = parse_check_dir_exists)]
55     pub dragonos_dir: PathBuf,
56     /// DADK任务配置文件所在目录
57     #[arg(short, long, value_parser = parse_check_dir_exists)]
58     config_dir: PathBuf,
59 
60     /// 要执行的操作
61     #[command(subcommand)]
62     action: Action,
63 
64     /// DADK缓存根目录
65     #[arg(long, value_parser = parse_check_dir_exists)]
66     cache_dir: Option<PathBuf>,
67 }
68 
69 /// @brief 检查目录是否存在
70 fn parse_check_dir_exists(path: &str) -> Result<PathBuf, String> {
71     let path = PathBuf::from(path);
72     if !path.exists() {
73         return Err(format!("Path '{}' not exists", path.display()));
74     }
75     if !path.is_dir() {
76         return Err(format!("Path '{}' is not a directory", path.display()));
77     }
78 
79     return Ok(path);
80 }
81 
82 fn main() {
83     SimpleLogger::new().init().unwrap();
84     // generate_tmp_dadk();
85     info!("DADK Starting...");
86     let args = CommandLineArgs::parse();
87 
88     info!("DADK run with args: {:?}", &args);
89     // DragonOS sysroot在主机上的路径
90     let dragonos_dir = args.dragonos_dir;
91     let config_dir = args.config_dir;
92     let action = args.action;
93     info!("DragonOS sysroot dir: {}", dragonos_dir.display());
94     info!("Config dir: {}", config_dir.display());
95     info!("Action: {:?}", action);
96 
97     // 初始化缓存目录
98     let r = cache_root_init(args.cache_dir);
99     if r.is_err() {
100         error!("Failed to init cache root: {:?}", r.unwrap_err());
101         exit(1);
102     }
103 
104     let mut parser = parser::Parser::new(config_dir);
105     let r = parser.parse();
106     if r.is_err() {
107         exit(1);
108     }
109     let tasks: Vec<(PathBuf, DADKTask)> = r.unwrap();
110     // info!("Parsed tasks: {:?}", tasks);
111 
112     let scheduler = Scheduler::new(dragonos_dir, action, tasks);
113     if scheduler.is_err() {
114         exit(1);
115     }
116 
117     let r = scheduler.unwrap().run();
118     if r.is_err() {
119         exit(1);
120     }
121 }
122 
123 #[allow(dead_code)]
124 fn generate_tmp_dadk() {
125     let x = DADKTask {
126         name: "test".to_string(),
127         version: "0.1.0".to_string(),
128         build: BuildConfig {
129             build_command: "echo test".to_string(),
130         },
131         install: InstallConfig {
132             in_dragonos_path: PathBuf::from("/bin"),
133             install_command: "echo test".to_string(),
134         },
135         depends: vec![Dependency {
136             name: "test".to_string(),
137             version: "0.1.0".to_string(),
138         }],
139         description: "test".to_string(),
140         // task_type: TaskType::BuildFromSource(CodeSource::Archive(ArchiveSource::new(
141         //     "123".to_string(),
142         // ))),
143         task_type: TaskType::BuildFromSource(CodeSource::Git(GitSource::new(
144             "123".to_string(),
145             "master".to_string(),
146             None,
147         ))),
148         envs: None,
149     };
150     let x = serde_json::to_string(&x).unwrap();
151     fs::write("test.json", x).unwrap();
152 }
153