Beispiel #1
0
        /// <summary>
        /// Sets the item content to the given string, optionally specifying the encoding.
        /// </summary>
        /// <param name="item">The item to overwrite.</param>
        /// <param name="content">The content to set for the item.</param>
        /// <param name="encoding">Optional encoding, which defaults to UTF8.</param>
        public static void SetContent(this IItem item, string content, Encoding encoding = null)
        {
            Guard.NotNull(() => item, item);
            Guard.NotNull(() => content, content);

            VsHelper.CheckOut(item.PhysicalPath);
            File.WriteAllText(item.PhysicalPath, content, encoding ?? Encoding.UTF8);
        }
Beispiel #2
0
        public virtual IItemContainer Unfold(string name, IItemContainer parent)
        {
            Guard.NotNullOrEmpty(() => name, name);
            Guard.NotNull(() => parent, parent);

            if (!typeof(HierarchyItem).IsAssignableFrom(parent.GetType()))
            {
                throw new NotSupportedException(Resources.VsFileTemplate_ErrorParentNotHierarchy);
            }

            Guard.NotNullOrEmpty(() => parent.PhysicalPath, parent.PhysicalPath);

            var targetPath = Path.Combine(Path.GetDirectoryName(parent.PhysicalPath), name);

            if (File.Exists(targetPath))
            {
                VsHelper.CheckOut(targetPath);
            }

            if (!this.SourcePath.Equals(targetPath, StringComparison.OrdinalIgnoreCase))
            {
                File.Copy(this.SourcePath, targetPath, this.overwrite);
            }

            var container = parent.As <dynamic>();

            EnvDTE.ProjectItem newlyAddedFile = null;

            if (!parent.Items.Any(i => i.Name == name))
            {
                newlyAddedFile = container.ProjectItems.AddFromFile(targetPath) as EnvDTE.ProjectItem;
            }

            if (this.openFile)
            {
                container.DTE.ItemOperations.OpenFile(targetPath);
            }
            else if (newlyAddedFile != null)
            {
                //
                // The file may have opened anyway, if we're not supposed to open it, we'll search for
                // the matching window and close it
                //
                foreach (EnvDTE.Window w in container.DTE.Windows)
                {
                    if (newlyAddedFile.Equals(w.ProjectItem))
                    {
                        w.Close(EnvDTE.vsSaveChanges.vsSaveChangesNo);
                        break;
                    }
                }
            }

            return(parent.Items.FirstOrDefault(item => item.Kind == ItemKind.Item && item.Name == name));
        }
        public static void Checkout(this IItem item)
        {
            Guard.NotNull(() => item, item);

            VsHelper.CheckOut(item.PhysicalPath);
        }