开发者

How do I get build details in a custom workflow activity?

I need to add a custom activity to the default workflow template to increase assembly versions at the earliest point possible in the build process.

What I would like to achieve is to create and map the exact same workspace (that is be created further down in the workflow) inside my custom activity so that I can check out an xml file, increase the version number held within, write it back to the开发者_JS百科 xml file and check the xml file back in.

I'm aware that this workspace will be created later on in the workflow but that will be too late in the build process for what I'm trying to achieve, so instead of moving any of the activities or duplicating them in a position above my custom activity (this should be ok as this workspace will be deleted and recreated again later)

I think the details I need are the BuildDirectory, WorkspaceName and SourcesDirectory. Can anyone tell me how to achieve the creation of the workspace or how obtain this data in code?

the build will be carried out on a build server, and I am using TFS 2010 and c#.

Thanks in advance


I followed the series of blog articles by Ewald Hofman as a primer and created a custom activity that does the check-out, update and check-in of a GlobalAssemblyInfo file that I parse the current version from. My task is inserted at the top of the "Update Drop Location" which is right after it does the "Get the build" portion of the workflow. I just use require the IBuildDetail and a File Mask as arguments from which you can pull out the VersionControlServer to be able to access TFS. My code is below:

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;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