Fix append only once

[GitHub #76]
This commit is contained in:
Aoran Zeng 2024-11-22 02:04:36 +08:00
parent c1c495cf6d
commit 7825acfff3
No known key found for this signature in database
GPG Key ID: 8F8BA8488E10ED98
7 changed files with 57 additions and 42 deletions

View File

@ -92,11 +92,11 @@ chsrc_note2 (const char* str)
}
void
chsrc_log_write (const char *file)
chsrc_log_write (const char *filename)
{
char *msg = CliOpt_InEnglish ? "WRITE" : "写入";
xy_log_brkt (blue(App_Name), bdblue(msg), blue(file));
xy_log_brkt (blue(App_Name), bdblue(msg), blue(filename));
}
#define YesMark "✓"
@ -1094,7 +1094,7 @@ chsrc_append_to_file (const char *str, const char *filename)
{
if (CliOpt_DryRun)
{
return;
goto log_anyway;
}
char *file = xy_normalize_path (filename);
@ -1104,7 +1104,8 @@ chsrc_append_to_file (const char *str, const char *filename)
FILE *f = fopen (file, "a");
if (NULL==f)
{
char *msg = xy_2strjoin ("Unable to open file to write: ", file);
char *msg = CliOpt_InEnglish ? xy_2strjoin ("Unable to open file to write: ", file)
: xy_2strjoin ("无法打开文件以写入: ", file);
chsrc_error2 (msg);
exit (Exit_UserCause);
}
@ -1116,13 +1117,18 @@ chsrc_append_to_file (const char *str, const char *filename)
size_t ret = fwrite (newstr, len, 1, f);
if (ret != 1)
{
char *msg = xy_2strjoin ("Write failed to ", file);
char *msg = CliOpt_InEnglish ? xy_2strjoin ("Write failed to ", file)
: xy_2strjoin ("写入文件失败: ", file);
chsrc_error2 (msg);
exit (Exit_UserCause);
}
fclose (f);
log_anyway:
/* 输出recipe指定的文件名 */
chsrc_log_write (filename);
/*
char *cmd = NULL;
if (xy_on_windows)
@ -1138,14 +1144,14 @@ chsrc_append_to_file (const char *str, const char *filename)
}
static void
chsrc_prepend_to_file (const char *str, const char *file)
chsrc_prepend_to_file (const char *str, const char *filename)
{
if (CliOpt_DryRun)
{
return;
goto log_anyway;
}
file = xy_normalize_path (file);
char *file = xy_normalize_path (filename);
char *dir = xy_parent_dir (file);
chsrc_ensure_dir (dir);
@ -1159,17 +1165,21 @@ chsrc_prepend_to_file (const char *str, const char *file)
cmd = xy_strjoin (4, "sed -i '1i ", str, "' ", file);
}
chsrc_run (cmd, RunOpt_No_Last_New_Line|RunOpt_Dont_Notify_On_Success);
log_anyway:
/* 输出recipe指定的文件名 */
chsrc_log_write (filename);
}
static void
chsrc_overwrite_file (const char *str, const char *file)
chsrc_overwrite_file (const char *str, const char *filename)
{
if (CliOpt_DryRun)
{
return;
goto log_anyway;
}
file = xy_normalize_path (file);
char *file = xy_normalize_path (filename);
char *dir = xy_parent_dir (file);
chsrc_ensure_dir (dir);
@ -1183,6 +1193,10 @@ chsrc_overwrite_file (const char *str, const char *file)
cmd = xy_strjoin (4, "echo '", str, "' > ", file);
}
chsrc_run (cmd, RunOpt_Default);
log_anyway:
/* 输出recipe指定的文件名 */
chsrc_log_write (filename);
}
static void

View File

