/// <summary>
 /// Generates the source files.
 /// </summary>
 /// <param name="pipelineComponent">The pipeline component.</param>
 /// <param name="generatedDestPath">The generated dest path.</param>
 private static void generateSourceFiles(PipelineSegmentSource pipelineComponent, string generatedDestPath)
 {
     foreach (SourceFile source in pipelineComponent.Files)
     {
         using (var sw = new StreamWriter(generatedDestPath.Combine(source.Name)))
         {
             sw.WriteLine(source.Source);
             sw.Close();
         }
     }
 }
        /// <summary>
        /// Gets the sub path.
        /// </summary>
        /// <param name="configuration">The configuration.</param>
        /// <param name="component">The component.</param>
        /// <param name="root">The root.</param>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        private static string getSubPath(PipelineConfiguration configuration,
                                         PipelineSegmentSource component, string root,
                                         out string name)
        {
            string  pathTakeRoot = name = component.Name;
            bool    found;
            Project proj;

            switch (component.Type)
            {
            case SegmentType.AddInSideAdapter:
                found = (proj = configuration.AddInSideAdapterProject) != null;
                break;

            case SegmentType.AddInView:
                found = (proj = configuration.AddInViewProject) != null;
                break;

            case SegmentType.HostAddInView:
                found = (proj = configuration.HostViewProject) != null;
                break;

            case SegmentType.HostSideAdapter:
                found = (proj = configuration.HostSideAdapterProject) != null;
                break;

            case SegmentType.View:
                found = (proj = configuration.AddInViewProject) != null;
                break;

            default:
                throw new InvalidOperationException(string.Format("Doesn't know segment type: {0}", component.Type));
            }

            if (found)
            {
                assertNotOutsideRoot(proj, root);

                pathTakeRoot = proj.FullName.Substring(root.Length).Trim('\\', '/');                                  // remove root
                pathTakeRoot = pathTakeRoot.Substring(0, pathTakeRoot.Length - Path.GetFileName(pathTakeRoot).Length) // remove filename
                               .Trim('\\', '/');                                                                      // firm it up a little
                name = proj.Name;
            }

            return(pathTakeRoot);
        }
        /// <summary>
        /// This function generated the project corresponding to the passed pipelineComponent.
        /// </summary>
        /// <param name="destPath">The destination path of the projects.</param>
        /// <param name="pipelineComponent">Which component to generate the project for.</param>
        /// <returns>The project generated.</returns>
        private Project createAddInProject(string destPath, PipelineSegmentSource pipelineComponent, PipelineConfiguration config)
        {
            // dest path will be the root of the project where we have addins,
            // so we need to select the correct project for this specific segment, if there is
            // one and take its path, otherwise do destPath.Combine(pipelineComponent.Name);
            string name;

            destPath = destPath.Combine(getSubPath(config, pipelineComponent, destPath, out name));
            var generatedDestPath = destPath.Combine(GENERETED_FILES_DIRECTORY);

            // the getSubPath will use the configuration to get the correct project,
            // if one was selected, and otherwise will return the name of the pipeline segment
            // so that getProjByName will return null if it's not existing and otherwise the correct
            // and selected project.
            Project proj = getProjByName(name);

            if (proj == null)
            {
                if (Directory.Exists(destPath))
                {
                    if (DialogResult.Yes == MessageBox.Show(string.Format("The directory {0} already exists, " +
                                                                          " would you like to delete it and use it for the generated files for the segment named {1}?",
                                                                          destPath, name), "Already existing directory", MessageBoxButtons.YesNoCancel))
                    {
                        Directory.Delete(destPath, true);
                    }
                    else
                    {
                        return(null);
                    }
                }

                Directory.CreateDirectory(destPath);

                string connectPath = Path.GetDirectoryName(typeof(PipelineBuilderCommand).Assembly.Location);

                proj = dte.Solution.AddFromTemplate(connectPath.Combine("Template.csproj"),
                                                    destPath,
                                                    pipelineComponent.Name + ".csproj",
                                                    false);
            }

            if (proj == null)
            {
                throw new InvalidOperationException("Problem in adding from template. "
                                                    + "You installation files for the pipeline builder may be corrupted.");
            }

            if (Directory.Exists(generatedDestPath))
            {
                CheckSumValidator.ValidateCheckSum(generatedDestPath);
            }

            deleteGeneratedFiles(proj);
            assertHasDirectory(proj, generatedDestPath);
            generateSourceFiles(pipelineComponent, generatedDestPath);
            addGeneratedFiles(proj, generatedDestPath);

            CheckSumValidator.StoreCheckSum(generatedDestPath);

            return(proj);
        }