ligershark / sidewafflev2

Other
30 stars 11 forks source link

Use custom Wizard with template #17

Closed slofre closed 6 years ago

slofre commented 6 years ago

Hi every one. I am trying to create multiple-project template with custom wizard to achieve next:

  1. Allow user enter project name.(and replace all default namespaces and file names)
  2. Allow user enter DB settings. (and replace default configs).

So, I follow this video to create Template pack https://www.youtube.com/watch?v=g6az_N95dVM&index=1&t=536s&list=PLqSOaIdv36hQdvGVkMXDJJ4Uh-nGj3NRK

After that I added new project with custom wizard(from msdn example): `public partial class TemplateWizard : IWizard { private WizardForm inputForm; private string customMessage;

    // 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()
    {
    }

    public void RunStarted(object automationObject,
        Dictionary<string, string> replacementsDictionary,
        WizardRunKind runKind, object[] customParams)
    {
        try
        {
            // Display a form to the user. The form collects   
            // input for the custom message.  
            inputForm = new WizardForm();
            inputForm.ShowDialog();

            customMessage = WizardForm.CustomMessage;

            // Add custom parameters.  
            replacementsDictionary.Add("$custommessage$",
                customMessage);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

    // This method is only called for item templates,  
    // not for project templates.  
    public bool ShouldAddProjectItem(string filePath)
    {
        return true;
    }
}` 

Then added it to Assets via Assembly type and updated .vstemplate with next: `

TestTemplateWizard, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
<FullClassName>TestTemplateWizard.TemplateWizard</FullClassName>

` And yes, I tried sign assembly and token was't null, but it's doesn't matter.

As a result I am able to build vsix project and I see template in "File-> New Project". Then I can enter name, solution name and click "Ok". Then I see my Wizard, enter some text and close it. After that I see created solution, but it's empty. If I use default Microsoft Wizard, template works as expected and I see all projects that I need in solution.

So, how can I allow users input some parameters and after that successfully create not empty solution? I don't want copy all files manually in my wizard if I can avoid that. Any help, useful links or direction would be appreciated, thanks.

sayedihashimi commented 6 years ago

@slofre thanks for the issue. If I understand correct you want to author a new Wizard which shows a WPF dialog which can accept some parameter value, and then you want to pass that parameter value to the template during creation.

I haven't tried this myself. I'll chat with @phenning and @mlorbetske to see what needs to be done and then create a sample/video.

sayedihashimi commented 6 years ago

OK I looked into it a bit and now have a working sample. The files are in this folder. The key here is the name parameter that is added. The name of the parameter should be in the format passthrough:PARAMETER_NAME where PARAMETER_NAME is the name of the parameter in template.json. For example for the copyrightName parameter the method call would look like the following.

replacementsDictionary.Add("passthrough:copyrightName", "value from wizard");

In the source.extension.vsixmanifest file make sure to list your wizard before the TemplateEngine wizard.

slofre commented 6 years ago

@sayedihashimi Thank you for quick response, your example was really helpful. Firstly I forgot to add both wizards in one time(custom and Microsoft) and second I did't know that I need to use "passthrough" with custom params. So, now it's working perfect. On more thing.. When I opened your sample and build that, I got an "Duplicate Assembly attribute" exceptions, so you can add next to .csproj to fix that:

<GenerateAssemblyInfo>false</GenerateAssemblyInfo>

Thank you one more time!