Beispiel #1
0
        /// <summary>
        /// Load all available snippets from disk
        /// </summary>
        private static void LoadSnippets()
        {
            // Get the embedded resource stream
            Assembly assembly = Assembly.GetExecutingAssembly();

            using (Stream snipsStream = assembly.GetManifestResourceStream(SnippetsResourceName))
            {
                if (snipsStream != null)
                {
                    using (StreamReader reader = new StreamReader(snipsStream))
                    {
                        loadedSnippets = new Dictionary <string, Snippet>();
                        string        currentLine    = string.Empty;
                        Snippet       currentSnip    = null;
                        StringBuilder snippetBuilder = new StringBuilder();

                        // Read all snippets
                        while (!reader.EndOfStream)
                        {
                            currentLine = reader.ReadLine();

                            // Check for start of a new snippet
                            if (currentLine.StartsWith("#snippet", System.StringComparison.OrdinalIgnoreCase))
                            {
                                // Save loaded snip
                                if (currentSnip != null)
                                {
                                    currentSnip.Code = snippetBuilder.ToString();
                                    loadedSnippets.Add(currentSnip.Tag, currentSnip);
                                }

                                //Start new snip
                                currentSnip     = new Snippet();
                                currentSnip.Tag = currentLine.Split('-')[1].Trim();

                                snippetBuilder.Clear();
                            }
                            else if (currentLine.StartsWith("#namespace", System.StringComparison.OrdinalIgnoreCase) && currentSnip != null)
                            {
                                currentSnip.Namespaces.Add(currentLine.Split('-')[1].Trim());
                            }
                            else if (currentLine.StartsWith("#assembly", System.StringComparison.OrdinalIgnoreCase) && currentSnip != null)
                            {
                                currentSnip.Assembly = currentLine.Split('-')[1].Trim();
                            }
                            else
                            {
                                // Add this line to the snip
                                snippetBuilder.AppendLine(currentLine);
                            }
                        }

                        // Save last loaded snip
                        if (currentSnip != null)
                        {
                            currentSnip.Code = snippetBuilder.ToString();
                            loadedSnippets.Add(currentSnip.Tag, currentSnip);
                        }
                    }
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Called by VS before initiating any other project creation activity.
        /// Any customizations / wizards goes in here.
        /// </summary>
        /// <param name="automationObject">VS application object. (DTE object)</param>
        /// <param name="replacementsDictionary">Dictionary which holds name-value pairs to make replacements of placeholders in any project item</param>
        /// <param name="runKind">Context of item creation. (ex: project / project item)</param>
        /// <param name="customParams">Any other environment variables set by VS</param>
        public void RunStarted(object automationObject, Dictionary <string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
        {
            if (replacementsDictionary == null)
            {
                replacementsDictionary = new Dictionary <string, string>();
            }

            // Show the wizard
            using (WizardForm wizard = new WizardForm())
            {
                Application.EnableVisualStyles();
                wizard.ShowDialog();

                // Replace placeholder for MBF reference.
                replacementsDictionary["$MBFReferencePath$"] = wizard.MBFAssemblyPath + @"mbf.dll";

                // Load snippets and push it to code file
                StringBuilder finalSnips = new StringBuilder();
                List <string> namespaces = new List <string>();
                List <string> assemblies = new List <string>();

                // Get what snippets are selected by user
                foreach (string tag in wizard.SnippetTags)
                {
                    Snippet currentSnip = Snippets.GetSnippet(tag);
                    if (currentSnip != null)
                    {
                        // Get the code snippet
                        finalSnips.Append(currentSnip.Code);

                        // Add any namespaces required
                        foreach (string namespaceRef in currentSnip.Namespaces)
                        {
                            if (!namespaces.Contains(namespaceRef))
                            {
                                namespaces.Add(namespaceRef);
                            }
                        }

                        // Check for any assembly references
                        if (!string.IsNullOrEmpty(currentSnip.Assembly))
                        {
                            assemblies.Add(currentSnip.Assembly);
                        }
                    }
                }

                // Add code snippets to file
                replacementsDictionary["$CodeSnippets$"] = finalSnips.ToString().Trim();

                // Add necessary namespaces to file
                StringBuilder finalNamespaces = new StringBuilder();
                foreach (string namespaceRef in namespaces)
                {
                    finalNamespaces.AppendLine("using " + namespaceRef);
                }
                replacementsDictionary["$MBFNamespaces$"] = finalNamespaces.ToString().Trim();

                // TODO: find this path from registry
                // Add additional assemblies if required
                StringBuilder finalAssemblies = new StringBuilder();
                if (assemblies.Contains("WebServiceHandlers"))
                {
                    string assemblyReference = "<Reference Include=\"MBF.WebServiceHandlers\">" + Environment.NewLine +
                                               @"<HintPath>" + wizard.MBFAssemblyPath + @"MBF.WebServiceHandlers.dll</HintPath>" + Environment.NewLine +
                                               "</Reference>";
                    finalAssemblies.AppendLine(assemblyReference);
                }
                if (assemblies.Contains("PAMSAM"))
                {
                    string assemblyReference = "<Reference Include=\"MBF.PAMSAM\">" + Environment.NewLine +
                                               @"<HintPath>" + wizard.MBFAssemblyPath + @"Addin\MBF.PAMSAM.dll</HintPath>" + Environment.NewLine +
                                               "</Reference>";
                    finalAssemblies.AppendLine(assemblyReference);
                }
                if (assemblies.Contains("PaDeNA"))
                {
                    string assemblyReference = "<Reference Include=\"MBF.PaDeNA\">" + Environment.NewLine +
                                               @"<HintPath>" + wizard.MBFAssemblyPath + @"Addin\MBF.PaDeNA.dll</HintPath>" + Environment.NewLine +
                                               "</Reference>";
                    finalAssemblies.AppendLine(assemblyReference);
                }
                replacementsDictionary["$OtherAssemblies$"] = finalAssemblies.ToString().Trim();
            }
        }