Beispiel #1
0
        public override void Execute()
        {
            if (ExcludeCondition)
            {
                return;
            }
            if (!AdditionalCondition)
            {
                return;
            }

            DTE dte = (DTE)this.GetService(typeof(DTE));

            //1. get correct parameters ("$(FeatureName)" as "FeatureX")
            string evaluatedTargetFileName   = EvaluateParameterAsString(dte, TargetFileName);
            string evaluatedTemplateFileName = EvaluateParameterAsString(dte, TemplateFileName);
            string contents = GenerateContent(evaluatedTemplateFileName, evaluatedTargetFileName);

            if (Helpers2.TemplateContentIsEmpty(contents))
            {
                return;
            }

            try
            {
                //2. save the file to a temporary folder
                SourceFileName = Path.GetTempFileName();
                WriteContentToTempFile(SourceFileName, contents, evaluatedTargetFileName);

                //now run parent action which adds the file to the project
                base.Execute();

                //now we can delete the temp file
                File.Delete(SourceFileName);
            }
            catch (Exception ex)
            {
                Helpers.LogMessage(dte, dte, ex.ToString());
                throw ex;
            }
        }
Beispiel #2
0
        public override void Execute()
        {
            if (ExcludeCondition)
            {
                return;
            }
            if (!AdditionalCondition)
            {
                return;
            }

            DTE     dte     = (DTE)this.GetService(typeof(DTE));
            Project project = this.GetTargetProject(dte);

            string evaluatedTemplateFileName = EvaluateParameterAsString(dte, TemplateFileName);


            string templateContent = GenerateContent(evaluatedTemplateFileName, "Resources.resx");

            if (Helpers2.TemplateContentIsEmpty(templateContent))
            {
                //do nothing if no content has been generated
                return;
            }

            //get the project where we pla
            Project globalresproject = Helpers.GetResourcesProject(dte);
            Project selectedproject  = Helpers.GetSelectedProject(dte);

            string resourcesFilename = "";

            if (FeatureResources)
            {
                //for feature resources name is "Resources.resx"
                resourcesFilename = "Resources.resx";
            }
            else if (ApplicationResources)
            {
                resourcesFilename = Helpers.GetOutputNameWithoutExtensions(selectedproject) + ".AppResources.resx";
            }
            else
            {
                //global resources get the name of the current project project
                resourcesFilename = Helpers.GetOutputNameWithoutExtensions(selectedproject) + ".resx";
            }

            //1. find a resourcefile
            if (ApplicationResources)
            {
                if (globalresproject != null)
                {
                    //global resources project -> use the file 12/resources
                    AddApplicationResources(globalresproject, resourcesFilename, templateContent);
                }
                else
                {
                    AddApplicationResources(selectedproject, resourcesFilename, templateContent);
                }
            }
            else if (FeatureResources)
            {
                string evaluatedFeatureName = EvaluateParameterAsString(dte, FeatureName);

                //Find the feature folder

                ProjectItem folderOfFeature = Helpers2.GetFolderOfFeature(project, evaluatedFeatureName);
                if (folderOfFeature != null)
                {
                    if (Helpers2.IsSharePointVSTemplate(dte, project))
                    {
                        //put resources.resx directly into under the feature
                        AddResourcesToFolder(folderOfFeature, resourcesFilename, templateContent);
                    }
                    else
                    {
                        //now add a folder "Resources" in the feature and put the "Resource.<culture>.resx" there
                        ProjectItems whereToAdd = Helpers.GetProjectItemsByPath(folderOfFeature.ProjectItems, "Resources");
                        AddResourcesToFolder(whereToAdd.Parent as ProjectItem, resourcesFilename, templateContent);
                    }
                }
            }
            else if (AdminResources)
            {
                //put the content into
                if (globalresproject != null)
                {
                    //global resources project -> use the file 12/resources
                    AddAdminResources(globalresproject, resourcesFilename, templateContent);
                }
                else
                {
                    AddAdminResources(selectedproject, resourcesFilename, templateContent);
                }
            }
            else
            {
                //put the content into
                if (globalresproject != null)
                {
                    //global resources project -> use the file 12/resources
                    AddGlobalResources(globalresproject, resourcesFilename, templateContent);
                }
                else
                {
                    AddGlobalResources(selectedproject, resourcesFilename, templateContent);
                }
            }
        }
