mirror of
https://gitee.com/RubyMetric/chsrc.git
synced 2025-12-25 20:52:49 +00:00
Add `chsrc-framework.h`
This commit is contained in:
parent
c1997265e1
commit
fbff0ab255
|
|
@ -0,0 +1,203 @@
|
|||
/** ------------------------------------------------------------
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
* -------------------------------------------------------------
|
||||
* File Name : chsrc-framework.h
|
||||
* File Authors : Aoran Zeng <ccmywish@qq.com>
|
||||
* | Heng Guo <2085471348@qq.com>
|
||||
* Contributors : Peng Gao <gn3po4g@outlook.com>
|
||||
* | Happy Game <happygame10124@gmail.com>
|
||||
* | Yangmoooo <yangmoooo@outlook.com>
|
||||
* |
|
||||
* Created On : <2023-08-29>
|
||||
* Last Modified : <2025-03-06>
|
||||
*
|
||||
* recipe 通过 include 此头文件来获得框架提供的功能
|
||||
* ------------------------------------------------------------*/
|
||||
|
||||
#include <xy.h>
|
||||
#include "struct.h"
|
||||
#include "mirror.h"
|
||||
|
||||
#define Chsrc_Version "0.2.0-beta1"
|
||||
#define Chsrc_Release_Date "2024/12/18"
|
||||
|
||||
#define App_Name "chsrc"
|
||||
|
||||
extern bool ProgMode_CMD_Measure;
|
||||
extern bool ProgMode_CMD_Reset;
|
||||
|
||||
extern bool ProgMode_Target_Group;
|
||||
extern int ProgMode_Leader_Selected_Index;
|
||||
|
||||
/* 此时 chsrc_run() 不再是recipe中指定要运行的一个外部命令,而是作为一个功能实现的支撑 */
|
||||
extern bool ProgMode_Run_as_a_Service;
|
||||
|
||||
extern enum ChgType_t ProgMode_ChgType;
|
||||
|
||||
|
||||
|
||||
/* 命令行选项 */
|
||||
extern bool CliOpt_IPv6;
|
||||
extern bool CliOpt_Locally;
|
||||
extern bool CliOpt_InEnglish;
|
||||
extern bool CliOpt_DryRun;
|
||||
extern bool CliOpt_NoColor;
|
||||
|
||||
/**
|
||||
* -local 的含义是启用 *项目级* 换源
|
||||
*
|
||||
* 每个 target 对 [-local] 的支持情况可使用 | chsrc ls <target> | 来查看
|
||||
*
|
||||
*
|
||||
* 1. 默认不使用该选项时,含义是 *全局* 换源,
|
||||
*
|
||||
* 全局分为 (1)系统级 (2)用户级
|
||||
*
|
||||
* 大多数第三方配置软件往往默认进行的是 *用户级* 的配置。所以 chsrc 首先将尝试使用 *用户级* 配置
|
||||
*
|
||||
* 2. 若不存在 *用户级* 的配置,chsrc 将采用 *系统级* 的配置
|
||||
*
|
||||
* 3. 最终效果本质由第三方软件决定,如 poetry 默认实现的就是项目级的换源
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#define Exit_OK 0
|
||||
#define Exit_Fatal 1
|
||||
#define Exit_Unknown 2
|
||||
#define Exit_Unsupported 3
|
||||
#define Exit_UserCause 4
|
||||
#define Exit_MaintainerCause 5
|
||||
#define Exit_ExternalError 6
|
||||
|
||||
|
||||
|
||||
#define red(str) xy_str_to_red(str)
|
||||
#define blue(str) xy_str_to_blue(str)
|
||||
#define green(str) xy_str_to_green(str)
|
||||
#define yellow(str) xy_str_to_yellow(str)
|
||||
#define purple(str) xy_str_to_purple(str)
|
||||
#define bold(str) xy_str_to_bold(str)
|
||||
#define bdred(str) xy_str_to_bold(xy_str_to_red(str))
|
||||
#define bdblue(str) xy_str_to_bold(xy_str_to_blue(str))
|
||||
#define bdgreen(str) xy_str_to_bold(xy_str_to_green(str))
|
||||
#define bdyellow(str) xy_str_to_bold(xy_str_to_yellow(str))
|
||||
#define bdpurple(str) xy_str_to_bold(xy_str_to_purple(str))
|
||||
|
||||
#define chsrc_log(str) xy_log(App_Name,str)
|
||||
#define chsrc_succ(str) xy_succ(App_Name,str)
|
||||
#define chsrc_info(str) xy_info(App_Name,str)
|
||||
#define chsrc_warn(str) xy_warn(App_Name,str)
|
||||
#define chsrc_error(str) xy_error(App_Name,str)
|
||||
#define chsrc_debug(str) xy_warn(App_Name "(DEBUG)",str)
|
||||
#define chsrc_verbose(str) xy_info(App_Name "(VERBOSE)",str)
|
||||
|
||||
// 2系列都是带有括号的
|
||||
#define chsrc_succ2(str) xy_succ_brkt(App_Name,CliOpt_InEnglish?"SUCCEED":"成功",str)
|
||||
#define chsrc_log2(str) xy_info_brkt(App_Name,"LOG",str)
|
||||
#define chsrc_warn2(str) xy_warn_brkt(App_Name,CliOpt_InEnglish?"WARN":"警告",str)
|
||||
#define chsrc_error2(str) xy_error_brkt(App_Name,CliOpt_InEnglish?"ERROR":"错误",str)
|
||||
#define chsrc_debug2(str) xy_warn_brkt(App_Name,"DEBUG",str)
|
||||
#define chsrc_verbose2(str) xy_info_brkt(App_Name,"VERBOSE",str)
|
||||
void chsrc_note2 (const char *str);
|
||||
|
||||
|
||||
|
||||
enum ChgType_t
|
||||
{
|
||||
ChgType_Auto,
|
||||
ChgType_Reset,
|
||||
ChgType_SemiAuto,
|
||||
ChgType_Manual,
|
||||
ChgType_Untested
|
||||
};
|
||||
|
||||
|
||||
int select_mirror_autoly (Source_t *sources, size_t size, const char *target_name);
|
||||
#define auto_select_mirror(s) select_mirror_autoly(s##_sources, s##_sources_n, (char*)#s+3)
|
||||
|
||||
int query_provider_exist (Source_t *sources, size_t size, char *target, char *input);
|
||||
|
||||
#define find_mirror(s, input) query_provider_exist(s##_sources, s##_sources_n, (char*)#s+3, input)
|
||||
|
||||
#define use_specific_mirror_or_auto_select(input, s) \
|
||||
(NULL!=(input)) ? find_mirror(s, input) : auto_select_mirror(s)
|
||||
|
||||
/**
|
||||
* 用户*只可能*通过下面5种方式来换源,无论哪一种都会返回一个 Source_t 出来
|
||||
* option:
|
||||
* 1. 用户指定某个 Mirror Code
|
||||
* 2. NULL: 用户什么都没指定 (将测速选择最快镜像)
|
||||
* 3. 用户给了一个 URL
|
||||
* 4. ChgType_Reset
|
||||
* 选用了Leader target
|
||||
* 5. ProgMode_Leader_Selected_Index 将给出所选索引
|
||||
*
|
||||
* @dependency 变量 option
|
||||
*/
|
||||
#define chsrc_yield_for_the_source(for_what) \
|
||||
if (ProgMode_Target_Group==true && ProgMode_Leader_Selected_Index==-1) \
|
||||
{ \
|
||||
ProgMode_Leader_Selected_Index = use_specific_mirror_or_auto_select (option, for_what); \
|
||||
source = for_what##_sources[ProgMode_Leader_Selected_Index]; \
|
||||
} \
|
||||
else if (ProgMode_Target_Group==true && ProgMode_Leader_Selected_Index!=-1) \
|
||||
{ \
|
||||
source = for_what##_sources[ProgMode_Leader_Selected_Index]; \
|
||||
} \
|
||||
else if (is_url (option)) \
|
||||
{ \
|
||||
Source_t __tmp = { &UserDefinedProvider, option }; \
|
||||
source = __tmp; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
int __index = use_specific_mirror_or_auto_select (option, for_what); \
|
||||
source = for_what##_sources[__index]; \
|
||||
}
|
||||
|
||||
#define chsrc_yield_source(for_what) \
|
||||
Source_t source; \
|
||||
chsrc_yield_for_the_source(for_what)
|
||||
|
||||
#define chsrc_yield_source_and_confirm(for_what) chsrc_yield_source(for_what); chsrc_confirm_source(&source)
|
||||
|
||||
|
||||
void chsrc_confirm_source (Source_t *source);
|
||||
|
||||
bool is_url (const char *str);
|
||||
|
||||
bool chsrc_query_program_exist (char *prog_name, char *check_cmd, int mode);
|
||||
bool chsrc_check_program (char *prog_name);
|
||||
bool chsrc_check_program_quietly (char *prog_name);
|
||||
bool chsrc_check_program_quietly_when_exist (char *prog_name);
|
||||
void chsrc_ensure_program (char *prog_name);
|
||||
bool chsrc_check_file (char *path);
|
||||
|
||||
|
||||
void chsrc_determine_chgtype (enum ChgType_t type);
|
||||
|
||||
void chsrc_ensure_root ();
|
||||
|
||||
void chsrc_conclude (Source_t *source);
|
||||
|
||||
#define RunOpt_Default 0x0000 // 默认若命令运行失败,直接退出
|
||||
#define RunOpt_Dont_Notify_On_Success 0x0010 // 运行成功不提示用户,只有运行失败时才提示用户
|
||||
#define RunOpt_No_Last_New_Line 0x0100 // 不输出最后的空行
|
||||
#define RunOpt_Dont_Abort_On_Failure 0x1000 // 命令运行失败也不退出
|
||||
|
||||
void chsrc_run (const char *cmd, int run_option);
|
||||
|
||||
void chsrc_view_file (const char *path);
|
||||
|
||||
void chsrc_ensure_dir (const char *dir);
|
||||
|
||||
void chsrc_append_to_file (const char *str, const char *filename);
|
||||
|
||||
void chsrc_prepend_to_file (const char *str, const char *filename);
|
||||
|
||||
void chsrc_overwrite_file (const char *str, const char *filename);
|
||||
void chsrc_backup (const char *path);
|
||||
|
||||
char * chsrc_get_cpuarch ();
|
||||
int chsrc_get_cpucore ();
|
||||
Loading…
Reference in New Issue