~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/Main/Base/Project/Src/Commands/ProjectMenuCommands.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
using System;
 
5
using System.Diagnostics;
 
6
using System.IO;
 
7
using System.Text;
 
8
using System.Xml;
 
9
 
 
10
using ICSharpCode.Core;
 
11
using ICSharpCode.SharpDevelop.Gui;
 
12
using ICSharpCode.SharpDevelop.Project.Dialogs;
 
13
using Microsoft.Win32;
 
14
 
 
15
namespace ICSharpCode.SharpDevelop.Project.Commands
 
16
{
 
17
        public class ViewProjectOptions : AbstractMenuCommand
 
18
        {
 
19
                public override void Run()
 
20
                {
 
21
                        ShowProjectOptions(ProjectService.CurrentProject);
 
22
                }
 
23
                
 
24
                public static void ShowProjectOptions(IProject project)
 
25
                {
 
26
                        if (project == null) {
 
27
                                return;
 
28
                        }
 
29
                        foreach (IViewContent viewContent in WorkbenchSingleton.Workbench.ViewContentCollection) {
 
30
                                ProjectOptionsView projectOptions = viewContent as ProjectOptionsView;
 
31
                                if (projectOptions != null && projectOptions.Project == project) {
 
32
                                        projectOptions.WorkbenchWindow.SelectWindow();
 
33
                                        return;
 
34
                                }
 
35
                        }
 
36
                        try {
 
37
                                AddInTreeNode projectOptionsNode = AddInTree.GetTreeNode("/SharpDevelop/BackendBindings/ProjectOptions/" + project.Language);
 
38
                                ProjectOptionsView projectOptions = new ProjectOptionsView(projectOptionsNode, project);
 
39
                                WorkbenchSingleton.Workbench.ShowView(projectOptions);
 
40
                        } catch (TreePathNotFoundException) {
 
41
                                MessageService.ShowError("${res:Dialog.ProjectOptions.NoPanelsInstalledForProject}");
 
42
                        }
 
43
                }
 
44
        }
 
45
        
 
46
        public class GenerateProjectDocumentation : AbstractMenuCommand
 
47
        {
 
48
                static string[] registryKeys = new string[] {
 
49
                        @"HKEY_CLASSES_ROOT\Sandcastle Help File Builder Project\shell\open\command",
 
50
                        @"HKEY_CLASSES_ROOT\SandcastleBuilder.shfbproj\shell\open\command"
 
51
                };
 
52
                
 
53
                static string FindSHFB()
 
54
                {
 
55
                        string envVar = Environment.GetEnvironmentVariable("SHFBROOT");
 
56
                        if (!string.IsNullOrEmpty(envVar)) {
 
57
                                return Path.Combine(envVar, "SandcastleBuilderGUI.exe");
 
58
                        }
 
59
                        foreach (string registryKey in registryKeys) {
 
60
                                string fileName = FindSHFB(registryKey);
 
61
                                if (fileName != null) {
 
62
                                        return fileName;
 
63
                                }
 
64
                        }
 
65
                        return null;
 
66
                }
 
67
                
 
68
                static string FindSHFB(string registryKey)
 
69
                {
 
70
                        string command = Registry.GetValue(registryKey, null, string.Empty) as string;
 
71
                        return ExtractExecutableFromCommand(command);
 
72
                }
 
73
                
 
74
                static string ExtractExecutableFromCommand(string command)
 
75
                {
 
76
                        if (string.IsNullOrEmpty(command))
 
77
                                return null;
 
78
                        command = command.Trim();
 
79
                        if (string.IsNullOrEmpty(command))
 
80
                                return null;
 
81
                        if (command[0] == '"') {
 
82
                                // "program" %1
 
83
                                int pos = command.IndexOf('"', 1);
 
84
                                if (pos < 0)
 
85
                                        return null;
 
86
                                return command.Substring(1, pos - 1);
 
87
                        } else {
 
88
                                // program %1
 
89
                                int pos = command.IndexOf(' ');
 
90
                                if (pos < 0)
 
91
                                        return command;
 
92
                                else
 
93
                                        return command.Substring(0, pos);
 
94
                        }
 
95
                }
 
96
                
 
97
                public override void Run()
 
98
                {
 
99
                        CompilableProject project = ProjectService.CurrentProject as CompilableProject;
 
100
                        if (project == null) {
 
101
                                return;
 
102
                        }
 
103
                        string sandcastleHelpFileBuilderPath = FindSHFB();
 
104
                        if (sandcastleHelpFileBuilderPath == null || !File.Exists(sandcastleHelpFileBuilderPath)) {
 
105
                                using (ToolNotFoundDialog dlg = new ToolNotFoundDialog(
 
106
                                        StringParser.Parse("${res:ProjectComponent.ContextMenu.GenerateDocumentation.SHFBNotFound}"),
 
107
                                        "http://www.codeplex.com/SHFB/", null))
 
108
                                {
 
109
                                        dlg.ShowDialog(WorkbenchSingleton.MainWin32Window);
 
110
                                }
 
111
                                return;
 
112
                        }
 
113
                        
 
114
                        string assembly = project.OutputAssemblyFullPath;
 
115
                        string xmlDocFile = project.DocumentationFileFullPath;
 
116
                        if (xmlDocFile == null) {
 
117
                                MessageService.ShowMessage("${res:ProjectComponent.ContextMenu.GenerateDocumentation.NeedToEditBuildOptions}");
 
118
                                return;
 
119
                        }
 
120
                        if (!File.Exists(assembly)) {
 
121
                                MessageService.ShowMessage("${res:ProjectComponent.ContextMenu.GenerateDocumentation.ProjectNeedsToBeCompiled}");
 
122
                                return;
 
123
                        }
 
124
                        if (!File.Exists(xmlDocFile)) {
 
125
                                MessageService.ShowMessage("${res:ProjectComponent.ContextMenu.GenerateDocumentation.ProjectNeedsToBeCompiled2}");
 
126
                                return;
 
127
                        }
 
128
                        string sandcastleHelpFileBuilderProjectFile = Path.ChangeExtension(project.FileName, ".shfbproj");
 
129
                        if (!File.Exists(sandcastleHelpFileBuilderProjectFile)) {
 
130
                                using (XmlTextWriter w = new XmlTextWriter(sandcastleHelpFileBuilderProjectFile, Encoding.UTF8)) {
 
131
                                        w.Formatting = Formatting.Indented;
 
132
                                        const string ns = "http://schemas.microsoft.com/developer/msbuild/2003";
 
133
                                        w.WriteStartElement("Project", ns);
 
134
                                        w.WriteAttributeString("DefaultTargets", "Build");
 
135
                                        w.WriteAttributeString("ToolsVersion", "3.5");
 
136
                                        
 
137
                                        w.WriteStartElement("PropertyGroup", ns);
 
138
                                        w.WriteComment("The configuration and platform will be used to determine which\n" +
 
139
                                                       "assemblies to include from solution and project documentation\n" +
 
140
                                                       "sources");
 
141
                                        w.WriteStartElement("Configuration", ns);
 
142
                                        w.WriteAttributeString("Condition", " '$(Configuration)' == '' ");
 
143
                                        w.WriteValue("Debug");
 
144
                                        w.WriteEndElement();  // </Configuration>
 
145
                                        
 
146
                                        w.WriteStartElement("Platform", ns);
 
147
                                        w.WriteAttributeString("Condition", " '$(Platform)' == '' ");
 
148
                                        w.WriteValue("AnyCPU");
 
149
                                        w.WriteEndElement();  // </AnyCPU>
 
150
                                        
 
151
                                        w.WriteElementString("SchemaVersion", ns, "2.0");
 
152
                                        w.WriteElementString("ProjectGuid", ns, Guid.NewGuid().ToString("B"));
 
153
                                        w.WriteElementString("SHFBSchemaVersion", ns, "1.8.0.3");
 
154
                                        
 
155
                                        w.WriteElementString("AssemblyName", ns, "Documentation");
 
156
                                        w.WriteElementString("RootNamespace", ns, "Documentation");
 
157
                                        w.WriteElementString("Name", ns, "Documentation");
 
158
                                        
 
159
                                        w.WriteElementString("OutputPath", ns, @".\Help\");
 
160
                                        w.WriteElementString("HtmlHelpName", ns, "Documentation");
 
161
                                        
 
162
                                        w.WriteStartElement("DocumentationSources", ns);
 
163
                                        w.WriteStartElement("DocumentationSource", "");
 
164
                                        w.WriteAttributeString("sourceFile", FileUtility.GetRelativePath(Path.GetDirectoryName(sandcastleHelpFileBuilderProjectFile), project.FileName));
 
165
                                        w.WriteEndElement(); // </DocumentationSource>
 
166
                                        w.WriteEndElement(); // </DocumentationSources>
 
167
                                        
 
168
                                        w.WriteEndElement(); // </PropertyGrup>
 
169
                                        
 
170
                                        w.WriteComment("There are no properties for these groups.  AnyCPU needs to appear in\n" +
 
171
                                                       "order for Visual Studio to perform the build.  The others are optional\n" +
 
172
                                                       "common platform types that may appear.");
 
173
                                        string[] confPlatList = {
 
174
                                                "Debug|AnyCPU", "Release|AnyCPU", "Debug|x86", "Release|x86", "Debug|x64", "Release|x64", "Debug|Win32", "Release|Win32"
 
175
                                        };
 
176
                                        foreach (string confPlat in confPlatList) {
 
177
                                                w.WriteStartElement("PropertyGroup", ns);
 
178
                                                w.WriteAttributeString("Condition", " '$(Configuration)|$(Platform)' == '" + confPlat + "' ");
 
179
                                                w.WriteEndElement(); // </PropertyGrup>
 
180
                                        }
 
181
                                        
 
182
                                        w.WriteComment("Import the SHFB build targets");
 
183
                                        w.WriteStartElement("Import", ns);
 
184
                                        w.WriteAttributeString("Project", @"$(SHFBROOT)\SandcastleHelpFileBuilder.targets");
 
185
                                        w.WriteEndElement(); // </Import>
 
186
                                        
 
187
                                        w.WriteEndElement(); // </Project>
 
188
                                }
 
189
                        }
 
190
                        
 
191
                        ProcessStartInfo psi = new ProcessStartInfo(sandcastleHelpFileBuilderPath, '"' + sandcastleHelpFileBuilderProjectFile + '"');
 
192
                        psi.WorkingDirectory = Path.GetDirectoryName(sandcastleHelpFileBuilderPath);
 
193
                        psi.UseShellExecute = false;
 
194
                        Process.Start(psi);
 
195
                }
 
196
        }
 
197
        
 
198
        /// <summary>
 
199
        /// Opens the projects output folder in an explorer window.
 
200
        /// </summary>
 
201
        public class OpenProjectFolder : AbstractMenuCommand
 
202
        {
 
203
                public override void Run()
 
204
                {
 
205
                        IProject project = ProjectService.CurrentProject;
 
206
                        if (project == null) {
 
207
                                return;
 
208
                        }
 
209
                        
 
210
                        OpenFolder.OpenFolderInExplorer(project.Directory);
 
211
                }
 
212
        }
 
213
        
 
214
        /// <summary>
 
215
        /// Opens the projects output folder in an explorer window.
 
216
        /// </summary>
 
217
        public class OpenProjectOutputFolder : AbstractMenuCommand
 
218
        {
 
219
                public override void Run()
 
220
                {
 
221
                        CompilableProject project = ProjectService.CurrentProject as CompilableProject;
 
222
                        if (project == null) {
 
223
                                return;
 
224
                        }
 
225
                        
 
226
                        // Explorer does not handle relative paths as a command line argument properly
 
227
                        string outputFolder =  project.OutputFullPath;
 
228
                        if (!Directory.Exists(outputFolder)) {
 
229
                                Directory.CreateDirectory(outputFolder);
 
230
                        }
 
231
                        
 
232
                        OpenFolder.OpenFolderInExplorer(outputFolder);
 
233
                }
 
234
        }
 
235
}