PageRenderTime 22ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Visual Studio 2010/CSVSXSaveProject/CSVSXSaveProjectPackage.cs

#
C# | 209 lines | 115 code | 20 blank | 74 comment | 14 complexity | 5ff88f41c06a7a7617a24743ef74822e MD5 | raw file
  1. /************************************* Module Header *********************************\
  2. * Module Name: CSVSXSaveProjectPackage.cs
  3. * Project : CSVSXSaveProject
  4. * Copyright (c) Microsoft Corporation
  5. *
  6. * This package adds menus to IDE, which supply following features
  7. * 1. Copy the whole project to a new location.
  8. * 2. Open the new project in the current IDE.
  9. *
  10. *
  11. * The source is subject to the Microsoft Public License.
  12. * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  13. * All other rights reserved
  14. *
  15. * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
  16. * EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  18. \*************************************************************************************/
  19. using System;
  20. using System.Collections.Generic;
  21. using System.ComponentModel.Design;
  22. using System.Diagnostics;
  23. using System.Globalization;
  24. using System.Linq;
  25. using System.Runtime.InteropServices;
  26. using System.Windows.Forms;
  27. using EnvDTE;
  28. using Microsoft.VisualStudio.Shell;
  29. namespace Microsoft.CSVSXSaveProject
  30. {
  31. /// <summary>
  32. /// This is the class that implements the package exposed by this assembly.
  33. ///
  34. /// The minimum requirement for a class to be considered a valid package for Visual
  35. /// Studio is to implement the IVsPackage interface and register itself with the
  36. /// shell. This package uses the helper classes defined inside the Managed Package
  37. /// Framework (MPF) to do it: it derives from the Package class that provides the
  38. /// implementation of the IVsPackage interface and uses the registration attributes
  39. /// defined in the framework to register itself and its components with the shell.
  40. /// </summary>
  41. // This attribute tells the PkgDef creation utility (CreatePkgDef.exe) that this
  42. // class is a package.
  43. [PackageRegistration(UseManagedResourcesOnly = true)]
  44. // This attribute is used to register the information needed to show the this
  45. // package in the Help/About dialog of Visual Studio.
  46. [InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
  47. // This attribute is needed to let the shell know that this package exposes some
  48. // menus.
  49. [ProvideMenuResource("Menus.ctmenu", 1)]
  50. [Guid(GuidList.guidCSVSXSaveProjectPkgString)]
  51. public sealed class CSVSXSaveProjectPackage : Package
  52. {
  53. /// <summary>
  54. /// Specify a DTE Object in this application.
  55. /// </summary>
  56. DTE dte;
  57. internal DTE DTEObject
  58. {
  59. get
  60. {
  61. if (dte == null)
  62. {
  63. dte = this.GetService(typeof(DTE)) as DTE;
  64. }
  65. return dte;
  66. }
  67. }
  68. /// <summary>
  69. /// Default constructor of the package.
  70. /// Inside this method you can place any initialization code that does not
  71. /// require any Visual Studio service because at this point the package object
  72. /// is created but not sited yet inside Visual Studio environment. The place
  73. /// to do all the other initialization is the Initialize method.
  74. /// </summary>
  75. public CSVSXSaveProjectPackage()
  76. {
  77. Trace.WriteLine(string.Format(CultureInfo.CurrentCulture,
  78. "Entering constructor for: {0}", this.ToString()));
  79. }
  80. /////////////////////////////////////////////////////////////////////////////////
  81. // Overriden Package Implementation
  82. #region Package Members
  83. /// <summary>
  84. /// Initialization of the package; this method is called right after the
  85. /// package is sited, so this is the place where you can put all the
  86. /// initialization code that rely on services provided by VisualStudio.
  87. /// </summary>
  88. protected override void Initialize()
  89. {
  90. Trace.WriteLine(string.Format(CultureInfo.CurrentCulture,
  91. "Entering Initialize() of: {0}", this.ToString()));
  92. base.Initialize();
  93. // Add our command handlers for menu (commands must exist in the .vsct file)
  94. OleMenuCommandService mcs = GetService(typeof(IMenuCommandService))
  95. as OleMenuCommandService;
  96. if (null != mcs)
  97. {
  98. // Create the command for the menu item.
  99. CommandID menuCommandID =
  100. new CommandID(GuidList.guidCSVSXSaveProjectCmdSet,
  101. (int)PkgCmdIDList.cmdidCSVSXSaveProjectCommandID);
  102. MenuCommand menuItem =
  103. new MenuCommand(MenuItemCallback, menuCommandID);
  104. mcs.AddCommand(menuItem);
  105. // Create the command for the VSXSaveProjectCmdSet menu item.
  106. CommandID cSVSXSaveProjectContextCommandID =
  107. new CommandID(GuidList.guidCSVSXSaveProjectContextCmdSet,
  108. (int)PkgCmdIDList.cmdidCSVSXSaveProjectContextCommandID);
  109. OleMenuCommand cSVSXSaveProjectMenuContextCommand =
  110. new OleMenuCommand(MenuItemCallback,
  111. cSVSXSaveProjectContextCommandID);
  112. mcs.AddCommand(cSVSXSaveProjectMenuContextCommand);
  113. }
  114. }
  115. #endregion
  116. /// <summary>
  117. /// This function is the callback used to execute a command when the a menu
  118. /// item is clicked. See the Initialize method to see how the menu item is
  119. /// associated to this function using the OleMenuCommandService service and
  120. /// the MenuCommand class.
  121. /// </summary>
  122. private void MenuItemCallback(object sender, EventArgs e)
  123. {
  124. try
  125. {
  126. // Get current active project object.
  127. var proj = this.GetActiveProject();
  128. if (proj != null)
  129. {
  130. // Get the project information.
  131. var vsProj = new Files.VSProject(proj);
  132. // Get the files included in the project.
  133. var includedFiles = vsProj.GetIncludedFiles();
  134. // Get the files under the project folder.
  135. var projfolderFiles =
  136. Files.ProjectFolder.GetFilesInProjectFolder(proj.FullName);
  137. // Add the other files such as documents under the project folder, so
  138. // the user can choose them.
  139. var totalItems = new List<Files.ProjectFileItem>(includedFiles);
  140. foreach (Files.ProjectFileItem fileItem in projfolderFiles)
  141. {
  142. if (includedFiles.Count(f => f.FullName.Equals(fileItem.FullName,
  143. StringComparison.OrdinalIgnoreCase)) == 0)
  144. {
  145. totalItems.Add(fileItem);
  146. }
  147. }
  148. // Display the user interface.
  149. using (SaveProjectDialog dialog = new SaveProjectDialog())
  150. {
  151. // Display the all the files.
  152. dialog.FilesItems = totalItems;
  153. dialog.OriginalFolderPath = vsProj.ProjectFolder.FullName;
  154. var result = dialog.ShowDialog();
  155. // Open the new project.
  156. if (result == DialogResult.OK && dialog.OpenNewProject)
  157. {
  158. string newProjectPath = string.Format("{0}\\{1}",
  159. dialog.NewFolderPath,
  160. proj.FullName.Substring(vsProj.ProjectFolder.FullName.Length));
  161. string cmd = string.Format("File.OpenProject \"{0}\"", newProjectPath);
  162. this.DTEObject.ExecuteCommand(cmd);
  163. }
  164. }
  165. }
  166. }
  167. catch (Exception ex)
  168. {
  169. MessageBox.Show(ex.Message);
  170. }
  171. }
  172. /// <summary>
  173. /// Get the active project object.
  174. /// </summary>
  175. internal Project GetActiveProject()
  176. {
  177. Project activeProject = null;
  178. // Get all project in Solution Explorer.
  179. Array activeSolutionProjects =
  180. this.DTEObject.ActiveSolutionProjects as Array;
  181. if (activeSolutionProjects != null && activeSolutionProjects.Length > 0)
  182. {
  183. // Get the active project.
  184. activeProject = activeSolutionProjects.GetValue(0) as Project;
  185. }
  186. return activeProject;
  187. }
  188. }
  189. }