Beispiel #1
0
 internal static Project GetProjectFromGuid(DTE dte, IServiceProvider provider, Guid guid)
 {
     return(DteHelperEx.FindProject(dte, delegate(Project match)
     {
         return guid == GetProjectGuid(provider, match);
     }));
 }
        // 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;
        }
Beispiel #3
0
        private Project FindProjectByName(string name, bool isWeb)
        {
            DTE     dte     = (DTE)GetService(typeof(DTE));
            Project project = null;

            if (!isWeb)
            {
                project = DteHelperEx.FindProjectByName(dte, name, isWeb);
            }
            else
            {
                foreach (Project projectTemp in dte.Solution.Projects)
                {
                    if (projectTemp.Name.Contains(name))
                    {
                        project = projectTemp;
                        break;
                    }

                    if (projectTemp.ProjectItems != null)
                    {
                        Project projectTemp1 = FindProjectByName(projectTemp.ProjectItems, name);
                        if (projectTemp1 != null)
                        {
                            project = projectTemp1;
                            break;
                        }
                    }
                }
            }

            return(project);
        }
Beispiel #4
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);
                }
            }
        }
Beispiel #5
0
 // This method fix the problem with GAx that founds a SolutionFolder as a project
 private Project FindProjectByNameEx(DTE dte, string projectName)
 {
     return(DteHelperEx.FindProject(dte, new Predicate <Project>(delegate(Project internalProject)
     {
         return internalProject.Name == projectName &&
         !(internalProject.Object is SolutionFolder);
     })));
 }
        /// <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>
        /// Determines whether the reference is enabled for a particular target item,
        /// based on the condition contained in the reference.
        /// </summary>
        /// <param name="target">The <see cref="T:System.Object"/> to check for references.</param>
        /// <returns>
        ///     <see langword="true"/> if the reference is enabled for the given <paramref name="target"/>.
        /// Otherwise, <see langword="false"/>.
        /// </returns>
        public override bool IsEnabledFor(object target)
        {
            if (!ReferenceUtil.IsCSharpProject(target) &&
                !DteHelperEx.IsWebCSharpProject(target))
            {
                return(false);
            }

            return(OnIsEnabledFor(target));
        }
        private void AddProjectTemplate(Project project)
        {
            DTE            dte       = (DTE)GetService(typeof(DTE));
            SolutionFolder slnFolder = null;

            if (project == null)
            {
                if (string.IsNullOrEmpty(this.Path))
                {
                    this.NewItem = dte.Solution.AddFromTemplate(this.Template, this.DestinationFolder, this.ItemName, false);
                }
                else
                {
                    Project subProject = DteHelper.FindProjectByPath(dte.Solution, this.Path);
                    slnFolder    = (SolutionFolder)subProject.Object;
                    this.NewItem = slnFolder.AddFromTemplate(this.Template, this.DestinationFolder, this.ItemName);
                }
            }
            else
            {
                slnFolder    = (SolutionFolder)project.Object;
                this.NewItem = slnFolder.AddFromTemplate(this.Template, this.DestinationFolder, this.ItemName);
            }

            if (this.newItem == null)
            {
                //Return the project already added if the AddFromTemplate method returns null
                ProjectItems childItems;

                if (slnFolder != null)
                {
                    childItems = slnFolder.Parent.ProjectItems;
                }
                else
                {
                    childItems = dte.Solution.Projects as ProjectItems;
                }

                if (childItems != null)
                {
                    foreach (ProjectItem item in childItems)
                    {
                        if (item.Name.Contains(this.ItemName))
                        {
                            this.NewItem = item.Object as Project;
                            break;
                        }
                    }
                }
                else
                {
                    this.NewItem = DteHelperEx.FindProjectByName(dte, this.ItemName, false);
                }
            }
        }
        public override void Execute()
        {
            DTE    dte = GetService <DTE>();
            string solutionDirectory =
                Path.GetDirectoryName((string)dte.Solution.Properties.Item("Path").Value);
            string targetDirectory = Path.Combine(solutionDirectory, DestinationDirectory);

            if (Root == null)
            {
                if (UseExtendedShell)
                {
                    AddTemplateToSolution(dte.Solution,
                                          ResolveTemplateDirectory(ExtendedShellTemplate), targetDirectory,
                                          LayoutProjectName);
                    AddTemplateToSolution(dte.Solution, ResolveTemplateDirectory(LayoutTemplate),
                                          targetDirectory, LayoutProjectName);
                }
                else
                {
                    AddTemplateToSolution(dte.Solution, ResolveTemplateDirectory(BasicShellTemplate),
                                          targetDirectory, ShellProjectName);
                }
            }
            if (Root is Project && ((Project)Root).Object is SolutionFolder)
            {
                SolutionFolder slnFolder = ((Project)Root).Object as SolutionFolder;
                if (UseExtendedShell)
                {
                    AddTemplateToSolutionFolder(slnFolder,
                                                ResolveTemplateDirectory(ExtendedShellTemplate), targetDirectory,
                                                ShellProjectName);
                    AddTemplateToSolutionFolder(slnFolder, ResolveTemplateDirectory(LayoutTemplate),
                                                targetDirectory, LayoutProjectName);
                }
                else
                {
                    AddTemplateToSolutionFolder(slnFolder,
                                                ResolveTemplateDirectory(BasicShellTemplate), targetDirectory,
                                                ShellProjectName);
                }
            }

            ShellProject   = DteHelperEx.FindProjectByName(dte, ShellProjectName, false);
            LayoutProject  = DteHelperEx.FindProjectByName(dte, LayoutProjectName, false);
            LibraryProject = DteHelperEx.FindProjectByName(dte, LibraryProjectName, false);
            if (_supportWPFViews)
            {
                if (UseExtendedShell)
                {
                    AddWPFReferenceToProject((Project)LayoutProject);
                }
                AddWPFReferenceToProject((Project)ShellProject);
                AddWPFReferenceToProject((Project)LibraryProject);
            }
        }
 /// <summary>
 /// Sets the value.
 /// </summary>
 /// <param name="currentValue">The current value.</param>
 /// <param name="newValue">The new value.</param>
 /// <returns></returns>
 private bool SetValue(object currentValue, out object newValue)
 {
     if (currentValue == null)
     {
         DTE dte = (DTE)GetService(typeof(DTE));
         newValue = DteHelperEx.GetCodeDomProvider(DteHelper.GetSelectedProject(dte));
         return(true);
     }
     newValue = null;
     return(false);
 }
        /// <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();
            }
        }
        /// <summary>
        /// Returns whether the given value object is valid for this type and for the specified context.
        /// </summary>
        /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"></see> that provides a format context.</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>
        public override bool IsValid(ITypeDescriptorContext context, object value)
        {
            bool response = true;

            if (value is string)
            {
                DTE vs = (DTE)context.GetService(typeof(DTE));
                response = !DteHelperEx.ProjectExists(vs.Solution, value.ToString(), this.language);
            }

            return(response);
        }
        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);
        }
