我目前正致力于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更改。
答案 0 :(得分: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));
}
}
}
}