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));
                        }
                    }
                }
            }
        }
Esempio n. 2
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));
                        }
                    }
                }
            }
        }
        /// <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));
        }
Esempio n. 4
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));
        }