Beispiel #15
0
        /// <summary>
        /// Uses <see cref="DteHelper.FindProjectByPath"/> to search for the project specified by the "Path" attributte
        /// </summary>
        /// <param name="currentValue"></param>
        /// <param name="newValue"></param>
        /// <returns></returns>
        /// <seealso cref="ValueProvider.OnBeginRecipe"/>
        /// <seealso cref="DteHelper.FindProjectByPath"/>
        public override bool OnBeginRecipe(object currentValue, out object newValue)
        {
            DTE dte = (DTE)GetService(typeof(DTE));

            if (currentValue == null)
            {
                newValue = DteHelperEx.FindProjectByName(dte, this.name, false);
                if (newValue != null)
                {
                    return(true);
                }
            }
            newValue = currentValue;
            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);
     }
 }
        private Project GetSourceProject(Web.AssemblyReference reference)
        {
            Project sourceProject = null;

            if (reference.ReferenceKind != Web.AssemblyReferenceType.AssemblyReferenceConfig)
            {
                sourceProject = DteHelperEx.FindProject(reference.DTE, new Predicate <Project>(delegate(Project match)
                {
                    return((match.Kind == VSLangProj.PrjKind.prjKindCSharpProject ||
                            match.Kind == VSLangProj.PrjKind.prjKindVBProject) &&
                           match.Name.Equals(reference.Name, StringComparison.OrdinalIgnoreCase));
                }));
            }
            return(sourceProject);
        }
Beispiel #18
0
        /// <summary>
        /// Uses <see cref="DteHelper.FindProjectByPath"/> to search for the project specified by the "Path" attributte
        /// </summary>
        /// <param name="currentValue"></param>
        /// <param name="newValue"></param>
        /// <returns></returns>
        /// <seealso cref="ValueProvider.OnBeginRecipe"/>
        /// <seealso cref="DteHelper.FindProjectByPath"/>
        public override bool OnBeginRecipe(object currentValue, out object newValue)
        {
            DTE dte = (DTE)GetService(typeof(DTE));
            IDictionaryService dictservice = (IDictionaryService)GetService(typeof(IDictionaryService));

            if (currentValue == null)
            {
                string projectName = ExpressionEvaluationHelper.EvaluateExpression(dictservice, this.nameExpression) as string;

                newValue = DteHelperEx.FindProjectByName(dte, projectName, false);
                if (newValue != null)
                {
                    return(true);
                }
            }
            newValue = currentValue;
            return(false);
        }
        /// <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);
            }
        }