@ -56,17 +56,17 @@ pl_dart_flutter_setsrc (char *option)
{
chsrc_yield_source_and_confirm (pl_dart_flutter);
char *towrite = NULL;
char *w = NULL;
if (xy_on_windows)
{
towrite = xy_strjoin (3, "$env:FLUTTER_STORAGE_BASE_URL = \"", source.url, "\"");
w = xy_strjoin (3, "$env:FLUTTER_STORAGE_BASE_URL = \"", source.url, "\"\n");
if (xy_file_exist (xy_win_powershell_profile))
chsrc_append_to_file (towrite, xy_win_powershell_profile);
chsrc_append_to_file (w, xy_win_powershell_profile);
if (xy_file_exist (xy_win_powershellv5_profile))
chsrc_append_to_file (towrite, xy_win_powershellv5_profile);
chsrc_append_to_file (w, xy_win_powershellv5_profile);
}
else
{
@ -74,13 +74,13 @@ pl_dart_flutter_setsrc (char *option)
char *bashrc = "~/.bashrc";
chsrc_backup (zshrc);
towrite = xy_strjoin (3, "export FLUTTER_STORAGE_BASE_URL=\"", source.url, "\"");
chsrc_append_to_file (towrite, zshrc);
w = xy_strjoin (3, "export FLUTTER_STORAGE_BASE_URL=\"", source.url, "\"\n");
chsrc_append_to_file (w, zshrc);
if (xy_file_exist (bashrc))
{
chsrc_backup (bashrc);
chsrc_append_to_file (towrite, bashrc);
chsrc_append_to_file (w, bashrc);
}
}
chsrc_conclude (&source, SetsrcType_Auto);

View File

@ -51,17 +51,17 @@ pl_dart_setsrc (char *option)
{
chsrc_yield_source_and_confirm (pl_dart);
char *towrite = NULL;
char *w = NULL;
if (xy_on_windows)
{
towrite = xy_strjoin (3, "$env:PUB_HOSTED_URL = \"", source.url, "\"");
w = xy_strjoin (3, "$env:PUB_HOSTED_URL = \"", source.url, "\"\n");
if (xy_file_exist (xy_win_powershell_profile))
chsrc_append_to_file (towrite, xy_win_powershell_profile);
chsrc_append_to_file (w, xy_win_powershell_profile);
if (xy_file_exist (xy_win_powershellv5_profile))
chsrc_append_to_file (towrite, xy_win_powershellv5_profile);
chsrc_append_to_file (w, xy_win_powershellv5_profile);
}
else
{
@ -69,14 +69,14 @@ pl_dart_setsrc (char *option)
char *bashrc = "~/.bashrc";
chsrc_backup (zshrc);
towrite = xy_strjoin (3, "export PUB_HOSTED_URL=\"", source.url, "\"");
w = xy_strjoin (3, "export PUB_HOSTED_URL=\"", source.url, "\"\n");
chsrc_append_to_file (towrite, zshrc);
chsrc_append_to_file (w, zshrc);
if (xy_file_exist (bashrc))
{
chsrc_backup (bashrc);
chsrc_append_to_file (towrite, bashrc);
chsrc_append_to_file (w, bashrc);
}
}
chsrc_conclude (&source, SetsrcType_Auto);

View File

@ -32,7 +32,7 @@ pl_nodejs_nvm_setsrc (char *option)
{
chsrc_yield_source_and_confirm (pl_nodejs_binary_release);
char *w = xy_2strjoin ("export NVM_NODEJS_ORG_MIRROR=", source.url);
char *w = xy_strjoin (3, "export NVM_NODEJS_ORG_MIRROR=", source.url, "\n");
char *zshrc = "~/.zshrc";
char *bashrc = "~/.bashrc";

View File

@ -50,7 +50,7 @@ pl_r_getsrc (char *option)
}
/**
* R https://help.mirrors.cernet.edu.cn/CRAN/
* @consult https://help.mirrors.cernet.edu.cn/CRAN/
*/
void
pl_r_setsrc (char *option)
@ -60,8 +60,8 @@ pl_r_setsrc (char *option)
char *bioconductor_url = xy_str_delete_suffix (xy_str_delete_suffix (source.url, "cran/"), "CRAN/");
bioconductor_url = xy_2strjoin(bioconductor_url, "bioconductor");
const char *w1 = xy_strjoin (3, "options(\"repos\" = c(CRAN=\"", source.url, "\"))" );
const char *w2 = xy_strjoin (3, "options(BioC_mirror=\"", bioconductor_url, "\")" );
const char *w1 = xy_strjoin (3, "options(\"repos\" = c(CRAN=\"", source.url, "\"))\n" );
const char *w2 = xy_strjoin (3, "options(BioC_mirror=\"", bioconductor_url, "\")\n" );
char *w = xy_2strjoin (w1, w2);

View File

@ -52,7 +52,7 @@ pl_rust_rustup_setsrc (char *option)
char *w1 = xy_strjoin (3, "export RUSTUP_DIST_SERVER=\"", source.url, "\"\n");
char *w2 = xy_strjoin (3, "export RUSTUP_UPDATE_ROOT=\"", source.url, "/rustup\"\n");
char *w = xy_2strjoin (w1, w2)
char *w = xy_2strjoin (w1, w2);
char *bashrc = "~/.bashrc";
if (xy_file_exist (bashrc))
@ -74,7 +74,7 @@ pl_rust_rustup_setsrc (char *option)
char *w1 = xy_strjoin (3, "set -x RUSTUP_DIST_SERVER ", source.url, "\n");
char *w2 = xy_strjoin (3, "set -x RUSTUP_UPDATE_ROOT ", source.url, "/rustup\n");
char *w = xy_2strjoin (w1, w2)
char *w = xy_2strjoin (w1, w2);
chsrc_backup (fishrc);
chsrc_append_to_file (w, fishrc);

View File

@ -48,13 +48,14 @@ wr_homebrew_setsrc (char *option)
{
chsrc_yield_source_and_confirm (wr_homebrew);
char *splitter = "\n\n# Generated by chsrc " Chsrc_Banner_Version;
char *w1 = xy_strjoin (3, "export HOMEBREW_API_DOMAIN=\"", source.url, "homebrew-bottles/api", "\"\n");
char *w2 = xy_strjoin (3, "export HOMEBREW_BOTTLE_DOMAIN=\"", source.url, "homebrew-bottles", "\"\n");
char *w3 = xy_strjoin (3, "export HOMEBREW_BREW_GIT_REMOTE=\"", source.url, "git/homebrew/brew.git", "\"\n");
char *w4 = xy_strjoin (3, "export HOMEBREW_CORE_GIT_REMOTE=\"", source.url, "git/homebrew/homebrew-core.git", "\"\n");
char *splitter = "\n# --- chsrc BLOCK BEGIN for Homebrew ---\n";
char *w1 = xy_strjoin (4, "export HOMEBREW_API_DOMAIN=\"", source.url, "homebrew-bottles/api", "\"\n");
char *w2 = xy_strjoin (4, "export HOMEBREW_BOTTLE_DOMAIN=\"", source.url, "homebrew-bottles", "\"\n");
char *w3 = xy_strjoin (4, "export HOMEBREW_BREW_GIT_REMOTE=\"", source.url, "git/homebrew/brew.git", "\"\n");
char *w4 = xy_strjoin (4, "export HOMEBREW_CORE_GIT_REMOTE=\"", source.url, "git/homebrew/homebrew-core.git", "\"\n");
char *end = "# --- chsrc BLOCK ENDIN for Homebrew ---\n";
char *w = xy_strjoin (5, splitter, w1, w2, w3, w4);
char *w = xy_strjoin (6, splitter, w1, w2, w3, w4, end);
char *zshrc = "~/.zshrc";
chsrc_backup (zshrc);
@ -70,12 +71,12 @@ wr_homebrew_setsrc (char *option)
char *fishrc = "~/.config/fish/config.fish";
if (xy_file_exist (fishrc))
{
char *w1 = xy_strjoin(3, "set -x HOMEBREW_API_DOMAIN \"", source.url, "homebrew-bottles/api", "\"\n");
char *w2 = xy_strjoin(3, "set -x HOMEBREW_BOTTLE_DOMAIN \"", source.url, "homebrew-bottles", "\"\n");
char *w3 = xy_strjoin(3, "set -x HOMEBREW_BREW_GIT_REMOTE \"",source.url, "git/homebrew/brew.git", "\"\n");
char *w4 = xy_strjoin(3, "set -x HOMEBREW_CORE_GIT_REMOTE \"",source.url, "git/homebrew/homebrew-core.git", "\"\n");
char *w1 = xy_strjoin(4, "set -x HOMEBREW_API_DOMAIN \"", source.url, "homebrew-bottles/api", "\"\n");
char *w2 = xy_strjoin(4, "set -x HOMEBREW_BOTTLE_DOMAIN \"", source.url, "homebrew-bottles", "\"\n");
char *w3 = xy_strjoin(4, "set -x HOMEBREW_BREW_GIT_REMOTE \"",source.url, "git/homebrew/brew.git", "\"\n");
char *w4 = xy_strjoin(4, "set -x HOMEBREW_CORE_GIT_REMOTE \"",source.url, "git/homebrew/homebrew-core.git", "\"\n");
char *w = xy_strjoin (5, splitter, w1, w2, w3, w4);
char *w = xy_strjoin (6, splitter, w1, w2, w3, w4, end);
chsrc_backup (fishrc);
chsrc_append_to_file (w, fishrc);
}