安装包版本号自增
AI工具:豆包
提示词如下:
有个文本文件,其内容如下:
---
; Script generated by the Inno Setup Script Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!
#define MyAppName "CLESTRARevitFamilyLibraryManagement"
#define MyAppVersion "1.0.67"
#define MyAppPublisher "Smart BIM, Inc."
[Setup]
; NOTE: The value of AppId uniquely identifies this application. Do not use the same AppId value in installers for other applications.
; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
AppId={{0952DF3A-5376-4498-8509-3346A6F105DD}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
DefaultDirName={autopf}\SmartBIM\{#MyAppName}
DefaultGroupName={#MyAppName}
; Uncomment the following line to run in non administrative install mode (install for current user only.)
;PrivilegesRequired=lowest
OutputDir=dist
OutputBaseFilename=CLESTRARevitFamilyLibraryManagement(20241121-V1.0.67)
Compression=lzma2/max
SolidCompression=no
WizardStyle=modern
---
使用C#实现一个算法,修改这个文件的内容,并且保存更新
1. 找到`#define MyAppVersion "1.0.67"`这一行,把`1.0.67`的最后一部分自增1,变成`1.0.68`。识别这一行的判断条件可以是`#define MyAppVersion`开头
2.找到`OutputBaseFilename=CLESTRARevitFamilyLibraryManagement(20241121-V1.0.67)`这一行把括号内的`1.0.67`也自增1,变成1.0.68,并且其中的日期部分自动变成今天的,如20241121,变成20250421。识别这一行的判断条件可以是`OutputBaseFilename=`开头,
3. 用C#实现这个算法,并且封装成方法,传入文件名,即可
4. 要求算法简洁,可靠,对字符串的处理考虑性能上的优化,代码上要简洁易读,最好有关键的注释,注释也不能过多,影响阅读
结果
public static void UpdateIssFileVersion(string filePath)
{
// 读取文件的所有行
string[] lines = File.ReadAllLines(filePath);
// 获取当前日期
string currentDate = DateTime.Now.ToString("yyyyMMdd");
for (int i = 0; i < lines.Length; i++)
{
// 修改 MyAppVersion
if (lines[i].StartsWith("#define MyAppVersion"))
{
// 匹配版本号
Match versionMatch = Regex.Match(lines[i], @"""(\d+\.\d+\.\d+)""");
if (versionMatch.Success)
{
string version = versionMatch.Groups[1].Value;
string[] versionParts = version.Split('.');
int lastPart = int.Parse(versionParts[2]);
versionParts[2] = (lastPart + 1).ToString();
string newVersion = string.Join(".", versionParts);
lines[i] = lines[i].Replace(version, newVersion);
Console.WriteLine($"oldVersion:{version}");
Console.WriteLine($"newVersion:{newVersion}\n");
}
}
// 修改 OutputBaseFilename
else if (lines[i].StartsWith("OutputBaseFilename="))
{
// 匹配日期和版本号
Match dateVersionMatch = Regex.Match(lines[i], @"\((\d+)-V(\d+\.\d+\.\d+)\)");
if (dateVersionMatch.Success)
{
string oldDate = dateVersionMatch.Groups[1].Value;
string version = dateVersionMatch.Groups[2].Value;
string[] versionParts = version.Split('.');
int lastPart = int.Parse(versionParts[2]);
versionParts[2] = (lastPart + 1).ToString();
string newVersion = string.Join(".", versionParts);
lines[i] = lines[i].Replace($"({oldDate}-V{version})", $"({currentDate}-V{newVersion})");
Console.WriteLine($"oldVersion:{version}");
Console.WriteLine($"newVersion:{newVersion}\n");
}
}
}
// 将修改后的内容写回文件
File.WriteAllLines(filePath, lines);
}