gpt4 book ai didi

ios - 我可以使用 Visual Studio 自动增加 Info.plist 文件中的 CFBundleVersion 值吗?

转载 作者:可可西里 更新时间:2023-11-01 06:14:12 25 4
gpt4 key购买 nike

我见过使用 Xcode 甚至 Xamarin Studio 执行此操作的解决方案,但没有使用 Visual Studio。

理想情况下,我希望项目的每个构建都自动增加 Info.plist 文件中的 CFBundleVersion 值。

<key>CFBundleVersion</key>
<string>9</string>

我什至不知道从哪里开始,也找不到关于任何包含 Visual Studio 的文章/博客文章/教程。

这可能吗?

只是想补充一点,我在 Windows 8.1 上使用 Visual Studio 2015。

最佳答案

与您处于同一条船上,因为找不到合适的解决方案,我决定创建自己的解决方案。也许迟到总比不到好! :)

在我的例子中,我使用了非常有用的自动版本设置工具(在 NuGet 上可用)来自动更新我的程序集信息,但希望它也能更新 Info.plist 信息,因为 HockeyApp 使用它来跟踪和通知新版本.

最后,我拼凑了一个最小的 C# 程序,它读取 AssemblyInfo.cs,从那里获取版本信息并编辑 Info.plist XML 文件并将其写回。

如果我没有进行大量偏执的检查,这将是一个 20 行的程序,以免冒不可挽回地破坏 Info.plist 的风险(即使那样它也会创建该文件的备份)。

“魔法”归结为两种方法,第一种是我在 SO 上找到的:

读取 AssemblyInfo.cs:

private static string GetVersionFromAssemblyInfo(string infile)
{
var version = String.Empty;

var readText = File.ReadAllLines(infile);
var versionInfoLines = readText.Where(t => t.Contains("[assembly: AssemblyVersion"));
foreach (var item in versionInfoLines)
{
version = item.Substring(item.IndexOf('(') + 2, item.LastIndexOf(')') - item.IndexOf('(') - 3);
}
return version;
}

编辑 Info.plist,其中程序集信息元组的前 3 个元素变为 CFBundleShortVersionString,最后一个元素变为 CFBundleVersion,HockeyApp 使用它作为内部版本号。

LINQ 中的不稳定是由于 Apple 在该文件中呈现键/值对的方式有点奇怪:

 private static bool SetVersionInInfoPlist(string infoplistFile, string version, string number)
{
var xelements = XDocument.Load(infoplistFile);
var dict = from el in xelements.Root?.Elements() select el;
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
if (dict == null) return false;

var cfshortversion =
from el in dict.Descendants("key")
where el.Value == "CFBundleShortVersionString"
select el.ElementsAfterSelf().FirstOrDefault();
;
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
if (cfshortversion == null) return false;
cfshortversion.FirstOrDefault()?.SetValue(version);

var cfversion =
from el in dict.Descendants("key")
where el.Value == "CFBundleVersion"
select el.ElementsAfterSelf().FirstOrDefault();

// ReSharper disable once ConditionIsAlwaysTrueOrFalse
if (cfversion == null) return false;
cfversion.FirstOrDefault()?.SetValue(number);

// Make backup
try
{
File.Copy(infoplistFile, $"{infoplistFile}-backup", true);
}
catch (Exception)
{
Console.WriteLine($"Failed to create backup of {infoplistFile}. Will not edit.");
return false;
}

try
{
using (StringWriter sw = new StringWriter())
{
using (XmlWriter xWrite = XmlWriter.Create(sw))
{
xelements.Save(xWrite);
}
}
xelements.Save(infoplistFile);
}
catch (Exception)
{
Console.WriteLine($"Failed to save the edited {infoplistFile}.");
return false;
}

Console.WriteLine($"Successfully edited and saved new {infoplistFile}.");
return true;

}

编辑:我还应该补充一点,我将 Bamboo 用于 CI 和构建自动化。因此,该程序成为远程构建代理的一项功能,然后我可以将其作为任务添加到 Bamboo 构建计划中。

关于ios - 我可以使用 Visual Studio 自动增加 Info.plist 文件中的 CFBundleVersion 值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31969720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com