/// <summary>
        /// Creates the template data node.
        /// </summary>
        /// <returns></returns>
        internal XmlNode CreateTemplateDataNode()
        {
            const string TEMPLATEDATA_NODE      = "TemplateData";
            const string NAME_ATT               = "Name";
            const string DESCRIPTION_ATT        = "Description";
            const string PROJECTTYPE_ATT        = "ProjectType";
            const string SORTORDER_ATT          = "SortOrder";
            const string CREATENEWFOLDER_ATT    = "CreateNewFolder";
            const string DEFAULTNAME_ATT_NAME   = "DefaultName";
            const string PROVIDEDEFAULTNAME_ATT = "ProvideDefaultName";
            const string ICON_ATT               = "Icon";

            XmlNode templateDataNode = vsTemplate.CreateElement(TEMPLATEDATA_NODE);

            templateDataNode.AppendChild(VSTemplateExporterHelper.CreateNode(vsTemplate, NAME_ATT, this.Name));
            templateDataNode.AppendChild(VSTemplateExporterHelper.CreateNode(vsTemplate, DESCRIPTION_ATT, this.Description));
            templateDataNode.AppendChild(VSTemplateExporterHelper.CreateNode(vsTemplate, PROJECTTYPE_ATT, this.ProjectType.ToString()));
            templateDataNode.AppendChild(VSTemplateExporterHelper.CreateNode(vsTemplate, SORTORDER_ATT, this.SortOrder.ToString(NumberFormatInfo.InvariantInfo)));
            templateDataNode.AppendChild(VSTemplateExporterHelper.CreateNode(vsTemplate, ICON_ATT, this.Icon));
            templateDataNode.AppendChild(VSTemplateExporterHelper.CreateNode(vsTemplate, CREATENEWFOLDER_ATT, this.CreateNewFolder.ToString().ToLowerInvariant()));
            templateDataNode.AppendChild(VSTemplateExporterHelper.CreateNode(vsTemplate, DEFAULTNAME_ATT_NAME, this.DefaultName));
            templateDataNode.AppendChild(VSTemplateExporterHelper.CreateNode(vsTemplate, PROVIDEDEFAULTNAME_ATT, this.ProvideDefaultName.ToString().ToLowerInvariant()));

            return(templateDataNode);
        }
        /// <summary>
        /// Creates the template content node.
        /// </summary>
        /// <returns></returns>
        internal XmlNode CreateTemplateContentNode()
        {
            const string TEMPLATECONTENT_NODE = "TemplateContent";

            XmlNode templateContent = VSTemplateExporterHelper.CreateNode(vsTemplate, TEMPLATECONTENT_NODE);

            return(templateContent);
        }
        /// <summary>
        /// Creates the projec collectiont node.
        /// </summary>
        /// <returns></returns>
        internal XmlNode CreateProjecCollectiontNode()
        {
            const string PROJECTCOLLECTION_NODE = "ProjectCollection";

            XmlNode projectCollection = VSTemplateExporterHelper.CreateNode(vsTemplate, PROJECTCOLLECTION_NODE);

            return(projectCollection);
        }
        /// <summary>
        /// Creates the project template link node.
        /// </summary>
        /// <param name="projectName">Name of the project.</param>
        /// <param name="relativeTemplatePath">The relative template path.</param>
        /// <returns></returns>
        internal XmlNode CreateProjectTemplateLinkNode(string projectName, string relativeTemplatePath)
        {
            const string PROJECTTEMPLATELINK_NODE = "ProjectTemplateLink";
            const string PROJECTNAME_ATT          = "ProjectName";

            XmlNode projectTemplateLinkNode = VSTemplateExporterHelper.CreateNode(vsTemplate, PROJECTTEMPLATELINK_NODE, relativeTemplatePath);

            projectTemplateLinkNode.Attributes.Append(VSTemplateExporterHelper.CreateAttribute(vsTemplate, PROJECTNAME_ATT, projectName));

            return(projectTemplateLinkNode);
        }
        /// <summary>
        /// Creates the solution folder node.
        /// </summary>
        /// <param name="solutionFolderName">Name of the solution folder.</param>
        /// <returns></returns>
        internal XmlNode CreateSolutionFolderNode(string solutionFolderName)
        {
            const string SOLUTIONFOLDER_NODE = "SolutionFolder";
            const string NAME_ATT            = "Name";

            XmlNode solutionFolderNode = VSTemplateExporterHelper.CreateNode(vsTemplate, SOLUTIONFOLDER_NODE);

            solutionFolderNode.Attributes.Append(VSTemplateExporterHelper.CreateAttribute(vsTemplate, NAME_ATT, solutionFolderName));

            return(solutionFolderNode);
        }
        /// <summary>
        /// Creates the project item node.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="replaceParameters">if set to <c>true</c> [replace parameters].</param>
        /// <returns></returns>
        internal XmlNode CreateProjectItemNode(string item, bool replaceParameters)
        {
            const string PROJECTITEM_NODE           = "ProjectItem";
            const string REPLACEPARAMETERS_ATT_NAME = "ReplaceParameters";
            const string TARGETFILENAME_ATT         = "TargetFileName";

            XmlNode projectItemNode = VSTemplateExporterHelper.CreateNode(vsTemplate, PROJECTITEM_NODE, item);

            projectItemNode.Attributes.Append(VSTemplateExporterHelper.CreateAttribute(vsTemplate, REPLACEPARAMETERS_ATT_NAME, replaceParameters.ToString().ToLowerInvariant()));
            projectItemNode.Attributes.Append(VSTemplateExporterHelper.CreateAttribute(vsTemplate, TARGETFILENAME_ATT, item));
            return(projectItemNode);
        }
        /// <summary>
        /// Creates the folder node.
        /// </summary>
        /// <param name="folderName">Name of the folder.</param>
        /// <returns></returns>
        internal XmlNode CreateFolderNode(string folderName)
        {
            const string FOLDER_NODE          = "Folder";
            const string NAME_ATT             = "Name";
            const string TARGETFOLDERNAME_ATT = "TargetFolderName";

            XmlNode folderNode = VSTemplateExporterHelper.CreateNode(vsTemplate, FOLDER_NODE);

            folderNode.Attributes.Append(VSTemplateExporterHelper.CreateAttribute(vsTemplate, NAME_ATT, folderName));
            folderNode.Attributes.Append(VSTemplateExporterHelper.CreateAttribute(vsTemplate, TARGETFOLDERNAME_ATT, folderName));
            return(folderNode);
        }
        /// <summary>
        /// Adds the project to template.
        /// </summary>
        /// <param name="project">The project.</param>
        /// <param name="vsTemplate">The vs template.</param>
        /// <param name="contextNode">The context node.</param>
        private void AddAssetsToTemplate(Project project, VSSolutionTemplate vsTemplate, XmlNode contextNode)
        {
            if (project.Object is SolutionFolder)
            {
                //Add SolutionFolder node
                SolutionFolder solutionFolder     = project.Object as SolutionFolder;
                XmlNode        solutionFolderNode = vsTemplate.CreateSolutionFolderNode(solutionFolder.Parent.Name);
                contextNode.AppendChild(solutionFolderNode);

                foreach (ProjectItem item in project.ProjectItems)
                {
                    if (item.Object != null &&
                        (item.Object is Project ||
                         ((Project)item.Object).Object is SolutionFolder))
                    {
                        AddAssetsToTemplate((Project)item.Object, vsTemplate, solutionFolderNode);
                    }
                }
            }
            else
            {
                string projectName;

                if (project.Object is VSWebSite)
                {
                    projectName = VSTemplateExporterHelper.GetProjectName(project.UniqueName);
                }
                else
                {
                    projectName = project.Name;
                }

                string relativeTemplatePath = Path.Combine
                                              (
                    Path.Combine(this.ProjectsDirectory, projectName),
                    string.Concat(projectName, VSTemplateExporterHelper.TEMPLATE_EXTENSION)
                                              );

                XmlNode projectTemplateLinkNode = vsTemplate.CreateProjectTemplateLinkNode(projectName, relativeTemplatePath);

                contextNode.AppendChild(projectTemplateLinkNode);

                //Create project template
                VSProjectTemplateExporter webProjectExporter = new VSProjectTemplateExporter(
                    project,
                    this.projectsTemplateIconFile,
                    outputProjectsDirectory);

                webProjectExporter.Export();
            }
        }
        /// <summary>
        /// Exports this instance.
        /// </summary>
        public void Export()
        {
            string solutionName = this.Solution.Properties.Item("Name").Value.ToString();

            //Clean or create output directory
            VSTemplateExporterHelper.CreateDirectoryIfNotExists(this.OutputDirectory);

            //Copy solution template icon file
            string outputSolutionTemplateIconFile = Path.Combine(
                this.OutputDirectory,
                Path.GetFileName(this.solutionTemplateIconFile));

            // Check if we need to copy to output or not
            if (!this.solutionTemplateIconFile.Equals(outputSolutionTemplateIconFile,
                                                      StringComparison.InvariantCultureIgnoreCase))
            {
                File.Copy(this.solutionTemplateIconFile, outputSolutionTemplateIconFile, true);
            }

            VSSolutionTemplate vsTemplate = new VSSolutionTemplate();

            vsTemplate.Name        = solutionName;
            vsTemplate.Description = solutionName;
            vsTemplate.DefaultName = solutionName;
            vsTemplate.Icon        = Path.GetFileName(this.SolutionTemplateIconFile);

            XmlNode rootNode              = vsTemplate.CreateRootNode();
            XmlNode templateContentNode   = vsTemplate.CreateTemplateContentNode();
            XmlNode projectCollectionNode = vsTemplate.CreateProjecCollectiontNode();

            foreach (Project project in this.Solution.Projects)
            {
                //Add ProjectTemplateLink and SolutionFolder nodes
                //Create project templates
                AddAssetsToTemplate(project, vsTemplate, projectCollectionNode);
            }

            rootNode.AppendChild(vsTemplate.CreateTemplateDataNode());
            templateContentNode.AppendChild(projectCollectionNode);
            rootNode.AppendChild(templateContentNode);
            rootNode.AppendChild(vsTemplate.CreateWizardExtensionNode());
            rootNode.AppendChild(vsTemplate.CreateWizardDataNode());

            //Save solution template
            vsTemplate.Save(
                this.OutputDirectory,
                string.Concat(solutionName, VSTemplateExporterHelper.TEMPLATE_EXTENSION));
        }
