<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>danielcolomb.com &#187; C#</title>
	<atom:link href="http://www.danielcolomb.com/category/programming/see-sharp/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.danielcolomb.com</link>
	<description>rantings of a technophile</description>
	<lastBuildDate>Tue, 13 Dec 2011 14:26:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Deploying Windows Services from TFS</title>
		<link>http://www.danielcolomb.com/2011/06/27/deploying-windows-services-from-tfs/</link>
		<comments>http://www.danielcolomb.com/2011/06/27/deploying-windows-services-from-tfs/#comments</comments>
		<pubDate>Mon, 27 Jun 2011 18:26:11 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Source Control]]></category>
		<category><![CDATA[TFS]]></category>
		<category><![CDATA[deploy]]></category>
		<category><![CDATA[PsExec]]></category>
		<category><![CDATA[Service]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://www.danielcolomb.com/?p=577</guid>
		<description><![CDATA[<p>As part of our initiative to automate code deployments, we needed to move windows services from TFS to our test servers. The way we did this before was to have a build stage all the files and then either stop the services on the destination machine manually, copy files, and restart the services, or [...]]]></description>
			<content:encoded><![CDATA[<p>As part of our initiative to automate code deployments, we needed to move windows services from TFS to our test servers. The way we did this before was to have a build stage all the files and then either stop the services on the destination machine manually, copy files, and restart the services, or use a homebrewed application to deploy the service; which is fine and dandy, except that the original developer isn&#8217;t with the company anymore and the application is limited in certain ways.</p>
<p>So to solve this, I went ahead and created a new build template that would deploy our windows services from a TFS build.</p>
<p><strong>Some prerequisites:<br />
</strong></p>
<ul>
<li><span style="font-weight: normal;">PsExec must be on the build machine </span></li>
<li>Your build service must be an admin on the machine you&#8217;re deploying to (to be able to stop and start services)</li>
</ul>
<p>First, we&#8217;ll set up a new Code Activity, it&#8217;s very similar to the <a title="Deploying to Multiple Locations in TFS 2010" href="http://www.danielcolomb.com/2011/02/23/deploying-to-multiple-locations-in-tfs-2010/">DeployFiles</a> class i wrote about before</p>
<pre class="brush: csharp; title: Code block; notranslate">
using System;
using System.Activities;
using System.IO;
using System.Text.RegularExpressions;
using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.Build.Workflow.Activities;
using Microsoft.TeamFoundation.Build.Workflow.Tracking;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
namespace BuildProcess.Activities
{
    [BuildActivity(HostEnvironmentOption.Agent)]
    public sealed class DeployWindowsService : CodeActivity
    {
        //Source dir being deployed from
        [RequiredArgument]
        public InArgument&lt;string&gt; SourceDir { get; set; }
        //Destination dir being copied from
        [RequiredArgument]
        public InArgument&lt;string[]&gt; DestinationDir { get; set; }
        //Files to include
        [RequiredArgument]
        public InArgument&lt;string&gt; FileInclusions { get; set; }
        //Folders to include
        [RequiredArgument]
        public InArgument&lt;string&gt; FolderInclusions { get; set; }
        [RequiredArgument]
        public InArgument&lt;string&gt; CollectionName { get; set; }
        public InArgument&lt;string&gt; ConfigLocations { get; set; }
        //globals
        public string fileInclusionPattern = &quot;&quot;;
        public string folderInclusionPattern = &quot;&quot;;
        protected override void Execute(CodeActivityContext context)
        {
            // Obtain the runtime value of the Text input argument
            string fileInclusions = context.GetValue(this.FileInclusions);
            string folderInclusions = context.GetValue(this.FolderInclusions);
            string configLocations = context.GetValue(this.ConfigLocations);
            string[] destinations = context.GetValue(this.DestinationDir);
            string collectionName = context.GetValue(this.CollectionName);
            DirectoryInfo sourceDir = new DirectoryInfo(context.GetValue(this.SourceDir));
            //parse exclusions, add them to regex patterns
            if (!String.IsNullOrWhiteSpace(fileInclusions))
            {
                string[] fileinarr = fileInclusions.Split(',');
                fileInclusionPattern = &quot;(&quot;;
                foreach (string s in fileinarr)
                {
                    fileInclusionPattern += s.ToUpper().Trim().Replace(&quot;.&quot;, @&quot;\.&quot;).Replace(&quot;*&quot;, @&quot;[a-zA-Z0-9]*&quot;) + &quot;$|&quot;;
                }
                if (fileInclusionPattern.EndsWith(&quot;|&quot;))
                    fileInclusionPattern = fileInclusionPattern.Substring(0, fileInclusionPattern.Length - 1);
                fileInclusionPattern += &quot;)&quot;;
            }
            if (!String.IsNullOrWhiteSpace(folderInclusions))
            {
                string[] folderinarr = folderInclusions.Split(',');
                folderInclusionPattern = &quot;(&quot;;
                foreach (string s in folderinarr)
                {
                    folderInclusionPattern += s.ToUpper().Trim().Replace(&quot;.&quot;, @&quot;\.&quot;).Replace(&quot;*&quot;, @&quot;[a-zA-Z0-9]*&quot;) + &quot;$|&quot;;
                }
                if (folderInclusionPattern.EndsWith(&quot;|&quot;))
                    folderInclusionPattern = folderInclusionPattern.Substring(0, folderInclusionPattern.Length - 1);
                folderInclusionPattern += &quot;)&quot;;
            }
            foreach (string dir in destinations)
            {
                DirectoryInfo destDir = new DirectoryInfo(dir);
                //Used for debugging, you don't need this, unless you want to display something custom here.
                context.Track(new BuildInformationRecord&lt;BuildMessage&gt;()
                {
                    Value = new BuildMessage()
                    {
                        Importance = BuildMessageImportance.High,
                        Message = &quot;Source: &quot; + sourceDir.FullName +
                            &quot;\r\nDestination: &quot; + destDir.FullName +
                            &quot;\r\nFolder Inclusion Pattern: &quot; + folderInclusionPattern +
                            &quot;\r\nFile Inclusion Pattern: &quot; + fileInclusionPattern +
                            &quot;\r\nFolder Inclusions String: &quot; + folderInclusions +
                            &quot;\r\nFile Inclusions String: &quot; + fileInclusions,
                    },
                });
                CopyDir(sourceDir, destDir);
            }
            //copy config files
            if (!String.IsNullOrWhiteSpace(configLocations))
            {
                string strTFSName = collectionName;
                //set up TFS connectivity
                Uri tfsName = new Uri(strTFSName);
                TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(tfsName);
                VersionControlServer vcs = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));
                //copy config files to proper location
                string timestamp = DateTime.Now.Ticks.ToString();
                string workspacePath = @&quot;C:\temp\TFSConfigDeployment_&quot; + timestamp;
                Workspace ws = vcs.CreateWorkspace(&quot;TFSConfigDeployment_&quot; + timestamp, vcs.AuthorizedUser);
                //get the workspace locally
                ws.Map(configLocations, workspacePath);
                ws.Get();
                foreach (string configDir in destinations)
                {
                    CopyDir(new DirectoryInfo(workspacePath), new DirectoryInfo(configDir));
                }
                RemoveWorkSpaceDirectory(workspacePath);
            }
        }
        private void RemoveWorkSpaceDirectory(string workspacePath)
        {
            DirectoryInfo dir = new DirectoryInfo(workspacePath);
            var files = dir.EnumerateFiles();
            foreach (FileSystemInfo info in files)
            {
                string filePath = info.FullName;
                if ((File.GetAttributes(filePath) &amp; FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                {
                    File.SetAttributes(filePath, FileAttributes.Normal);
                }
            }
            var subDirs = dir.EnumerateDirectories();
            foreach (DirectoryInfo subDir in subDirs)
            {
                RemoveWorkSpaceDirectory(subDir.FullName);
            }
            dir.Delete(true);
        }
        private void CopyDir(DirectoryInfo sourceDir, DirectoryInfo destDir)
        {
            //create destination dir if it doesn't exist
            if (!destDir.Exists)
            {
                destDir.Create();
            }
            // get all files from current dir
            FileInfo[] files = sourceDir.GetFiles();
            //copy ze files!!
            foreach (FileInfo file in files)
            {
                string destFilePath = Path.Combine(destDir.FullName, file.Name);
                if (!String.IsNullOrWhiteSpace(fileInclusionPattern))
                {
                    if (Regex.IsMatch(file.Name.ToUpper(), fileInclusionPattern))
                    {
                        if (File.Exists(destFilePath))
                        {
                            if ((File.GetAttributes(destFilePath) &amp; FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                            {
                                File.SetAttributes(destFilePath, FileAttributes.Normal);
                            }
                            File.Delete(destFilePath);
                        }
                        file.CopyTo(destFilePath);
                        if ((File.GetAttributes(destFilePath) &amp; FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                        {
                            File.SetAttributes(destFilePath, FileAttributes.Normal);
                        }
                    }
                }
            }
            // get subdirectories.
            DirectoryInfo[] dirs = sourceDir.GetDirectories();
            foreach (DirectoryInfo dir in dirs)
            {
                // Get destination directory.
                string destinationDir = Path.Combine(destDir.FullName, dir.Name);
                if (!String.IsNullOrWhiteSpace(folderInclusionPattern))
                {
                    if (Regex.IsMatch(dir.Name.ToUpper(), folderInclusionPattern))
                    {
                        // Call CopyDirectory() recursively.
                        CopyDir(dir, new DirectoryInfo(destinationDir));
                    }
                }
            }
        }
    }
}
</pre>
<p>For the next step, you&#8217;ll also want to look at the previous post about <a title="Deploying to Multiple Locations in TFS 2010" href="http://www.danielcolomb.com/2011/02/23/deploying-to-multiple-locations-in-tfs-2010/">deploying files from TFS</a>. After you&#8217;ve opened up your ProcessTemplate, you&#8217;ll want to have the following arguements:</p>
<ul>
<li>DeploymentDir &#8211; In &#8211; String[]</li>
<li>SourceRootDir &#8211; In &#8211; String</li>
<li>CollectionName &#8211; In &#8211; String</li>
<li>PsExecPath &#8211; In &#8211; String</li>
<li>ServiceName &#8211; In &#8211; String</li>
<li>ServerName &#8211; In &#8211; String</li>
<li>FileInclusions &#8211; In &#8211; String</li>
<li>FolderInclustions &#8211; In &#8211; String</li>
<li>RestartService &#8211; In &#8211; Boolean &#8211; True</li>
</ul>
<p>Each of these you&#8217;ll probably want to add to the Metadata Argument so you can keep your build definitions organized.</p>
<p><a href="http://www.danielcolomb.com/wp-content/uploads/2011/06/buildDef.png"><img class="alignnone size-full wp-image-584" title="buildDef" src="http://www.danielcolomb.com/wp-content/uploads/2011/06/buildDef.png" alt="" width="745" height="197" /></a></p>
<p>And you&#8217;ll want to add the following Variable:</p>
<ul>
<li>PsExecResult &#8211; Int32 &#8211; Revert Workspace and Copy Files&#8230;</li>
</ul>
<p>Under Process &gt; Sequence &gt; Run On Agent &gt; Try Compile, Test, and Associate Changesets and Work Items &gt; Revert Workspace and Copy Files to Drop Location &gt; If Deployment Location is Set you&#8217;ll want to place a new Sequence and call it Stop Service, Copy Files, Start Service.</p>
<p><a href="http://www.danielcolomb.com/wp-content/uploads/2011/06/StopCopyStartServices.png"><img class="alignnone size-full wp-image-578" title="StopCopyStartServices" src="http://www.danielcolomb.com/wp-content/uploads/2011/06/StopCopyStartServices.png" alt="" width="508" height="673" /></a></p>
<p>Inside that Sequence you&#8217;ll want to place an Invoke Process activity, the new DeployWindowsServices code activity, and an If statement with another Invoke Process activity in the Then clause.</p>
<p>For the first Process Invocation:</p>
<p><a href="http://www.danielcolomb.com/wp-content/uploads/2011/06/InvokePsExecStopService.png"><img class="alignnone size-full wp-image-581" title="InvokePsExecStopService" src="http://www.danielcolomb.com/wp-content/uploads/2011/06/InvokePsExecStopService.png" alt="" width="351" height="176" /></a></p>
<p>Arguements:  ServerName + &#8221; -s -d net stop &#8221; + ServiceName<br />
DisplayName: Stop Service<br />
FileName: PsExecPath<br />
Result: PsExecResult</p>
<p>For the Deployment Activity:</p>
<p><a href="http://www.danielcolomb.com/wp-content/uploads/2011/06/DeployService.png"><img class="alignnone size-full wp-image-579" title="DeployService" src="http://www.danielcolomb.com/wp-content/uploads/2011/06/DeployService.png" alt="" width="354" height="173" /></a></p>
<p>CollectionName: CollectionName<br />
DestinationDir: DeploymentDir<br />
DisplayName: Deploy Windows Service<br />
FileInclustions: FileInclusions<br />
FolderInclustions: FolderInclusions<br />
SourceDir:  BinariesDirectory + &#8220;\&#8221; + SourceRootDir</p>
<p>For the If statement, just check to see if RestartService is set to True, and then enter the values for the second Process Invocation:</p>
<p><a href="http://www.danielcolomb.com/wp-content/uploads/2011/06/InvokePsExecStartService.png"><img class="alignnone size-full wp-image-580" title="InvokePsExecStartService" src="http://www.danielcolomb.com/wp-content/uploads/2011/06/InvokePsExecStartService.png" alt="" width="353" height="179" /></a></p>
<p>Arguements:  ServerName + &#8221; -s -d net start &#8221; + ServiceName<br />
DisplayName: Start Service<br />
FileName: PsExecPath</p>
<p><strong>*NOTE, THE FOLLOWING SECTION DOES NOT WORK PROPERLY YET*</strong></p>
<p>After the If statement, you&#8217;ll want to add in another If, this will check to see if PsExec has failed or not.<a href="http://www.danielcolomb.com/wp-content/uploads/2011/06/PsExecFail.png"><img class="alignnone size-full wp-image-582" title="PsExecFail" src="http://www.danielcolomb.com/wp-content/uploads/2011/06/PsExecFail.png" alt="" width="482" height="223" /></a></p>
<p>Since this isn&#8217;t working properly yet, i just check to see if PsExecResult is &#8217;1&#8242;, which it hasn&#8217;t been as of this writing. In the Then block, add a Throw activity and have it throw an exception of:</p>
<pre class="brush: csharp; title: Code block; notranslate">
New Exception(&quot;PsExecResult: &quot; + PsExecResult.ToString())
</pre>
<p>Optionally, you can also add WriteBuildMessage and WriteBuildWarning activities to your Invoke Process Activities, these are nice because you can see what the process you are invoking outputs.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.danielcolomb.com/2011/06/27/deploying-windows-services-from-tfs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Deploying to Multiple Locations in TFS 2010</title>
		<link>http://www.danielcolomb.com/2011/02/23/deploying-to-multiple-locations-in-tfs-2010/</link>
		<comments>http://www.danielcolomb.com/2011/02/23/deploying-to-multiple-locations-in-tfs-2010/#comments</comments>
		<pubDate>Wed, 23 Feb 2011 16:32:55 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[TFS]]></category>
		<category><![CDATA[deploy]]></category>
		<category><![CDATA[multiple locations]]></category>
		<category><![CDATA[TFS 2010]]></category>

		<guid isPermaLink="false">http://www.danielcolomb.com/?p=517</guid>
		<description><![CDATA[<p>This is just a forewarning, I am not an expert in the ways of TFS. I&#8217;ve only been working with TFS in general for about a year, and TFS 2010 for about 2 months or so. That being said, if anyone has recommendations on a better way to do this, please point me in [...]]]></description>
			<content:encoded><![CDATA[<p>This is just a forewarning, I am not an expert in the ways of TFS. I&#8217;ve only been working with TFS in general for about a year, and TFS 2010 for about 2 months or so. That being said, if anyone has recommendations on a better way to do this, please point me in the right direction <img src='http://www.danielcolomb.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h4>Scenario:</h4>
<p>We want to deploy our built code to different environments, but, we don&#8217;t want everything that&#8217;s dumped into the Release folder.</p>
<h4>The Plan:</h4>
<p>The plan is to create a custom build activity to copy files from one place to another checking each file against an exclusion list.</p>
<h4>The Guts:</h4>
<p>Referencing my previous post, <a title="Dependency Replication in TFS 2010" href="http://www.danielcolomb.com/2011/01/20/dependency-replication-in-tfs-2010/">Dependency Replication in TFS 2010</a>, I will build upon the write-ups by <a title="Customizing Team Build 2010" href="http://www.ewaldhofman.nl/post/2010/04/20/Customize-Team-Build-2010-e28093-Part-1-Introduction.aspx">Ewald Hofman</a>.</p>
<p>You can grab the file here: <a title="C# source" href="http://www.danielcolomb.com/files/DeployFiles.cs">DeployFiles.cs</a></p>
<p>Here&#8217;s the whole activity:</p>
<pre class="brush: csharp; title: Code block; notranslate">
using System;
using System.Activities;
using System.IO;
using System.Text.RegularExpressions;
using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.Build.Workflow.Activities;
using Microsoft.TeamFoundation.Build.Workflow.Tracking;

namespace BuildProcess.Activities
{
    [BuildActivity(HostEnvironmentOption.Agent)]
    public sealed class DeployFiles : CodeActivity
    {
        //Source dir being deployed from
        [RequiredArgument]
        public InArgument&lt;string&gt; SourceDir { get; set; }
        //Destination dir being copied from
        [RequiredArgument]
        public InArgument&lt;string[]&gt; DestinationDir { get; set; }
        //Files to exclude
        [RequiredArgument]
        public InArgument&lt;string&gt; FileExclusions { get; set; }
        //Folders to exclude
        [RequiredArgument]
        public InArgument&lt;string&gt; FolderExclusions { get; set; }

        //globals
        public string fileExclusionPattern = &quot;&quot;;
        public string folderExclusionPattern = &quot;&quot;;

        protected override void Execute(CodeActivityContext context)
        {
            // Obtain the runtime value of the Text input argument
            string fileExclusions = context.GetValue(this.FileExclusions);
            string folderExclusions = context.GetValue(this.FolderExclusions);
            string[] destinations = context.GetValue(this.DestinationDir);
            DirectoryInfo sourceDir = new DirectoryInfo(context.GetValue(this.SourceDir));

            //parse exclusions, add them to regex patterns
            if (!String.IsNullOrWhiteSpace(fileExclusions))
            {
                string[] fileexarr = fileExclusions.Split(',');
                fileExclusionPattern = &quot;(&quot;;
                foreach (string s in fileexarr)
                {
                    fileExclusionPattern += s.ToUpper().Trim().Replace(&quot;.&quot;, @&quot;\.&quot;).Replace(&quot;*&quot;, @&quot;[a-zA-Z0-9]*&quot;) + &quot;|&quot;;
                }

                if(fileExclusionPattern.EndsWith(&quot;|&quot;))
                    fileExclusionPattern = fileExclusionPattern.Substring(0, fileExclusionPattern.Length - 1);

                fileExclusionPattern += &quot;)&quot;;
            }

            if (!String.IsNullOrWhiteSpace(folderExclusions))
            {
                string[] folderexarr = folderExclusions.Split(',');
                folderExclusionPattern = &quot;(&quot;;

                foreach (string s in folderexarr)
                {
                    folderExclusionPattern += s.ToUpper().Trim().Replace(&quot;.&quot;, @&quot;\.&quot;).Replace(&quot;*&quot;, @&quot;[a-zA-Z0-9]*&quot;) + &quot;|&quot;;
                }

                if(folderExclusionPattern.EndsWith(&quot;|&quot;))
                    folderExclusionPattern = folderExclusionPattern.Substring(0, folderExclusionPattern.Length - 1);

                folderExclusionPattern += &quot;)&quot;;
            }

            foreach (string dir in destinations)
            {
                DirectoryInfo destDir = new DirectoryInfo(dir);

                context.Track(new BuildInformationRecord&lt;BuildMessage&gt;()
                {
                    Value = new BuildMessage()
                    {
                        Importance = BuildMessageImportance.High,
                        Message = &quot;Source: &quot; + sourceDir.FullName + &quot;\nDestination: &quot; + destDir.FullName +
                            &quot;\nFolder Exclusion Pattern: &quot; + folderExclusionPattern +
                            &quot;\nFile Exclusion Pattern: &quot; + fileExclusionPattern,
                    },
                });

                CopyDir(sourceDir, destDir);
            }
        }

        private void CopyDir(DirectoryInfo sourceDir, DirectoryInfo destDir)
        {
            //create destination dir if it doesn't exist
            if (!destDir.Exists)
            {
                destDir.Create();
            }

            // get all files from current dir
            FileInfo[] files = sourceDir.GetFiles();

            //copy ze files!!
            foreach (FileInfo file in files)
            {
                if (!String.IsNullOrWhiteSpace(fileExclusionPattern))
                {
                    if (!Regex.IsMatch(file.Name.ToUpper(), fileExclusionPattern))
                    {
                        if (File.Exists(Path.Combine(destDir.FullName, file.Name)))
                        {
                            File.Delete(Path.Combine(destDir.FullName, file.Name));
                        }
                        file.CopyTo(Path.Combine(destDir.FullName, file.Name));
                    }
                }
                else
                {
                    if (File.Exists(Path.Combine(destDir.FullName, file.Name)))
                    {
                        File.Delete(Path.Combine(destDir.FullName, file.Name));
                    }
                    file.CopyTo(Path.Combine(destDir.FullName, file.Name),true);
                }
            }

            // get subdirectories.
            DirectoryInfo[] dirs = sourceDir.GetDirectories();

            foreach (DirectoryInfo dir in dirs)
            {
                // Get destination directory.
                string destinationDir = Path.Combine(destDir.FullName, dir.Name);

                if (!String.IsNullOrWhiteSpace(folderExclusionPattern))
                {
                    if (!Regex.IsMatch(dir.Name.ToUpper(), folderExclusionPattern))
                    {
                        // Call CopyDirectory() recursively.
                        CopyDir(dir, new DirectoryInfo(destinationDir));
                    }
                }
                else
                {
                    // Call CopyDirectory() recursively.
                    CopyDir(dir, new DirectoryInfo(destinationDir));
                }
            }
        }
    }
}
</pre>
<h4>Explanation:</h4>
<p>Now i&#8217;ll go section by section explaining exactly what&#8217;s going on. First up are the input requirements:</p>
<pre class="brush: csharp; light: true; title: Code block; notranslate">
         //Source dir being deployed from
        [RequiredArgument]
        public InArgument&lt;string&gt; SourceDir { get; set; }
</pre>
<p>The SourceDir is where the deployment starts from. This means what subdirectory from the BinariesDirectory gets deployed. This is important for Websites, since they get dropped in a _PublishedWebsites folder inside the BinariesDirectory.</p>
<pre class="brush: csharp; light: true; title: Code block; notranslate">
        //Destination dir being copied from
        [RequiredArgument]
        public InArgument&lt;string[]&gt; DestinationDir { get; set; }
</pre>
<p>The DestinationDir argument is a string array of paths being passed in (we use UNC network paths, ie: \\deploymentbox\websites\sitename)</p>
<pre class="brush: csharp; light: true; title: Code block; notranslate">
        //Files to exclude
        [RequiredArgument]
        public InArgument&lt;string&gt; FileExclusions { get; set; }
</pre>
<p>The FileExclusions argument is meant to be a comma delimited string of filenames and patterns you want to exclude. (ie: &#8220;web.config, *.pdb&#8221;)</p>
<pre class="brush: csharp; light: true; title: Code block; notranslate">
        //Folders to exclude
        [RequiredArgument]
        public InArgument&lt;string&gt; FolderExclusions { get; set; }
</pre>
<p>The FolderExclusions argument is similar to the FileExclusions, except that if there are certain folders you don&#8217;t want deployed, you can specify them here</p>
<pre class="brush: csharp; light: true; title: Code block; notranslate">
        //globals
        public string fileExclusionPattern = &quot;&quot;;
        public string folderExclusionPattern = &quot;&quot;;
</pre>
<p>These two variables will be used to build the Regex pattern which we will check files and folders against.</p>
<p>Now we&#8217;ll go into</p>
<pre class="brush: csharp; light: true; title: Code block; notranslate">protected override void Execute(CodeActivityContext context)</pre>
<p>explaining each part in more detail.</p>
<pre class="brush: csharp; light: true; title: Code block; notranslate">
            // Obtain the runtime value of the Text input argument
            string fileExclusions = context.GetValue(this.FileExclusions);
            string folderExclusions = context.GetValue(this.FolderExclusions);
            string[] destinations = context.GetValue(this.DestinationDir);
            DirectoryInfo sourceDir = new DirectoryInfo(context.GetValue(this.SourceDir));
</pre>
<p>Here we get the values being passed in at execution time</p>
<pre class="brush: csharp; light: true; title: Code block; notranslate">
            //parse exclusions, add them to regex patterns
            if (!String.IsNullOrWhiteSpace(fileExclusions))
            {
                string[] fileexarr = fileExclusions.Split(',');
                fileExclusionPattern = &quot;(&quot;;
                foreach (string s in fileexarr)
                {
                    fileExclusionPattern += s.ToUpper().Trim().Replace(&quot;.&quot;, @&quot;\.&quot;).Replace(&quot;*&quot;, @&quot;[a-zA-Z0-9]*&quot;) + &quot;|&quot;;
                }

                if(fileExclusionPattern.EndsWith(&quot;|&quot;))
                    fileExclusionPattern = fileExclusionPattern.Substring(0, fileExclusionPattern.Length - 1);

                fileExclusionPattern += &quot;)&quot;;
            }

            if (!String.IsNullOrWhiteSpace(folderExclusions))
            {
                string[] folderexarr = folderExclusions.Split(',');
                folderExclusionPattern = &quot;(&quot;;

                foreach (string s in folderexarr)
                {
                    folderExclusionPattern += s.ToUpper().Trim().Replace(&quot;.&quot;, @&quot;\.&quot;).Replace(&quot;*&quot;, @&quot;[a-zA-Z0-9]*&quot;) + &quot;|&quot;;
                }

                if(folderExclusionPattern.EndsWith(&quot;|&quot;))
                    folderExclusionPattern = folderExclusionPattern.Substring(0, folderExclusionPattern.Length - 1);

                folderExclusionPattern += &quot;)&quot;;
            }
</pre>
<p>Here we check to see if anything was passed into the file or folder exclusion parameters. If something was passed in, we split the comma delimited string into an array and then iterate through it adding the exclusions to their appropriate Regex pattern.</p>
<pre class="brush: csharp; light: true; title: Code block; notranslate">
            foreach (string dir in destinations)
            {
                DirectoryInfo destDir = new DirectoryInfo(dir);

                context.Track(new BuildInformationRecord&lt;BuildMessage&gt;()
                {
                    Value = new BuildMessage()
                    {
                        Importance = BuildMessageImportance.High,
                        Message = &quot;Source: &quot; + sourceDir.FullName + &quot;\nDestination: &quot; + destDir.FullName +
                            &quot;\nFolder Exclusion Pattern: &quot; + folderExclusionPattern +
                            &quot;\nFile Exclusion Pattern: &quot; + fileExclusionPattern,
                    },
                });

                CopyDir(sourceDir, destDir);
            }
</pre>
<p>Now we loop through all the destinations we need to deploy to, copying the files and folders.</p>
<p>Now lets take a look at</p>
<pre class="brush: csharp; light: true; title: Code block; notranslate">private void CopyDir(DirectoryInfo sourceDir, DirectoryInfo destDir)</pre>
<pre class="brush: csharp; light: true; title: Code block; notranslate">
            //create destination dir if it doesn't exist
            if (!destDir.Exists)
            {
                destDir.Create();
            }
</pre>
<p>Let&#8217;s make sure the destination directory exists, creating it if it doesn&#8217;t exist already.</p>
<pre class="brush: csharp; light: true; title: Code block; notranslate">
            // get all files from current dir
            FileInfo[] files = sourceDir.GetFiles();
</pre>
<p>Here we get the all the files from the source directory</p>
<pre class="brush: csharp; light: true; title: Code block; notranslate">
            //copy ze files!!
            foreach (FileInfo file in files)
            {
                if (!String.IsNullOrWhiteSpace(fileExclusionPattern))
                {
                    if (!Regex.IsMatch(file.Name.ToUpper(), fileExclusionPattern))
                    {
                        if (File.Exists(Path.Combine(destDir.FullName, file.Name)))
                        {
                            File.Delete(Path.Combine(destDir.FullName, file.Name));
                        }
                        file.CopyTo(Path.Combine(destDir.FullName, file.Name));
                    }
                }
                else
                {
                    if (File.Exists(Path.Combine(destDir.FullName, file.Name)))
                    {
                        File.Delete(Path.Combine(destDir.FullName, file.Name));
                    }
                    file.CopyTo(Path.Combine(destDir.FullName, file.Name),true);
                }
            }
</pre>
<p>Let&#8217;s copy some files! Check each file against the exclusion pattern, if one is set, and copy it if needed. If the file needs to be copied and it exists in the destination already, we&#8217;ll delete it and copy the file over.</p>
<pre class="brush: csharp; light: true; title: Code block; notranslate">
            // get subdirectories.
            DirectoryInfo[] dirs = sourceDir.GetDirectories();

            foreach (DirectoryInfo dir in dirs)
            {
                // Get destination directory.
                string destinationDir = Path.Combine(destDir.FullName, dir.Name);

                if (!String.IsNullOrWhiteSpace(folderExclusionPattern))
                {
                    if (!Regex.IsMatch(dir.Name.ToUpper(), folderExclusionPattern))
                    {
                        // Call CopyDirectory() recursively.
                        CopyDir(dir, new DirectoryInfo(destinationDir));
                    }
                }
                else
                {
                    // Call CopyDirectory() recursively.
                    CopyDir(dir, new DirectoryInfo(destinationDir));
                }
            }
</pre>
<p>Last we check to see if the directory has any sub-directories and we call CopyDir() recursively to copy all sub directories and files inside them, only of course if they aren&#8217;t on the folder exclusion list.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.danielcolomb.com/2011/02/23/deploying-to-multiple-locations-in-tfs-2010/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Week One Complete!</title>
		<link>http://www.danielcolomb.com/2010/01/10/week-one-complete/</link>
		<comments>http://www.danielcolomb.com/2010/01/10/week-one-complete/#comments</comments>
		<pubDate>Sun, 10 Jan 2010 15:28:12 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[life]]></category>
		<category><![CDATA[web programming]]></category>
		<category><![CDATA[body clock]]></category>
		<category><![CDATA[Daybreakers]]></category>
		<category><![CDATA[first week]]></category>
		<category><![CDATA[Infiniti G35]]></category>
		<category><![CDATA[Za's]]></category>

		<guid isPermaLink="false">http://www.danielcolomb.com/?p=424</guid>
		<description><![CDATA[<p>I&#8217;ve finished my first week at my new job. It&#8217;s a weird sensation. I feel like I&#8217;ve been there for quite a while already, but that time has gone by really fast. Maybe it has to do with the fact that i was totally sick for about half the week? The first three days [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve finished my first week at my new job. It&#8217;s a weird sensation. I feel like I&#8217;ve been there for quite a while already, but that time has gone by really fast. Maybe it has to do with the fact that i was totally sick for about half the week? The first three days were pretty much getting to know what the company does and how they do things on a very high level. Thursday and Friday were pretty much dedicated to scouring over thousands of lines of C#, ASP.Net, and Javascript to learn how their Consumer site works. Not the most exciting stuff in the world. Everyone is very nice and patient with our questions.</p>
<p>Boy, was i ready for saturday though! I guess my body was still used to sleeping for 10 hours every night. Instead i&#8217;ve been up by 7am everyday and out the door by 7:30. Yesterday i finally got to sleep in a bit, lounge around in my PJ&#8217;s, and then finally in the afternoon leave the house. Josh and I went up to Louisville so Josh could test drive a Infiniti G35. A very nice car! After that we went and ate pizza and drank beer at Za&#8217;s where we met up with some friends, one of whom neither of us had seen in years! After pizza and beer we went and saw Daybreakers, which was ok. It&#8217;s nothing to rave about, and I feel the previews mislead the audience to thinking the movie was something else. But exploding vampires are always nice.</p>
<p>Tomorrow starts week two! Hopefully nothing will go wrong, and the roads will stay clear!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.danielcolomb.com/2010/01/10/week-one-complete/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

