mod.rs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //! # DADK控制台
  2. //!
  3. //! DADK控制台能够让用户通过命令行交互的方式使用DADK。
  4. //!
  5. //! ## 创建配置文件
  6. //!
  7. //! DADK控制台提供了一个命令,用于创建一个配置文件。您可以通过以下命令创建一个配置文件:
  8. //!
  9. //! ```bash
  10. //! dadk new
  11. //! ```
  12. //!
  13. pub mod clean;
  14. pub mod elements;
  15. pub mod interactive;
  16. pub mod new_config;
  17. use std::path::PathBuf;
  18. use clap::{Parser, Subcommand};
  19. use crate::parser::task::TargetArch;
  20. use self::clean::CleanArg;
  21. #[derive(Debug, Parser, Clone)]
  22. #[command(author, version, about)]
  23. pub struct CommandLineArgs {
  24. /// DragonOS sysroot在主机上的路径
  25. #[arg(short, long, value_parser = parse_check_dir_exists)]
  26. pub sysroot_dir: Option<PathBuf>,
  27. /// DADK任务配置文件所在目录
  28. #[arg(short, long, value_parser = parse_check_dir_exists)]
  29. pub config_dir: Option<PathBuf>,
  30. /// 要执行的操作
  31. #[command(subcommand)]
  32. pub action: Action,
  33. /// DADK缓存根目录
  34. #[arg(long, value_parser = parse_check_dir_exists)]
  35. pub cache_dir: Option<PathBuf>,
  36. /// DADK任务并行线程数量
  37. #[arg(short, long)]
  38. pub thread: Option<usize>,
  39. /// 目标架构,可选: ["aarch64", "x86_64", "riscv64", "riscv32"]
  40. #[arg(long, value_parser = parse_target_arch)]
  41. pub target_arch: Option<TargetArch>,
  42. }
  43. /// @brief 检查目录是否存在
  44. fn parse_check_dir_exists(path: &str) -> Result<PathBuf, String> {
  45. let path = PathBuf::from(path);
  46. if !path.exists() {
  47. return Err(format!("Path '{}' not exists", path.display()));
  48. }
  49. if !path.is_dir() {
  50. return Err(format!("Path '{}' is not a directory", path.display()));
  51. }
  52. return Ok(path);
  53. }
  54. fn parse_target_arch(s: &str) -> Result<TargetArch, String> {
  55. let x = TargetArch::try_from(s);
  56. if x.is_err() {
  57. return Err(format!("Invalid target arch: {}", s));
  58. }
  59. return Ok(x.unwrap());
  60. }
  61. #[derive(Debug, Subcommand, Clone, Copy, PartialEq, Eq)]
  62. pub enum Action {
  63. /// 构建所有项目
  64. Build,
  65. /// 清理缓存
  66. Clean(CleanArg),
  67. /// 安装到DragonOS sysroot
  68. Install,
  69. /// 尚不支持
  70. Uninstall,
  71. /// 使用交互式命令行创建dadk任务配置文件
  72. New,
  73. }
  74. #[allow(dead_code)]
  75. #[derive(Debug)]
  76. pub enum ConsoleError {
  77. CommandError(String),
  78. IOError(std::io::Error),
  79. /// 错误次数超过限制
  80. RetryLimitExceeded(String),
  81. /// 无效的输入
  82. InvalidInput(String),
  83. }