Наша сборка Qt VS Tools
giy
2022-09-02 ca47896204482bf4a6979e3838bf7f09f61cebeb
QtVsTools.Core/ProjectImporter.cs
@@ -1,6 +1,6 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Copyright (C) 2022 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt VS Tools.
@@ -26,18 +26,19 @@
**
****************************************************************************/
using EnvDTE;
using Microsoft.VisualStudio.VCProjectEngine;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using Microsoft.VisualStudio.Shell;
using EnvDTE;
using Microsoft.VisualStudio.VCProjectEngine;
namespace QtVsTools.Core
{
    public class ProjectImporter
    {
        private DTE dteObject;
        private readonly DTE dteObject;
        const string projectFileExtension = ".vcxproj";
        public ProjectImporter(DTE dte)
@@ -47,6 +48,8 @@
        public void ImportProFile(string qtVersion)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            FileDialog toOpen = new OpenFileDialog();
            toOpen.FilterIndex = 1;
            toOpen.CheckFileExists = true;
@@ -78,6 +81,8 @@
        private void ImportSolution(FileInfo mainInfo, string qtVersion)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            var versionInfo = QtVersionManager.The().GetVersionInfo(qtVersion);
            var VCInfo = RunQmake(mainInfo, ".sln", true, versionInfo);
            if (null == VCInfo)
@@ -88,7 +93,7 @@
                if (CheckQtVersion(versionInfo)) {
                    dteObject.Solution.Open(VCInfo.FullName);
                    if (qtVersion != null) {
                        QtVersionManager.The().SaveSolutionQtVersion(dteObject.Solution, qtVersion);
                        Legacy.QtVersionManager.SaveSolutionQtVersion(dteObject.Solution, qtVersion);
                        foreach (var prj in HelperFunctions.ProjectsInSolution(dteObject)) {
                            QtVersionManager.The().SaveProjectQtVersion(prj, qtVersion);
                            var qtPro = QtProject.Create(prj);
@@ -100,12 +105,14 @@
                Messages.Print("--- (Import): Finished opening " + VCInfo.Name);
            } catch (Exception e) {
                Messages.DisplayCriticalErrorMessage(e);
                Messages.DisplayErrorMessage(e);
            }
        }
        public void ImportProject(FileInfo mainInfo, string qtVersion)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            var versionInfo = QtVersionManager.The().GetVersionInfo(qtVersion);
            var VCInfo = RunQmake(mainInfo, projectFileExtension, false, versionInfo);
            if (null == VCInfo)
@@ -116,9 +123,11 @@
            try {
                if (CheckQtVersion(versionInfo)) {
                    // no need to add the project again if it's already there...
                    if (!HelperFunctions.IsProjectInSolution(dteObject, VCInfo.FullName)) {
                    var fullName = VCInfo.FullName;
                    var pro = HelperFunctions.ProjectFromSolution(dteObject, fullName);
                    if (pro == null) {
                        try {
                            dteObject.Solution.AddFromFile(VCInfo.FullName, false);
                            pro = dteObject.Solution.AddFromFile(fullName, false);
                        } catch (Exception /*exception*/) {
                            Messages.Print("--- (Import): Generated project could not be loaded.");
                            Messages.Print("--- (Import): Please look in the output above for errors and warnings.");
@@ -129,13 +138,6 @@
                        Messages.Print("Project already in Solution");
                    }
                    Project pro = null;
                    foreach (var p in HelperFunctions.ProjectsInSolution(dteObject)) {
                        if (p.FullName.ToLower() == VCInfo.FullName.ToLower()) {
                            pro = p;
                            break;
                        }
                    }
                    if (pro != null) {
                        var qtPro = QtProject.Create(pro);
                        qtPro.SetQtEnvironment();
@@ -151,21 +153,16 @@
                                Messages.Print("Can't select the platform " + platformName + ".");
                        }
                        // try to figure out if the project is a plugin project
                        try {
                            var activeConfig = pro.ConfigurationManager.ActiveConfiguration.ConfigurationName;
                            var config = (VCConfiguration)((IVCCollection)qtPro.VCProject.Configurations).Item(activeConfig);
                            if (config.ConfigurationType == ConfigurationTypes.typeDynamicLibrary) {
                                var compiler = CompilerToolWrapper.Create(config);
                                var linker = (VCLinkerTool)((IVCCollection)config.Tools).Item("VCLinkerTool");
                                if (compiler.GetPreprocessorDefinitions().IndexOf("QT_PLUGIN", StringComparison.Ordinal) > -1
                                    && compiler.GetPreprocessorDefinitions().IndexOf("QDESIGNER_EXPORT_WIDGETS", StringComparison.Ordinal) > -1
                                    && compiler.GetAdditionalIncludeDirectories().IndexOf("QtDesigner", StringComparison.Ordinal) > -1
                                    && linker.AdditionalDependencies.IndexOf("QtDesigner", StringComparison.Ordinal) > -1) {
                                    qtPro.MarkAsDesignerPluginProject();
                                }
                            }
                        } catch (Exception) { }
                        // figure out if the imported project is a plugin project
                        var tmp = qtPro.Project.ConfigurationManager.ActiveConfiguration
                            .ConfigurationName;
                        var vcConfig = (qtPro.VCProject.Configurations as IVCCollection).Item(tmp)
                            as VCConfiguration;
                        var def = CompilerToolWrapper.Create(vcConfig)?.GetPreprocessorDefinitions();
                        if (!string.IsNullOrEmpty(def)
                            && def.IndexOf("QT_PLUGIN", StringComparison.Ordinal) > -1) {
                            QtProject.MarkAsQtPlugin(qtPro);
                        }
                        qtPro.SetQtEnvironment();
                        ApplyPostImportSteps(qtPro);
@@ -222,18 +219,19 @@
            if (ok)
                ok = xmlProject.UpdateProjectFormatVersion();
            if (!ok) {
                Messages.Print(
                    SR.GetString("ImportProject_CannotConvertProject", projectFile.Name));
            if (ok) {
                xmlProject.Save();
                // Initialize Qt variables
                xmlProject.BuildTarget("QtVarsDesignTime");
            } else {
                Messages.Print($"Could not convert project file {projectFile.Name} to Qt/MSBuild.");
            }
            xmlProject.Save();
            // Initialize Qt variables
            xmlProject.BuildTarget("QtVarsDesignTime");
        }
        private static void ApplyPostImportSteps(QtProject qtProject)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            qtProject.RemoveResFilesFromGeneratedFilesFilter();
            qtProject.TranslateFilterNames();
@@ -249,32 +247,38 @@
        private FileInfo RunQmake(FileInfo mainInfo, string ext, bool recursive, VersionInformation vi)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            var name = mainInfo.Name.Remove(mainInfo.Name.IndexOf('.'));
            var VCInfo = new FileInfo(mainInfo.DirectoryName + "\\" + name + ext);
            if (!VCInfo.Exists || DialogResult.Yes == MessageBox.Show(SR.GetString("ExportProject_ProjectExistsRegenerateOrReuse", VCInfo.Name),
                SR.GetString("ProjectExists"), MessageBoxButtons.YesNo, MessageBoxIcon.Question)) {
                Messages.Print("--- (Import): Generating new project of " + mainInfo.Name + " file");
                var waitDialog = WaitDialog.Start(
                    "Open Qt Project File",
                    "Generating Visual Studio project...", delay: 2);
                var qmake = new QMakeImport(vi, mainInfo.FullName, recursive, dteObject);
                int exitCode = qmake.Run(setVCVars: true);
                waitDialog.Stop();
                if (exitCode == 0)
                    return VCInfo;
            var vcxproj = new FileInfo(mainInfo.DirectoryName + "\\" + name + ext);
            if (vcxproj.Exists) {
                var result = MessageBox.Show($@"{vcxproj.Name} already exists. Select 'OK' to " +
                    "regenerate the file or 'Cancel' to quit importing the project.",
                    "Project file already exists.",
                    MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (result == DialogResult.Cancel)
                    return null;
            }
            Messages.Print("--- (Import): Generating new project of " + mainInfo.Name + " file");
            var waitDialog = WaitDialog.Start("Open Qt Project File",
                "Generating Visual Studio project...", delay: 2);
            var qmake = new QMakeImport(vi, mainInfo.FullName, recursive, dteObject);
            int exitCode = qmake.Run(setVCVars: true);
            waitDialog.Stop();
            if (exitCode == 0)
                return vcxproj;
            return null;
        }
        private static bool CheckQtVersion(VersionInformation vi)
        {
            if (!vi.qt5Version) {
            if (vi.qtMajor < 5) {
                Messages.DisplayWarningMessage(SR.GetString("ExportProject_EditProjectFileManually"));
                return false;
            }