Beispiel #20
0
 /// <summary>
 /// Gets the language from project.
 /// </summary>
 /// <param name="project">The project.</param>
 /// <returns></returns>
 public static string GetLanguageFromProject(EnvDTE.Project project)
 {
     if (project != null)
     {
         if (project.CodeModel != null)
         {
             return(project.CodeModel.Language);
         }
         CodeDomProvider provider = DteHelperEx.GetCodeDomProvider(project);
         if (provider is CSharp.CSharpCodeProvider)
         {
             return(EnvDTE.CodeModelLanguageConstants.vsCMLanguageCSharp);
         }
         else if (provider is VisualBasic.VBCodeProvider)
         {
             return(EnvDTE.CodeModelLanguageConstants.vsCMLanguageVB);
         }
     }
     return(null);
 }
Beispiel #21
0
        /// <summary>
        /// Contains code that will be called when recipe execution begins. This is the first method in the lifecycle.
        /// </summary>
        /// <param name="currentValue">An <see cref="T:System.Object"/> that contains the current value of the argument.</param>
        /// <param name="newValue">When this method returns, contains an <see cref="T:System.Object"/> that contains
        /// the new value of the argument, if the returned value
        /// of the method is <see langword="true"/>. Otherwise, it is ignored.</param>
        /// <returns>
        ///     <see langword="true"/> if the argument value should be replaced with
        /// the value in <paramref name="newValue"/>; otherwise, <see langword="false"/>.
        /// </returns>
        /// <remarks>By default, always returns <see langword="false"/>, unless overriden by a derived class.</remarks>
        public override bool OnBeginRecipe(object currentValue, out object newValue)
        {
            DTE dte = (DTE)GetService(typeof(DTE));

            ExpressionEvaluationService evaluator   = new ExpressionEvaluationService();
            IDictionaryService          dictservice = (IDictionaryService)GetService(typeof(IDictionaryService));

            string defaultName = evaluator.Evaluate(
                this.expression,
                new ServiceAdapterDictionary(dictservice)).ToString();

            string projectName = defaultName;

            while (DteHelperEx.ProjectExists(dte.Solution, projectName, this.language))
            {
                projectName = ProvideNewDefaultValue(defaultName, projectName);
            }

            newValue = projectName;
            return(true);
        }
        /// <summary>
        /// Unfolds the template
        /// </summary>
        public override void Execute()
        {
            DTE     dte     = (DTE)GetService(typeof(DTE));
            Project project = DteHelperEx.FindProjectByName(dte, this.ItemName, false);

            if (project != null)
            {
                this.NewItem = project;
            }
            else
            {
                if (string.IsNullOrEmpty(this.DestinationFolder))
                {
                    this.DestinationFolder = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(dte.Solution.FileName), this.ItemName);
                    if (Directory.Exists(this.DestinationFolder))
                    {
                        Directory.Delete(this.DestinationFolder, true);
                    }
                }
                InternalExecute();
            }
        }
Beispiel #23
0
        /// <summary>
        /// Runs the coordination using the configuration data specified in the configuration file.
        /// </summary>
        /// <param name="declaredActions">Actions defined in the package configuration file for the currently executing recipe.</param>
        /// <param name="coordinationData">The configuration data used to setup the coordination.</param>
        public void Run(Dictionary <string, Config.Action> declaredActions, XmlElement coordinationData)
        {
            IActionExecutionService exec = GetService <IActionExecutionService>(true);
            string currentAction         = null;

            try
            {
                foreach (Config.Action action in declaredActions.Values)
                {
                    currentAction = action.Name;
                    exec.Execute(action.Name);
                }
            }
            catch (Exception e)
            {
                IConfigurationService config = GetService <IConfigurationService>(true);
                DteHelperEx.ShowMessageInOutputWindow(
                    GetService <DTE>(true),
                    string.Format(CultureInfo.CurrentCulture,
                                  Properties.Resources.FailSafeCoordinatorExceptionMessage,
                                  config.CurrentRecipe.Caption, config.CurrentRecipe.Name, currentAction, e.Message),
                    DteHelperEx.GetPackageFriendlyName(this.Site));
            }
        }
 // Methods
 public override void Execute()
 {
     this.ProjectItem = DteHelperEx.FindItemByName(this.Project.ProjectItems, this.ItemName, true);
 }
        /// <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);
        }
