📄 createnantbuildfilecommand.cs
字号:
//
// SharpDevelop NAnt add-in.
//
// Copyright (C) 2004 Matthew Ward
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// Matthew Ward (mrward@users.sourceforge.net)
using ICSharpCode.Core.AddIns.Codons;
using ICSharpCode.Core.Services;
using ICSharpCode.NAntAddIn;
using ICSharpCode.SharpDevelop.Internal.Project;
using ICSharpCode.SharpDevelop.Gui;
using ICSharpCode.SharpDevelop.Gui.Pads.ProjectBrowser;
using ICSharpCode.SharpDevelop.Services;
using System;
using System.IO;
namespace ICSharpCode.NAntAddIn.Commands
{
/// <summary>
/// Represents the command to generate a NAnt build file
/// from a SharpDevelop project file.
/// </summary>
public class CreateNAntBuildFileCommand : AbstractMenuCommand
{
/// <summary>
/// Runs the <see cref="CreateNAntBuildFileCommand"/>.
/// </summary>
public override void Run()
{
// Get the currently selected active project.
IProject project = GetProjectSelected();
try {
string projectFileName = GetFileName(project);
string buildFileName = Path.ChangeExtension(projectFileName, Generator.NAntBuildFileExtension);
// Check if build file exists.
bool createBuildFile = true;
if (File.Exists(buildFileName)) {
// Ask user to confirm creation.
createBuildFile = SharpDevelopApplication.MessageService.AskQuestion(SharpDevelopApplication.StringParserService.Parse("${res:ICSharpCode.NAntAddIn.CreateNAntBuildFileCommand.OverwriteBuildFileQuestion}"));
}
// Create NAnt build file.
if (createBuildFile) {
// Close the build file view if it is open.
CloseView(buildFileName);
// Save combine if needed - this is a workaround
// to fix a combine that has a entry of the form ".\.\myproj.prjx".
// Saving the combine fixes the various relative paths
SaveOpenCombine();
Generator.CreateBuildFile(projectFileName, buildFileName, AddInOptions.StylesheetFileName);
}
// Add build file to project if it does not already exist.
if (createBuildFile) {
if (false == project.IsFileInProject(buildFileName)) {
AddBuildFileToProject(project, buildFileName);
}
}
// Display generated build file.
if (createBuildFile) {
OpenFile(buildFileName);
}
} catch (Exception ex) {
SharpDevelopApplication.MessageService.ShowError(ex, SharpDevelopApplication.StringParserService.Parse("${res:ICSharpCode.NAntAddIn.CreateNAntBuildFileCommand.CreateBuildFileErrorText}"));
}
}
/// <summary>
/// Gets the selected project.
/// </summary>
/// <returns>An IProject object.</returns>
IProject GetProjectSelected()
{
IProjectService projectService =
(IProjectService)ServiceManager.Services.GetService(typeof(IProjectService));
return projectService.CurrentSelectedProject;
}
/// <summary>
/// Gets the project's filename.
/// </summary>
/// <returns>The project's filename.</returns>
string GetFileName(IProject project)
{
IProjectService projectService =
(IProjectService)ServiceManager.Services.GetService(typeof(IProjectService));
return projectService.GetFileName(project);
}
/// <summary>
/// Adds the build file to the project.
/// </summary>
void AddBuildFileToProject(IProject project, string fileName)
{
IProjectService projectService =
(IProjectService)ServiceManager.Services.GetService(typeof(IProjectService));
ProjectFile projectFile =
projectService.AddFileToProject(project, fileName, BuildAction.Nothing);
projectService.SaveCombine();
RefreshProjectView();
}
/// <summary>
/// Refreshes the project view.
/// </summary>
void RefreshProjectView()
{
IWorkbench workbench = ICSharpCode.SharpDevelop.Gui.WorkbenchSingleton.Workbench;
IPadContent padContent = workbench.GetPad(typeof(ProjectBrowserView));
if (padContent != null) {
ProjectBrowserView projectView = (ProjectBrowserView)padContent;
IProjectService projectService =
(IProjectService)ServiceManager.Services.GetService(typeof(IProjectService));
projectView.RefreshTree(projectService.CurrentOpenCombine);
}
}
/// <summary>
/// Closes the view associated with the given filename.
/// </summary>
/// <param name="fileName">The name of the file.</param>
void CloseView(string fileName)
{
IWorkbench workbench = ICSharpCode.SharpDevelop.Gui.WorkbenchSingleton.Workbench;
foreach (IViewContent viewContent in workbench.ViewContentCollection) {
if (viewContent.FileName.ToLower() == fileName.ToLower()) {
IWorkbenchWindow window = viewContent.WorkbenchWindow;
window.CloseWindow(false);
break;
}
}
}
/// <summary>
/// Opens the specified file in the SharpDevelop IDE.
/// </summary>
/// <param name="fileName">The file to open.</param>
void OpenFile(string fileName)
{
IFileService fileService = (IFileService)ICSharpCode.Core.Services.ServiceManager.Services.GetService(typeof(IFileService));
fileService.OpenFile(fileName);
}
/// <summary>
/// Saves the combine if it has an combine entry of the form
/// ".\.\myproj.prjx". Saving the combine will fix all these
/// relative paths.</summary>
void SaveOpenCombine()
{
IProjectService projectService =
(IProjectService)ServiceManager.Services.GetService(typeof(IProjectService));
Combine openCombine = projectService.CurrentOpenCombine;
if (openCombine != null) {
// Check that the combine has any entries of the form
// ".\.\myproj.prjx".
bool saveCombine = false;
foreach (CombineEntry entry in openCombine.Entries) {
// Check for single occurrence of ".\" since the
// entry filenames are full paths. A single occurrence
// of ".\" indicates that the entry relative path
// was of the form ".\.\".
if (entry.Filename.IndexOf(".\\") != -1) {
saveCombine = true;
break;
}
}
if (saveCombine) {
projectService.SaveCombine();
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -