2023-08-29 15:54:21 +08:00
|
|
|
|
/* --------------------------------------------------------------
|
|
|
|
|
* File : chsrc.c
|
|
|
|
|
* Authors : Aoran Zeng <ccmywish@qq.com>
|
|
|
|
|
* Created on : <2023-08-28>
|
2023-08-30 08:31:46 +08:00
|
|
|
|
* Last modified : <2023-08-30>
|
2023-08-29 15:54:21 +08:00
|
|
|
|
*
|
|
|
|
|
* chsrc:
|
|
|
|
|
*
|
|
|
|
|
* Change Source —— 换源命令行工具
|
|
|
|
|
* -------------------------------------------------------------*/
|
|
|
|
|
|
2023-08-29 23:07:48 +08:00
|
|
|
|
#include "chsrc.h"
|
2023-08-28 22:21:33 +08:00
|
|
|
|
|
|
|
|
|
#define Chsrc_Version "v0.1.0"
|
|
|
|
|
|
|
|
|
|
|
2023-08-30 17:10:23 +08:00
|
|
|
|
/**
|
|
|
|
|
* 检测二进制程序是否存在
|
|
|
|
|
*
|
|
|
|
|
* @param[in] check_cmd 检测 `progname` 是否存在的一段命令,一般来说,填 `progname` 本身即可,
|
|
|
|
|
* 但是某些情况下,需要使用其他命令绕过一些特殊情况,比如 python 这个命令在Windows上
|
|
|
|
|
* 会自动打开 Microsoft Store,需避免
|
|
|
|
|
*
|
|
|
|
|
* @param[in] progname 要检测的二进制程序名
|
2023-08-30 15:37:02 +08:00
|
|
|
|
*/
|
|
|
|
|
bool
|
2023-08-30 17:10:23 +08:00
|
|
|
|
does_the_program_exist (char* check_cmd, char* progname)
|
2023-08-30 15:37:02 +08:00
|
|
|
|
{
|
|
|
|
|
char* which = check_cmd;
|
|
|
|
|
|
|
|
|
|
int ret = system(which);
|
|
|
|
|
|
|
|
|
|
char buf[32] = {0};
|
2023-08-30 17:10:23 +08:00
|
|
|
|
sprintf(buf, "错误码: %d", ret);
|
2023-08-30 15:37:02 +08:00
|
|
|
|
|
|
|
|
|
if (0!=ret) {
|
2023-08-30 20:34:01 +08:00
|
|
|
|
xy_warn (xy_strjoin(4, "× 命令 ", progname, " 不存在,", buf));
|
2023-08-30 15:37:02 +08:00
|
|
|
|
return false;
|
|
|
|
|
} else {
|
2023-08-30 20:34:01 +08:00
|
|
|
|
xy_success (xy_strjoin(3, "√ 命令 ", progname, " 存在"));
|
2023-08-30 15:37:02 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-08-28 22:21:33 +08:00
|
|
|
|
/**
|
|
|
|
|
* Python换源
|
|
|
|
|
*
|
|
|
|
|
* 参考:https://mirrors.tuna.tsinghua.edu.cn/help/pypi/
|
2023-08-30 17:10:23 +08:00
|
|
|
|
*
|
|
|
|
|
* 经测试,Windows上调用换源命令,会写入 C:\Users\RubyMetric\AppData\Roaming\pip\pip.ini
|
2023-08-28 22:21:33 +08:00
|
|
|
|
*/
|
|
|
|
|
void
|
2023-08-30 11:33:23 +08:00
|
|
|
|
pl_python_chsrc (char* option)
|
2023-08-28 22:21:33 +08:00
|
|
|
|
{
|
2023-08-30 17:10:23 +08:00
|
|
|
|
int selected = 0; char* check_cmd, *prog = NULL;
|
|
|
|
|
|
|
|
|
|
// 不要调用 python 自己,而是使用 python --version,避免Windows弹出Microsoft Store
|
|
|
|
|
if (xy_on_windows) check_cmd = "python --version >nul 2>nul";
|
|
|
|
|
else check_cmd = "python --version 1>/dev/null 2>&1";
|
|
|
|
|
|
|
|
|
|
bool exist_b = does_the_program_exist (check_cmd, "python");
|
|
|
|
|
|
|
|
|
|
if (!exist_b) {
|
|
|
|
|
if (xy_on_windows) check_cmd = "python3 --version >nul 2>nul";
|
|
|
|
|
else check_cmd = "python3 --version 1>/dev/null 2>&1";
|
|
|
|
|
exist_b = does_the_program_exist (check_cmd, "python3");
|
|
|
|
|
if (exist_b) prog = "python3";
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
prog = "python";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!exist_b) {
|
|
|
|
|
xy_error ("chsrc: 未找到 Python 相关命令,请检查是否存在");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-08-28 23:10:09 +08:00
|
|
|
|
|
2023-08-30 11:33:23 +08:00
|
|
|
|
for (int i=0;i<sizeof(pl_ruby_sources);i++) {
|
|
|
|
|
// 循环测速
|
2023-08-28 23:10:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-30 11:33:23 +08:00
|
|
|
|
const char* source_name = pl_ruby_sources[selected].mirror->name;
|
|
|
|
|
const char* source_abbr = pl_ruby_sources[selected].mirror->abbr;
|
|
|
|
|
const char* source_url = pl_ruby_sources[selected].url;
|
|
|
|
|
|
2023-08-30 20:34:01 +08:00
|
|
|
|
xy_info (xy_2strjoin("chsrc: 选中镜像站:", source_abbr));
|
2023-08-28 22:21:33 +08:00
|
|
|
|
|
2023-08-30 20:34:01 +08:00
|
|
|
|
char* cmd = xy_2strjoin(prog, xy_2strjoin(" -m pip config set global.index-url ", source_url));
|
2023-08-28 22:21:33 +08:00
|
|
|
|
system(cmd);
|
|
|
|
|
free(cmd);
|
2023-08-30 11:33:23 +08:00
|
|
|
|
|
2023-08-30 20:34:01 +08:00
|
|
|
|
xy_success(xy_2strjoin("chsrc: 感谢镜像提供方:", source_name));
|
2023-08-28 22:21:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Ruby换源
|
|
|
|
|
*
|
|
|
|
|
* 参考:https://gitee.com/RubyKids/rbenv-cn
|
|
|
|
|
*/
|
|
|
|
|
void
|
2023-08-30 08:31:46 +08:00
|
|
|
|
pl_ruby_chsrc (char* option)
|
2023-08-28 22:21:33 +08:00
|
|
|
|
{
|
2023-08-29 23:07:48 +08:00
|
|
|
|
int selected = 0;
|
|
|
|
|
for (int i=0;i<sizeof(pl_ruby_sources);i++) {
|
|
|
|
|
// 循环测速
|
2023-08-28 23:10:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-29 23:07:48 +08:00
|
|
|
|
const char* source_name = pl_ruby_sources[selected].mirror->name;
|
|
|
|
|
const char* source_abbr = pl_ruby_sources[selected].mirror->abbr;
|
|
|
|
|
const char* source_url = pl_ruby_sources[selected].url;
|
2023-08-28 22:21:33 +08:00
|
|
|
|
|
2023-08-30 20:34:01 +08:00
|
|
|
|
xy_info (xy_2strjoin("chsrc: 选中镜像站:", source_abbr));
|
2023-08-28 22:21:33 +08:00
|
|
|
|
|
2023-08-30 11:33:23 +08:00
|
|
|
|
xy_info("chsrc: 为 gem 命令换源");
|
2023-08-29 23:07:48 +08:00
|
|
|
|
system("gem source -r https://rubygems.org/");
|
2023-08-28 22:21:33 +08:00
|
|
|
|
|
2023-08-30 20:34:01 +08:00
|
|
|
|
char* cmd = xy_2strjoin("gem source -a ", source_url);
|
2023-08-28 22:21:33 +08:00
|
|
|
|
system(cmd);
|
|
|
|
|
free(cmd);
|
|
|
|
|
|
2023-08-30 20:34:01 +08:00
|
|
|
|
cmd = xy_2strjoin("bundle config 'mirror.https://rubygems.org' ", source_url);
|
2023-08-30 11:33:23 +08:00
|
|
|
|
xy_info("chsrc: 为 bundler 命令换源");
|
2023-08-28 22:21:33 +08:00
|
|
|
|
system(cmd);
|
|
|
|
|
free(cmd);
|
2023-08-29 23:07:48 +08:00
|
|
|
|
|
2023-08-30 20:34:01 +08:00
|
|
|
|
xy_success(xy_2strjoin("chsrc: 感谢镜像提供方:", source_name));
|
2023-08-28 22:21:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-30 20:05:03 +08:00
|
|
|
|
void
|
2023-08-30 17:12:57 +08:00
|
|
|
|
os_ubuntu_chsrc (char* option)
|
2023-08-30 14:24:26 +08:00
|
|
|
|
{
|
|
|
|
|
int selected = 0;
|
2023-08-30 20:05:03 +08:00
|
|
|
|
for (int i=0;i<sizeof(os_ubuntu_sources);i++) {
|
2023-08-30 14:24:26 +08:00
|
|
|
|
// 循环测速
|
|
|
|
|
}
|
2023-08-30 20:05:03 +08:00
|
|
|
|
const char* source_name = os_ubuntu_sources[selected].mirror->name;
|
|
|
|
|
const char* source_abbr = os_ubuntu_sources[selected].mirror->abbr;
|
|
|
|
|
const char* source_url = os_ubuntu_sources[selected].url;
|
|
|
|
|
|
2023-08-30 20:34:01 +08:00
|
|
|
|
char* backup = "cp -rf /etc/apt/sources.list /etc/apt/sources.list.bak";
|
|
|
|
|
system(backup);
|
|
|
|
|
|
|
|
|
|
xy_info ("chsrc: 备份文件名: /etc/apt/sources.list.bak");
|
2023-08-30 17:12:57 +08:00
|
|
|
|
const char* current_url = xy_strch(source_url,'/',"\\/");
|
2023-08-30 14:24:26 +08:00
|
|
|
|
|
2023-08-30 20:34:01 +08:00
|
|
|
|
char* cmd = xy_strjoin(3, "sed -E \'s/(^[^#]* .*)http[:|\\.|\\/|a-z|A-Z]*\\/ubuntu\\//\\1",
|
|
|
|
|
current_url,
|
|
|
|
|
"/\'< /etc/apt/sources.list.bak | cat > /etc/apt/sources.list");
|
2023-08-30 14:24:26 +08:00
|
|
|
|
system(cmd);
|
|
|
|
|
free(cmd);
|
|
|
|
|
|
|
|
|
|
char* rm = "rm -rf /etc/apt/source.list.bak";
|
|
|
|
|
system(rm);
|
2023-08-30 20:05:03 +08:00
|
|
|
|
// free(rm);
|
2023-08-30 14:24:26 +08:00
|
|
|
|
|
2023-08-30 20:34:01 +08:00
|
|
|
|
xy_info ("chsrc: 为'ubuntu'命令换源");
|
|
|
|
|
xy_success (xy_2strjoin("chsrc: 感谢镜像提供方:", source_name));
|
2023-08-30 14:24:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-30 20:34:01 +08:00
|
|
|
|
|
2023-08-30 15:37:02 +08:00
|
|
|
|
#define chsrcfunc(func) (const char const*)func
|
2023-08-28 22:43:37 +08:00
|
|
|
|
static const char const
|
2023-08-30 17:10:23 +08:00
|
|
|
|
*pl_ruby[] = {"gem", "ruby", "rb", NULL, chsrcfunc(pl_ruby_chsrc) },
|
|
|
|
|
*pl_python[] = {"pip", "python", "py", "pypi", NULL, chsrcfunc(pl_python_chsrc)},
|
2023-08-28 22:43:37 +08:00
|
|
|
|
*pl_nodejs[] = {"npm", "node", "nodejs", "js", NULL},
|
|
|
|
|
*pl_perl[] = {"perl", "cpan", NULL},
|
|
|
|
|
*pl_php[] = {"php", "composer", NULL},
|
|
|
|
|
*pl_cran[] = {"r", "cran", NULL},
|
|
|
|
|
*pl_rust[] = {"rust", "cargo", "crate", "crates", NULL},
|
|
|
|
|
*pl_go[] = {"go", "golang", "goproxy", NULL},
|
|
|
|
|
*pl_dotnet[] = {"nuget","net", "dotnet", ".net", NULL},
|
|
|
|
|
*pl_maven[] = {"maven", NULL},
|
|
|
|
|
*pl_gradle[] = {"gradel",NULL},
|
|
|
|
|
*pl_julia[] = {"julia", NULL},
|
2023-08-30 17:12:57 +08:00
|
|
|
|
|
2023-08-30 20:05:03 +08:00
|
|
|
|
*os_ubuntu[] = {"ubuntu", NULL, chsrcfunc(os_ubuntu_chsrc)},
|
2023-08-28 22:43:37 +08:00
|
|
|
|
// Java暂时需要直接指定包管理器
|
|
|
|
|
// pl_java
|
|
|
|
|
**pl_packagers[] = {
|
|
|
|
|
pl_ruby, pl_python, pl_nodejs, pl_perl, pl_php, pl_cran,
|
|
|
|
|
pl_rust, pl_go, pl_dotnet, pl_maven, pl_gradle, pl_julia
|
2023-08-30 17:12:57 +08:00
|
|
|
|
},
|
|
|
|
|
**os_packagers[] = {
|
2023-08-30 19:24:18 +08:00
|
|
|
|
os_ubuntu
|
2023-08-28 22:43:37 +08:00
|
|
|
|
};
|
2023-08-30 20:06:28 +08:00
|
|
|
|
|
2023-08-30 15:37:02 +08:00
|
|
|
|
#undef chsrcfunc
|
2023-08-28 22:21:33 +08:00
|
|
|
|
|
|
|
|
|
static const char const*
|
|
|
|
|
usage[] = {
|
|
|
|
|
"chsrc: Change Source " Chsrc_Version " by RubyMetric\n",
|
2023-08-30 11:33:23 +08:00
|
|
|
|
|
|
|
|
|
"维护: https://gitee.com/RubyMetric/chsrc\n",
|
|
|
|
|
|
2023-08-29 15:54:21 +08:00
|
|
|
|
"使用:\n"
|
2023-08-30 11:33:23 +08:00
|
|
|
|
" chsrc <要换源的对象> [所换源名称]\n",
|
2023-08-28 22:21:33 +08:00
|
|
|
|
|
2023-08-29 15:54:21 +08:00
|
|
|
|
"选项:\n"
|
2023-08-30 11:33:23 +08:00
|
|
|
|
" -h, --help 打印该帮助\n",
|
2023-08-28 22:21:33 +08:00
|
|
|
|
|
2023-08-29 15:54:21 +08:00
|
|
|
|
"支持:\n"
|
2023-08-28 22:21:33 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2023-08-28 22:43:37 +08:00
|
|
|
|
void
|
2023-08-29 15:54:21 +08:00
|
|
|
|
call_cmd (void* cmdptr, const char* arg)
|
2023-08-28 22:43:37 +08:00
|
|
|
|
{
|
2023-08-29 15:54:21 +08:00
|
|
|
|
void (*cmd_func)(const char*) = cmdptr;
|
2023-08-28 23:10:09 +08:00
|
|
|
|
if (NULL==arg) {
|
2023-08-30 11:33:23 +08:00
|
|
|
|
xy_info("chsrc: 将使用默认镜像");
|
2023-08-28 23:10:09 +08:00
|
|
|
|
}
|
2023-08-28 22:43:37 +08:00
|
|
|
|
cmd_func(arg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-08-28 22:21:33 +08:00
|
|
|
|
int
|
2023-08-29 15:54:21 +08:00
|
|
|
|
print_help ()
|
2023-08-28 22:21:33 +08:00
|
|
|
|
{
|
2023-08-30 08:31:46 +08:00
|
|
|
|
for (int i=0; i<Array_Size(usage); i++) {
|
2023-08-30 11:33:23 +08:00
|
|
|
|
xy_info(usage[i]);
|
2023-08-28 22:21:33 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2023-08-29 15:54:21 +08:00
|
|
|
|
main (int argc, char const *argv[])
|
2023-08-28 22:21:33 +08:00
|
|
|
|
{
|
2023-08-29 21:58:51 +08:00
|
|
|
|
xy_useutf8();
|
|
|
|
|
|
2023-08-28 23:10:09 +08:00
|
|
|
|
// 未提供参数时
|
|
|
|
|
if (argc<=1) {
|
2023-08-28 22:21:33 +08:00
|
|
|
|
print_help(); return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-28 23:10:09 +08:00
|
|
|
|
// 第一个参数
|
2023-08-29 15:54:21 +08:00
|
|
|
|
const char* target = NULL;
|
2023-08-30 11:33:23 +08:00
|
|
|
|
if (0==strcmp("-h",argv[1]) || 0==strcmp("--help",argv[1])) {
|
2023-08-28 23:10:09 +08:00
|
|
|
|
print_help(); return 0;
|
|
|
|
|
} else {
|
|
|
|
|
target = argv[1];
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-29 15:54:21 +08:00
|
|
|
|
const char* option = NULL;
|
|
|
|
|
const char* cmdarg = NULL;
|
2023-08-28 23:10:09 +08:00
|
|
|
|
// 第二个参数
|
2023-08-29 23:07:48 +08:00
|
|
|
|
if (argc>=3)
|
2023-08-28 23:10:09 +08:00
|
|
|
|
{
|
2023-08-29 23:07:48 +08:00
|
|
|
|
// printf ("argc = %d\n", argc);
|
2023-08-28 23:10:09 +08:00
|
|
|
|
if (argv[2][0]=='-') {
|
|
|
|
|
option = argv[2];
|
|
|
|
|
} else {
|
|
|
|
|
cmdarg = argv[2];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-28 22:43:37 +08:00
|
|
|
|
int matched = 0;
|
|
|
|
|
|
2023-08-30 08:31:46 +08:00
|
|
|
|
for (int i=0; i<Array_Size(pl_packagers); i++) {
|
2023-08-28 22:21:33 +08:00
|
|
|
|
const char const** packager = pl_packagers[i];
|
|
|
|
|
int k = 0;
|
|
|
|
|
const char* alias = packager[k];
|
|
|
|
|
while (NULL!=alias) {
|
2023-08-30 19:24:18 +08:00
|
|
|
|
if (0==strcmp(target, alias)) {
|
|
|
|
|
// printf("matched: %s\n", alias);
|
|
|
|
|
matched = 1; break;
|
|
|
|
|
}
|
|
|
|
|
k++;
|
|
|
|
|
alias = packager[k];
|
|
|
|
|
}
|
|
|
|
|
if (matched) {
|
|
|
|
|
do {
|
|
|
|
|
k++; alias = packager[k];
|
|
|
|
|
} while (NULL!=alias);
|
|
|
|
|
call_cmd ((void*) packager[k+1], cmdarg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (int i=0; i<Array_Size(os_packagers); i++) {
|
|
|
|
|
const char const** packager = os_packagers[i];
|
|
|
|
|
int k = 0;
|
|
|
|
|
const char* alias = packager[k];
|
2023-08-28 22:21:33 +08:00
|
|
|
|
while (NULL!=alias) {
|
2023-08-30 17:12:57 +08:00
|
|
|
|
// printf("%s matched: %s\n",target, alias);
|
2023-08-28 23:10:09 +08:00
|
|
|
|
if (0==strcmp(target, alias)) {
|
2023-08-28 22:43:37 +08:00
|
|
|
|
// printf("matched: %s\n", alias);
|
|
|
|
|
matched = 1; break;
|
2023-08-28 22:21:33 +08:00
|
|
|
|
}
|
|
|
|
|
k++;
|
|
|
|
|
alias = packager[k];
|
|
|
|
|
}
|
2023-08-28 22:43:37 +08:00
|
|
|
|
if (matched) {
|
|
|
|
|
do {
|
|
|
|
|
k++; alias = packager[k];
|
|
|
|
|
} while (NULL!=alias);
|
2023-08-28 23:10:09 +08:00
|
|
|
|
call_cmd ((void*) packager[k+1], cmdarg);
|
2023-08-28 22:43:37 +08:00
|
|
|
|
}
|
2023-08-28 22:21:33 +08:00
|
|
|
|
}
|
2023-08-28 22:43:37 +08:00
|
|
|
|
|
2023-08-29 23:07:48 +08:00
|
|
|
|
if (!matched) {
|
2023-08-30 11:33:23 +08:00
|
|
|
|
xy_info("chsrc: 暂不支持的换源类型,请使用-h查看可换源");
|
2023-08-29 23:07:48 +08:00
|
|
|
|
}
|
2023-08-28 22:21:33 +08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|