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-28 22:21:33 +08:00
|
|
|
|
#include <stdio.h>
|
2023-08-29 23:07:48 +08:00
|
|
|
|
#include "chsrc.h"
|
2023-08-28 22:21:33 +08:00
|
|
|
|
#include "helper.h"
|
|
|
|
|
|
|
|
|
|
#define Chsrc_Version "v0.1.0"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Python换源
|
|
|
|
|
*
|
|
|
|
|
* 参考:https://mirrors.tuna.tsinghua.edu.cn/help/pypi/
|
|
|
|
|
*/
|
|
|
|
|
void
|
2023-08-30 08:31:46 +08:00
|
|
|
|
pl_python_chsrc (char* source_name)
|
2023-08-28 22:21:33 +08:00
|
|
|
|
{
|
|
|
|
|
char* source_url = NULL;
|
2023-08-28 23:10:09 +08:00
|
|
|
|
|
|
|
|
|
if (NULL==source_name) {
|
|
|
|
|
source_name = "tuna";
|
|
|
|
|
puts("chsrc: Default selection is TsingHua Tuna");
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-28 22:21:33 +08:00
|
|
|
|
if (0==strcmp("tuna", source_name)) {
|
|
|
|
|
puts("chsrc: Selected source provider: Tuna");
|
|
|
|
|
source_url = "https://pypi.tuna.tsinghua.edu.cn/simple";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char* cmd = xy_strjoin("pip config set global.index-url ", source_url);
|
|
|
|
|
system(cmd);
|
|
|
|
|
free(cmd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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-29 23:07:48 +08:00
|
|
|
|
puts (xy_strjoin("chsrc: 选中镜像站:", source_abbr));
|
2023-08-28 22:21:33 +08:00
|
|
|
|
|
2023-08-29 23:07:48 +08:00
|
|
|
|
puts("chsrc: 为'gem'命令换源");
|
|
|
|
|
system("gem source -r https://rubygems.org/");
|
2023-08-28 22:21:33 +08:00
|
|
|
|
|
|
|
|
|
char* cmd = xy_strjoin("gem source -a ", source_url);
|
|
|
|
|
system(cmd);
|
|
|
|
|
free(cmd);
|
|
|
|
|
|
|
|
|
|
cmd = xy_strjoin("bundle config 'mirror.https://rubygems.org' ", source_url);
|
2023-08-29 23:07:48 +08:00
|
|
|
|
puts("chsrc: 为'bundler'命令换源");
|
2023-08-28 22:21:33 +08:00
|
|
|
|
system(cmd);
|
|
|
|
|
free(cmd);
|
2023-08-29 23:07:48 +08:00
|
|
|
|
|
|
|
|
|
puts(xy_strjoin("chsrc: 感谢镜像提供方:", source_name));
|
2023-08-28 22:21:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-29 15:54:21 +08:00
|
|
|
|
#define cmdfunc(func) (const char const*)func
|
2023-08-28 22:43:37 +08:00
|
|
|
|
static const char const
|
2023-08-30 08:31:46 +08:00
|
|
|
|
*pl_ruby[] = {"gem", "ruby", "rb", NULL, cmdfunc(pl_ruby_chsrc)},
|
2023-08-28 22:43:37 +08:00
|
|
|
|
*pl_python[] = {"pip", "python", "py", NULL},
|
|
|
|
|
*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},
|
|
|
|
|
// 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-29 21:58:51 +08:00
|
|
|
|
#undef cmdfunc
|
2023-08-28 22:21:33 +08:00
|
|
|
|
|
|
|
|
|
static const char const*
|
|
|
|
|
usage[] = {
|
|
|
|
|
"chsrc: Change Source " Chsrc_Version " by RubyMetric\n",
|
2023-08-29 15:54:21 +08:00
|
|
|
|
"使用:\n"
|
|
|
|
|
" chsrc <要换源的对象> [所换源名称]\n",
|
2023-08-28 22:21:33 +08:00
|
|
|
|
|
2023-08-29 15:54:21 +08:00
|
|
|
|
"选项:\n"
|
|
|
|
|
" -h 打印该帮助\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-29 23:07:48 +08:00
|
|
|
|
puts("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-28 22:21:33 +08:00
|
|
|
|
puts(usage[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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-28 23:10:09 +08:00
|
|
|
|
if (0==strcmp("-h",argv[1])) {
|
|
|
|
|
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-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) {
|
|
|
|
|
puts("chsrc: 暂不支持的换源类型,请使用-h查看可换源");
|
|
|
|
|
}
|
2023-08-28 22:21:33 +08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|