-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
169 lines (147 loc) · 4.84 KB
/
lib.rs
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
//! # mini2react
//!
//! A library for modeling artistic concepts.
pub use self::utils::{
build_file_tree, create_or_replace_folder, generate_react_function_component,
generate_typescript_default_export, capitalize_first_letter
};
use std::collections::HashMap;
#[derive(Debug)]
/// 定义文件树结构
pub struct FileNode {
name: String,
is_file: bool,
path: String,
children: HashMap<String, FileNode>,
}
/// 继承文件树结构
/// 定义遍历文件树方法
/// 生成目标文件
impl FileNode {
fn new(name: &str, is_file: bool, path: &str) -> Self {
FileNode {
name: name.to_string(),
is_file,
path: path.to_string(),
children: HashMap::new(),
}
}
fn add_child(&mut self, child: FileNode) {
self.children.insert(child.name.clone(), child);
}
pub fn traverse(&self) {
self.traverse_recursive(0);
}
pub fn traverse_recursive(&self, depth: usize) {
if !self.is_file {
if depth == 0 {
create_or_replace_folder("widgets/");
} else {
create_or_replace_folder(&format!("widgets/{}", &capitalize_first_letter(&self.name)));
}
}
// 递归遍历子节点
for child in self.children.values() {
child.traverse_recursive(depth + 1);
}
}
}
pub mod utils {
use crate::FileNode;
use std::{
env, fs::{self, File}, io::Write, path::PathBuf
};
/// &str 首字母大写
pub fn capitalize_first_letter(s: &str) -> String {
if let Some(ch) = s.chars().next() {
let first_letter = ch.to_uppercase().collect::<String>();
let rest_of_string = s.chars().skip(1).collect::<String>();
format!("{}{}", first_letter, rest_of_string)
} else {
s.to_string()
}
}
/// 遍历文件夹目录结构
/// 构建文件树
pub fn build_file_tree(path: &str) -> FileNode {
let path_buf = PathBuf::from(path);
let name = path_buf.file_name().unwrap().to_str().unwrap();
let mut root = FileNode::new(name, false, path);
if path_buf.is_dir() {
if let Ok(entries) = std::fs::read_dir(path) {
for entry in entries {
if let Ok(entry) = entry {
let entry_path = entry.path();
let entry_name = entry_path.file_name().unwrap().to_str().unwrap();
if entry_path.is_dir() {
let child = build_file_tree(entry_path.to_str().unwrap());
root.add_child(child);
} else {
let child =
FileNode::new(entry_name, true, entry_path.to_str().unwrap());
root.add_child(child);
}
}
}
}
}
root
}
/// 创建或替换文件
pub fn create_or_replace_folder(folder_name: &str) {
// 尝试删除已有文件夹
if let Err(err) = fs::remove_dir_all(folder_name) {
// 如果该文件夹不存在,则返回 Err,但我们可以忽略该错误
if err.kind() != std::io::ErrorKind::NotFound {
eprintln!("Error removing folder '{}': {}", folder_name, err);
return;
}
}
// 使用create_dir创建文件夹
match fs::create_dir(folder_name) {
Ok(_) => println!("Folder '{}' created successfully.", folder_name),
Err(err) => eprintln!("Error creating folder '{}': {}", folder_name, err),
}
}
pub fn get_executable_path() -> Option<String> {
if let Ok(exe_path) = env::current_exe() {
exe_path.to_str().map(String::from)
} else {
None
}
}
/// 生成 TypeScript 默认导出文件
pub fn generate_typescript_default_export(
content: &str,
file_path: &str,
) -> std::io::Result<()> {
let mut file = File::create(file_path)?;
writeln!(file, "export {};", content)?;
Ok(())
}
/// 生成 React 函数组件示例代码
pub fn generate_react_function_component(
component_name: &str,
file_path: &str,
) -> std::io::Result<()> {
let component_code = format!(
r#"
import React from 'react';
// Example React function component
const {} = () => {{
return (
<div>
<h1>{}</h1>
<p>This is an example React function component.</p>
</div>
);
}};
export default {};
"#,
component_name, component_name, component_name
);
let mut file = File::create(file_path)?;
writeln!(file, "{}", component_code)?;
Ok(())
}
}