2017-06-28 27 views
0

私は、この作品は完璧なDTEで自動化プロジェクトを作成しようが、私はnugetパッケージを追加することはできません...プロジェクトテンプレートDTEのVisual Studio 2017 nuget

オプション1(以下InstallNuGetPackageコード)

var componentModel = (IComponentModel)Package.GetGlobalService(typeof(SComponentModel)); 
//componentModel is always null 

私はこのnugetパッケージ

  • にNuGet.VisualStudio 4.0.0がインストールされている

そして、次のフレームワークを追加するには

  • Microsoft.VisualStudio.ComponentModelHost私はこの例を発見したが、動作していない15.0.0.0
  • Microsoft.VisualStudio.Shell.15.0 15.0.0.0

を参照します http://tylerhughes.info/archive/2015/05/06/installing-a-nuget-package-programmatically/

オプション2(追加自身package.config)

public string GetPackagesConfig() 
{ 
    var sb = new StringBuilder(); 
    sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); 
    sb.AppendLine("<packages>"); 
    sb.AppendLine("<package id=\"log4net\" version=\"2.0.8\" targetFramework=\"net461\" />"); 
    sb.AppendLine("</packages>"); 

    return sb.ToString(); 
    //Add file to project 
} 

Visual Studioの制御

...私はまたpackages.config XMLを作成してみましたが、その後、私は、このパッケージへの参照を持っていないとcsprojを編集する必要があります
var type = Type.GetTypeFromProgID("VisualStudio.DTE.15.0"); 
var obj = Activator.CreateInstance(type, true); 
this._applicationObject = (DTE2)obj; 

InstallNuGetPackage

public bool InstallNuGetPackage(EnvDTE.Project project, string package) 
{ 
    bool installedPkg = true; 
    try 
    { 
     var componentModel = (IComponentModel)Package.GetGlobalService(typeof(SComponentModel)); 

     IVsPackageInstallerServices installerServices = componentModel.GetService<IVsPackageInstallerServices>(); 
     if (!installerServices.IsPackageInstalled(project, package)) 
     { 
      var installer = componentModel.GetService<IVsPackageInstaller>(); 
      installer.InstallPackage(null, project, package, (System.Version)null, false); 
     } 
    } 
    catch (Exception ex) 
    { 
     installedPkg = false; 
    } 
    return installedPkg; 
} 

あなたはパッケージマネージャコンソールウィンドウを開き、インストール・パッケージをコマンドを送信することができます。この例では、プロジェクト

private void CreateProject(string projectSubFolder, string projectName) 
{ 
    Solution2 solution2; 
    string solutionFileFullName; 
    string solutionFolderFullName; 
    string projectFolderFullName; 

    try 
    { 
     solution2 = (Solution2)_applicationObject.Solution; 

     // Get the full name of the solution file 
     solutionFileFullName = solution2.FileName; 

     // Get the full name of the solution folder 
     solutionFolderFullName = Path.GetDirectoryName(solutionFileFullName); 

     // Compose the full name of the project folder 
     projectFolderFullName = Path.Combine(solutionFolderFullName, projectSubFolder); 
     if (!(projectFolderFullName.EndsWith("\\"))) 
     { 
      projectFolderFullName += "\\"; 
     } 

     var programfiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86); 
     var template = @"Microsoft Visual Studio\2017\Community\Common7\IDE\ProjectTemplates\CSharp\Windows\1033\ClassLibrary\csClassLibrary.vstemplate"; 
     var projectTemplateFileName = Path.Combine(programfiles, template); 

     // Add the project 
     solution2.AddFromTemplate(projectTemplateFileName, projectFolderFullName, projectName, false); 

     //Save 
     _applicationObject.Solution.SaveAs(_solutionFullFileName); 

    } 
    catch (Exception exception) 
    { 
     Log.Error(nameof(CreateProject), exception); 
    } 
} 

答えて

0

を作成します。

var packageManagerConsoleGuid = "{0AD07096-BBA9-4900-A651-0598D26F6D24}"; 
var window = this._visualStudioInstance.Windows.Item(packageManagerConsoleGuid); 
window.Activate(); 

var commandName = "View.PackageManagerConsole"; 
var nugetCommand = "install-package log4net -ProjectName DemoProject"; 

this._visualStudioInstance.ExecuteCommand(commandName, nugetCommand); 

私はあなたがここに Nager.TemplateBuilder

この例では、2つのnugetパッケージ

//Configure Project 
var demoProject = new ProjectInfo($"DemoProject", ProjectTemplate.WindowsClassicDesktopWindowsFormsApp); 
demoProject.NugetPackages = new List<string> { "System.ServiceModel.NetTcp", "System.Runtime.Serialization.Xml" }; 

//Configure Solution 
var folder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); 
var solutionInfo = new SolutionInfo("Test", folder); 
solutionInfo.ProjectInfos.Add(demoProject); 

//Start building machine 
var buildingMachine = new SolutionBuildingMachine(); 
buildingMachine.Build(solutionInfo); 
buildingMachine.Dispose(); 
でWindowsデスクトップアプリケーションを作成し、それを見つけたことができたプロジェクトでソリューションを作成自動化するプロジェクトを開発
関連する問題