-
-
Notifications
You must be signed in to change notification settings - Fork 274
Expand file tree
/
Copy pathuv.c
More file actions
198 lines (171 loc) · 5.51 KB
/
uv.c
File metadata and controls
198 lines (171 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/** ------------------------------------------------------------
* SPDX-License-Identifier: GPL-3.0-or-later
* ------------------------------------------------------------*/
def_target(pl_python_uv, "uv");
void
pl_python_uv_prelude (void)
{
chef_prep_this (pl_python_uv, gsr);
chef_set_recipe_created_on (this, "2024-12-11");
chef_set_recipe_last_updated (this, "2025-12-29");
chef_set_sources_last_updated (this, "2025-08-09");
chef_set_chef (this, NULL);
chef_set_cooks (this, 2, "@happy-game", "@MingriLingran");
chef_set_sauciers (this, 2, "@Kattos", "@ccmywish");
chef_set_scope_cap (this, ProjectScope, ScopeCap_Able_And_Implemented);
chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented);
chef_set_scope_cap (this, SystemScope, ScopeCap_Able_But_Not_Implemented);
chef_set_default_scope (this, UserScope);
chef_allow_english(this);
chef_allow_user_define(this);
chef_use_other_target_sources (this, &pl_python_group_target);
}
/**
* chsrc get uv
*
* uv的配置优先级顺序如下(高到低):
* 1. $workspaces/uv.toml
* 2. $workspaces/pyproject.toml
* 3. ~/.config/uv/uv.toml
* 4. /etc/uv/uv.toml
*/
#define PL_Python_uv_ConfigFile "uv.toml"
#define PL_Python_uv_Local_ConfigPath "./"
#define PL_Python_uv_User_ConfigPath "~/.config/uv/"
char *
pl_python_find_uv_config (bool mkdir)
{
if (chsrc_in_project_scope_mode())
{
return xy_2strcat (PL_Python_uv_Local_ConfigPath, PL_Python_uv_ConfigFile);
}
else
{
if (xy.on_windows)
{
/* config path on Windows */
char *appdata = getenv ("APPDATA");
if (!appdata)
{
chsrc_error2 ("未能获取 APPDATA 环境变量");
return NULL;
}
char *config_dir = xy_2strcat(appdata, "\\uv\\");
if (mkdir)
{
chsrc_ensure_dir (config_dir);
}
return xy_2strcat (config_dir, PL_Python_uv_ConfigFile);
}
else
{
/* config path on Linux or macOS */
if (mkdir)
{
chsrc_ensure_dir (PL_Python_uv_User_ConfigPath);
}
return xy_2strcat (PL_Python_uv_User_ConfigPath, PL_Python_uv_ConfigFile);
}
}
}
void
pl_python_uv_getsrc (char *option)
{
char *uv_config = pl_python_find_uv_config (false);
if (!chsrc_check_file (uv_config))
{
chsrc_error2 ("未找到 uv 配置文件");
return;
}
/* 获取 [[index]] 配置项的 url */
if (xy.on_windows)
{
/* 在 Windows 上使用 PowerShell 替代 grep */
char *script = xy_str_gsub (RAWSTR_pl_python_get_uv_config_on_windows, "@f@", uv_config);
chsrc_run_as_powershell_file (script);
}
else
{
/* 在类 Unix 系统上使用 grep */
char *cmd = xy_str_gsub (RAWSTR_pl_python_get_uv_config, "@f@", uv_config);
chsrc_run (cmd, RunOpt_Default);
}
}
/**
* @consult https://docs.astral.sh/uv/configuration/files/
* https://github.com/RubyMetric/chsrc/issues/139
*/
void
pl_python_uv_setsrc (char *option)
{
chsrc_ensure_program ("uv");
Source_t source = chsrc_yield_source (&pl_python_group_target, option);
if (chsrc_in_standalone_mode())
chsrc_confirm_source(&source);
char *uv_config = pl_python_find_uv_config (true);
if (NULL==uv_config)
{
chsrc_error2 ("无法获取 uv 配置文件路径");
return;
}
chsrc_backup (uv_config);
const char *source_content = xy_str_gsub (RAWSTR_pl_python_uv_config_source_content, "@url@", source.url);
if (!xy_file_exist (uv_config))
{
/* 当 uv_config 不存在,直接写入文件 */
chsrc_append_to_file (source_content, uv_config);
}
else
{
/* 当 uv_config 存在,如果存在 [[index]] 则更新,否则追加到文件末尾 */
char *cmd = NULL;
if (xy.on_windows)
{
/* 在 Windows 上使用 PowerShell 替代 grep */
cmd = xy_str_gsub (RAWSTR_pl_python_test_uv_if_set_source_on_windows, "@f@", uv_config);
}
else
{
cmd = xy_str_gsub (RAWSTR_pl_python_test_uv_if_set_source, "@f@", uv_config);
}
int status = xy_run_get_status (cmd);
if (0==status)
{
if (xy.on_windows)
{
/* 在 Windows 上使用 PowerShell 替代 sed */
char *powershell_cmd_with_file = xy_str_gsub(RAWSTR_pl_python_set_uv_config_on_windows, "@f@", uv_config);
char *powershell_cmd = xy_str_gsub(powershell_cmd_with_file, "@url@", source.url);
chsrc_run (powershell_cmd, RunOpt_Default);
}
else
{
/* 非 Windows 系统使用 sed */
char *sed_cmd = NULL;
#if defined(XY_Build_On_macOS) || defined(XY_Build_On_BSD)
sed_cmd = "sed -i '' ";
#else
sed_cmd = "sed -i ";
#endif
char *update_config_cmd = xy_str_gsub (RAWSTR_pl_python_set_uv_config, "@sed@", sed_cmd);
update_config_cmd = xy_str_gsub (update_config_cmd, "@f@", uv_config);
update_config_cmd = xy_str_gsub (update_config_cmd, "@url@", source.url);
chsrc_run (update_config_cmd, RunOpt_Default);
}
}
else
{
chsrc_append_to_file (source_content, uv_config);
}
}
if (chsrc_in_standalone_mode())
{
chsrc_determine_chgtype (ChgType_Auto);
chsrc_conclude (&source);
}
}
void
pl_python_uv_resetsrc (char *option)
{
pl_python_uv_setsrc (option);
}