Ejemplo n.º 1
0
        private void OnAddTransformCommand(object sender, EventArgs e)
        {
            IVsHierarchy hierarchy = null;
            uint         itemid    = VSConstants.VSITEMID_NIL;

            if (!IsSingleProjectItemSelection(out hierarchy, out itemid))
            {
                return;
            }

            var vsProject = (IVsProject)hierarchy;

            if (!ProjectSupportsTransforms(vsProject))
            {
                return;
            }

            string projectFullPath = null;

            if (ErrorHandler.Failed(vsProject.GetMkDocument(VSConstants.VSITEMID_ROOT, out projectFullPath)))
            {
                return;
            }

            var buildPropertyStorage = vsProject as IVsBuildPropertyStorage;

            if (buildPropertyStorage == null)
            {
                return;
            }

            // get the name of the item
            string itemFullPath = null;

            if (ErrorHandler.Failed(vsProject.GetMkDocument(itemid, out itemFullPath)))
            {
                return;
            }

            // Save the project file
            var solution = (IVsSolution)Package.GetGlobalService(typeof(SVsSolution));
            int hr       = solution.SaveSolutionElement((uint)__VSSLNSAVEOPTIONS.SLNSAVEOPT_SaveIfDirty, hierarchy, 0);

            if (hr < 0)
            {
                throw new COMException(string.Format("Failed to add project item", itemFullPath, GetErrorInfo()), hr);
            }

            var     selectedProjectItem = GetProjectItemFromHierarchy(hierarchy, itemid);
            Project project             = null;

            if (selectedProjectItem != null)
            {
                string itemFolder    = Path.GetDirectoryName(itemFullPath);
                string itemFilename  = Path.GetFileNameWithoutExtension(itemFullPath);
                string itemExtension = Path.GetExtension(itemFullPath);

                string content = Resources.TransformFileContents;

                var dialog = new EnvironmentEntry();
                dialog.ShowDialog();
                if (string.IsNullOrEmpty(dialog.InputText))
                {
                    return;
                }

                string itemName = string.Format("{0}.{1}{2}", itemFilename, dialog.InputText.ToCamelCase(), itemExtension);

                var newItemFullPath = Path.Combine(itemFolder, itemName);
                AddXdtTransformFile(selectedProjectItem, content, itemName, itemFolder, hierarchy);
                AddTransformToSolution(selectedProjectItem, itemName, itemFolder, hierarchy);

                // also add the web.oncheckin.config file as well if it's there.
                AddTransformToSolution(selectedProjectItem, "web.oncheckin.config", itemFolder, hierarchy);

                // now force the storage type.
                uint addedFileId;
                hierarchy.ParseCanonicalName(newItemFullPath, out addedFileId);
                //buildPropertyStorage.SetItemAttribute(addedFileId, IsTransformFile, "True");
            }
        }
        private void OnAddTransformCommand(object sender, EventArgs e)
        {
            IVsHierarchy hierarchy = null;
            uint itemid = VSConstants.VSITEMID_NIL;

            if (!IsSingleProjectItemSelection(out hierarchy, out itemid)) return;

            var vsProject = (IVsProject)hierarchy;
            if (!ProjectSupportsTransforms(vsProject)) return;

            string projectFullPath = null;
            if (ErrorHandler.Failed(vsProject.GetMkDocument(VSConstants.VSITEMID_ROOT, out projectFullPath))) return;

            var buildPropertyStorage = vsProject as IVsBuildPropertyStorage;
            if (buildPropertyStorage == null) return;

            // get the name of the item
            string itemFullPath = null;
            if (ErrorHandler.Failed(vsProject.GetMkDocument(itemid, out itemFullPath))) return;

            // Save the project file
            var solution = (IVsSolution)Package.GetGlobalService(typeof(SVsSolution));
            int hr = solution.SaveSolutionElement((uint)__VSSLNSAVEOPTIONS.SLNSAVEOPT_SaveIfDirty, hierarchy, 0);
            if (hr < 0)
            {
                throw new COMException(string.Format("Failed to add project item", itemFullPath, GetErrorInfo()), hr);
            }

            var selectedProjectItem = GetProjectItemFromHierarchy(hierarchy, itemid);
            Project project = null;
            if (selectedProjectItem != null)
            {
                string itemFolder = Path.GetDirectoryName(itemFullPath);
                string itemFilename = Path.GetFileNameWithoutExtension(itemFullPath);
                string itemExtension = Path.GetExtension(itemFullPath);

                string content = Resources.TransformFileContents;

                var dialog = new EnvironmentEntry();
                dialog.ShowDialog();
                if (string.IsNullOrEmpty(dialog.InputText)) return;

                string itemName = string.Format("{0}.{1}{2}", itemFilename, dialog.InputText.ToCamelCase(), itemExtension);

                var newItemFullPath = Path.Combine(itemFolder, itemName);
                AddXdtTransformFile(selectedProjectItem, content, itemName, itemFolder, hierarchy);
                AddTransformToSolution(selectedProjectItem, itemName, itemFolder, hierarchy);

                // also add the web.oncheckin.config file as well if it's there.
                AddTransformToSolution(selectedProjectItem, "web.oncheckin.config", itemFolder, hierarchy);

                // now force the storage type.
                uint addedFileId;
                hierarchy.ParseCanonicalName(newItemFullPath, out addedFileId);
                //buildPropertyStorage.SetItemAttribute(addedFileId, IsTransformFile, "True");
            }
        }