Ejemplo n.º 1
0
        VsTemplate.Template GetWizardData(XmlNode wizardData)
        {
            XmlReaderSettings settings = new XmlReaderSettings();

            settings.ValidationType   = ValidationType.Schema;
            settings.ConformanceLevel = ConformanceLevel.Auto;

            // Pull the XSD from the config assembly (it's an embedded resource).
            using (Stream xsdstream = typeof(VsTemplate.Template).Assembly.GetManifestResourceStream(
                       VsTemplate.Template.SchemaResourceName))
            {
                Debug.Assert(xsdstream != null, "XSD not embedded in config assembly");
                // If the schema is not valid (we must check that at design-time), this will throw.
                XmlSchema xsd = XmlSchema.Read(xsdstream, null);
                settings.Schemas.Add(xsd);
            }
            using (XmlReader xr = XmlReader.Create(new XmlNodeReader(wizardData), settings))
            {
                VsTemplate.Template template = (VsTemplate.Template) new VsTemplate.TemplateSerializer().Deserialize(xr);
                return(template);
            }
        }
Ejemplo n.º 2
0
        private void ReadTemplate()
        {
            try
            {
                #region Start reading Xml .vstemplate file
                XmlDocument templateDoc = new XmlDocument();
                templateDoc.Load(templateFileName);
                XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(templateDoc.NameTable);
                xmlNamespaceManager.AddNamespace("vstemplns", "http://schemas.microsoft.com/developer/vstemplate/2005");
                #endregion
                #region Set VsTemplate data
                XmlNodeList templateData = templateDoc.SelectNodes("/vstemplns:VSTemplate/vstemplns:TemplateData", xmlNamespaceManager);
                foreach (XmlNode xmlNode in templateData[0].ChildNodes)
                {
                    CommandID    valueAsCommandID = null;
                    string       valueAsString    = null;
                    XmlAttribute xmlPackage       = xmlNode.Attributes["Package"];
                    if (xmlPackage != null)
                    {
                        XmlAttribute xmlId       = xmlNode.Attributes["ID"];
                        Guid         guidPackage = new Guid(xmlPackage.Value);
                        if (xmlId != null)
                        {
                            valueAsCommandID = new CommandID(guidPackage, int.Parse(xmlId.Value));
                        }
                    }
                    else
                    {
                        valueAsString = xmlNode.InnerText;
                    }
                    switch (xmlNode.Name)
                    {
                    case "Name":
                        this.name          = valueAsString;
                        this.commandIdName = valueAsCommandID;
                        break;

                    case "Description":
                        this.description          = valueAsString;
                        this.commandIdDescription = valueAsCommandID;
                        break;

                    case "Icon":
                        this.icon = valueAsCommandID;
                        if (valueAsString != null)
                        {
                            this.iconFileName = valueAsString;
                        }
                        break;

                    case "CreateNewFolder":
                        this.createNewFolder = bool.Parse(valueAsString);
                        break;

                    case "AppendDefaultFileExtension":
                        this.appendDefaultFileExtension = bool.Parse(valueAsString);
                        break;

                    case "SupportsLanguageDropDown":
                        this.supportsLanguageDropDown = bool.Parse(valueAsString);
                        break;

                    case "SupportsMasterPage":
                        this.supportsMasterPage = bool.Parse(valueAsString);
                        break;

                    case "ProvideDefaultName":
                        this.provideDefaultName = bool.Parse(valueAsString);
                        break;

                    case "SortOrder":
                        this.sortPriority = int.Parse(valueAsString);
                        break;

                    case "ProjectType":
                        this.language = valueAsString;
                        break;

                    case "DefaultName":
                        this.suggestedBaseName = valueAsString;
                        break;

                    case "EnableLocationBrowseButton":
                        this.enableLocationBrowseButton = bool.Parse(valueAsString);
                        break;

                    case "EnableEditOfLocationField":
                        this.enableEditOfLocationField = bool.Parse(valueAsString);
                        break;

                    case "LocationField":
                        this.locationField = (LocationField)Enum.Parse(typeof(LocationField), valueAsString);
                        break;

                    default:
                        break;
                    }
                }
                #endregion
                #region Set vsKind
                XmlNode projectTypeNode = templateDoc.SelectSingleNode("/vstemplns:VSTemplate/@Type", xmlNamespaceManager);
                if (projectTypeNode == null)
                {
                    throw new RecipeFrameworkException(Properties.Resources.Templates_MissingType);
                }
                else if (string.Compare(projectTypeNode.InnerText, "Project", true, CultureInfo.InvariantCulture) == 0)
                {
                    this.vsKind = WizardRunKind.AsNewProject;
                }
                else if (string.Compare(projectTypeNode.InnerText, "Item", true, CultureInfo.InvariantCulture) == 0)
                {
                    this.vsKind = WizardRunKind.AsNewItem;
                }
                else if (string.Compare(projectTypeNode.InnerText, "ProjectGroup", true, CultureInfo.InvariantCulture) == 0)
                {
                    this.vsKind = WizardRunKind.AsMultiProject;
                }
                #endregion
                #region Set Kind
                string   folderName   = Path.GetDirectoryName(this.FileName);
                string[] folders      = folderName.Split('\\');
                string[] foldersArray = new string[2] {
                    folders[folders.Length - 1], folders[folders.Length - 2]
                };
                foreach (string folder in foldersArray)
                {
                    if (folder.Equals("Items", StringComparison.InvariantCultureIgnoreCase))
                    {
                        kind = TemplateKind.ProjectItem;
                        break;
                    }
                    else if (folder.Equals("Projects", StringComparison.InvariantCultureIgnoreCase))
                    {
                        kind = TemplateKind.Project;
                        if (folders[0].Equals("Solutions"))
                        {
                        }
                        break;
                    }
                    else if (folder.Equals("Solutions", StringComparison.InvariantCultureIgnoreCase))
                    {
                        kind = TemplateKind.Solution;
                        break;
                    }
                }
                #endregion
                #region Set extension data
                XmlNode wizardData = templateDoc.SelectSingleNode("/vstemplns:VSTemplate/vstemplns:WizardData", xmlNamespaceManager);
                if (wizardData == null)
                {
                    this.extensionData = new VsTemplate.Template();
                }
                else
                {
                    this.extensionData = this.GetWizardData(wizardData.ChildNodes[0]);
                }
                #endregion
                #region Make sure that the there is an custom UI specified:
                try
                {
                    XmlNode wizardUINode = templateDoc.SelectSingleNode(
                        "/vstemplns:VSTemplate/vstemplns:WizardExtension", xmlNamespaceManager);
                    XmlNode wizardAsmNode = wizardUINode.SelectSingleNode(
                        "vstemplns:Assembly", xmlNamespaceManager);
                    XmlNode wizardClassNode = wizardUINode.SelectSingleNode(
                        "vstemplns:FullClassName", xmlNamespaceManager);

                    string extensionFullName = wizardClassNode.InnerText + ", " + wizardAsmNode.InnerText;
                    Type   extensionType     = ReflectionHelper.LoadType(extensionFullName);
                    if (extensionType == null)
                    {
                        throw new TypeLoadException(extensionFullName);
                    }
                    if (!(typeof(UnfoldTemplate).IsAssignableFrom(extensionType)))
                    {
                        throw new ArgumentException(Properties.Resources.Templates_NoExtension);
                    }
                }
                catch
                {
                    throw new ArgumentException(Properties.Resources.Templates_NoExtension);
                }
                #endregion
                #region Additional Checks
                CheckTemplateKinds();
                CheckTemplateReferences();
                #endregion
            }
            catch (Exception e)
            {
                throw new RecipeFrameworkException(
                          String.Format(
                              CultureInfo.CurrentCulture,
                              Properties.Resources.Templates_InvalidTemplateFile,
                              templateFileName), e);
            }
        }