diff --git a/Makefile b/Makefile index ffd03e4..6a73e61 100644 --- a/Makefile +++ b/Makefile @@ -65,15 +65,15 @@ ifeq ($(STATIC), 1) CFLAGS += -static endif -all: - @$(CC) src/chsrc-main.c $(CFLAGS) $(_C_Warning_Flags) -o $(Target-Name) +all: framework + @$(CC) src/chsrc-main.c src/framework/core.o $(CFLAGS) $(_C_Warning_Flags) -o $(Target-Name) @echo; echo Compile done using \'$(CC)\' $(CFLAGS) CI: all @mv $(Target-Name) $(CI_ARTIFACT_NAME) framework: src/framework/core.o -src/framework/core.o: +src/framework/core.o: src/framework/core.c @$(CC) src/framework/core.c -c $(CFLAGS) $(_C_Warning_Flags) -o src/framework/core.o @echo; echo Compile done: core.o diff --git a/src/chsrc-main.c b/src/chsrc-main.c index d72c1b2..4f3e023 100644 --- a/src/chsrc-main.c +++ b/src/chsrc-main.c @@ -23,17 +23,15 @@ * | juzeon * | * Created On : <2023-08-28> - * Last Modified : <2025-03-04> + * Last Modified : <2025-03-07> * * chsrc: Change Source —— 全平台通用命令行换源工具 * ------------------------------------------------------------*/ -#define Chsrc_Version "0.2.0-dev1" -#define Chsrc_Release_Date "2024/12/18" #define Chsrc_Maintain_URL "https://github.com/RubyMetric/chsrc" #define Chsrc_Maintain_URL2 "https://gitee.com/RubyMetric/chsrc" -#include "framework/core.c" +#include "chsrc-framework.h" #include "recipe/lang/Ruby.c" diff --git a/src/framework/chsrc-framework.h b/src/framework/chsrc-framework.h index 5449bdd..2ef7482 100644 --- a/src/framework/chsrc-framework.h +++ b/src/framework/chsrc-framework.h @@ -14,6 +14,9 @@ * recipe 通过 include 此头文件来获得框架提供的功能 * ------------------------------------------------------------*/ +#ifndef CHSRC_FRAMEWORK_H +#define CHSRC_FRAMEWORK_H + #include #include "struct.h" #include "mirror.h" @@ -102,15 +105,12 @@ extern bool CliOpt_NoColor; void chsrc_note2 (const char *str); +#define YesMark "✓" +#define NoMark "x" +#define HalfYesMark "⍻" -enum ChgType_t -{ - ChgType_Auto, - ChgType_Reset, - ChgType_SemiAuto, - ChgType_Manual, - ChgType_Untested -}; + +#define hr() say ("--------------------------------"); int select_mirror_autoly (Source_t *sources, size_t size, const char *target_name); @@ -167,6 +167,12 @@ void chsrc_confirm_source (Source_t *source); bool is_url (const char *str); + + +#define Quiet_When_Exist 0x00 +#define Noisy_When_Exist 0x01 +#define Quiet_When_NonExist 0x00 +#define Noisy_When_NonExist 0x10 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); @@ -175,29 +181,39 @@ void chsrc_ensure_program (char *prog_name); bool chsrc_check_file (char *path); + +enum ChgType_t +{ + ChgType_Auto, + ChgType_Reset, + ChgType_SemiAuto, + ChgType_Manual, + ChgType_Untested +}; + 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_ensure_root (); +void chsrc_ensure_dir (const char *dir); + 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 (); + + +#endif diff --git a/src/framework/core.c b/src/framework/core.c index 413f923..131254c 100644 --- a/src/framework/core.c +++ b/src/framework/core.c @@ -64,9 +64,6 @@ log_backup_op (const char *filename) xy_log_brkt (blue(App_Name), bdblue(msg), xy_strjoin (3, bdyellow(filename), " -> ", bdgreen(bak))); } -#define YesMark "✓" -#define NoMark "x" -#define HalfYesMark "⍻" static void @@ -144,11 +141,6 @@ is_url (const char *str) -#define Quiet_When_Exist 0x00 -#define Noisy_When_Exist 0x01 -#define Quiet_When_NonExist 0x00 -#define Noisy_When_NonExist 0x10 - /** * 检测二进制程序是否存在 * @@ -717,8 +709,6 @@ is_reset_mode () -#define hr() say ("--------------------------------"); - /** * 用于 _setsrc 函数 diff --git a/src/recipe/lang/Clojure.c b/src/recipe/lang/Clojure.c index 25c49de..b606809 100644 --- a/src/recipe/lang/Clojure.c +++ b/src/recipe/lang/Clojure.c @@ -7,6 +7,8 @@ * Last Modified : <2024-08-09> * ------------------------------------------------------------*/ +#include + /** * @update 2023-09-10 * @note 不太好换,且用户可能不多,我们暂时只给用户提供文档 diff --git a/src/recipe/lang/Go.c b/src/recipe/lang/Go.c index 79e05a6..da1c66e 100644 --- a/src/recipe/lang/Go.c +++ b/src/recipe/lang/Go.c @@ -9,6 +9,8 @@ * Last Modified : <2024-12-18> * ------------------------------------------------------------*/ +#include + static SourceProvider_t pl_go_upstream = { def_upstream, "https://pkg.go.dev/", @@ -44,11 +46,11 @@ def_sources_n(pl_go); -void +static void pl_go_check_cmd () { char *check_cmd = xy_str_to_quietcmd ("go version"); - bool exist = query_program_exist (check_cmd, "go", Noisy_When_Exist|Noisy_When_NonExist); + bool exist = chsrc_query_program_exist ("go", check_cmd, Noisy_When_Exist|Noisy_When_NonExist); if (!exist) { diff --git a/src/recipe/lang/Haskell.c b/src/recipe/lang/Haskell.c index dbc43a2..8eb1cab 100644 --- a/src/recipe/lang/Haskell.c +++ b/src/recipe/lang/Haskell.c @@ -7,6 +7,8 @@ * Last Modified : <2024-08-15> * ------------------------------------------------------------*/ +#include + /** * @update 2023-09-10 */ diff --git a/src/recipe/lang/Java.c b/src/recipe/lang/Java.c index 35853d0..a49b54c 100644 --- a/src/recipe/lang/Java.c +++ b/src/recipe/lang/Java.c @@ -8,6 +8,8 @@ * Last Modified : <2024-12-18> * ------------------------------------------------------------*/ +#include + static SourceProvider_t pl_java_upstream = { def_upstream, "https://mvnrepository.com/", @@ -27,7 +29,7 @@ static Source_t pl_java_sources[] = def_sources_n(pl_java); -void +static void pl_java_check_cmd (bool *maven_exist, bool *gradle_exist) { *maven_exist = chsrc_check_program ("mvn"); @@ -40,7 +42,7 @@ pl_java_check_cmd (bool *maven_exist, bool *gradle_exist) } } -char * +static char * pl_java_find_maven_config () { char *buf = xy_run ("mvn -v", 2); diff --git a/src/recipe/lang/Julia.c b/src/recipe/lang/Julia.c index ff135da..794ab67 100644 --- a/src/recipe/lang/Julia.c +++ b/src/recipe/lang/Julia.c @@ -7,6 +7,8 @@ * Last Modified : <2024-11-22> * ------------------------------------------------------------*/ +#include + /** * @update 2023-09-05 * @note 缺少商业公司或开源社区软件源 diff --git a/src/recipe/lang/Lua.c b/src/recipe/lang/Lua.c index 1b883e9..21adbe1 100644 --- a/src/recipe/lang/Lua.c +++ b/src/recipe/lang/Lua.c @@ -7,6 +7,8 @@ * Last Modified : <2024-08-09> * ------------------------------------------------------------*/ +#include + static MirrorSite_t Api7 = { "api7", "api7.ai", "深圳支流科技有限公司", "https://www.apiseven.com/", diff --git a/src/recipe/lang/Node.js/Yarn.c b/src/recipe/lang/Node.js/Yarn.c index 061d4bc..1508849 100644 --- a/src/recipe/lang/Node.js/Yarn.c +++ b/src/recipe/lang/Node.js/Yarn.c @@ -43,7 +43,7 @@ pl_nodejs_yarn_setsrc (char *option) Source_t source; chsrc_yield_for_the_source (pl_nodejs); if (ProgMode_Target_Group!=true) - chsrc_confirm_source; + chsrc_confirm_source(&source); char *cmd = NULL; diff --git a/src/recipe/lang/Node.js/npm.c b/src/recipe/lang/Node.js/npm.c index 92554f0..16d3c06 100644 --- a/src/recipe/lang/Node.js/npm.c +++ b/src/recipe/lang/Node.js/npm.c @@ -29,7 +29,7 @@ pl_nodejs_npm_setsrc (char *option) Source_t source; chsrc_yield_for_the_source (pl_nodejs); if (ProgMode_Target_Group!=true) - chsrc_confirm_source; + chsrc_confirm_source(&source); char *cmd = NULL; diff --git a/src/recipe/lang/Node.js/pnpm.c b/src/recipe/lang/Node.js/pnpm.c index 6f8e420..c7833d2 100644 --- a/src/recipe/lang/Node.js/pnpm.c +++ b/src/recipe/lang/Node.js/pnpm.c @@ -30,7 +30,7 @@ pl_nodejs_pnpm_setsrc (char *option) Source_t source; chsrc_yield_for_the_source (pl_nodejs); if (ProgMode_Target_Group!=true) - chsrc_confirm_source; + chsrc_confirm_source(&source); char *cmd = NULL; diff --git a/src/recipe/lang/NuGet.c b/src/recipe/lang/NuGet.c index bc31c49..fa42b7b 100644 --- a/src/recipe/lang/NuGet.c +++ b/src/recipe/lang/NuGet.c @@ -7,6 +7,8 @@ * Last Modified : <2024-08-09> * ------------------------------------------------------------*/ +#include + static MirrorSite_t NugetOrg = { "nuget.org", "NuGet Org", "Nuget Organization", "https://www.nuget.org/", diff --git a/src/recipe/lang/OCaml.c b/src/recipe/lang/OCaml.c index 80d7bb4..880752e 100644 --- a/src/recipe/lang/OCaml.c +++ b/src/recipe/lang/OCaml.c @@ -7,6 +7,8 @@ * Last Modified : <2024-08-15> * ------------------------------------------------------------*/ +#include + /** * @update 2023-09-15 */ @@ -18,7 +20,7 @@ static Source_t pl_ocaml_sources[] = def_sources_n(pl_ocaml); -void +static void pl_ocaml_check_cmd () { chsrc_ensure_program ("opam"); diff --git a/src/recipe/lang/PHP.c b/src/recipe/lang/PHP.c index 5b442b4..74a0e6b 100644 --- a/src/recipe/lang/PHP.c +++ b/src/recipe/lang/PHP.c @@ -7,6 +7,8 @@ * Last Modified : <2024-11-22> * ------------------------------------------------------------*/ +#include + /** * @update 2024-09-14 * @note 缺少教育网或开源社区软件源 diff --git a/src/recipe/lang/Perl.c b/src/recipe/lang/Perl.c index 616739e..608df8c 100644 --- a/src/recipe/lang/Perl.c +++ b/src/recipe/lang/Perl.c @@ -8,6 +8,8 @@ * Last Modified : <2024-12-18> * ------------------------------------------------------------*/ +#include + static SourceProvider_t pl_perl_upstream = { def_upstream, "https://metacpan.org/", @@ -30,7 +32,7 @@ static Source_t pl_perl_sources[] = def_sources_n(pl_perl); -void +static void pl_perl_check_cmd () { chsrc_ensure_program ("perl"); diff --git a/src/recipe/lang/Python/PDM.c b/src/recipe/lang/Python/PDM.c index 03def58..98f9384 100644 --- a/src/recipe/lang/Python/PDM.c +++ b/src/recipe/lang/Python/PDM.c @@ -31,7 +31,7 @@ pl_python_pdm_setsrc (char *option) Source_t source; chsrc_yield_for_the_source (pl_python); if (ProgMode_Target_Group!=true) - chsrc_confirm_source; + chsrc_confirm_source(&source); char *cmd = NULL; diff --git a/src/recipe/lang/Python/Poetry.c b/src/recipe/lang/Python/Poetry.c index e5fe417..3688d15 100644 --- a/src/recipe/lang/Python/Poetry.c +++ b/src/recipe/lang/Python/Poetry.c @@ -30,7 +30,7 @@ pl_python_poetry_setsrc (char *option) Source_t source; chsrc_yield_for_the_source (pl_python); if (ProgMode_Target_Group!=true) - chsrc_confirm_source; + chsrc_confirm_source(&source); char *cmd = NULL; diff --git a/src/recipe/lang/Python/pip.c b/src/recipe/lang/Python/pip.c index c197dae..c509459 100644 --- a/src/recipe/lang/Python/pip.c +++ b/src/recipe/lang/Python/pip.c @@ -41,7 +41,7 @@ pl_python_pip_setsrc (char *option) Source_t source; chsrc_yield_for_the_source (pl_python); if (ProgMode_Target_Group!=true) - chsrc_confirm_source; + chsrc_confirm_source(&source); char *py_prog_name = NULL; pl_python_get_py_program_name (&py_prog_name); diff --git a/src/recipe/lang/R.c b/src/recipe/lang/R.c index a7243b8..2c8e1ac 100644 --- a/src/recipe/lang/R.c +++ b/src/recipe/lang/R.c @@ -7,6 +7,8 @@ * Last Modified : <2024-11-22> * ------------------------------------------------------------*/ +#include + /** * @update 2023-09-04 * @note { diff --git a/src/recipe/lang/Ruby.c b/src/recipe/lang/Ruby.c index e1ec0fe..889f0e3 100644 --- a/src/recipe/lang/Ruby.c +++ b/src/recipe/lang/Ruby.c @@ -8,6 +8,8 @@ * Last Modified : <2024-12-18> * ------------------------------------------------------------*/ +#include + static SourceProvider_t pl_ruby_upstream = { def_upstream, "https://rubygems.org", @@ -51,7 +53,7 @@ pl_ruby_getsrc (char *option) chsrc_run ("bundle config get mirror.https://rubygems.org", RunOpt_Default); } -void +static void pl_ruby_remove_gem_source (const char *source) { char *cmd = NULL; diff --git a/src/recipe/os/BSD/FreeBSD.c b/src/recipe/os/BSD/FreeBSD.c index c154859..d8ca02c 100644 --- a/src/recipe/os/BSD/FreeBSD.c +++ b/src/recipe/os/BSD/FreeBSD.c @@ -42,7 +42,7 @@ os_freebsd_setsrc (char *option) int index = use_specific_mirror_or_auto_select (option, os_freebsd); Source_t source = os_freebsd_sources[index]; - chsrc_confirm_source; + chsrc_confirm_source(&source); chsrc_log2 ("1. 添加 freebsd-pkg 源 (二进制安装包)"); chsrc_ensure_dir ("/usr/local/etc/pkg/repos"); @@ -67,7 +67,7 @@ os_freebsd_setsrc (char *option) // @ccmywish: [2023-09-27] 据 @ykla , NJU的freebsd-ports源没有设置 Git, // 但是我认为由于使用Git还是要比非Git方便许多,我们尽可能坚持使用Git // 而 gitup 又要额外修改它自己的配置,比较麻烦 - bool git_exist = query_program_exist (xy_str_to_quietcmd ("git version"), "git", Noisy_When_Exist|Noisy_When_NonExist); + bool git_exist = chsrc_query_program_exist ("git", xy_str_to_quietcmd ("git version"), Noisy_When_Exist|Noisy_When_NonExist); if (git_exist) { if (xy_streql("nju",source.mirror->code)) diff --git a/src/recipe/ware/Anaconda.c b/src/recipe/ware/Anaconda.c index 55128c0..7da3d39 100644 --- a/src/recipe/ware/Anaconda.c +++ b/src/recipe/ware/Anaconda.c @@ -23,7 +23,7 @@ static Source_t wr_anaconda_sources[] = {&Zju, "https://mirrors.zju.edu.cn/anaconda/"}, {&Sjtug_Zhiyuan, "https://mirror.sjtu.edu.cn/anaconda"}, {&Pku, "https://mirrors.pku.edu.cn/anaconda/"}, - {&NJTech, "https://mirrors.njtech.edu.cn/anaconda/"}, + {&Njtech, "https://mirrors.njtech.edu.cn/anaconda/"}, }; def_sources_n(wr_anaconda);