ksmm 0.4.3

KernelSU Module Manager CLI
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
use dialoguer::{Input, Confirm};
use owo_colors::OwoColorize;
use std::fs;
use std::path::Path;
use std::process::Command;
use chrono::{Datelike, Timelike, Utc};
use regex::Regex;

fn get_git_info() -> (String, Option<String>, Option<String>, Option<String>, Option<String>) {
    // 获取分支信息
    let branch_output = Command::new("git")
        .args(&["branch", "--show-current"])
        .output();

    let branch = if let Ok(output) = branch_output {
        if output.status.success() {
            Some(String::from_utf8_lossy(&output.stdout).trim().to_string())
        } else {
            None
        }
    } else {
        None
    };

    // 获取远程仓库URL
    let remote_output = Command::new("git")
        .args(&["remote", "get-url", "origin"])
        .output();

    let mut remote_url = None;
    let mut update_json = String::new();
    let mut username = None;

    if let Ok(output) = remote_output {
        if output.status.success() {
            let url = String::from_utf8_lossy(&output.stdout).trim().to_string();
            remote_url = Some(url.clone());

            // 解析GitHub URL
            let github_regex = Regex::new(r"github\.com[\/:]([^\/]+)\/([^\/\.]+)").unwrap();

            if let Some(captures) = github_regex.captures(&url) {
                if let (Some(user), Some(repo)) = (captures.get(1), captures.get(2)) {
                    let user = user.as_str();
                    let repo = repo.as_str().trim_end_matches(".git");
                    update_json = format!("https://github.com/{}/{}/releases/latest/download/update.json", user, repo);
                    username = Some(user.to_string());
                }
            }
        }
    }

    // 如果无法从远程URL获取,尝试获取git用户名
    if username.is_none() {
        let user_output = Command::new("git")
            .args(&["config", "user.name"])
            .output();

        if let Ok(output) = user_output {
            if output.status.success() {
                let user = String::from_utf8_lossy(&output.stdout).trim().to_string();
                if !user.is_empty() {
                    update_json = format!("https://github.com/{}/ksmm/releases/latest/download/update.json", user);
                    username = Some(user);
                }
            }
        }
    }

    // 获取工作目录状态
    let status_output = Command::new("git")
        .args(&["status", "--porcelain"])
        .output();

    let is_clean = if let Ok(output) = status_output {
        output.stdout.is_empty()
    } else {
        false
    };

    let status = if is_clean { "工作目录清洁" } else { "工作目录有变更" };

    // 如果都获取不到,使用默认的ksmm
    if update_json.is_empty() {
        update_json = "https://github.com/ksmm/ksmm/releases/latest/download/update.json".to_string();
    }

    (update_json, username, branch, remote_url, Some(status.to_string()))
}

fn create_system_directory(base_path: &Path) {
    let system_path = base_path.join("system");
    if system_path.exists() {
        println!("{}", format!("  [!] system 目录已存在,跳过创建").dimmed());
    } else {
        fs::create_dir_all(&system_path).expect("无法创建 system 目录");
        println!("{} 创建 system 目录", "[+]".green());

        // 创建 system/etc 目录
        let etc_path = system_path.join("etc");
        fs::create_dir_all(&etc_path).expect("无法创建 system/etc 目录");

        // 创建示例配置文件
        let example_conf_path = etc_path.join("example.conf");
        let example_conf_content = "# 这是一个示例配置文件\n# 将此文件放置在system目录中,它会被挂载到 /system/etc/example.conf\n";
        fs::write(&example_conf_path, example_conf_content).expect("无法写入 example.conf");
        println!("{} 创建 system/etc/example.conf", "[+]".green());
    }
}

fn create_module_prop(base_path: &Path, id: &str, name: &str, version: &str, version_code: i32, author: &str, description: &str, update_json: &str) {
    let module_prop_path = base_path.join("module.prop");
    if module_prop_path.exists() {
        println!("{}", format!("  [!] module.prop 文件已存在,跳过创建").dimmed());
    } else {
        let module_prop_content = format!(
            "id={}\nname={}\nversion={}\nversionCode={}\nauthor={}\ndescription={}\nupdateJson={}\n",
            id, name, version, version_code, author, description, update_json
        );
        fs::write(&module_prop_path, module_prop_content).expect("无法写入 module.prop");
        println!("{} 创建 module.prop", "[+]".green());
    }
}

