locked
Template: Manually calling Nuget wizard does not add in the packages RRS feed

  • Question

  • I have a multi-project template I am creating for my company.  It is all working great except the nuget packages.  

    Also, I have my own wizard that I need to have run as well as the Nuget wizard.  

    I setup the nuget package stuff following the documentation found here:  https://docs.microsoft.com/en-us/nuget/visual-studio-extensibility/visual-studio-templates.  But try as I might, I could not get it to work.

    I put in my wizard and the nuget wizard in the vstemplate file, but it does not seem to run the nuget wizard. (This is observed by the packages folder not being created, and the nuget references are "broken".)

    As I was trying to debug this, I found this post that said you can only have one <WizardExtension> element.  It indicated that if you need the Nuget wizard and a custom one, that you need to manually call the Nuget wizard from your wizard.

    I set this up like this:

    private bool isRunningNuget = false;
    
    public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
    {
    	// If package data was supplied then run the nuget wizard
    	string wizardDataString;
    	var hasWizardData = replacementsDictionary.TryGetValue("WizardData", out wizardDataString);
    	if (hasWizardData && wizardDataString.Contains("<packages"))
    	{
    		isRunningNuget = true;
    		nugetWizard = this.GetNuGetWizard();
    		nugetWizard.RunStarted(automationObject, replacementsDictionary, runKind, customParams);
    	}
    
    	// Then run my wizard stuff		
    }
    
    
    public bool ShouldAddProjectItem(string filePath)
    {
    	if (isRunningNuget)
    		return nugetWizard.ShouldAddProjectItem(filePath);
    	else
    		return true;
    }
    
    public void RunFinished()
    {
    	if (isRunningNuget) nugetWizard.RunFinished();
    }
    
    public void BeforeOpeningFile(ProjectItem projectItem)
    {
    	if (isRunningNuget) nugetWizard.BeforeOpeningFile(projectItem);
    }
    
    public void ProjectItemFinishedGenerating(ProjectItem projectItem)
    {
    	if (isRunningNuget) nugetWizard.ProjectItemFinishedGenerating(projectItem);
    }
    
    public void ProjectFinishedGenerating(Project project)
    {
    	if (isRunningNuget) nugetWizard.ProjectFinishedGenerating(project);
    }
    
    private IWizard GetNuGetWizard()
    {
    	Assembly asm = Assembly.Load("NuGet.VisualStudio.Interop, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=b03f5f7f11d50a3a");
    	var wizard = (IWizard)asm.CreateInstance("NuGet.VisualStudio.TemplateWizard");
    
    	return wizard;
    }


    It all runs without errors, but my project still does not have any nuget packages installed.

    I have made sure I have a dependency listed in for the Nuget wizard, and I have tried removing the Packages.config file to see if that was conflicting.

    Any ideas of what else I can try to get my nuget packages to install for my sub-projects?

    Incase it is helpful, here is how my sub-project templates are setup (in relation to my wizard):

    <WizardExtension>
        <Assembly>MyVsixProject, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=53745e0ee077d560</Assembly>
        <FullClassName>MyVsixProject.WizardImplementation</FullClassName>
    </WizardExtension>
    <WizardData>
        <packages repository="extension" repositoryId="MyVsixProject.7b0b584a-d7cb-46a8-1317-84e1ea273a08">
            <package id="AutoMapper" version="5.2.0" />
            <package id="EntityFramework" version="6.1.3" />
            <package id="Microsoft.Net.Compilers" version="1.3.2" />
            <package id="SimpleInjector" version="3.2.0" />            
        </packages>
    </WizardData>

    And I have the matching nupkg files in my vsix under a folder called Packages.  I have them declared as assets like this:

    <Asset Type="AutoMapper.5.2.0.nupkg" d:Source="File" Path="Packages\AutoMapper.5.2.0.nupkg" d:VsixSubPath="Packages" />
    I am really confused here and would really appreciate some help.

    Friday, February 24, 2017 12:21 AM

All replies