Exemplo n.º 10
0
        /// <summary>
        /// Creates the project node.
        /// </summary>
        /// <returns></returns>
        internal XmlNode CreateProjectNode(string projectFileName)
        {
            const string PROJECT_NODE                = "Project";
            const string TARGETFILENAME_ATT          = "TargetFileName";
            const string FILE_ATT                    = "File";
            const string REPLACEPARAMETERS_ATT_NAME  = "ReplaceParameters";
            const string REPLACEPARAMETERS_ATT_VALUE = "true";

            XmlNode projectContent = VSTemplateExporterHelper.CreateNode(vsTemplate, PROJECT_NODE);

            projectContent.Attributes.Append(VSTemplateExporterHelper.CreateAttribute(vsTemplate, TARGETFILENAME_ATT, projectFileName));
            projectContent.Attributes.Append(VSTemplateExporterHelper.CreateAttribute(vsTemplate, FILE_ATT, projectFileName));
            projectContent.Attributes.Append(VSTemplateExporterHelper.CreateAttribute(vsTemplate, REPLACEPARAMETERS_ATT_NAME, REPLACEPARAMETERS_ATT_VALUE));

            return(projectContent);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Creates the root node.
        /// </summary>
        /// <returns></returns>
        internal XmlNode CreateRootNode()
        {
            const string VSTEMPLATE_NODE   = "VSTemplate";
            const string VSTEMPLATE_NS     = "http://schemas.microsoft.com/developer/vstemplate/2005";
            const string VERSION_ATT_NAME  = "Version";
            const string VERSION_ATT_VALUE = "2.0.0";
            const string TYPE_ATT_NAME     = "Type";
            const string TYPE_ATT_VALUE    = "Project";

            XmlNode root = vsTemplate.CreateElement(VSTEMPLATE_NODE, VSTEMPLATE_NS);

            root.Attributes.Append(VSTemplateExporterHelper.CreateAttribute(vsTemplate, VERSION_ATT_NAME, VERSION_ATT_VALUE));
            root.Attributes.Append(VSTemplateExporterHelper.CreateAttribute(vsTemplate, TYPE_ATT_NAME, TYPE_ATT_VALUE));
            vsTemplate.AppendChild(root);
            return(root);
        }
Exemplo n.º 12
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:VSWebProjectTemplateExporter"/> class.
        /// </summary>
        /// <param name="project">The project.</param>
        /// <param name="projectTemplateIconFile">The project template icon file.</param>
        /// <param name="outputDirectory">The output directory.</param>
        public VSProjectTemplateExporter(Project project, string projectTemplateIconFile, string outputDirectory)
        {
            this.Project = project;
            this.projectTemplateIconFile = projectTemplateIconFile;
            this.outputDirectory         = outputDirectory;
            isWebProject = (project.Object is VSWebSite);

            if (isWebProject)
            {
                this.projectName     = VSTemplateExporterHelper.GetProjectName(project.UniqueName);
                this.projectFileName = Path.Combine(project.FullName, this.projectName + WebProjectExtension);
            }
            else
            {
                this.projectName     = project.Name;
                this.projectFileName = project.FullName;
            }
        }
Exemplo n.º 13
0
        /// <summary>
        /// Creates the template data node.
        /// </summary>
        /// <returns></returns>
        internal XmlNode CreateTemplateDataNode()
        {
            const string TEMPLATEDATA_NODE              = "TemplateData";
            const string NAME_ATT                       = "Name";
            const string DESCRIPTION_ATT                = "Description";
            const string PROJECTTYPE_ATT                = "ProjectType";
            const string PROJECTSUBTYPE_ATT             = "ProjectSubType";
            const string SORTORDER_ATT                  = "SortOrder";
            const string CREATENEWFOLDER_ATT            = "CreateNewFolder";
            const string DEFAULTNAME_ATT_NAME           = "DefaultName";
            const string PROVIDEDEFAULTNAME_ATT         = "ProvideDefaultName";
            const string LOCATIONFIELD_ATT_NAME         = "LocationField";
            const string ENABLELOCATIONBROWSEBUTTON_ATT = "EnableLocationBrowseButton";
            const string ICON_ATT                       = "Icon";

            XmlNode templateDataNode = vsTemplate.CreateElement(TEMPLATEDATA_NODE, string.Empty);

            templateDataNode.AppendChild(VSTemplateExporterHelper.CreateNode(vsTemplate, NAME_ATT, this.Name));
            templateDataNode.AppendChild(VSTemplateExporterHelper.CreateNode(vsTemplate, DESCRIPTION_ATT, this.Description));
            templateDataNode.AppendChild(VSTemplateExporterHelper.CreateNode(vsTemplate, PROJECTTYPE_ATT, this.ProjectType.ToString()));

            if (this.projectSubType != ProjectType.None)
            {
                templateDataNode.AppendChild(VSTemplateExporterHelper.CreateNode(vsTemplate, PROJECTSUBTYPE_ATT, this.projectSubType.ToString()));
            }

            templateDataNode.AppendChild(VSTemplateExporterHelper.CreateNode(vsTemplate, SORTORDER_ATT, this.SortOrder.ToString(NumberFormatInfo.InvariantInfo)));
            templateDataNode.AppendChild(VSTemplateExporterHelper.CreateNode(vsTemplate, CREATENEWFOLDER_ATT, this.CreateNewFolder.ToString(CultureInfo.InvariantCulture).ToLowerInvariant()));
            templateDataNode.AppendChild(VSTemplateExporterHelper.CreateNode(vsTemplate, DEFAULTNAME_ATT_NAME, this.DefaultName));
            templateDataNode.AppendChild(VSTemplateExporterHelper.CreateNode(vsTemplate, PROVIDEDEFAULTNAME_ATT, this.ProvideDefaultName.ToString(CultureInfo.InvariantCulture).ToLowerInvariant()));
            templateDataNode.AppendChild(VSTemplateExporterHelper.CreateNode(vsTemplate, LOCATIONFIELD_ATT_NAME, this.LocationField.ToString()));
            templateDataNode.AppendChild(VSTemplateExporterHelper.CreateNode(vsTemplate, ENABLELOCATIONBROWSEBUTTON_ATT, this.EnableLocationBrowseButton.ToString(CultureInfo.InvariantCulture).ToLowerInvariant()));
            templateDataNode.AppendChild(VSTemplateExporterHelper.CreateNode(vsTemplate, ICON_ATT, this.Icon));

            return(templateDataNode);
        }
Exemplo n.º 14
0
 /// <summary>
 /// Creates the wizard data node.
 /// </summary>
 /// <returns></returns>
 internal XmlNode CreateWizardDataNode()
 {
     return(VSTemplateExporterHelper.CreateWizardDataNode(vsTemplate));
 }
Exemplo n.º 15
0
        private void AddAssetsToTemplate(ProjectItems projectItems, VSProjectTemplate vsTemplate, XmlNode contextNode, string sourceBaseDir, string destinationBaseDir)
        {
            if (projectItems == null)
            {
                return;
            }

            foreach (ProjectItem item in projectItems)
            {
                bool isFolder = true;
                // if we don't find the ExtensionAttribute property or the value is null, then this is a folder
                foreach (Property property in item.Properties)
                {
                    if (string.Compare(property.Name, ExtensionAttribute, StringComparison.InvariantCultureIgnoreCase) == 0)
                    {
                        isFolder = (property.Value == null);
                        break;
                    }
                }

                if (isFolder)
                {
                    if (item.Name != InternalFolderName)
                    {
                        string sourcePath = Path.Combine(sourceBaseDir, item.Name);
                        string destPath   = Path.Combine(destinationBaseDir, item.Name);

                        //Create Folder
                        VSTemplateExporterHelper.CreateDirectoryIfNotExists(destPath);

                        //Add Folder node
                        XmlNode folderNode = vsTemplate.CreateFolderNode(item.Name);
                        contextNode.AppendChild(folderNode);

                        AddAssetsToTemplate(
                            item.ProjectItems,
                            vsTemplate,
                            folderNode,
                            sourcePath,
                            destPath);
                    }
                }
                else
                {
                    if (!item.Name.EndsWith(WebProjectExtension, StringComparison.InvariantCultureIgnoreCase))
                    {
                        string sourcePath = Path.Combine(sourceBaseDir, item.Name);
                        string destPath   = Path.Combine(destinationBaseDir, item.Name);

                        //Add ProjectItem node

                        XmlNode itemNode =
                            vsTemplate.CreateProjectItemNode(
                                item.Name,
                                ReplaceableExtension.IsReplaceable(sourcePath));

                        contextNode.AppendChild(itemNode);

                        if (isWebProject)
                        {
                            File.Copy(sourcePath, destPath, true);
                            //TODO: Verify if we need to Replace project variables
                        }
                        else
                        {
                            if (item.FileCodeModel != null)
                            {
                                string fileContent =
                                    VSTemplateExporterHelper.ReplaceVariables(
                                        sourcePath,
                                        this.Project.Properties.Item("DefaultNamespace").Value.ToString());

                                File.WriteAllText(destPath, fileContent);
                            }
                            else
                            {
                                if (File.Exists(sourcePath))
                                {
                                    File.Copy(sourcePath, destPath, true);
                                }
                                else if (Directory.Exists(sourcePath))
                                {
                                    // Copy all files in this folder
                                    foreach (string file in Directory.GetFiles(sourcePath, "*.*", SearchOption.TopDirectoryOnly))
                                    {
                                        File.Copy(file, Path.Combine(destPath, Path.GetFileName(file)), true);
                                    }
                                }
                            }
                        }

                        //Web project items doesn't have subitems
                        if (!isWebProject)
                        {
                            AddAssetsToTemplate(item.ProjectItems,
                                                vsTemplate,
                                                contextNode,
                                                Path.GetDirectoryName(sourcePath),
                                                Path.GetDirectoryName(destPath));
                        }
                    }
                }
            }
        }
Exemplo n.º 16
0
        /// <summary>
        /// Exports this instance.
        /// </summary>
        public void Export()
        {
            string sourceBaseDir      = Path.GetDirectoryName(this.projectFileName);
            string destinationBaseDir = Path.Combine(this.OutputDirectory, this.projectName);

            VSTemplateExporterHelper.CreateDirectoryIfNotExists(destinationBaseDir);

            if (isWebProject)
            {
                //Create csproj file because it doesnt exists in WebProjects
                File.Create(Path.Combine(destinationBaseDir, Path.GetFileName(this.projectFileName)));
            }
            else
            {
                CopyProjectFile(this.projectFileName, destinationBaseDir);
            }

            //Copy project template icon file
            File.Copy(
                this.projectTemplateIconFile,
                Path.Combine(
                    destinationBaseDir,
                    Path.GetFileName(this.projectTemplateIconFile)),
                true);

            VSProjectTemplate vsTemplate = new VSProjectTemplate();

            vsTemplate.Name        = this.projectName;
            vsTemplate.Description = this.projectName;

            if (isWebProject)
            {
                vsTemplate.ProjectType    = ProjectType.Web;
                vsTemplate.ProjectSubType = ProjectType.CSharp;
            }
            else
            {
                vsTemplate.ProjectType    = ProjectType.CSharp;
                vsTemplate.ProjectSubType = ProjectType.None;
            }

            vsTemplate.DefaultName = this.projectName;
            vsTemplate.Icon        = Path.GetFileName(this.ProjectTemplateIconFile);

            XmlNode rootNode            = vsTemplate.CreateRootNode();
            XmlNode templateContentNode = vsTemplate.CreateTemplateContentNode();
            XmlNode projectNode         = vsTemplate.CreateProjectNode(Path.GetFileName(this.projectFileName));

            //Add ProjectItem and ProjectItem nodes
            AddAssetsToTemplate(this.Project.ProjectItems, vsTemplate, projectNode, sourceBaseDir, destinationBaseDir);

            rootNode.AppendChild(vsTemplate.CreateTemplateDataNode());
            templateContentNode.AppendChild(projectNode);
            rootNode.AppendChild(templateContentNode);
            rootNode.AppendChild(vsTemplate.CreateWizardExtensionNode());
            rootNode.AppendChild(vsTemplate.CreateWizardDataNode());

            vsTemplate.Save(
                destinationBaseDir,
                string.Concat(
                    this.projectName,
                    VSTemplateExporterHelper.TEMPLATE_EXTENSION));
        }