Reputation: 2078
I am currently working on a build definition for a TFS Implementation. I have almost everything working except i cannot get this last part to work. Below is the CodeActivity class that I am currently implementing.
In the workflow diagram i am utilising the default associatedChangesets
variable as the InArgument.
The following code works and creates the folder Database
but the AssociatedChangesets
does not seem to contain any items.
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);
}
}
}
Would anyone be able to help me figure out how to get All SQL changes since the last build was done.
Upvotes: 1
Views: 712
Reputation: 2078
I eventually found how to do this. I will put most of the code below for future reference and people who run into the same problems.
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));
}
}
}
}
Upvotes: 2