/// <summary>
 /// Deep copies this object to a new instance
 /// </summary>
 public DepConfigItem Copy()
 {
     DepConfigItem result = new DepConfigItem();
     result.ContainerKind = this.ContainerKind;
     result.Name = this.Name;
     result.PackageExpression = this.PackageExpression;
     result.Path = this.Path;
     result.RelativePackagePathToBinaries = this.RelativePackagePathToBinaries;
     if (this.FileGlobs != null)
     {
         result.FileGlobs = this.FileGlobs.Select(item => string.Copy(item)).ToList();
     }
     return result;
 }
        private void Copy(DepConfigItem sourceCfg, DepConfigItem targetCfg, bool promptOverwrites, bool testMode)
        {
            //Find the source folder
            DirectoryInfo bin = new DirectoryInfo(sourceCfg.Path);
            if (bin.Exists == false)
            {
                throw new Exception("Configuration error: The bin directory for component " + sourceCfg.Name + " does not exist!");
            }

            DirectoryInfo packages = new DirectoryInfo(targetCfg.Path);
            if (packages.Exists == false)
            {
                throw new Exception("Configuration error: The packages directory for component " + targetCfg.Name + " does not exist!");
            }

            //Get files to copy from the bin
            List<FileInfo> filesToCopy = sourceCfg.FileGlobs.SelectMany(item => bin.GetFiles(item)).Distinct().ToList();

            //Determine the package path
            Regex regex = new Regex(sourceCfg.PackageExpression);
            List<DirectoryInfo> potentialPackageFolders = packages.GetDirectories().Where(item => regex.IsMatch(item.Name)).OrderByDescending(item => item.Name, new VersionAwareStringComparer()).ToList();

            if (!potentialPackageFolders.Any())
            {
                throw new Exception("Could not find a package folder matching pattern " + sourceCfg.PackageExpression);
            }

            //Determine the final path for files
            string finalPath = Path.Combine(potentialPackageFolders[0].FullName, sourceCfg.RelativePackagePathToBinaries);
            if (!Directory.Exists(finalPath))
            {
                throw new Exception("Could not find final path " + finalPath);
            }

            StringBuilder testModeOutput = new StringBuilder();

            //Copy the filesToCopy into the first potentialPackageFolders item (which would be the newest)
            foreach (FileInfo file in filesToCopy)
            {
                string targetPath = Path.Combine(finalPath, file.Name);
                if (testMode)
                {
                    testModeOutput.AppendLine(string.Format("Copying {0} to {1}", file.FullName, targetPath));
                }
                else
                {
                    if (File.Exists(targetPath) && promptOverwrites)
                    {
                        DialogResult confirmOverwrite = MessageBox.Show("Confirm file overwrite?\r\n" + targetPath + "\r\nWith: " + file.FullName, "Confirm", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                        if (confirmOverwrite == DialogResult.Cancel)
                        {
                            tsslStatus.Text = "Cancelled";
                            return;
                        }
                        else if (confirmOverwrite == DialogResult.No)
                        {
                            continue; //Move to next file
                        }
                    }
                    file.CopyTo(targetPath, true);
                }
            }

            if (testMode)
            {
                MessageBox.Show(testModeOutput.ToString());
            }
            else
            {
                tsslStatus.Text = "Copied " + filesToCopy.Count + " files from " + sourceCfg.Name + " to " + targetCfg.Name;
            }
        }