gpt4 book ai didi

c# - 如何在自定义工作流事件中获取构建详细信息?

转载 作者:行者123 更新时间:2023-11-30 22:38:14 27 4
gpt4 key购买 nike

我需要向默认工作流模板添加自定义事件,以便在构建过程中尽早增加程序集版本。

我想要实现的是在我的自定义事件中创建和映射完全相同的工作区(在工作流中进一步创建),以便我可以 checkout xml 文件,增加其中包含的版本号,将其写回 xml 文件并重新检查 xml 文件。

我知道这个工作区将在稍后的工作流程中创建,但在构建过程中对于我想要实现的目标来说已经太晚了,所以不要移动任何事件或将它们复制到位于我的自定义事件上方(这应该没问题,因为此工作区将被删除并稍后重新创建)

我认为我需要的详细信息是 BuildDirectory、WorkspaceName 和 SourcesDirectory。谁能告诉我如何实现工作区的创建或如何在代码中获取这些数据?

构建将在构建服务器上执行,我使用的是 TFS 2010 和 c#。

提前致谢

最佳答案

我关注了Ewald Hofman的系列博文|作为入门,并创建了一个自定义事件,该事件执行我从中解析当前版本的 GlobalAssemblyInfo 文件的 checkout 、更新和 checkin 。我的任务被插入到“更新放置位置”的顶部,就在它完成工作流的“获取构建”部分之后。我只是使用需要 IBuildDetail 和文件掩码作为参数,您可以从中提取 VersionControlServer 以便能够访问 TFS。我的代码如下:

protected override string Execute(CodeActivityContext context)
{
// Obtain the runtime value of the input arguments.
string assemblyInfoFileMask = context.GetValue(AssemblyInfoFileMask);
IBuildDetail buildDetail = context.GetValue(BuildDetail);

var workspace = buildDetail.BuildDefinition.Workspace;
var versionControl = buildDetail.BuildServer.TeamProjectCollection.GetService<VersionControlServer>();

Regex regex = new Regex(AttributeKey + VersionRegex);

// Iterate of the folder mappings in the workspace and find the AssemblyInfo files
// that match the mask.
foreach (var folder in workspace.Mappings)
{
string path = Path.Combine(folder.ServerItem, assemblyInfoFileMask);
context.TrackBuildMessage(string.Format("Checking for file: {0}", path));
ItemSet itemSet = versionControl.GetItems(path, RecursionType.Full);

foreach (Item item in itemSet.Items)
{
context.TrackBuildMessage(string.Format("Download {0}", item.ServerItem));
string localFile = Path.GetTempFileName();

try
{
// Download the file and try to extract the version.
item.DownloadFile(localFile);
string text = File.ReadAllText(localFile);
Match match = regex.Match(text);

if (match.Success)
{
string versionNumber = match.Value.Substring(AttributeKey.Length + 2, match.Value.Length - AttributeKey.Length - 4);
Version version = new Version(versionNumber);
Version newVersion = new Version(version.Major, version.Minor, version.Build + 1, version.Revision);

context.TrackBuildMessage(string.Format("Version found {0}", newVersion));

return newVersion.ToString();
}
}
finally
{
File.Delete(localFile);
}
}
}

return null;
}

关于c# - 如何在自定义工作流事件中获取构建详细信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6188273/

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