// Methods
        public override void Execute()
        {
            ProjectItem item = DteHelperEx.FindItemByName(this.Project.ProjectItems, this.ItemName, recursive);

            if (item == null)
            {
                TextTemplateAction templateAction = new TextTemplateAction();
                templateAction.Template = this.Template;

                templateAction.Site = this.Site;
                templateAction.Execute();
                string templateContent = templateAction.Content;

                AddItemFromStringToProjectItemByNameAction addItemAction = new AddItemFromStringToProjectItemByNameAction();
                addItemAction.Content        = templateContent;
                addItemAction.Project        = this.Project;
                addItemAction.TargetFileName = this.ItemName;
                addItemAction.Site           = this.Site;
                addItemAction.Execute();

                item = addItemAction.ProjectItem;
            }

            this.ProjectItem = item;
        }
Exemplo n.º 2
0
        /// <summary>
        /// The method that creates a new item from the intput string.
        /// </summary>
        public override void Execute()
        {
            DTE    vs       = (DTE)GetService(typeof(DTE));
            string tempfile = Path.GetTempFileName();

            try
            {
                using (StreamWriter sw = new StreamWriter(tempfile, false, new UTF8Encoding(true, true)))
                {
                    sw.WriteLine(content);
                }
                if (!string.IsNullOrEmpty(itemName))
                {
                    ProjectItem projectItem = DteHelperEx.FindItemByName(this.project.ProjectItems, this.itemName, true);
                    SecureAddItem(projectItem, tempfile);
                }
                else
                {
                    SecureAddItem(tempfile);
                }
            }
            finally
            {
                if (File.Exists(tempfile))
                {
                    File.Delete(tempfile);
                }
            }
        }
        /// <summary>
        /// Returns whether the given value object is valid for this type and for the specified context.
        /// </summary>
        /// <param name="service">The <see cref="IServiceProvider"/> value.</param>
        /// <param name="value">The <see cref="T:System.Object"></see> to test for validity.</param>
        /// <returns>
        /// true if the specified value is valid for this object; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.ArgumentNullException">The context parameter is null.</exception>
        public virtual bool IsValid(IServiceProvider service, object value)
        {
            // validate input
            Guard.ArgumentNotNull(service, "service");
            // set return value
            bool overwrite = true;

            // check if there is any monitored argument to use instead of the current value
            IDictionaryService dictionary = (IDictionaryService)service.GetService(typeof(IDictionaryService));

            if (this.monitorArgument != null)
            {
                value = dictionary.GetValue(this.monitorArgument);
            }
            string itemName = value as string;

            if (!string.IsNullOrEmpty(itemName))
            {
                // add the postfix argument value if any.
                itemName += this.itemPostfixArgument;
                // get the project item associated to this value.
                ProjectItem item    = null;
                Project     project = (Project)dictionary.GetValue(this.projectArgument);
                if (project != null)
                {
                    item = DteHelperEx.FindItemByName(project.ProjectItems, itemName, true);
                }
                if (item != null)
                {
                    // Check if we need to reset the state in case we are running a new instance of the current recipe.
                    ResetStateIfNewInstance(service);
                    // check if we already processed this item
                    // (this is to avoid recurrent UI showing up because of multiple calls to this method)
                    if (itemName.Equals(storedItemName, StringComparison.InvariantCulture))
                    {
                        overwrite = storedItemStatus;
                    }
                    else
                    {
                        // Ask the user if she wants to overwrite the item or not.
                        IUIService   svc           = (IUIService)service.GetService(typeof(IUIService));
                        DialogResult userSelection = svc.ShowMessage(
                            string.Format(CultureInfo.CurrentUICulture, Properties.Resources.OverwriteItemMessage, itemName),
                            null,
                            System.Windows.Forms.MessageBoxButtons.YesNo);
                        overwrite = (userSelection == DialogResult.Yes);
                    }
                }
            }

            // Persist state so we may compare with
            // the current value on the next validation
            storedItemStatus = overwrite;
            storedItemName   = itemName;

            return(overwrite);
        }
        private ProjectItem GetOrCreateItem(ProjectItems items, string item)
        {
            ProjectItem parentProjectItem = DteHelperEx.FindItemByName(items, item, true);

            if (parentProjectItem == null)
            {
                parentProjectItem = items.AddFolder(item, SolutionFolderKind);
            }
            return(parentProjectItem);
        }
        /// <summary>
        /// Delete an item from a project
        /// </summary>
        public override void Execute()
        {
            DTE dte1 = (DTE)base.GetService(typeof(DTE));

            // The search will be recursive to explore every sub filder inside the specified project.
            ProjectItem item = DteHelperEx.FindItemByName(this.Project.ProjectItems, this.itemName, true);

            if (item != null)
            {
                item.Delete();
            }
        }
        private bool Evaluate(out object newValue)
        {
            DTE dte = (DTE)GetService(typeof(DTE));
            IDictionaryService dictservice = (IDictionaryService)GetService(typeof(IDictionaryService));
            string             itemName    = ExpressionEvaluationHelper.EvaluateExpression(dictservice, this.itemNameExpression) as string;
            Project            project     = ExpressionEvaluationHelper.EvaluateExpression(dictservice, this.projectExpression) as Project;

            newValue = DteHelperEx.FindItemByName(project.ProjectItems, itemName, true);
            if (newValue != null)
            {
                return(true);
            }
            return(false);
        }
 private bool FileExists(ICreateViewPageBaseModel pageModel, string fileName)
 {
     if (pageModel.ModuleProject == null ||
         !(pageModel.ModuleProject is IProjectModel) ||
         pageModel.ModuleProject.Project == null ||
         !(pageModel.ModuleProject.Project is Project))
     {
         return(false);
     }
     else
     {
         ProjectItem item = DteHelperEx.FindItemByName(((Project)pageModel.ModuleProject.Project).ProjectItems, fileName, true);
         return(item != null);
     }
 }
        /// <summary>
        /// The method that creates a new item from the intput string.
        /// </summary>
        public override void Execute()
        {
            DTE    vs       = (DTE)GetService(typeof(DTE));
            string tempfile = Path.GetTempFileName();

            try
            {
                using (StreamWriter sw = new StreamWriter(tempfile, false, new UTF8Encoding(true, true)))
                {
                    sw.WriteLine(content);
                }

                // Check it the targetFileName already exists and delete it so it can be added.
                ProjectItem targetItem = DteHelperEx.FindItemByName(Project.ProjectItems, targetFileName, true);
                if (targetItem != null)
                {
                    targetItem.Delete();
                }

                if (!String.IsNullOrEmpty(itemName))
                {
                    ProjectItem item = DteHelperEx.FindItemByName(Project.ProjectItems, itemName, true);
                    if (item != null)
                    {
                        projectItem = item.ProjectItems.AddFromTemplate(tempfile, targetFileName);
                    }
                }
                else
                {
                    projectItem = project.ProjectItems.AddFromTemplate(tempfile, targetFileName);
                }

                if (open && projectItem != null)
                {
                    Window wnd = projectItem.Open(Constants.vsViewKindPrimary);
                    wnd.Visible = true;
                    wnd.Activate();
                }
            }
            finally
            {
                File.Delete(tempfile);
            }
        }
 // Methods
 public override void Execute()
 {
     this.ProjectItem = DteHelperEx.FindItemByName(this.Project.ProjectItems, this.ItemName, true);
 }
Exemplo n.º 10
0
        /// <summary>
        /// Returns true if the file is in the current project.
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <returns></returns>
        public bool ProjectContainsFile(string filename)
        {
            ProjectItem itemFound = DteHelperEx.FindItemByName(project.ProjectItems, filename, true);

            return(itemFound != null);
        }
        public override void Execute()
        {
            CodeClass codeClass = _target as CodeClass;

            // Determine which using sentence to add
            Project     prjContainer    = codeClass.ProjectItem.ContainingProject;
            ProjectItem constantsFolder = DteHelperEx.FindItemByName(prjContainer.ProjectItems, "Constants", false);
            string      usingNamespace  = constantsFolder == null ? GlobalUsingNamespace : LocalUsingNamespace;

            if (codeClass == null && !ReferenceUtil.HaveAClass(_target, out codeClass))
            {
                return;
            }
            if (codeClass != null)
            {
                TextPoint tp = codeClass.StartPoint;
                EditPoint ep = tp.CreateEditPoint();
                ep.StartOfDocument();

                int lastUsing = -1;

                string keyword     = string.Empty;
                string patternText = string.Empty;

                switch (codeClass.Language)
                {
                case CodeModelLanguageConstants.vsCMLanguageCSharp:

                    keyword     = "using";
                    patternText = String.Concat(keyword, " {0};");
                    break;

                case CodeModelLanguageConstants.vsCMLanguageVB:
                    keyword     = "Imports";
                    patternText = String.Concat(keyword, " {0}");
                    break;

                default:
                    throw new NotSupportedException("Language not supported");
                }

                //string usingText = String.Format("using {0}", usingNamespace);
                string usingText = String.Format(patternText, usingNamespace);
                while (!ep.AtEndOfDocument)
                {
                    int    length = ep.LineLength;
                    string line   = ep.GetText(ep.LineLength);
                    if (line.Contains(usingText))
                    {
                        return;
                    }
                    //if (line.StartsWith("using")) lastUsing = ep.Line;
                    if (line.StartsWith(keyword))
                    {
                        lastUsing = ep.Line;
                    }
                    ep.LineDown(1);
                }
                ep.StartOfDocument();
                if (lastUsing > 0)
                {
                    ep.LineDown(lastUsing);
                }
                ep.Insert(usingText);
                //ep.Insert(";");
                ep.Insert(Environment.NewLine);
                if (ep.LineLength != 0)
                {
                    ep.Insert(Environment.NewLine);
                }
            }
        }