1

我目前正在研究 TFS 实现的构建定义。我几乎所有东西都在工作,除了我无法让最后一部分工作。下面是我目前正在实现的 CodeActivity 类。

在工作流程图中,我使用默认associatedChangesets变量作为 InArgument。

以下代码有效并创建文件夹Database,但AssociatedChangesets似乎不包含任何项目。

public sealed class CreateDatabaseDrop : CodeActivity
    {

        public InArgument<Workspace> Workspace { get; set; }
        public InArgument<string> DropLocation { get; set; }
        public InArgument<IList<Changeset>> AssociatedChangesets { get; set; }
        // If your activity returns a value, derive from CodeActivity<TResult>
        // and return the value from the Execute method.

        Workspace workspace;
        string dropLocation;
        IList<Changeset> associatedChangesets;
        protected override void Execute(CodeActivityContext context)
        {
            List<string> filesChanged = new List<string>();
            workspace = context.GetValue(this.Workspace);
            dropLocation = context.GetValue(this.DropLocation);
            associatedChangesets = context.GetValue(this.AssociatedChangesets);

            if (!Directory.Exists(dropLocation + @"\database\"))
                Directory.CreateDirectory(dropLocation + @"\database\");

            foreach (var c in associatedChangesets.OrderBy(x => x.CreationDate))
            {
                foreach (var change in c.Changes)
                {
                    context.WriteBuildMessage(change.Item.ServerItem);
                }
                foreach (var change in c.Changes.Where(x => x.Item.ItemType == ItemType.File && x.Item.ServerItem.Split('/').Last().ToLower().Contains(".sql")))
                {
                    string fileName = change.Item.ServerItem.Split('/').Last();
                    context.WriteBuildMessage(string.Format("SQL File Found: {0}", change.Item.ServerItem));
                    WorkingFolder wf = workspace.GetWorkingFolderForServerItem(change.Item.ServerItem);
                    string copyFrom = Path.Combine(wf.LocalItem, fileName),
                    copyTo = dropLocation + @"\database\" + fileName;
                    context.WriteBuildMessage(string.Format("Copying {0} to {1}", fileName, copyTo));
                    File.Copy(copyFrom, copyTo, true);
                }
            }
        }

自上次构建完成以来,任何人都可以帮助我弄清楚如何获得所有 SQL 更改。

4

1 回答 1

2

我最终找到了如何做到这一点。我将把大部分代码放在下面以供将来参考和遇到同样问题的人。

    public sealed class CreateDatabaseDrop : CodeActivity
    {
        [RequiredArgument]
        public InArgument<Workspace> Workspace { get; set; }
        [RequiredArgument]
        public InArgument<IBuildDefinition> BuildDefinition { get; set; }
        [RequiredArgument]
        public InArgument<string> ProjectName { get; set; }
        Workspace workspace;

        protected override void Execute(CodeActivityContext context)
        {
            workspace = context.GetValue(this.Workspace);
            IBuildDefinition buildDef = context.GetValue(this.BuildDefinition);
            DateTime? comparison = null;
            var details = buildDef.QueryBuilds().Where(x => x.Status == BuildStatus.Succeeded).OrderBy(x => x.StartTime);
            if (details.Count() > 0)
            {
                comparison = details.Last().StartTime;
            }
            if (!comparison.HasValue)
            {
                return;
            }
            IEnumerable history = workspace.VersionControlServer.QueryHistory("$/" + context.GetValue(ProjectName),
                VersionSpec.Latest,
                0,
                RecursionType.Full,
                null,
                new DateVersionSpec(comparison.Value),
                VersionSpec.Latest,
                Int32.MaxValue,
                true,
                false);

            foreach (Changeset c in history.OfType<Changeset>().OrderBy(x => x.CreationDate))
            {
                foreach (var change in c.Changes.Where(x => x.Item.ItemType == ItemType.File
                    && x.Item.ServerItem.Split('/').Last().ToLower().Contains(".sql")
                    && (x.ChangeType != (ChangeType.Delete | ChangeType.SourceRename | ChangeType.Rename))))
                {
                    string fileName = change.Item.ServerItem.Split('/').Last();
                    WorkingFolder wf = workspace.TryGetWorkingFolderForServerItem(change.Item.ServerItem);
                    if (wf != null && !string.IsNullOrEmpty(wf.LocalItem))
                    {
                        context.WriteBuildMessage(string.Format("SQL File found: {0}", fileName));

                    }
                }
            }
    }
于 2012-08-24T10:17:40.960 回答