e_utils/
build.rs

1use crate::chrono::{china_now, Datelike as _, STANDARD_DATETIME_FORMAT};
2use crate::{
3  chrono::now,
4  cmd::Cmd,
5  fs::{_fs::AutoPath as _, write_utf8},
6};
7use std::path::Path;
8
9/// 包信息结构体
10#[derive(Debug)]
11pub struct PackageInfo {
12  /// 包名
13  pub name: String,
14  /// 版本
15  pub version: String,
16  /// 描述
17  pub description: String,
18  /// 作者
19  pub authors: String,
20  /// 主页
21  pub homepage: String,
22  /// 仓库
23  pub repository: String,
24}
25
26impl PackageInfo {
27  /// 生成版本信息
28  pub fn comments(&self) -> String {
29    format!(
30      "版本: {}\n主页: {}\n仓库: {}\n作者: {}",
31      self.version,
32      self.homepage,
33      self.repository,
34      self.formatted_authors()
35    )
36  }
37  /// 生成版权信息
38  pub fn copyright(&self) -> String {
39    format!(
40      "版权所有 © {} {}",
41      chrono::Local::now().year(),
42      if self.authors.is_empty() {
43        String::new()
44      } else {
45        self.formatted_authors()
46      }
47    )
48  }
49  /// 格式化作者信息
50  pub fn formatted_authors(&self) -> String {
51    self.authors.replace(':', ", ")
52  }
53}
54
55/// 导出git历史
56pub fn export_git_history(
57  pkg_version: impl AsRef<str>,
58  target: impl AsRef<Path>,
59  is_china: bool,
60) -> crate::AnyResult<()> {
61  target.as_ref().auto_remove_file()?;
62  let build_time = if is_china {
63    china_now()
64      .ok_or("获取时间失败")?
65      .format(STANDARD_DATETIME_FORMAT)
66  } else {
67    now().format(STANDARD_DATETIME_FORMAT)
68  };
69  let tag_commits = git_tag_commits().unwrap_or_default();
70  let version_info = format!(
71    "当前版本号: {}\n\
72      构建时间: {}\n\
73      构建类型: {}\n\
74      历史提交 :\n\n{}",
75    pkg_version.as_ref(),
76    build_time,
77    if cfg!(debug_assertions) {
78      "Debug"
79    } else {
80      "Release"
81    },
82    tag_commits,
83  );
84  write_utf8(&target.as_ref().to_path_buf(), &version_info, false, false)?;
85
86  Ok(())
87}
88
89/// 获取git标签提交历史
90pub fn git_tag_commits() -> crate::Result<String> {
91  // 获取所有标签的提交历史
92  let tag_commits = Cmd::new("git")
93    .args([
94      "log",
95      "--simplify-by-decoration", // 只显示有标签的提交
96      "--date=\"format-local:%Y-%m-%d %H:%M:%S\"",
97      "--format=\"标识: %h\n标签: %D\n时间: %cd\n作者: %an <%ae>\n提交: %s\n\"",
98    ])
99    .output()
100    .map(|output| output.stdout.to_string())
101    .unwrap_or("获取失败".to_string());
102
103  Ok(tag_commits)
104}