Ejemplo n.º 1
0
        public async Task BuildAsync(ProjectInfo projectInfo)
        {
            try
            {
                if (projectInfo.SelectedProject != null)
                {
                    string configName = dte.Solution.SolutionBuild.ActiveConfiguration.Name;
                    Microsoft.VisualStudio.VCProjectEngine.VCProject vcProj =
                        (Microsoft.VisualStudio.VCProjectEngine.VCProject)projectInfo.SelectedProject.DTEProject.Object;
                    Microsoft.VisualStudio.VCProjectEngine.IVCCollection configs =
                        (Microsoft.VisualStudio.VCProjectEngine.IVCCollection)vcProj.Configurations;
                    Microsoft.VisualStudio.VCProjectEngine.VCConfiguration config =
                        FindConfig(configs, configName);
                    var solutionName = Path.GetFileNameWithoutExtension(dte.Solution.FileName);

                    dte.Windows.Item(EnvDTE.Constants.vsWindowKindSolutionExplorer).Activate();
                    dte.ToolWindows.SolutionExplorer.GetItem($"{solutionName}\\{projectInfo.SelectedProject.DTEProject.Name}").Select(EnvDTE.vsUISelectionType.vsUISelectionTypeSelect);
                    m_buildCompletedEvent.Reset();
                    dte.Events.BuildEvents.OnBuildDone += BuildEvents_OnBuildDone;
                    dte.ExecuteCommand("ClassViewContextMenus.ClassViewProject.Build");
                    await Task.Run(() => m_buildCompletedEvent.WaitOne());
                }
            }
            catch (Exception ex)
            {
                WriteLine(1, "Could not start debugging\nEXCEPTION: " + ex.ToString());
            }
        }
Ejemplo n.º 2
0
        /// [start debugging]
        public void StartDebugging(ProjectInfo projectInfo, string in_cmdLineParams)
        {
            try
            {
                if (projectInfo.SelectedProject != null)
                {
                    //m_startupProj.ConfigurationManager.ActiveConfiguration.Properties.
                    //    Item("CommandArguments").Value = in_cmdLineParams;
                    string configName = dte.Solution.SolutionBuild.ActiveConfiguration.Name;
                    Microsoft.VisualStudio.VCProjectEngine.VCProject vcProj =
                        (Microsoft.VisualStudio.VCProjectEngine.VCProject)projectInfo.SelectedProject.DTEProject.Object;
                    Microsoft.VisualStudio.VCProjectEngine.IVCCollection configs =
                        (Microsoft.VisualStudio.VCProjectEngine.IVCCollection)vcProj.Configurations;
                    Microsoft.VisualStudio.VCProjectEngine.VCConfiguration config =
                        FindConfig(configs, configName);
                    Microsoft.VisualStudio.VCProjectEngine.VCDebugSettings dbgSettings =
                        (Microsoft.VisualStudio.VCProjectEngine.VCDebugSettings)config.DebugSettings;
                    dbgSettings.CommandArguments = in_cmdLineParams;
                    dbgSettings.Command          = projectInfo.GetExePath();
                    var solutionName = Path.GetFileNameWithoutExtension(dte.Solution.FileName);

                    dte.Windows.Item(EnvDTE.Constants.vsWindowKindSolutionExplorer).Activate();
                    dte.ToolWindows.SolutionExplorer.GetItem($"{solutionName}\\{projectInfo.SelectedProject.DTEProject.Name}").Select(EnvDTE.vsUISelectionType.vsUISelectionTypeSelect);

                    WriteLine(2, "StartDebugging: now starting debugger with dbgSettings.CommandArguments=" + dbgSettings.CommandArguments);
                    dte.ExecuteCommand("ClassViewContextMenus.ClassViewProject.Debug.Startnewinstance");
                    //dte.Debugger.Go(false /* do not wait for end of debugging*/);
                }
            }
            catch (Exception ex)
            {
                WriteLine(1, "Could not start debugging\nEXCEPTION: " + ex.ToString());
            }
        }