fn create_script_files(base_path: &Path) {
    let customize_content = r#"#!/system/bin/sh
# KernelSU 模块自定义安装脚本

# 检查设备信息
ui_print "- 设备架构: $ARCH"
ui_print "- Android API: $API"
ui_print "- KernelSU 版本: $KSU_VER"

# 根据设备架构进行不同的处理
case $ARCH in
    arm64)
        ui_print "- 64位ARM设备"
        ;;
    arm)
        ui_print "- 32位ARM设备"
        ;;
    x64)
        ui_print "- x86_64设备"
        ;;
    x86)
        ui_print "- x86设备"
        ;;
esac

# 根据Android版本进行处理
# 示例shellcheck 自动修复 $API -> "$API"
if [ $API -lt 29 ]; then
    ui_print "- Android 10以下版本"
else
    ui_print "- Android 10及以上版本"
fi

# 设置权限(如果需要)
# set_perm_recursive $MODPATH/system/bin 0 0 0755 0755
# set_perm $MODPATH/system/etc/example.conf 0 0 0644

# 示例:删除系统文件(取消注释以使用)
# REMOVE="
# /system/app/SomeSystemApp
# /system/etc/some_config_file
# "

# 示例:替换系统目录(取消注释以使用)
# REPLACE="
# /system/app/SomeSystemApp
# "

ui_print "- 模块安装完成"
"#;

    let scripts = [
        ("post-fs-data.sh", "#!/system/bin/sh\n# 在文件系统挂载后执行\n"),
        ("service.sh", "#!/system/bin/sh\n# 服务脚本\n"),
        ("customize.sh", customize_content),
    ];

    for (filename, content) in &scripts {
        let file_path = base_path.join(filename);
        if file_path.exists() {
            println!("{}", format!("  [!] {} 文件已存在,跳过创建", filename).dimmed());
        } else {
            fs::write(&file_path, content).expect(&format!("无法写入 {}", filename));
            println!("{} 创建 {}", "[+]".green(), filename);
        }
    }
}

fn create_changelog(base_path: &Path) {
    let changelog_path = base_path.join("CHANGELOG.md");
    if changelog_path.exists() {
        println!("{}", format!("  [!] CHANGELOG.md 文件已存在,跳过创建").dimmed());
    } else {
        let changelog_content = "# 更新日志\n## v0.1.0\n";
        fs::write(&changelog_path, changelog_content).expect("无法写入 CHANGELOG.md");
        println!("{} 创建 CHANGELOG.md", "[+]".green());
    }
}

fn create_action_script(base_path: &Path) {
    let action_path = base_path.join("action.sh");
    if action_path.exists() {
        println!("{}", format!("  [!] action.sh 文件已存在,跳过创建").dimmed());
    } else {
        fs::write(&action_path, "#!/system/bin/sh\n# 执行按钮脚本\n").expect("无法写入 action.sh");
        println!("{} 创建 action.sh", "[+]".green());
    }
}

