Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build:1.2.2.212 #81

Merged
merged 2 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# 更新日志 (工作日)
# 更新日志 (元宵节)

- 新增:winget下载方式 感谢@YU-7
- 优化:优化任务删除逻辑
- 修复:修复高级规则的结果与实际不符的情况。
- 修复:更新程序不生效的BUG #77
- 修复:最小化启动不生效的BUG #78
- 新增:正则替换跟普通替换重命名方式
- 新增:重命名规则组合方式,可各种规则自由组合,也可跟自定义前缀进行组合
- 新增:重命名获取上级目录名称跟获取源文件名称 #79

## 版本说明

Expand All @@ -14,7 +12,7 @@

## 更新日志详情

- **[Full Changelog](https://github.com/SaboZhang/EasyTidy/compare/1.2.0.0120...1.2.1.208)**
- **[Full Changelog](https://github.com/SaboZhang/EasyTidy/compare/1.2.1.208...1.2.2.212)**

## 下载链接

Expand Down
4 changes: 2 additions & 2 deletions src/EasyTidy.Model/AppConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace EasyTidy.Model;
public partial class AppConfig : JsonSettings, IVersionable
{
[EnforcedVersion("1.2.1.208")]
public virtual Version Version { get; set; } = new Version(1, 2, 1, 208);
[EnforcedVersion("1.2.2.212")]
public virtual Version Version { get; set; } = new Version(1, 2, 2, 212);

public override string FileName { get; set; } = Constants.AppConfigPath;

Expand Down
173 changes: 151 additions & 22 deletions src/EasyTidy.Util/Renamer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public partial class Renamer
private const string RSTRING_ALPHA_PARAMETER = "rstringalpha";
private const string RSTRING_DIGIT_PARAMETER = "rstringdigit";
private const string RUUIDV4_PARAMETER = "ruuidv4";
private const string FOLDER_PARAMETER = "source";
private const string PARENT_PARAMETER = "parent";
private const string REGEX_PARAMETER = "regex";
private const string REPLACE_PARAMETER = "replace";

/// <summary>
/// 解析模板参数
Expand All @@ -38,28 +42,11 @@ public static string ParseTemplate(string source, string target)
{
string[] parameters = match.Groups[1].Value.Split(',');

// 判断不同的模板参数类型并处理
if (match.Value.Contains(INCREMENT_PARAMETER) || match.Value.Contains("start") || match.Value.Contains("padding") || match.Value.Equals("${}"))
{
return ParseIncrement(parameters);
}
else if (match.Value.Contains(RSTRING_ALNUM_PARAMETER))
{
return ParseRandom(parameters, "alnum");
}
else if (match.Value.Contains(RSTRING_ALPHA_PARAMETER))
{
return ParseRandom(parameters, "alpha");
}
else if (match.Value.Contains(RSTRING_DIGIT_PARAMETER))
{
return ParseRandom(parameters, "digit");
}
else if (match.Value.Contains(RUUIDV4_PARAMETER))
{
return GenerateUUID();
}
return match.Value;
// 获取处理方法
Func<string[], string> templateHandler = GetTemplateHandler(match.Value, source);

// 执行模板处理
return templateHandler != null ? templateHandler(parameters) : match.Value;
});

// 替换日期模板
Expand All @@ -68,6 +55,90 @@ public static string ParseTemplate(string source, string target)
return File.Exists(source) ? ReplaceFileName(source, result) : result;
}

// 获取模板参数处理方法
private static Func<string[], string> GetTemplateHandler(string matchValue, string source)
{
if (matchValue.Contains(INCREMENT_PARAMETER) || matchValue.Contains("start") || matchValue.Contains("padding") || matchValue.Equals("${}"))
{
return ParseIncrement;
}
else if (matchValue.Contains(RSTRING_ALNUM_PARAMETER))
{
return parameters => ParseRandom(parameters, "alnum");
}
else if (matchValue.Contains(RSTRING_ALPHA_PARAMETER))
{
return parameters => ParseRandom(parameters, "alpha");
}
else if (matchValue.Contains(RSTRING_DIGIT_PARAMETER))
{
return parameters => ParseRandom(parameters, "digit");
}
else if (matchValue.Contains(RUUIDV4_PARAMETER))
{
return _ => GenerateUUID();
}
else if (matchValue.Contains(PARENT_PARAMETER) || matchValue.Contains(FOLDER_PARAMETER))
{
return _ => GetFolderNameFromSource(source, matchValue);
}
else if (matchValue.Contains(REGEX_PARAMETER))
{
return parameters => RegexReplaceHandler(parameters, source);
}
else if (matchValue.Contains(REPLACE_PARAMETER))
{
return parameters => ReplaceHandler(parameters, source);
}

return null; // 如果没有匹配到处理方法,返回null
}

/// <summary>
/// 根据传入的 source(文件或目录路径)获取目录名称。
/// 如果 source 是目录,则返回其名称;
/// 如果 source 是文件,则返回文件所在目录的名称;
/// 如果 source 不存在,则返回空字符串。
/// </summary>
/// <param name="source">文件或目录的完整路径</param>
/// <returns>目录名称或空字符串</returns>
private static string GetFolderNameFromSource(string source, string matchValue)
{
if (Directory.Exists(source))
{
// 获取上一级目录名称
if (matchValue.Contains(PARENT_PARAMETER))
{
return GetParentFolderName(source);
}
// 其他情况直接返回文件夹名称
return new DirectoryInfo(source).Name;
}
else if (File.Exists(source) && matchValue.Contains(PARENT_PARAMETER))
{
// source 为文件,获取其所在目录
string directory = Path.GetDirectoryName(source);
return !string.IsNullOrEmpty(directory) ? new DirectoryInfo(directory).Name : string.Empty;
}
else if (File.Exists(source) && matchValue.Contains(FOLDER_PARAMETER))
{
// source 为文件,获取其所在目录
string fileName = Path.GetFileNameWithoutExtension(source);
return !string.IsNullOrEmpty(fileName) ? fileName : string.Empty;
}
return string.Empty;
}

private static string GetParentFolderName(string source)
{
if (Directory.Exists(source))
{
DirectoryInfo parentDirectory = Directory.GetParent(source);
return parentDirectory?.Name ?? string.Empty;
}
return string.Empty;
}

/// <summary>
/// 使用生成的新文件名替换 source 文件路径中的文件名,并返回完整路径
/// </summary>
Expand Down Expand Up @@ -237,4 +308,62 @@ public static string GenerateUUID()
/// <returns></returns>
[GeneratedRegex(@"\$\{(.*?)\}")]
private static partial Regex TemplateRegex();

/// <summary>
/// 正则替换
/// </summary>
/// <param name="parameters">解析的参数</param>
/// <param name="source">要修改的源文件或者文件夹名称</param>
/// <returns></returns>
private static string RegexReplaceHandler(string[] parameters, string source)
{
if (parameters.Length < 2) return string.Empty;

// 路径预处理
source = File.Exists(source) ? Path.GetFileNameWithoutExtension(source)
: Directory.Exists(source) ? new DirectoryInfo(source).Name
: source;

string pattern = parameters[0].Replace("regex=", "");
string replacement = parameters[1] ?? string.Empty;

return Regex.Replace(source, pattern, replacement, RegexOptions.Compiled);
}

/// <summary>
/// 替换处理
/// </summary>
/// <param name="parameters">替换参数</param>
/// <param name="source">原名称</param>
/// <returns></returns>
private static string ReplaceHandler(string[] parameters, string source)
{
if (parameters.Length < 2) return string.Empty;

// 路径预处理
source = File.Exists(source) ? Path.GetFileNameWithoutExtension(source)
: Directory.Exists(source) ? new DirectoryInfo(source).Name
: source;

string oldValue = parameters[0].Replace("replace=","");
string newValue = parameters[1];
bool caseSensitive = parameters.Length > 2 && bool.TryParse(parameters[2], out bool result) && result;

StringComparison comparison = caseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;

return ReplaceWithComparison(source, oldValue, newValue, comparison);
}

// 自定义字符串替换,支持大小写敏感控制
private static string ReplaceWithComparison(string input, string oldValue, string newValue, StringComparison comparison)
{
int index = input.IndexOf(oldValue, comparison);
if (index < 0)
{
return input; // 没找到匹配项,返回原始字符串
}

return string.Concat(input.AsSpan(0, index), newValue, input.AsSpan(index + oldValue.Length));
}

}
2 changes: 1 addition & 1 deletion src/EasyTidy/EasyTidy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PublishProfile>win-$(Platform).pubxml</PublishProfile>
<UseWinUI>true</UseWinUI>
<EnableMsixTooling>true</EnableMsixTooling>
<Version>1.2.1.208</Version>
<Version>1.2.2.212</Version>
<ImplicitUsings>true</ImplicitUsings>
<SuportedOSPlatformsVersion>10.0.22621.0</SuportedOSPlatformsVersion>
<WindowsSdkPackageVersion>10.0.22621.45</WindowsSdkPackageVersion>
Expand Down
32 changes: 32 additions & 0 deletions src/EasyTidy/MultilingualResources/EasyTidy.fr-FR.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -1856,6 +1856,38 @@
<source>Execute on close</source>
<target state="translated">Exécuter à la fermeture</target>
</trans-unit>
<trans-unit id="ReplaceCheatSheet_ReplaceSource" translate="yes" xml:space="preserve">
<source>source file name</source>
<target state="translated">Nom du fichier d'origine</target>
</trans-unit>
<trans-unit id="ReplaceCheatSheet_ReplaceParent" translate="yes" xml:space="preserve">
<source>Parent directory name</source>
<target state="translated">Nom du répertoire parent</target>
</trans-unit>
<trans-unit id="ReplaceCheatSheet_Replace" translate="yes" xml:space="preserve">
<source>Replace specified characters case-insensitively</source>
<target state="translated">Remplacer indifféremment les caractères spécifiés par des minuscules ou des majuscules</target>
</trans-unit>
<trans-unit id="ReplaceCheatSheet_ReplaceIgnoreCase" translate="yes" xml:space="preserve">
<source>Replace specified string with case sensitivity</source>
<target state="translated">Remplacer la chaîne spécifiée avec la sensibilité à la casse</target>
</trans-unit>
<trans-unit id="ReplaceCheatSheet_ReplaceDelete" translate="yes" xml:space="preserve">
<source>Replace the specified character with an empty character</source>
<target state="translated">Remplacer le caractère spécifié par un caractère vide</target>
</trans-unit>
<trans-unit id="ReplaceCheatSheet_ReplaceRegex" translate="yes" xml:space="preserve">
<source>Replace with specific character by regex</source>
<target state="translated">Remplacer par un caractère spécifique par regex</target>
</trans-unit>
<trans-unit id="ReplaceCheatSheet_ReplaceRegexDelete" translate="yes" xml:space="preserve">
<source>Replace the specified characters with spaces using regex</source>
<target state="translated">Remplacer les caractères spécifiés par des espaces en utilisant regex</target>
</trans-unit>
<trans-unit id="ReplaceCheatSheet_Title.Text" translate="yes" xml:space="preserve">
<source>Substitute with a regular expression or specified characters</source>
<target state="translated">Substituer par une expression régulière ou des caractères spécifiés</target>
</trans-unit>
</group>
</body>
</file>
Expand Down
32 changes: 32 additions & 0 deletions src/EasyTidy/MultilingualResources/EasyTidy.ja-JP.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -1856,6 +1856,38 @@
<source>Execute on close</source>
<target state="translated">閉じると実行する</target>
</trans-unit>
<trans-unit id="ReplaceCheatSheet_ReplaceSource" translate="yes" xml:space="preserve">
<source>source file name</source>
<target state="translated">ソースファイル名</target>
</trans-unit>
<trans-unit id="ReplaceCheatSheet_ReplaceParent" translate="yes" xml:space="preserve">
<source>Parent directory name</source>
<target state="translated">親ディレクトリ名</target>
</trans-unit>
<trans-unit id="ReplaceCheatSheet_Replace" translate="yes" xml:space="preserve">
<source>Replace specified characters case-insensitively</source>
<target state="translated">指定された文字を大文字小文字に関係なく置き換えます</target>
</trans-unit>
<trans-unit id="ReplaceCheatSheet_ReplaceIgnoreCase" translate="yes" xml:space="preserve">
<source>Replace specified string with case sensitivity</source>
<target state="translated">指定した文字列を大文字小文字を区別して置換する</target>
</trans-unit>
<trans-unit id="ReplaceCheatSheet_ReplaceDelete" translate="yes" xml:space="preserve">
<source>Replace the specified character with an empty character</source>
<target state="translated">指定された文字を空文字に置換する</target>
</trans-unit>
<trans-unit id="ReplaceCheatSheet_ReplaceRegex" translate="yes" xml:space="preserve">
<source>Replace with specific character by regex</source>
<target state="translated">正規表現で特定の文字に置き換える</target>
</trans-unit>
<trans-unit id="ReplaceCheatSheet_ReplaceRegexDelete" translate="yes" xml:space="preserve">
<source>Replace the specified characters with spaces using regex</source>
<target state="translated">正規表現を使用して指定された文字をスペースに置き換える</target>
</trans-unit>
<trans-unit id="ReplaceCheatSheet_Title.Text" translate="yes" xml:space="preserve">
<source>Substitute with a regular expression or specified characters</source>
<target state="translated">正規表現または指定された文字で置換します</target>
</trans-unit>
</group>
</body>
</file>
Expand Down
32 changes: 32 additions & 0 deletions src/EasyTidy/MultilingualResources/EasyTidy.qps-ploc.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -1856,6 +1856,38 @@
<source>Execute on close</source>
<target state="new">Execute on close</target>
</trans-unit>
<trans-unit id="ReplaceCheatSheet_ReplaceSource" translate="yes" xml:space="preserve">
<source>source file name</source>
<target state="new">source file name</target>
</trans-unit>
<trans-unit id="ReplaceCheatSheet_ReplaceParent" translate="yes" xml:space="preserve">
<source>Parent directory name</source>
<target state="new">Parent directory name</target>
</trans-unit>
<trans-unit id="ReplaceCheatSheet_Replace" translate="yes" xml:space="preserve">
<source>Replace specified characters case-insensitively</source>
<target state="new">Replace specified characters case-insensitively</target>
</trans-unit>
<trans-unit id="ReplaceCheatSheet_ReplaceIgnoreCase" translate="yes" xml:space="preserve">
<source>Replace specified string with case sensitivity</source>
<target state="new">Replace specified string with case sensitivity</target>
</trans-unit>
<trans-unit id="ReplaceCheatSheet_ReplaceDelete" translate="yes" xml:space="preserve">
<source>Replace the specified character with an empty character</source>
<target state="new">Replace the specified character with an empty character</target>
</trans-unit>
<trans-unit id="ReplaceCheatSheet_ReplaceRegex" translate="yes" xml:space="preserve">
<source>Replace with specific character by regex</source>
<target state="new">Replace with specific character by regex</target>
</trans-unit>
<trans-unit id="ReplaceCheatSheet_ReplaceRegexDelete" translate="yes" xml:space="preserve">
<source>Replace the specified characters with spaces using regex</source>
<target state="new">Replace the specified characters with spaces using regex</target>
</trans-unit>
<trans-unit id="ReplaceCheatSheet_Title.Text" translate="yes" xml:space="preserve">
<source>Substitute with a regular expression or specified characters</source>
<target state="new">Substitute with a regular expression or specified characters</target>
</trans-unit>
</group>
</body>
</file>
Expand Down
32 changes: 32 additions & 0 deletions src/EasyTidy/MultilingualResources/EasyTidy.zh-CN.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -1856,6 +1856,38 @@
<source>Execute on close</source>
<target state="translated">关闭时执行</target>
</trans-unit>
<trans-unit id="ReplaceCheatSheet_ReplaceSource" translate="yes" xml:space="preserve">
<source>source file name</source>
<target state="translated">源文件名称</target>
</trans-unit>
<trans-unit id="ReplaceCheatSheet_ReplaceParent" translate="yes" xml:space="preserve">
<source>Parent directory name</source>
<target state="translated">上级目录名称</target>
</trans-unit>
<trans-unit id="ReplaceCheatSheet_Replace" translate="yes" xml:space="preserve">
<source>Replace specified characters case-insensitively</source>
<target state="translated">替换指定字符串不区分大小写</target>
</trans-unit>
<trans-unit id="ReplaceCheatSheet_ReplaceIgnoreCase" translate="yes" xml:space="preserve">
<source>Replace specified string with case sensitivity</source>
<target state="translated">替换指定字符串区分大小写</target>
</trans-unit>
<trans-unit id="ReplaceCheatSheet_ReplaceDelete" translate="yes" xml:space="preserve">
<source>Replace the specified character with an empty character</source>
<target state="translated">替换指定字符串为空</target>
</trans-unit>
<trans-unit id="ReplaceCheatSheet_ReplaceRegex" translate="yes" xml:space="preserve">
<source>Replace with specific character by regex</source>
<target state="translated">使用正则替换为指定字符串</target>
</trans-unit>
<trans-unit id="ReplaceCheatSheet_ReplaceRegexDelete" translate="yes" xml:space="preserve">
<source>Replace the specified characters with spaces using regex</source>
<target state="translated">使用正则替换为空字符</target>
</trans-unit>
<trans-unit id="ReplaceCheatSheet_Title.Text" translate="yes" xml:space="preserve">
<source>Substitute with a regular expression or specified characters</source>
<target state="translated">使用正则表达式或者指定字符替换</target>
</trans-unit>
</group>
</body>
</file>
Expand Down
Loading