Ejemplo n.º 3
0
        private void AddProjectRecursive(EnvDTE.Project project, string configName, List <string> foundExecutables)
        {
            if (project == null)
            {
                return;
            }
            WriteLine(3, "AddProjectRecursive: found project.Name= " + project.Name + " project.Kind=" + project.Kind);

            if (project.Kind == EnvDTE.Constants.vsProjectKindSolutionItems)
            {
                WriteLine(3, "AddProjectRecursive: found solution item " + project.Name);

                foreach (EnvDTE.ProjectItem item in project.ProjectItems)
                {
                    EnvDTE.Project realProject = item.Object as EnvDTE.Project;

                    if (realProject != null)
                    {
                        AddProjectRecursive(realProject, configName, foundExecutables);
                    }
                }
            }
            else // may be real project
            {
                Microsoft.VisualStudio.VCProjectEngine.VCProject vcProj = (Microsoft.VisualStudio.VCProjectEngine.VCProject)project.Object;
                if (vcProj == null)
                {
                    WriteLine(3, "AddProjectRecursive/may be real project: vcProj == null");
                    return;
                }

                Microsoft.VisualStudio.VCProjectEngine.IVCCollection   configs = (Microsoft.VisualStudio.VCProjectEngine.IVCCollection)vcProj.Configurations;
                Microsoft.VisualStudio.VCProjectEngine.VCConfiguration config  = FindConfig(configs, configName);
                if (config == null)
                {
                    WriteLine(3, "AddProjectRecursive/may be real project: config == null");
                    return;
                }

                string outputPath = config.PrimaryOutput;
                outputPath.Trim();
                string tmpOutputPath = outputPath.ToLower();
                if ((tmpOutputPath.Length > 0) && (tmpOutputPath.IndexOf(".exe") == tmpOutputPath.Length - 4))
                {
                    WriteLine(3, "AddProjectRecursive: Found project: " + project.Name + " exe: " + outputPath);
                    foundExecutables.Add(outputPath);
                }
                else
                {
                    WriteLine(3, "AddProjectRecursive: Ignored project: " + project.Name + " is no exe: " + outputPath);
                }
            }
        }
