예제 #1
0
        /// <summary>Run project output without debugging.</summary>
        /// <returns>true - if it's OK</returns>
        static bool StartNoDebug(EnvDTE.DTE dte)
        {
            var startupProjects = (object[])dte.Solution.SolutionBuild.StartupProjects;

            if (startupProjects.Length < 1)
                throw new ApplicationException("No startup projects.");

            var startupProjectFullName = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(dte.Solution.FullName), (string)startupProjects[0]));
            NemerleOAProject nemerleOAProject = GetProject(dte, startupProjectFullName) as NemerleOAProject;

            if (nemerleOAProject == null)
                return false;

            var projectNode = nemerleOAProject.Project.ProjectMgr;
            var currentConfigName = Utilities.GetActiveConfigurationName(nemerleOAProject);

            projectNode.SetConfiguration(currentConfigName);

            dte.ExecuteCommand("File.SaveAll", "");

            bool ok;
            projectNode.BuildTarget("Build", out ok);

            if (!ok)
            {
                var message = "There were build errors. Would you like to continue and run the last successful build?";
                OLEMSGICON icon = OLEMSGICON.OLEMSGICON_QUERY;
                OLEMSGBUTTON buttons = OLEMSGBUTTON.OLEMSGBUTTON_YESNO;
                OLEMSGDEFBUTTON defaultButton = OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST;
                var res = VsShellUtilities.ShowMessageBox(projectNode.Site,
                    message, NemerleConstants.ProductName, icon, buttons, defaultButton);

                if (res == NativeMethods.IDNO)
                    return true;
            }

            var path = projectNode.GetProjectProperty("StartProgram");

            if (string.IsNullOrEmpty(path))
                path = projectNode.GetOutputAssembly(currentConfigName);

            if (!File.Exists(path))
                throw new ApplicationException("Visual Studio cannot start debugging because the debug target '"
                    + path + "' is missing. Please build the project and retry, or set the OutputPath and AssemblyName properties appropriately to point at the correct location for the target assembly.");

            if (string.Compare(Path.GetExtension(path), ".dll", StringComparison.InvariantCultureIgnoreCase) == 0)
                throw new ApplicationException("A project with an Output Type of Class Library cannot be started directly.\nAlso you can't use DLL as debug target.\n\nIn order to debug this project, add an executable project to this solution which references the library project. Set the executable project as the startup project.");

            var isConsole = PEReader.IsConsole(path);

            var cmdArgs = projectNode.GetProjectProperty("CmdArgs");
            var workingDirectory = projectNode.GetProjectProperty("WorkingDirectory");

            if (!string.IsNullOrEmpty(workingDirectory) && !Path.IsPathRooted(workingDirectory))
                workingDirectory = Path.Combine(projectNode.BaseURI.AbsoluteUrl, workingDirectory);

            if (string.IsNullOrEmpty(workingDirectory))
                workingDirectory = Path.GetDirectoryName(path);

            if (!Directory.Exists(workingDirectory))
                throw new ApplicationException("The working directory '" + workingDirectory + "' not exists.");

            var psi = new ProcessStartInfo();
            string cmdFilePath = null;

            if (isConsole)
            {
                // Make temp cmd-file and run it...
                cmdFilePath = Path.Combine(Path.GetDirectoryName(path), Path.GetTempFileName());
                cmdFilePath = Path.ChangeExtension(cmdFilePath, "cmd");
                var oemCodePage = CultureInfo.CurrentCulture.TextInfo.OEMCodePage;
                var consoleEncoding = Encoding.GetEncoding(oemCodePage);
                File.WriteAllText(cmdFilePath, "@echo off\n\"" + path + "\" " + cmdArgs + "\npause",
                        consoleEncoding);
                psi.FileName = cmdFilePath;
            }
            else
            {
                psi.FileName = path;
                psi.Arguments = cmdArgs;
            }

            psi.WorkingDirectory = workingDirectory;

            var process = OsProcess.Start(psi);

            if (isConsole)
                DeleteTempCmdFile(process, cmdFilePath);

            return true;
        }
        private void AddToolBoxItem(EnvDTE.DTE env)
        {
            EnvDTE.Window toolboxWindow = env.Windows.Item(EnvDTE.Constants.vsWindowKindToolbox);
            EnvDTE.ToolBox toolbox = (EnvDTE.ToolBox)toolboxWindow.Object;
            EnvDTE.ToolBoxTabs toolboxTabs = toolbox.ToolBoxTabs;

            EnvDTE.ToolBoxTab newTab = null;

            bool tabExists = false;
            foreach (EnvDTE.ToolBoxTab tab in toolboxTabs)
            {
                if (tab.Name == this.TabName)
                {
                    newTab = tab;
                    tabExists = true;
                    break;
                }
            }

            if (!tabExists)
            {
                newTab = toolboxTabs.Add(this.TabName);
            }

            newTab.Activate();
            env.ExecuteCommand("View.PropertiesWindow", "");
            newTab.ToolBoxItems.Item(1).Select();

            newTab.ToolBoxItems.Add(this.ComponentName, this.ComponentPath,
                EnvDTE.vsToolBoxItemFormat.vsToolBoxItemFormatDotNETComponent);

            env.Quit();
        }