Beispiel #26
0
        public override void Execute()
        {
            DTE dte = GetService <DTE>();

            string projectName     = ModuleProject.Name;
            object target          = ModuleProject.ParentProjectItem == null ? ModuleProject.DTE.Solution : ModuleProject.ParentProjectItem.ContainingProject.Object;
            string targetDirectory = Path.GetDirectoryName(ModuleProject.FileName);

            // NOTE: Visual Studio always creates:
            // - a project file
            // - a obj folder
            // - a Properties folder (VB only)
            // before running the wizard. We need to remove those
            // because we want to store the project in a subfolder instead.

            // Remove original (handle and file) project
            string obsoleteProjectPath = ModuleProject.FullName;

            dte.Solution.Remove(ModuleProject);

            if (!string.IsNullOrEmpty(obsoleteProjectPath))
            {
                if (File.Exists(obsoleteProjectPath))
                {
                    File.Delete(obsoleteProjectPath);
                }
                string objFolder = Path.Combine(Path.GetDirectoryName(obsoleteProjectPath), "obj");
                if (Directory.Exists(objFolder))
                {
                    // Visual basic project not always can delete de obj folder
                    try
                    {
                        Directory.Delete(objFolder, true);
                    }
                    catch { }
                }
                string propertiesFolder = Path.Combine(Path.GetDirectoryName(obsoleteProjectPath), "Properties");
                if (Directory.Exists(propertiesFolder))
                {
                    try
                    {
                        Directory.Delete(propertiesFolder, true);
                    }
                    catch { }
                }
            }

            if (CreateLayout)
            {
                UnfoldTemplateOnTarget(target, LayoutTemplate, targetDirectory, projectName);
            }
            else
            {
                UnfoldTemplateOnTarget(target, BaseTemplate, targetDirectory, projectName);
            }
            //if (target is SolutionFolder)
            //{
            //    ModuleProject = (Project)DteHelper.FindItemByName(((SolutionFolder)target).Parent.ProjectItems, projectName, true).Object;
            //}
            //else
            //{
            ModuleProject = FindProjectByNameEx(dte, projectName);
            //}

            // Unfold required projects
            if (CreateInterface)
            {
                string interfaceProjectName = String.Format("{0}.Interface", ModuleProject.Name);
                UnfoldTemplateOnTarget(target, InterfaceTemplate, targetDirectory, interfaceProjectName);
                ModuleInterfaceProject = DteHelperEx.FindProjectByName(dte, interfaceProjectName, false);
            }


            // Update projects references
            if (CreateInterface)
            {
                ((VSProject)ModuleInterfaceProject.Object).References.AddProject(InterfaceProject);
                ((VSProject)ModuleProject.Object).References.AddProject(ModuleInterfaceProject);
            }
            else
            {
                ((VSProject)ModuleProject.Object).References.AddProject(InterfaceProject);
            }

            // create test project
            if (CreateTestProject)
            {
                string testProjectName = String.Format("{0}.Tests", ModuleProject.Name);
                UnfoldTemplateOnTarget(target, TestProjectTemplate, targetDirectory, testProjectName);
                TestProject = DteHelperEx.FindProjectByName(dte, testProjectName, false);

                // Update projects references
                ((VSProject)TestProject.Object).References.AddProject(ModuleProject);
                ((VSProject)TestProject.Object).References.AddProject(InterfaceProject);
                if (CreateInterface)
                {
                    ((VSProject)TestProject.Object).References.AddProject(ModuleInterfaceProject);
                }
            }
        }
        private void PrepareConfigFile()
        {
            // add a temporal file on App_Code folder in case there's no code so we force precompilation and
            // get code analysis for the web.config file
            // Note: this file may be removed after the code analysis process is done.
            Web.VSWebSite webProject = this.project.Object as Web.VSWebSite;
            if (webProject.CodeFolders.Count == 0)
            {
                // add App_Code folder
                webProject.CodeFolders.Add("App_Code\\");
            }
            Web.CodeFolder codeFolder = (Web.CodeFolder)webProject.CodeFolders.Item(1);
            // check if we need to add a temp file
            foreach (ProjectItem item in codeFolder.ProjectItem.ProjectItems)
            {
                string ext = Path.GetExtension(item.Name);
                if (!string.IsNullOrEmpty(ext) &&
                    !ext.Equals(".exclude", StringComparison.OrdinalIgnoreCase))
                {
                    // do not add the temp file since we already have some code file
                    return;
                }
            }

            this.webTempFile = Path.Combine(webProject.Project.FileName,
                                            codeFolder.FolderPath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar)
                                            + Path.ChangeExtension(Path.GetRandomFileName(), DteHelperEx.GetDefaultExtension(this.project)));

            if (!File.Exists(this.webTempFile))
            {
                File.WriteAllText(this.webTempFile, string.Empty);
            }
        }
        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);
                }
            }
        }