Ejemplo n.º 4
0
        bool TryCompileSingleFile()
        {
            DTE DTE = UnrealVSPackage.Instance.DTE;

            // Activate the output window
            Window Window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput);

            Window.Activate();
            OutputWindow OutputWindow = Window.Object as OutputWindow;

            // Try to find the 'Build' window
            OutputWindowPane BuildOutputPane = null;

            foreach (OutputWindowPane Pane in OutputWindow.OutputWindowPanes)
            {
                if (Pane.Guid.ToUpperInvariant() == "{1BD8A850-02D1-11D1-BEE7-00A0C913D1F8}")
                {
                    BuildOutputPane = Pane;
                    break;
                }
            }
            if (BuildOutputPane == null)
            {
                return(false);
            }

            // If there's already a build in progress, offer to cancel it
            if (ChildProcess != null && !ChildProcess.HasExited)
            {
                if (MessageBox.Show("Cancel current compile?", "Compile in progress", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    KillChildProcess();
                    BuildOutputPane.OutputString("1>  Build cancelled.\r\n");
                }
                return(true);
            }

            // Check we've got a file open
            if (DTE.ActiveDocument == null)
            {
                return(false);
            }

            // Grab the current startup project
            IVsHierarchy ProjectHierarchy;

            UnrealVSPackage.Instance.SolutionBuildManager.get_StartupProject(out ProjectHierarchy);
            if (ProjectHierarchy == null)
            {
                return(false);
            }
            Project StartupProject = Utils.HierarchyObjectToProject(ProjectHierarchy);

            if (StartupProject == null)
            {
                return(false);
            }
            Microsoft.VisualStudio.VCProjectEngine.VCProject VCStartupProject = StartupProject.Object as Microsoft.VisualStudio.VCProjectEngine.VCProject;
            if (VCStartupProject == null)
            {
                return(false);
            }

            // Get the active configuration for the startup project
            Configuration ActiveConfiguration     = StartupProject.ConfigurationManager.ActiveConfiguration;
            string        ActiveConfigurationName = String.Format("{0}|{1}", ActiveConfiguration.ConfigurationName, ActiveConfiguration.PlatformName);

            Microsoft.VisualStudio.VCProjectEngine.VCConfiguration ActiveVCConfiguration = VCStartupProject.Configurations.Item(ActiveConfigurationName);
            if (ActiveVCConfiguration == null)
            {
                return(false);
            }

            // Get the NMake settings for this configuration
            Microsoft.VisualStudio.VCProjectEngine.VCNMakeTool ActiveNMakeTool = ActiveVCConfiguration.Tools.Item("VCNMakeTool");
            if (ActiveNMakeTool == null)
            {
                return(false);
            }

            // Save all the open documents
            DTE.ExecuteCommand("File.SaveAll");

            // Check it's a cpp file
            string FileToCompile = DTE.ActiveDocument.FullName;

            if (!FileToCompile.EndsWith(".c", StringComparison.InvariantCultureIgnoreCase) && !FileToCompile.EndsWith(".cpp", StringComparison.InvariantCultureIgnoreCase))
            {
                MessageBox.Show("Invalid file extension for single-file compile.", "Invalid Extension", MessageBoxButtons.OK);
                return(true);
            }

            // If there's already a build in progress, don't let another one start
            if (DTE.Solution.SolutionBuild.BuildState == vsBuildState.vsBuildStateInProgress)
            {
                if (MessageBox.Show("Cancel current compile?", "Compile in progress", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    DTE.ExecuteCommand("Build.Cancel");
                }
                return(true);
            }

            // Make sure any existing build is stopped
            KillChildProcess();

            // Set up the output pane
            BuildOutputPane.Activate();
            BuildOutputPane.Clear();
            BuildOutputPane.OutputString(String.Format("1>------ Build started: Project: {0}, Configuration: {1} {2} ------\r\n", StartupProject.Name, ActiveConfiguration.ConfigurationName, ActiveConfiguration.PlatformName));
            BuildOutputPane.OutputString(String.Format("1>  Compiling {0}\r\n", FileToCompile));

            // Set up event handlers
            DTE.Events.BuildEvents.OnBuildBegin += BuildEvents_OnBuildBegin;

            // Create a delegate for handling output messages
            DataReceivedEventHandler OutputHandler = (Sender, Args) => { if (Args.Data != null)
                                                                         {
                                                                             BuildOutputPane.OutputString("1>  " + Args.Data + "\r\n");
                                                                         }
            };

            // Get the build command line and escape any environment variables that we use
            string BuildCommandLine = ActiveNMakeTool.BuildCommandLine;

            BuildCommandLine = BuildCommandLine.Replace("$(SolutionDir)", Path.GetDirectoryName(UnrealVSPackage.Instance.SolutionFilepath).TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar);
            BuildCommandLine = BuildCommandLine.Replace("$(ProjectName)", VCStartupProject.Name);

            // Spawn the new process
            ChildProcess = new System.Diagnostics.Process();
            ChildProcess.StartInfo.FileName               = Path.Combine(Environment.SystemDirectory, "cmd.exe");
            ChildProcess.StartInfo.Arguments              = String.Format("/C {0} -singlefile=\"{1}\"", BuildCommandLine, FileToCompile);
            ChildProcess.StartInfo.WorkingDirectory       = Path.GetDirectoryName(StartupProject.FullName);
            ChildProcess.StartInfo.UseShellExecute        = false;
            ChildProcess.StartInfo.RedirectStandardOutput = true;
            ChildProcess.StartInfo.RedirectStandardError  = true;
            ChildProcess.StartInfo.CreateNoWindow         = true;
            ChildProcess.OutputDataReceived              += OutputHandler;
            ChildProcess.ErrorDataReceived += OutputHandler;
            ChildProcess.Start();
            ChildProcess.BeginOutputReadLine();
            ChildProcess.BeginErrorReadLine();
            return(true);
        }
Ejemplo n.º 5
0
        /// [disconnect from VS]


        public ProjectInfo ReadSettingsOfAllProjects()
        {
            ProjectInfo projectInfo = new ProjectInfo();

            WriteLine(3, "ReadSettingsOfAllProjects-Begin");
            try
            {
                if (dte == null)
                {
                    WriteLine(1, "dte is null (checking for projects is not possible)");
                    return(projectInfo);
                }
                if (dte.Solution == null)
                {
                    WriteLine(1, "dte.Solution is null (checking for projects is not possible)");
                    return(projectInfo);
                }
                if (dte.Solution.SolutionBuild == null)
                {
                    WriteLine(1, "dte.Solution.SolutionBuild is null (checking for projects is not possible)");
                    return(projectInfo);
                }
                if (dte.Solution.SolutionBuild.ActiveConfiguration == null)
                {
                    WriteLine(1, "dte.Solution.SolutionBuild.ActiveConfiguration is null (checking for projects is not possible)");
                    return(projectInfo);
                }

                projectInfo.solutionFullPath = dte.Solution.FileName;

                /// [get config name]
                //Get name of config (e.g. "Debug", "Release"
                string configName = dte.Solution.SolutionBuild.ActiveConfiguration.Name;
                /// [get config name]

                string msg = "";

                /// [get name startup project]
                EnvDTE80.SolutionBuild2 sb = (EnvDTE80.SolutionBuild2)dte.Solution.SolutionBuild;
                if (sb == null)
                {
                    WriteLine(1, "SolutionBuild is null (checking for projects is not possible)");
                    return(projectInfo);
                }

                // for further processing extract the name without path and extension
                // e.g. nameStartupProject = "TestRunner"
                // (extraction code not shown here)
                /// [get name startup project]

                // To simple solution which does not work for startup projects within a filter
                //EnvDTE.Project startupProj = dte.Solution.Item(msg);

                /// [get startup project]
                // Perform recursive search for the startup project through all solution filters:
                EnvDTE.Project[] projects = GetAllProjects(false);
                /// [get startup project]

                //iterate through main projects
                foreach (var dteProject in projects)
                {
                    var project = new Project();
                    project.DTEProject  = dteProject;
                    project.ProjectName = dteProject.Name;

                    var projectFiles = new List <FileInfo>();

                    foreach (EnvDTE.ProjectItem fileItem in GetAllProjectItemsFromKind(project.DTEProject, EnvDTE.Constants.vsProjectItemKindPhysicalFile))
                    {
                        for (short i = 0; i < fileItem.FileCount; i++)
                        {
                            var fileInfo = new FileInfo(fileItem.FileNames[i]);
                            projectFiles.Add(fileInfo);
                        }
                    }
                    project.ProjectFiles = projectFiles.Distinct().ToArray();

                    try
                    {
                        project.SourceDirPath = System.IO.Path.GetDirectoryName(dteProject.FullName);
                    } catch (Exception ex)
                    {
                        WriteLine(3, "ReadSettingsOfAllProjects EXCEPTION: " + ex.ToString());
                        continue;
                    }

                    EnvDTE.ConfigurationManager cm = dteProject.ConfigurationManager;
                    if (cm == null)
                    {
                        WriteLine(1, "No ConfigurationManager found");
                        WriteLine(3, "ReadSettingsOfAllProjects: no ConfigurationManager found");
                        continue;
                    }
                    if (cm.ActiveConfiguration == null)
                    {
                        WriteLine(1, "No ActiveConfiguration found");
                        WriteLine(3, "ReadSettingsOfAllProjects: no ActiveConfiguration found");
                        continue;
                    }
                    msg = "Platform=" + cm.ActiveConfiguration.PlatformName;
                    WriteLine(2, msg);

                    EnvDTE.Properties props = cm.ActiveConfiguration.Properties;
                    if (props != null)
                    {
                        WriteLine(2, "Now iterating over ActiveConfiguration.Properties...");

                        // Scan properties of ActiveConfiguration to be used for future extended requests
                        msg = "ReadSettingsOfStartupProject: ActiveConfiguration.Properties";
                        foreach (EnvDTE.Property p in props)
                        {
                            msg += "  " + p.Name;
                        }
                        WriteLine(2, msg);
                    }
                    /// [get exe path]
                    // Get full path of executable depending on found
                    // startup project and configuration (Debug/Release)
                    Microsoft.VisualStudio.VCProjectEngine.VCProject vcProj =
                        (Microsoft.VisualStudio.VCProjectEngine.VCProject)dteProject.Object;
                    Microsoft.VisualStudio.VCProjectEngine.IVCCollection configs =
                        (Microsoft.VisualStudio.VCProjectEngine.IVCCollection)vcProj.Configurations;
                    Microsoft.VisualStudio.VCProjectEngine.VCConfiguration config =
                        FindConfig(configs, configName);
                    if (config == null)
                    {
                        WriteLine(1, "Config " + configName + " not found");
                        continue;
                    }
                    msg = "PrimaryOutput (FullExePath)=" + config.PrimaryOutput;
                    WriteLine(2, msg);
                    /// [get exe path]
                    project.FullExePath = config.PrimaryOutput;
                    string delimiter  = "/\\";
                    int    posPathEnd = project.FullExePath.LastIndexOfAny(delimiter.ToCharArray());
                    if (posPathEnd > 0)
                    {
                        project.TargetDirPath = project.FullExePath.Substring(0, posPathEnd);
                    }
                    msg = "ReadSettingsOfAllProjects: OutputPath=" + project.TargetDirPath;
                    WriteLine(2, msg);

                    // Scan properties to be used for future extended requests
                    msg = "ReadSettingsOfAllProjects: startupProj.Properties";
                    foreach (EnvDTE.Property p in dteProject.Properties)
                    {
                        msg += "  " + p.Name;
                    }
                    WriteLine(3, msg);
                    projectInfo.AddProject(project);
                }
                projectInfo.config = configName;

                WriteLine(3, "ReadSettingsOfAllProjects-End");
            }
            catch (Exception ex)
            {
                WriteLine(1, "ReadSettingsOfAllProjects-End: EXCEPTION: " + ex.ToString());
            }
            return(projectInfo);
        }