fn create_webui(base_path: &Path) {
    let webroot_path = base_path.join("webroot");
    if webroot_path.exists() {
        println!("{}", format!("  [!] webroot 目录已存在,跳过创建").dimmed());
    } else {
        fs::create_dir_all(&webroot_path).expect("无法创建 webroot 目录");
        println!("{} 创建 webroot 目录", "[+]".green());
    }

    let index_html_path = webroot_path.join("index.html");
    if index_html_path.exists() {
        println!("{}", format!("  [!] index.html 文件已存在,跳过创建").dimmed());
    } else {
        let index_html = r#"<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>KernelSU Module WebUI</title>
    <style>
        body { font-family: Arial, sans-serif; text-align: center; padding: 50px; }
        h1 { color: #333; }
    </style>
</head>
<body>
    <h1>欢迎使用 KernelSU 模块</h1>
    <p>这是一个简单的 WebUI 示例。</p>
</body>
</html>"#;
        fs::write(&index_html_path, index_html).expect("无法写入 index.html");
        println!("{} 创建 index.html", "[+]".green());
    }
}

fn create_ksmm_config(base_path: &Path) {
    let ksmm_path = base_path.join(".ksmm");
    if ksmm_path.exists() {
        println!("{}", format!("  [!] .ksmm 目录已存在,跳过创建").dimmed());
    } else {
        fs::create_dir_all(&ksmm_path).expect("无法创建 .ksmm 目录");
        println!("{} 创建 .ksmm 目录", "[+]".green());
    }

    // 创建构建配置文件
    let build_conf_path = ksmm_path.join("build.conf");
    if build_conf_path.exists() {
        println!("{}", format!("  [!] .ksmm/build.conf 文件已存在,跳过创建").dimmed());
    } else {
        let build_conf_content = r#"# KernelSU 模块构建配置文件
# 控制哪些文件被复制到构建目录
#
# 语法说明:
#   - 普通行: 忽略这些文件/目录 (支持通配符)
#   - ! 开头的行: 强制包括这些文件/目录 (即使在忽略列表中)
#   - # 开头的行: 注释
#   - 空行: 被忽略
#
# 示例:
#   *.log      # 忽略所有 .log 文件
#   build/     # 忽略 build 目录
#   !system/   # 强制包括 system 目录

# 版本控制文件
.git/
.gitignore
.github/

# 构建产物
build/
target/
*.zip
*.tar.gz

# 临时文件
*.tmp
*.bak
*~

# 日志文件
*.log

# IDE 和编辑器文件
.vscode/
.idea/
*.swp
*.swo

# 操作系统文件
.DS_Store
Thumbs.db

# 文档文件
README.md
CHANGELOG.md

# 强制包括的核心模块文件
!module.prop
!system/
!webroot/

# 强制包括脚本文件
!*.sh
!action.sh

# 强制包括配置文件
!system.prop
!sepolicy.rule
"#;
        fs::write(&build_conf_path, build_conf_content).expect("无法写入 .ksmm/build.conf");
        println!("{} 创建 .ksmm/build.conf", "[+]".green());
    }
}

fn create_github_workflows(base_path: &Path) {
        let workflows_path = base_path.join(".github/workflows");

        // 如果目录不存在则创建;如果存在则继续(不要直接返回)
        if !workflows_path.exists() {
                match fs::create_dir_all(&workflows_path) {
                        Ok(_) => println!("{} 创建 .github/workflows 目录", "[+]".green()),
                        Err(e) => {
                                println!("{} 创建 .github/workflows 目录失败: {}", "[-]".red(), e);
                                return;
                        }
                }
        } else {
                println!("{}", format!("  [!] .github/workflows 目录已存在,继续检查文件").dimmed());
        }

        // 始终尝试创建 ci.yml(如果文件已存在则跳过)
        let ci_yml_path = workflows_path.join("ci.yml");
        if ci_yml_path.exists() {
                println!("{}", format!("  [!] .github/workflows/ci.yml 已存在,跳过创建").dimmed());
                return;
        }

        let ci_yml_content = r#"name: Release with KSMM

on:
  push:
    tags:
      - "v*"    # 当推送 tag 时触发,比如 v1.0.0

permissions:
  contents: write

jobs:
  release:
    name: Build and Release
    runs-on: ubuntu-latest

    steps:
      - name: "🧩 Checkout repository"
        uses: actions/checkout@v4

      - name: "🦀 Install Rust"
        uses: dtolnay/rust-toolchain@stable

      - name: "📦 Install ksmm"
        run: cargo install ksmm

      - name: "🏷️ Get tag name"
        id: get_tag
        run: |
            TAG_NAME=${GITHUB_REF#refs/tags/}
            echo "TAG_NAME=$TAG_NAME" >> $GITHUB_ENV
            echo "✅ Detected tag: $TAG_NAME"

      - name: ⚙️ Run ksmm build
        run: |
              ksmm build

      - name: "📁 Show release contents"
        run: |
            echo "🪣 Build output:"
            ls -R .ksmm/release || echo "❌ .ksmm/release not found"

      - name: "🚀 Create GitHub Release"
        uses: softprops/action-gh-release@v2
        with:
            tag_name: ${{ env.TAG_NAME }}
            name: "Release ${{ env.TAG_NAME }}"
            files: |
                .ksmm/release/**
        env:
            GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
"#;

        fs::write(&ci_yml_path, ci_yml_content).expect("无法写入 .github/workflows/ci.yml");
        println!("{} 创建 .github/workflows/ci.yml", "[+]".green());
}

fn update_gitignore(base_path: &Path) {
    // 只在项目目录(base_path)内查找或创建 .gitignore
    let gitignore_path = base_path.join(".gitignore");

    // 读取或创建 .gitignore
    let content = if gitignore_path.exists() {
        match fs::read_to_string(&gitignore_path) {
            Ok(c) => c,
            Err(e) => {
                println!("{}", format!("  [!] 无法读取 .gitignore: {}", e).dimmed());
                return;
            }
        }
    } else {
        // 如果不存在,创建一个空的内容
        String::new()
    };

    let is_empty = content.is_empty();

    // 检查是否已经包含 .ksmm/
    let has_ksmm = content.lines().any(|line| line.trim() == ".ksmm/");
    let has_build_conf = content.lines().any(|line| line.trim() == "!.ksmm/build.conf");
    
    if has_ksmm && has_build_conf {
        println!("{}", format!("  [!] .gitignore 已包含 .ksmm/ 和 !.ksmm/build.conf,跳过添加").dimmed());
    } else {
        // 追加内容到 .gitignore
        let mut new_content = content;
        if !is_empty && !new_content.ends_with('\n') {
            new_content.push('\n');
        }
        
        if !has_ksmm {
            new_content.push_str(".ksmm/\n");
        }
        if !has_build_conf {
            new_content.push_str("!.ksmm/build.conf\n");
        }
        
        match fs::write(&gitignore_path, new_content) {
            Ok(_) => {
                if is_empty {
                    println!("{} 创建 .gitignore", "[+]".green());
                } else {
                    println!("{} 更新 .gitignore", "[+]".green());
                }
            }
            Err(e) => {
                println!("{}", format!("  [!] 无法写入 .gitignore: {}", e).dimmed());
            }
        }
    }
}

pub fn execute() {
    println!("{} {}", "🚀", "初始化 KernelSU 模块...".cyan());

    // 输入创建地址
    let path: String = Input::new()
        .with_prompt("请输入创建地址 (默认当前目录: ksmm)")
        .default("ksmm".to_string())
        .interact_text()
        .unwrap();

    let base_path = Path::new(&path);

    // 确定项目名称
    let mut project_name = if path == "." {
        // 使用当前目录名称
        match std::env::current_dir() {
            Ok(current_dir) => {
                current_dir.file_name()
                    .and_then(|n| n.to_str())
                    .unwrap_or("unknown")
                    .to_string()
            }
            Err(_) => "unknown".to_string(),
        }
    } else {
        base_path.file_name()
            .and_then(|n| n.to_str())
            .unwrap_or("unknown")
            .to_string()
    };

    // 验证项目名称/id格式
    let id_regex = Regex::new(r"^[a-zA-Z][a-zA-Z0-9._-]+$").unwrap();
    if !id_regex.is_match(&project_name) {
        println!("{} 当前目录名称 '{}' 不符合模块ID格式要求。", "⚠️", project_name);
        project_name = Input::new()
            .with_prompt("请输入项目名称 (必须以字母开头,只能包含字母、数字、点、下划线和连字符)")
            .interact_text()
            .unwrap();
        
        // 再次验证用户输入
        if !id_regex.is_match(&project_name) {
            println!("{} 模块ID格式无效。必须以字母开头,只能包含字母、数字、点、下划线和连字符。", "");
            return;
        }
    }

    // 使用项目名称作为id和name
    let id = project_name.clone();
    let name = project_name;

    // 获取git仓库信息
    let (update_json, git_username, branch, remote_url, status) = get_git_info();

    // 输出git信息
    if remote_url.is_some() || branch.is_some() {
        println!("{} 检测到 Git 仓库", "🔍");
        if let Some(branch_name) = &branch {
            println!("  {}: {}", "分支".blue(), branch_name.green());
        }
        if let Some(url) = &remote_url {
            println!("  {}: {}", "远程仓库".blue(), url.green());
        }
        if let Some(user) = &git_username {
            println!("  {}: {}", "用户名".blue(), user.green());
        }
        if let Some(url) = &remote_url {
            let github_regex = Regex::new(r"github\.com[\/:]([^\/]+)\/([^\/\.]+)").unwrap();
            if let Some(captures) = github_regex.captures(url) {
                if let Some(repo) = captures.get(2) {
                    let repo_name = repo.as_str().trim_end_matches(".git");
                    println!("  {}: {}", "仓库".blue(), repo_name.green());
                }
            }
        }
        if let Some(work_status) = &status {
            println!("  {}: {}", "状态".blue(), work_status.green());
        }
        println!();
    }

    // 确定作者
    let author = if let Some(username) = git_username {
        username
    } else {
        println!("{} 无法获取git用户信息,使用默认作者: ksmm", "ℹ️".blue());
        "ksmm".to_string()
    };

    // 默认版本信息
    let version = "0.1.0".to_string();
    let now = Utc::now();
    let version_code_int = (now.year() * 1000000 + now.month() as i32 * 10000 + now.day() as i32 * 100 + now.hour() as i32) as i32;

    // 自动生成描述
    let description = format!("一个用ksmm创建的{}模块", name);

    // 确保项目目录存在
    if !base_path.exists() {
        if let Err(e) = fs::create_dir_all(base_path) {
            println!("{} 创建项目目录失败: {}", "", e);
            return;
        }
    }

    // 创建 system 目录
    create_system_directory(base_path);

    // 创建 .ksmm 配置目录
    create_ksmm_config(base_path);

    // 更新 .gitignore 文件
    update_gitignore(base_path);

    // 创建 GitHub Actions CI 工作流
    create_github_workflows(base_path);

    // 创建 module.prop
    create_module_prop(base_path, &id, &name, &version, version_code_int, &author, &description, &update_json);

    // 创建脚本文件
    create_script_files(base_path);

    // 创建 CHANGELOG.md
    create_changelog(base_path);

    // 检查是否需要执行按钮
    let action_path = base_path.join("action.sh");
    if action_path.exists() {
        println!("{}", format!("  [!] action.sh 文件已存在,跳过执行按钮配置").dimmed());
    } else {
        let need_action = Confirm::new()
            .with_prompt("是否需要执行按钮?")
            .default(true)
            .interact()
            .unwrap();

        if need_action {
            create_action_script(base_path);
        }
    }

    // 检查是否需要 webui
    let webroot_path = base_path.join("webroot");
    if webroot_path.exists() {
        println!("{}", format!("  [!] webroot 目录已存在,跳过WebUI配置").dimmed());
    } else {
        let need_webui = Confirm::new()
            .with_prompt("是否需要 WebUI?")
            .default(true)
            .interact()
            .unwrap();

        if need_webui {
            create_webui(base_path);
        }
    }

    println!("{} {}", "", "模块初始化完成!".cyan());
    println!();
    println!("{} 项目路径: {}", "📁", base_path.canonicalize().unwrap_or(base_path.to_path_buf()).display().green());
    println!("{} 项目ID: {}", "🔧", id.green());
    println!();
    println!("{} 下一步:", "📋");
    println!("  1. 编辑 {} 目录,添加你要修改的系统文件", "system/".green());
    println!("  2. 根据需要修改 {} 安装脚本", "customize.sh".green());
    println!("  3. 运行 {} 构建模块", "'ksmm build'".green());
    println!();
    println!("{} 项目初始化成功!", "🎉");
}