Beispiel #3
0
        public override void Execute()
        {
            if (ExcludeCondition)
            {
                return;
            }
            if (!AdditionalCondition)
            {
                return;
            }

            DTE     dte     = (DTE)this.GetService(typeof(DTE));
            Project project = this.GetTargetProject(dte);

            //1. get correct parameters ("$(FeatureName)" as "FeatureX")

            string targetFilename            = Path.GetTempFileName();
            string evaluatedTemplateFileName = EvaluateParameterAsString(dte, TemplateFileName);
            string Content = GenerateContent(evaluatedTemplateFileName, targetFilename);

            if (Helpers2.TemplateContentIsEmpty(Content))
            {
                return;
            }

            try
            {
                //loading the generated xml content
                string codeToBeAdded = Content;

                //find the feature receiver code
                ProjectItem featureXMLFile = Helpers.GetFeatureXML(project, ParentFeatureName);

                if (featureXMLFile == null)
                {
                    throw new Exception("Feature with name " + ParentFeatureName + " not found");
                }
                string path = Helpers.GetFullPathOfProjectItem(featureXMLFile); //Helpers.GetFullPathOfProjectItem(featureXMLFile);

                XmlDocument doc = new XmlDocument();
                doc.Load(path);
                XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
                nsmgr.AddNamespace("ns", "http://schemas.microsoft.com/sharepoint/");

                XmlNode featureNode = doc.SelectSingleNode("/ns:Feature", nsmgr);
                if (featureNode == null)
                {
                    throw new Exception("XmlNode 'Feature' not found in file " + path);
                }

                //is there already a propertiesNode
                XmlNode propertiesNode = doc.SelectSingleNode("/ns:Feature/ns:Properties", nsmgr);
                if (propertiesNode == null)
                {
                    propertiesNode = doc.CreateNode(XmlNodeType.Element, "Properties", "http://schemas.microsoft.com/sharepoint/");
                    featureNode.AppendChild(propertiesNode);
                }

                //now add the new properties
                XmlDocument newPropertiesdoc = new XmlDocument();
                newPropertiesdoc.LoadXml(Content);
                XmlNodeList newPropertiesNodes = newPropertiesdoc.SelectNodes("/Properties/Property");
                foreach (XmlNode newPropNode in newPropertiesNodes)
                {
                    XmlNode copiedNode = doc.CreateNode(XmlNodeType.Element, "Property", "http://schemas.microsoft.com/sharepoint/");
                    copiedNode.Attributes.Append(doc.CreateAttribute("Key")).Value   = newPropNode.Attributes["Key"].Value;
                    copiedNode.Attributes.Append(doc.CreateAttribute("Value")).Value = newPropNode.Attributes["Value"].Value;
                    propertiesNode.AppendChild(copiedNode);
                }

                //save the feature.xml after the changes
                Helpers.EnsureCheckout(dte, path);

                XmlWriter xw = XmlWriter.Create(path, Helpers.GetXmlWriterSettings(path));
                doc.Save(xw);
                xw.Flush();
                xw.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        public override void Execute()
        {
            if (ExcludeCondition)
            {
                return;
            }
            if (!AdditionalCondition)
            {
                return;
            }

            DTE     dte     = (DTE)this.GetService(typeof(DTE));
            Project project = this.GetTargetProject(dte);

            string evaluatedTemplateFileName = EvaluateParameterAsString(dte, TemplateFileName);
            string templateContent           = GenerateContent(evaluatedTemplateFileName, "CASPolicy.txt");

            if (Helpers2.TemplateContentIsEmpty(templateContent))
            {
                //do nothing if no content has been generated
                return;
            }

            if (Helpers2.IsSharePointVSTemplate(dte, project))
            {
                Helpers.LogMessage(dte, this, "Adding CAS Policy to file 'Package/Package.Template.xml");

                //merge the contents with the cas policy in the package
                //find the package/package.Template.xml
                try
                {
                    ProjectItem packageItem = Helpers.GetProjectItemByName(Helpers.GetProjectItemByName(project.ProjectItems, "Package").ProjectItems, "Package.Template.xml");
                    if (packageItem == null)
                    {
                        packageItem = Helpers.GetProjectItemByName(Helpers.GetProjectItemByName(Helpers.GetProjectItemByName(project.ProjectItems, "Package").ProjectItems, "Package.package").ProjectItems, "Package.Template.xml");
                    }

                    if (packageItem != null)
                    {
                        Helpers.EnsureCheckout(dte, packageItem);

                        string      packageFilepath = Helpers.GetFullPathOfProjectItem(packageItem);
                        XmlDocument packageXml      = new XmlDocument();
                        packageXml.Load(packageFilepath);

                        XmlNodeList permissionSetNodes = null;

                        //1. if there is no node Solution/CodeAccessSecurity then we add a new one automatically
                        XmlNamespaceManager featurensmgr = new XmlNamespaceManager(packageXml.NameTable);
                        featurensmgr.AddNamespace("ns", "http://schemas.microsoft.com/sharepoint/");

                        XmlNodeList allCodeAccessSecurityNodes = packageXml.SelectNodes("ns:Solution/ns:CodeAccessSecurity", featurensmgr);
                        if (allCodeAccessSecurityNodes.Count == 0)
                        {
                            //add default cas policy element
                            string      templateFilepath = Path.Combine(GetTemplateBasePath(), "Text\\CodeAccessSecurity.xml");
                            XmlDocument resdoc           = new XmlDocument();
                            resdoc.Load(templateFilepath);

                            //select the solution node
                            XmlNode solutionNode = packageXml.SelectSingleNode("ns:Solution", featurensmgr);

                            XmlDocumentFragment docFrag = packageXml.CreateDocumentFragment();
                            docFrag.InnerXml = "<dummy xmlns='http://schemas.microsoft.com/sharepoint/'>" + resdoc.OuterXml + "</dummy>";
                            solutionNode.AppendChild(docFrag.FirstChild.FirstChild);

                            //CleanNamespace(solutionNode);

                            //select the nodes again
                            allCodeAccessSecurityNodes = packageXml.SelectNodes("ns:Solution/ns:CodeAccessSecurity", featurensmgr);
                        }

                        permissionSetNodes = packageXml.SelectNodes("ns:Solution/ns:CodeAccessSecurity/ns:PolicyItem/ns:PermissionSet", featurensmgr);

                        //now append the IPolicies to all existing PermissionSets
                        XmlDocument generatedCASPolicy = new XmlDocument();
                        generatedCASPolicy.LoadXml(templateContent);

                        XmlNodeList generatedIPermissions = generatedCASPolicy.SelectNodes("PermissionSet/IPermission");

                        //remove duplicate class
                        foreach (XmlNode permissionSetNode in permissionSetNodes)
                        {
                            foreach (XmlNode generatedIPermission in generatedIPermissions)
                            {
                                //compare new IPermission with existing permission
                                RemoveDuplicateClass(permissionSetNode, generatedIPermission);
                            }
                            //CleanNamespace(permissionSetNode);
                        }

                        foreach (XmlNode permissionSetNode in permissionSetNodes)
                        {
                            foreach (XmlNode generatedIPermission in generatedIPermissions)
                            {
                                XmlDocumentFragment docFrag = packageXml.CreateDocumentFragment();
                                docFrag.InnerXml = "<dummy xmlns='http://schemas.microsoft.com/sharepoint/'>" + generatedIPermission.OuterXml + "</dummy>";
                                permissionSetNode.AppendChild(docFrag.FirstChild.FirstChild);
                            }
                        }

                        XmlWriter xw2 = XmlWriter.Create(packageFilepath, Helpers.GetXmlWriterSettingsAttributesInOneLine());
                        packageXml.Save(xw2);
                        xw2.Flush();
                        xw2.Close();

                        Window window = packageItem.Open("{00000000-0000-0000-0000-000000000000}");
                        window.Visible = true;
                        window.Activate();
                    }
                }
                catch
                {
                    Helpers.LogMessage(dte, this, "Could not add CAS policy to package");
                }
            }
            else
            {
                //add the file directly into the root
                this.TargetFileName = "CASPolicy.txt";
                base.Execute();
            }
        }
        public override void Execute()
        {
            DTE     dte     = (DTE)this.GetService(typeof(DTE));
            Project project = this.GetTargetProject(dte);

            //1. transformiere Template
            string evaluatedTemplateFileName = EvaluateParameterAsString(dte, TemplateFileName);
            string contents = GenerateContent(evaluatedTemplateFileName, "");

            if (Helpers2.TemplateContentIsEmpty(contents))
            {
                return;
            }
            string evaluatedXPath = EvaluateParameterAsString(dte, XPath);

            //2. Finde das ProjectItem wo der XPath enthalten ist
            ProjectItem xmlElementManifestFile = null;

            if (SelectedItem != null)
            {
                if (SelectedItem.Kind == EnvDTE.Constants.vsProjectItemKindPhysicalFile)
                {
                    //is the xml file clicked?
                    try
                    {
                        if (Helpers2.IsXPathInFile(SelectedItem, evaluatedXPath, XPathNamespace))
                        {
                            xmlElementManifestFile = SelectedItem;
                        }
                    }
                    catch { }
                }
                else
                {
                    //folder is clicked, we search for xml files below the folder which contains the needed xpath
                    foreach (ProjectItem childItem in SelectedItem.ProjectItems)
                    {
                        if (Helpers2.IsXPathInFile(childItem, evaluatedXPath, XPathNamespace))
                        {
                            xmlElementManifestFile = childItem;
                        }
                        foreach (ProjectItem subchildItem in childItem.ProjectItems)
                        {
                            if (Helpers2.IsXPathInFile(subchildItem, evaluatedXPath, XPathNamespace))
                            {
                                xmlElementManifestFile = subchildItem;
                            }
                        }
                    }
                }

                if (xmlElementManifestFile != null)
                {
                    Helpers.EnsureCheckout(dte, xmlElementManifestFile);

                    //settings output parameters
                    CreatedElementFolder = xmlElementManifestFile.Collection.Parent as ProjectItem;
                    CreatedElementFile   = xmlElementManifestFile;

                    string pathToElementsXmlFile = Helpers.GetFullPathOfProjectItem(xmlElementManifestFile);

                    //bestehendes Dokument laden
                    XmlDocument existingDoc = new XmlDocument();
                    existingDoc.Load(pathToElementsXmlFile);

                    //backup for undo action
                    previousXmlContent = existingDoc.OuterXml;
                    previousXmlPath    = pathToElementsXmlFile;

                    //neues Xml laden
                    XmlDocument newdoc = new XmlDocument();
                    newdoc.LoadXml(contents);

                    if (string.IsNullOrEmpty(evaluatedXPath))
                    {
                        //Fall 1. kein xpath angegeben, documente werden gemerged
                        XmlDocument d   = AddXmlToDoc(existingDoc, newdoc);
                        string      xml = d.InnerXml;

                        XmlWriter xw2 = XmlWriter.Create(previousXmlPath, Helpers.GetXmlWriterSettings(previousXmlPath));
                        existingDoc.Save(xw2);
                        xw2.Flush();
                        xw2.Close();
                    }
                    else
                    {
                        //Fall 2. ein xpath ist angegeben für die Node, an die etwas angehängt werden soll
                        existingDoc = Transform(existingDoc, newdoc, evaluatedXPath, XPathNamespace);

                        XmlWriter xw2 = XmlWriter.Create(previousXmlPath, Helpers.GetXmlWriterSettings(previousXmlPath));
                        existingDoc.Save(xw2);
                        xw2.Flush();
                        xw2.Close();
                    }
                }
            }

            if (Open)
            {
                if (xmlElementManifestFile != null)
                {
                    Window window = xmlElementManifestFile.Open("{00000000-0000-0000-0000-000000000000}");
                    window.Visible = true;
                    window.Activate();

                    try
                    {
                        XPathDocument  doc       = new XPathDocument(previousXmlPath);
                        XPathNavigator navigator = doc.CreateNavigator();

                        XmlNamespaceManager newnsmgr = new XmlNamespaceManager(navigator.NameTable);
                        newnsmgr.AddNamespace("ns", XPathNamespace);
                        XPathNavigator iterator = navigator.SelectSingleNode(evaluatedXPath, newnsmgr);
                        IXmlLineInfo   lineInfo = ((IXmlLineInfo)iterator);

                        ((TextDocument)dte.ActiveDocument.Object("TextDocument")).Selection.GotoLine(lineInfo.LineNumber);
                        ((TextDocument)dte.ActiveDocument.Object("TextDocument")).Selection.PadToColumn(lineInfo.LinePosition);
                    }
                    catch { }
                }
            }
        }