Esempio n. 1
0
            public string GetSerializationData(object target)
            {
                ProjectItem targetItem = target as ProjectItem;

                if (targetItem == null)
                {
                    throw new ArgumentException("target");
                }

                string projectData = new ProjectReference.WebProjectStrategy().GetSerializationData(targetItem.ContainingProject);
                string itemPath    = "";
                bool   found       = DteHelper.BuildPathFromCollection(targetItem.ContainingProject.ProjectItems, targetItem, ref itemPath);

                // This would indicate a bug in the DTE itself.
                Debug.Assert(found, "Item does not belong to containing project?");

                return(projectData + "||" + itemPath);
            }
Esempio n. 2
0
            public string GetAppliesTo(object target)
            {
                ProjectItem targetItem = target as ProjectItem;

                if (targetItem == null)
                {
                    throw new ArgumentException("target");
                }

                string itemPath = "";
                bool   found    = DteHelper.BuildPathFromCollection(targetItem.ContainingProject.ProjectItems, targetItem, ref itemPath);

                // This would indicate a bug in the DTE itself.
                Debug.Assert(found, "Item does not belong to containing project?");

                webType kind = (webType)targetItem.ContainingProject.Properties.Item("WebSiteType").Value;

                if (kind == webType.webTypeFileSystem)
                {
                    // Use the simplified representation of the path if available
                    if (targetItem.ContainingProject.ParentProjectItem != null)
                    {
                        itemPath = targetItem.ContainingProject.ParentProjectItem.Name + itemPath;
                    }
                    else
                    {
                        itemPath = targetItem.ContainingProject.Name + itemPath;
                    }
                }
                else
                {
                    itemPath = targetItem.ContainingProject.FullName +
                               itemPath.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
                }

                return(itemPath);
            }
        /// <summary>
        /// The method that creates a new item from the input string.
        /// </summary>
        public override void Execute()
        {
            if (ownerItem == null && ownerProject == null)
            {
                throw new ArgumentNullException(Properties.Resources.OwnerProjectOrItemRequired);
            }

            if (ownerItem != null)
            {
                ownerProject = ownerItem.ContainingProject;
                // If we got a parent item, build the virtual path to it
                // for the real full path we will work againt.
                string itemPath = String.Empty;
                DteHelper.BuildPathFromCollection(ownerProject.ProjectItems, ownerItem, ref itemPath);
                path = System.IO.Path.Combine(itemPath, path);
            }

            DTE         vs = GetService <DTE>();
            ProjectItem containingFolder;
            string      targetPath;

            using (EnsureProjectFolderAction ensureFolder = new EnsureProjectFolderAction())
            {
                this.Site.Container.Add(ensureFolder);
                ensureFolder.Path         = path;
                ensureFolder.OwnerProject = ownerProject;
                ensureFolder.Execute();
                containingFolder = ensureFolder.TargetFolder;
            }

            if (containingFolder.Object is Project)
            {
                containingFolder = new ProjectProjectItemAdapter((Project)containingFolder.Object);
            }

            using (EvaluateExpressionAction evaluateExpression = new EvaluateExpressionAction())
            {
                this.Site.Container.Add(evaluateExpression);
                evaluateExpression.Expression = path;
                evaluateExpression.Execute();
                targetPath = evaluateExpression.ReturnValue.ToString();
            }

            string targetFileName = System.IO.Path.GetFileName(targetPath);

            // This may be different than the containingFolder variable,
            // as the EnsureProjectFolder only ensures folders, not
            // hierarchy of dependent items. So we need to look for
            // the actual parent which may be a project item, not a folder.
            ProjectItem parent = DteHelper.FindItemByPath(
                vs.Solution,
                System.IO.Path.Combine(
                    DteHelper.BuildPath(ownerProject),

                    /* This call just strips the last segment in the path. "Directory"
                     * can still be a file name, as in the case of dependent items */
                    System.IO.Path.GetDirectoryName(targetPath)
                    )
                );

            if (parent == null)
            {
                parent = containingFolder;
            }

            Debug.Assert(parent != null, "Didn't find parent with path: " + targetPath);

            addedItem = DteHelper.FindItemByName(parent.ProjectItems, targetFileName, false);
            if (addedItem != null)
            {
                if (overwrite)
                {
                    OverwriteFile(vs, addedItem.get_FileNames(1), content);
                    addedItem.Delete();
                }
            }

            addedItem = DteHelper.FindItemByName(parent.ProjectItems, targetFileName, false);
            if (addedItem == null)
            {
                // At the FS level, dependent files are always inside the folder we determined
                // above using the EnsureProjectFolderAction.
                string folderPath = System.IO.Path.Combine(
                    System.IO.Path.GetDirectoryName(ownerProject.FileName),
                    DteHelper.BuildPath(containingFolder));
                string fullPath = System.IO.Path.Combine(folderPath, targetFileName);
                if (File.Exists(fullPath))
                {
                    OverwriteFile(vs, fullPath, content);
                    addedItem = parent.ProjectItems.AddFromFile(fullPath);
                }
                else
                {
                    string tempfile = System.IO.Path.GetTempFileName();
                    try
                    {
                        File.WriteAllText(tempfile, content);
                        addedItem = parent.ProjectItems.AddFromTemplate(tempfile, targetFileName);
                    }
                    finally
                    {
                        File.Delete(tempfile);
                    }
                }
            }

            if (open)
            {
                Window wnd = addedItem.Open(Constants.vsViewKindPrimary);
                wnd.Visible = true;
                wnd.Activate();
            }
        }