第一个比较简单。当您使用新的 TFS 2013 构建服务器和流程模板时,您只需在构建定义配置中添加构建后的 powershell 脚本,签入脚本并在构建期间运行它。
第二个取决于您使用的是 TFVC 还是 Git,在第一种情况下,使用 VersionControlServer 类来查询 BranchObjects,然后检查哪个是您的工作文件夹的根。但请注意,在 TFVC 中,可以在一个工作区中引用多个分支,因此此查询可能有多个答案,具体取决于您使用哪个文件来查找分支根。自定义 CodeActivity 可以解决问题,类似于 this check in a custom checkin policy。
代码将类似于:
IBuildDetail buildDetail = context.GetExtension<IBuildDetail>();
var workspace = buildDetail.BuildDefinition.Workspace;
var versionControlServer = buildDetail.BuildServer.TeamProjectCollection.GetService<VersionControlServer>();
var branches = versionControlServer.QueryRootBranchObjects(RecursionType.Full);
var referencedBranches = listOfFilePaths.GroupBy(
file =>
branches.SingleOrDefault(
branch => file.ServerItem.StartsWith(branch.Properties.RootItem.Item)
)
).Where(group => group.Key != null);
要获取 yo 工作区中所有项目的列表,您可以使用 Workspace.GetItems。
如果您使用的是 Git,您也有一些选择。最简单的就是invoke the command line:
git symbolic-ref --short HEAD
或深入LibGit2Sharp 并使用它根据自定义活动的当前工作文件夹查找分支名称。
如果您想将其包含在 MsBuild 任务中,这也可能是可行的。这个答案完全概述了所需的步骤有点远,但是一旦你知道该怎么做就不是那么难了。
Create a custom MsBuild task that invokes the same snippet of code above,虽然不是通过BuildDetail.BuildDefinition.Workspace 访问工作区,而是through the WorkStation class:
Workstation workstation = Workstation.Current;
WorkspaceInfo info = workstation.GetLocalWorkspaceInfo(path);
TfsTeamProjectCollection collection = new TfsTeamProjectCollection(info.ServerUri);
Workspace workspace = info.GetWorkspace(collection);
VersionControlServer versionControlServer = collection.GetService<VersionControlServer>();
创建任务后,您可以创建一个自定义 .targets 文件,该文件通过覆盖某些变量或在构建完成时复制数据来连接到 MsBuild 进程。您可以挂钩多个目标并定义是否需要在它们之前或之后做某事。
您可以将 <import> 这些添加到您的每个项目中,也可以将其放置在您的 MsBuild 版本的 ImportAfter 或 ImportBefore 文件夹中以使其全局加载。您可以在此处找到该文件夹:
C:\Program Files (x86)\MSBuild\{MsBuild Version}\Microsoft.Common.Targets\ImportAfter