Visual Studio 2017 bietet integrierte Unterstützung für die Abwicklung von CMake-Projekten. Die Dokumentation deckt hauptsächlich Szenarien ab, die auf bereits bestehenden cmake-Projekten basieren. Aber gibt es Unterstützung für die Erstellung eines cmake-Projekts, ohne mit der CMakeLists.txt-Datei arbeiten zu müssen?
Mit Version 15.6 kam die Funktion "Erstellen von CMake-Projekten über das Dialogfeld" Neues Projekt hinzufügen ".
Dadurch wird ein einfaches ninja - basiertes C++ "Hello CMake" -Projekt erstellt.
Ihre Frage und das Fehlen eines bestehenden Wizard inspirierten mich dazu, eine zu schreiben. Es ist eine sehr grundlegende Einrichtung und würde auf jeden Fall von Vorteil sein, wenn Personen mit mehr Erfahrung im Schreiben von Visual Studio-Erweiterungen dazu beitragen würden, aber hier ist es:
https://github.com/FloriansGit/VSCMakeWizards
Edit: Das neueste VSIX-Installationsprogramm ist jetzt auch auf VS Marketplace kostenlos verfügbar
https://marketplace.visualstudio.com/items?itemName=oOFlorianOo.CMakeProjectWizards
Die neue "CMake Executable Template" wird nach einem Neustart von Visual Studio 2017 unter "Datei/Neu/Projekt/Visual C++" angezeigt:
Es generiert die folgenden Dateien im angegebenen Ordner und verwendet dann "Open Folder" (Ordner öffnen):
CMakeLists.txt
CMakeSettings.json
MyProject1.cpp
Mögliche nächste Schritte wären:
CMakeLists.txt
hinzufügen zu können.Ich freue mich auf Feedback zur Grundidee. Bitte fügen Sie alle Anfragen direkt zu:
https://github.com/FloriansGit/VSCMakeWizards/issues
Und hier ist der Wizards Basic/Initial Code als Referenz:
WizardImplementationClass.cs
// Based on https://docs.Microsoft.com/en-us/visualstudio/extensibility/how-to-use-wizards-with-project-templates
// and https://stackoverflow.com/questions/3882764/issue-with-visual-studio-template-directory-creation
using System;
using System.IO;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using EnvDTE;
using Microsoft.VisualStudio.TemplateWizard;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using EnvDTE80;
namespace VSCMakeWizards
{
public class WizardImplementation : IWizard
{
public void RunStarted(object automationObject,
Dictionary<string, string> replacementsDictionary,
WizardRunKind runKind, object[] customParams)
{
var destinationDir = replacementsDictionary["$destinationdirectory$"];
var desiredNamespace = replacementsDictionary["$safeprojectname$"];
var templatePath = Path.GetDirectoryName((string)customParams[0]);
var dte = automationObject as DTE2;
var solution = dte.Solution as EnvDTE100.Solution4;
if (solution.IsOpen)
{
solution.Close();
}
File.Copy(Path.Combine(templatePath, "CMakeSettings.json"), Path.Combine(destinationDir, "CMakeSettings.json"));
File.Copy(Path.Combine(templatePath, "main.cpp"), Path.Combine(destinationDir, desiredNamespace + ".cpp"));
// see https://stackoverflow.com/questions/1231768/c-sharp-string-replace-with-dictionary
Regex re = new Regex(@"(\$\w+\$)", RegexOptions.Compiled);
string input = File.ReadAllText(Path.Combine(templatePath, "CMakeLists.txt"));
string output = re.Replace(input, match => replacementsDictionary[match.Groups[1].Value]);
File.WriteAllText(Path.Combine(destinationDir, "CMakeLists.txt"), output);
var vsSolution = Package.GetGlobalService(typeof(SVsSolution)) as IVsSolution7;
if (vsSolution != null)
{
vsSolution.OpenFolder(destinationDir);
}
throw new WizardCancelledException();
}
// This method is called before opening any item that
// has the OpenInEditor attribute.
public void BeforeOpeningFile(ProjectItem projectItem)
{
}
public void ProjectFinishedGenerating(Project project)
{
}
// This method is only called for item templates,
// not for project templates.
public void ProjectItemFinishedGenerating(ProjectItem
projectItem)
{
}
// This method is called after the project is created.
public void RunFinished()
{
}
// This method is only called for item templates,
// not for project templates.
public bool ShouldAddProjectItem(string filePath)
{
return false;
}
}
}
Hinweis: Die WizardCancelledException
ist erforderlich, da Visual Studio andernfalls versuchen würde, eine tatsächliche Lösung zu generieren/zu öffnen. Ein Projekt-Assistent vom Typ "Open Folder" wird noch nicht unterstützt (dazu keine SDK-API).
Soweit ich weiß, gibt es kein Wizard, um ein neues CMake-Projekt zu erstellen. Dies kann jedoch durch Konfigurieren einer CMakeSettings.json
-Datei erfolgen. https://blogs.msdn.Microsoft.com/vcblog/2017/08/14/cmake-support-in-visual-studio-customizing-your-environment/