From 2e33ef697e72d6627c889a29b541ed126484c788 Mon Sep 17 00:00:00 2001 From: Aoran Zeng Date: Fri, 13 Sep 2024 22:51:11 +0800 Subject: [PATCH] Only measure once when in group for `Node.js` [GitHub #78] --- src/chsrc-framework.h | 44 ++++++++++++++++++++++--------- src/recipe/lang/Node.js/Node.js.c | 34 ++++++++---------------- src/recipe/lang/Node.js/Yarn.c | 21 +++++---------- src/recipe/lang/Node.js/common.h | 19 ++++++++++++- src/recipe/lang/Node.js/npm.c | 23 +++++----------- src/recipe/lang/Node.js/pnpm.c | 23 +++++----------- 6 files changed, 81 insertions(+), 83 deletions(-) diff --git a/src/chsrc-framework.h b/src/chsrc-framework.h index 25fa5b4..cff953c 100644 --- a/src/chsrc-framework.h +++ b/src/chsrc-framework.h @@ -7,9 +7,9 @@ * Contributors : Peng Gao * | * Created On : <2023-08-29> - * Last Modified : <2024-09-10> + * Last Modified : <2024-09-13> * - * chsrc 头文件 + * chsrc 框架 * ------------------------------------------------------------*/ #include "xy.h" @@ -23,6 +23,9 @@ static int chsrc_get_cpucore (); bool ProgMode_CMD_Measure = false; bool ProgMode_CMD_Reset = false; +bool ProgMode_Target_Group = false; +int ProgMode_Leader_Selected_Index = -1; + /* 命令行选项 */ bool CliOpt_IPv6 = false; @@ -748,19 +751,31 @@ source_has_empty_url (SourceInfo *source) return source->url == NULL; } + + /** - * 用户*只可能*通过下面三种方式来换源,无论哪一种都会返回一个 SourceInfo 出来 - * - * 1. 用户指定 MirrorCode - * 2. 用户什么都没指定 (将测速选择最快镜像) - * 3. 用户给了一个 URL + * 用户*只可能*通过下面5种方式来换源,无论哪一种都会返回一个 SourceInfo 出来 + * option: + * 1. 用户指定某个 MirrorCode + * 2. NULL: 用户什么都没指定 (将测速选择最快镜像) + * 3. 用户给了一个 URL + * 4. ChsrcTypeReset + * 选用了Leader target + * 5. ProgMode_Leader_Selected_Index 将给出所选索引 * * @dependency 变量 option - * @dependency 变量 source */ -#define chsrc_yield_source(for_what) \ - SourceInfo source; \ - if (is_url (option)) \ +#define chsrc_yield_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)) \ { \ SourceInfo __tmp = { &UserDefine, option }; \ source = __tmp; \ @@ -771,6 +786,11 @@ source_has_empty_url (SourceInfo *source) source = for_what##_sources[__index]; \ } +#define chsrc_yield_source(for_what) \ + SourceInfo source; \ + chsrc_yield_the_source(for_what) + + #define split_between_source_changing_process puts ("--------------------------------") @@ -793,7 +813,7 @@ confirm_source (SourceInfo *source) // 之所以禁用,是因为有的 reset 我们并没有实现,我们在这里阻止这些邪恶的用户 if (source_is_upstream (source) && source_has_empty_url (source)) { - char *msg = CliOpt_InEnglish ? "Not implement RESET for the target yet" : "暂未对该目标实现重置"; + char *msg = CliOpt_InEnglish ? "Not implement `reset` for the target yet" : "暂未对该目标实现重置"; chsrc_error (msg); exit (Exit_Unsupported); } diff --git a/src/recipe/lang/Node.js/Node.js.c b/src/recipe/lang/Node.js/Node.js.c index 221f1eb..7e052da 100644 --- a/src/recipe/lang/Node.js/Node.js.c +++ b/src/recipe/lang/Node.js/Node.js.c @@ -5,26 +5,9 @@ * Contributors : Mr. Will * Created On : <2023-08-30> * Major Revision : 2 - * Last Modified : <2024-09-10> + * Last Modified : <2024-09-13> * ------------------------------------------------------------*/ -/** - * @time 2024-04-18 更新 - * @note { - * Sjtug, Tuna, Lzuoss, Jlu, Bfsu, 网易,搜狐 都没有 - * 腾讯软件源的npm源一直未证实是否可用 - * } - */ -static SourceInfo -pl_nodejs_sources[] = { - {&Upstream, "https://registry.npmjs.org/"}, - {&NpmMirror, "https://registry.npmmirror.com"}, - {&Huawei, "https://mirrors.huaweicloud.com/repository/npm/"}, - {&Zju, "https://mirrors.zju.edu.cn/npm"} -}; -def_sources_n(pl_nodejs); - - void pl_nodejs_check_cmd (bool *npm_exist, bool *yarn_exist, bool *pnpm_exist) { @@ -79,11 +62,19 @@ pl_nodejs_getsrc (char *option) void pl_nodejs_setsrc (char *option) { + { + char *msg = CliOpt_InEnglish ? "** Three package managers will be replaced for you at the same time: " \ + "npm, pnpm, yarn. If you need to change the source independently, " \ + "please run independently `chsrc set ` **\n" + : "** 将同时为您更换3个包管理器 npm, pnpm, Yarn 的源,若需要独立换源,请独立运行 chsrc set **\n"; + chsrc_log (bdyellow(msg)); + } + bool npm_exist, yarn_exist, pnpm_exist; pl_nodejs_check_cmd (&npm_exist, &yarn_exist, &pnpm_exist); - // chsrc_yield_source_and_confirm (pl_nodejs); - split_between_source_changing_process; + ProgMode_Target_Group = true; + chsrc_yield_source_and_confirm (pl_nodejs); if (npm_exist) { @@ -100,10 +91,7 @@ pl_nodejs_setsrc (char *option) if (pnpm_exist) { pl_nodejs_pnpm_setsrc (option); - say (""); } - - // chsrc_conclude (&source, ChsrcTypeAuto); } diff --git a/src/recipe/lang/Node.js/Yarn.c b/src/recipe/lang/Node.js/Yarn.c index 941ebf3..2fac5cd 100644 --- a/src/recipe/lang/Node.js/Yarn.c +++ b/src/recipe/lang/Node.js/Yarn.c @@ -8,19 +8,6 @@ * Last Modified : <2024-09-13> * ------------------------------------------------------------*/ -/** - * @update 2024-04-08 - */ -static SourceInfo -pl_nodejs_yarn_sources[] = { - {&Upstream, "https://registry.npmjs.org/"}, // @note 根据 pnpm 官网,有最后的斜线 - {&NpmMirror, "https://registry.npmmirror.com"}, - {&Huawei, "https://mirrors.huaweicloud.com/repository/npm/"}, - {&Zju, "https://mirrors.zju.edu.cn/npm"} -}; -def_sources_n(pl_nodejs_yarn); - - static double pl_nodejs_yarn_get_yarn_version () { @@ -53,7 +40,10 @@ pl_nodejs_yarn_getsrc (char *option) void pl_nodejs_yarn_setsrc (char *option) { - chsrc_yield_source_and_confirm (pl_nodejs_yarn); + SourceInfo source; + chsrc_yield_the_source (pl_nodejs); + if (ProgMode_Target_Group!=true) + chsrc_confirm_source; char *cmd = NULL; @@ -115,4 +105,5 @@ pl_nodejs_yarn_feat (char *option) return fi; } -def_target_gsrf(pl_nodejs_yarn); +// def_target_gsrf(pl_nodejs_yarn); +TargetInfo pl_nodejs_yarn_target = {def_target_inner_gsrf(pl_nodejs_yarn),def_target_sourcesn(pl_nodejs)}; diff --git a/src/recipe/lang/Node.js/common.h b/src/recipe/lang/Node.js/common.h index 1dee9f0..9dfa2ea 100644 --- a/src/recipe/lang/Node.js/common.h +++ b/src/recipe/lang/Node.js/common.h @@ -5,10 +5,27 @@ * Contributors : Nul None * Created On : <2023-09-09> * Major Revision : 1 - * Last Modified : <2024-09-10> + * Last Modified : <2024-09-13> * ------------------------------------------------------------*/ static MirrorSite NpmMirror = {"npmmirror", "npmmirror", "npmmirror (阿里云赞助)", "https://npmmirror.com/", // 注意,这个是跳转后的地址,不确定未来会不会改变 "https://cdn.npmmirror.com/packages/%40tensorflow/tfjs/4.10.0/tfjs-4.10.0.tgz"}; // 29MB + + +/** + * @time 2024-04-18 更新 + * @note { + * Sjtug, Tuna, Lzuoss, Jlu, Bfsu, 网易,搜狐 都没有 + * 腾讯软件源的npm源一直未证实是否可用 + * } + */ +static SourceInfo +pl_nodejs_sources[] = { + {&Upstream, "https://registry.npmjs.org/"}, // @note 根据 pnpm 官网,有最后的斜线 + {&NpmMirror, "https://registry.npmmirror.com"}, + {&Huawei, "https://mirrors.huaweicloud.com/repository/npm/"}, + {&Zju, "https://mirrors.zju.edu.cn/npm"} +}; +def_sources_n(pl_nodejs); diff --git a/src/recipe/lang/Node.js/npm.c b/src/recipe/lang/Node.js/npm.c index bef3480..7ff5fa2 100644 --- a/src/recipe/lang/Node.js/npm.c +++ b/src/recipe/lang/Node.js/npm.c @@ -5,22 +5,9 @@ * Contributors : Mr. Will * Created On : <2023-08-30> * Major Revision : 2 - * Last Modified : <2024-09-10> + * Last Modified : <2024-09-13> * ------------------------------------------------------------*/ -/** - * @update 2024-04-08 - */ -static SourceInfo -pl_nodejs_npm_sources[] = { - {&Upstream, "https://registry.npmjs.org/"}, // @note 根据 pnpm 官网,有最后的斜线 - {&NpmMirror, "https://registry.npmmirror.com"}, - {&Huawei, "https://mirrors.huaweicloud.com/repository/npm/"}, - {&Zju, "https://mirrors.zju.edu.cn/npm"} -}; -def_sources_n(pl_nodejs_npm); - - /** * chsrc get npm */ @@ -39,7 +26,10 @@ pl_nodejs_npm_getsrc (char *option) void pl_nodejs_npm_setsrc (char *option) { - chsrc_yield_source_and_confirm (pl_nodejs_npm); + SourceInfo source; + chsrc_yield_the_source (pl_nodejs); + if (ProgMode_Target_Group!=true) + chsrc_confirm_source; char *cmd = NULL; @@ -84,4 +74,5 @@ pl_nodejs_npm_feat (char *option) return fi; } -def_target_gsrf(pl_nodejs_npm); +// def_target_gsrf(pl_nodejs_npm); +TargetInfo pl_nodejs_npm_target = {def_target_inner_gsrf(pl_nodejs_npm),def_target_sourcesn(pl_nodejs)}; diff --git a/src/recipe/lang/Node.js/pnpm.c b/src/recipe/lang/Node.js/pnpm.c index fa741da..f3cad47 100644 --- a/src/recipe/lang/Node.js/pnpm.c +++ b/src/recipe/lang/Node.js/pnpm.c @@ -5,22 +5,9 @@ * Contributors : Nul None * Created On : <2024-04-18> * Major Revision : 2 - * Last Modified : <2024-09-10> + * Last Modified : <2024-09-13> * ------------------------------------------------------------*/ -/** - * @update 2024-04-08 - */ -static SourceInfo -pl_nodejs_pnpm_sources[] = { - {&Upstream, "https://registry.npmjs.org/"}, // @note 根据 pnpm 官网,有最后的斜线 - {&NpmMirror, "https://registry.npmmirror.com"}, - {&Huawei, "https://mirrors.huaweicloud.com/repository/npm/"}, - {&Zju, "https://mirrors.zju.edu.cn/npm"} -}; -def_sources_n(pl_nodejs_pnpm); - - /** * chsrc get pnpm */ @@ -40,7 +27,10 @@ pl_nodejs_pnpm_getsrc (char *option) void pl_nodejs_pnpm_setsrc (char *option) { - chsrc_yield_source_and_confirm (pl_nodejs_pnpm); + SourceInfo source; + chsrc_yield_the_source (pl_nodejs); + if (ProgMode_Target_Group!=true) + chsrc_confirm_source; char *cmd = NULL; @@ -86,4 +76,5 @@ pl_nodejs_pnpm_feat (char *option) } -def_target_gsrf(pl_nodejs_pnpm); +// def_target_gsrf(pl_nodejs_pnpm); +TargetInfo pl_nodejs_pnpm_target = {def_target_inner_gsrf(pl_nodejs_pnpm),def_target_sourcesn(pl_nodejs)};