private static string BuildShapePropertyEditorsReplacement(ContentPartWizardContext context)
        {
            if (!context.PropertyItems.Any(property => !property.SkipFromShapeTemplate))
            {
                return("");
            }

            var templatePath = Path.Combine(CodeTemplateLocation, "shapepropertyeditor.template");

            var template = File.Exists(templatePath) ? File.ReadAllText(templatePath) : "";

            var finalReplacementsList = new List <string>();

            foreach (var item in context.PropertyItems.Where(property => !property.SkipFromShapeTemplate))
            {
                var finalReplacement = template.Replace("#propertyname#", item.Name);
                if (item.Type == "bool")
                {
                    finalReplacement = finalReplacement.Replace("#editortype#", "CheckBox");
                }
                else if (item.Type == "string")
                {
                    finalReplacement = finalReplacement.Replace("#editortype#", "TextBox");
                }
                else
                {
                    finalReplacement = finalReplacement.Replace("#editortype#", "Editor");
                }

                finalReplacementsList.Add(finalReplacement);
            }

            return(string.Join(Environment.NewLine, finalReplacementsList));
        }
        private static string BuildInfosetPropertiesReplacement(ContentPartWizardContext context)
        {
            if (!context.PropertyItems.Any())
            {
                return("");
            }

            var infosetPropertyTemplatePath       = Path.Combine(CodeTemplateLocation, "infoset.template");
            var hybridInfosetPropertyTemplatePath = Path.Combine(CodeTemplateLocation, "hybridinfoset.template");

            var infosetPropertyTemplate       = File.Exists(infosetPropertyTemplatePath) ? File.ReadAllText(infosetPropertyTemplatePath) : "";
            var hybridInfosetPropertyTemplate = File.Exists(hybridInfosetPropertyTemplatePath) ? File.ReadAllText(hybridInfosetPropertyTemplatePath) : "";

            var finalPropertiesList = new List <string>();

            foreach (var item in context.PropertyItems)
            {
                var finalProperty = item.HybridInfoset ? hybridInfosetPropertyTemplate.Replace("#propertyname#", item.Name) : infosetPropertyTemplate.Replace("#propertyname#", item.Name);
                finalProperty = finalProperty.Replace("#propertytype#", item.Type);

                finalPropertiesList.Add(finalProperty);
            }

            return(string.Join(Environment.NewLine + Environment.NewLine, finalPropertiesList));
        }
        private static string BuildShapePropertyDisplaysReplacement(ContentPartWizardContext context)
        {
            if (!context.PropertyItems.Any(property => !property.SkipFromShapeTemplate))
            {
                return("");
            }

            var templatePath = Path.Combine(CodeTemplateLocation, "shapepropertydisplay.template");

            var template = File.Exists(templatePath) ? File.ReadAllText(templatePath) : "";

            var finalReplacementsList = new List <string>();

            foreach (var item in context.PropertyItems.Where(property => !property.SkipFromShapeTemplate))
            {
                var finalReplacement = template.Replace("#propertyname#", item.Name);

                finalReplacementsList.Add(finalReplacement);
            }

            return(string.Join(Environment.NewLine, finalReplacementsList));
        }
        private static string BuildMigrationsRecordIndexesReplacement(ContentPartWizardContext context)
        {
            if (!context.PropertyItems.Any(property => property.HybridInfoset))
            {
                return("");
            }

            var templatePath = Path.Combine(CodeTemplateLocation, "migrationsrecordindex.template");

            var template = File.Exists(templatePath) ? File.ReadAllText(templatePath) : "";

            var finalReplacementsList = new List <string>();

            foreach (var item in context.PropertyItems.Where(property => property.HybridInfoset))
            {
                var finalReplacement = template
                                       .Replace("#propertyname#", item.Name)
                                       .Replace("#recordname#", context.FullContentPartName + "Record");

                finalReplacementsList.Add(finalReplacement);
            }

            return(string.Join(Environment.NewLine, finalReplacementsList));
        }
        public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
        {
            try
            {
                // Create and initialize the context first.
                _context = new ContentPartWizardContext();

                var dte = automationObject as DTE;

                if (dte == null)
                {
                    MessageBox.Show("Something unexpected happened. Couldn't create the items.", "Create items", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    _context.Cancelled = true;

                    return;
                }

                var activeProjects = (Array)dte.ActiveSolutionProjects;

                if (activeProjects.Length > 0)
                {
                    var activeProject = (Project)activeProjects.GetValue(0);

                    _context.TargetProjectPath = Path.GetDirectoryName(activeProject.FullName);
                }

                var serviceProvider = new ServiceProvider(dte as Microsoft.VisualStudio.OLE.Interop.IServiceProvider);
                if (serviceProvider != null)
                {
                    var vsActivityLog = serviceProvider.GetService(typeof(SVsActivityLog)) as IVsActivityLog;

                    if (vsActivityLog != null)
                    {
                        _logger = new VisualStudioActivityLogger(vsActivityLog);
                    }
                }

                // Gather additional information from the user.
                using (var dialog = new ContentPartWizardDialog())
                {
                    var dialogResult = dialog.ShowDialog();

                    // The form has been filled.
                    if (dialogResult == DialogResult.OK)
                    {
                        _context.PropertyItems = dialog.PropertyItems;
                        _context.UpdatePlacementInfoIfExists = dialog.UpdatePlacementInfoIfExists;
                    }
                    // It was cancelled so don't create any file in the project.
                    else if (dialogResult == DialogResult.Cancel)
                    {
                        _context.Cancelled = true;

                        return;
                    }
                    // It was skipped so use default values.
                    else
                    {
                        _context.UpdatePlacementInfoIfExists = DefaultUpdatePlacementIfExists;
                    }
                }

                // Create the necessary replacements.
                var safeItemName = string.Empty;
                replacementsDictionary.TryGetValue("$safeitemname$", out safeItemName);

                _context.ContentPartNameWithoutSuffix = safeItemName.TrimEnd("Part");
                _context.FullContentPartName = _context.ContentPartNameWithoutSuffix + "Part";

                var settingsPartName = _context.ContentPartNameWithoutSuffix.TrimEnd("Settings");

                // Add custom parameters.
                replacementsDictionary.Add("$contentpartname$", _context.ContentPartNameWithoutSuffix);
                replacementsDictionary.Add("$settingscontentpartname$", settingsPartName);

                replacementsDictionary.Add("$infosetproperties$", BuildInfosetPropertiesReplacement(_context));
                replacementsDictionary.Add("$virtualproperties$", BuildVirtualPropertiesReplacement(_context));
                replacementsDictionary.Add("$shapepropertyeditors$", BuildShapePropertyEditorsReplacement(_context));
                replacementsDictionary.Add("$shapepropertydisplays$", BuildShapePropertyDisplaysReplacement(_context));
                replacementsDictionary.Add("$migrationsrecordproperties$", BuildMigrationsRecordPropertiesReplacement(_context));
                replacementsDictionary.Add("$migrationsrecordindexes$", BuildMigrationsRecordIndexesReplacement(_context));
            }
            catch (Exception ex)
            {
                _logger.Error(this.ToString(), "Couldn't start generating items. Exception: " + ex.ToString());

                MessageBox.Show("Couldn't start generating items because of an unexpected exception. For further information check the activity log.", "Create items", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private static string BuildVirtualPropertiesReplacement(ContentPartWizardContext context)
        {
            if (!context.PropertyItems.Any(property => property.HybridInfoset))
                return "";

            var virtualPropertyTemplatePath = Path.Combine(CodeTemplateLocation, "virtualproperty.template");

            var virtualPropertyTemplate = File.Exists(virtualPropertyTemplatePath) ? File.ReadAllText(virtualPropertyTemplatePath) : "";

            var finalPropertiesList = new List<string>();
            foreach (var item in context.PropertyItems.Where(property => property.HybridInfoset))
            {
                var finalProperty = virtualPropertyTemplate
                    .Replace("#propertyname#", item.Name)
                    .Replace("#propertytype#", item.Type);

                finalPropertiesList.Add(finalProperty);
            }

            return string.Join(Environment.NewLine, finalPropertiesList);
        }
        private static string BuildShapePropertyEditorsReplacement(ContentPartWizardContext context)
        {
            if (!context.PropertyItems.Any(property => !property.SkipFromShapeTemplate))
                return "";

            var templatePath = Path.Combine(CodeTemplateLocation, "shapepropertyeditor.template");

            var template = File.Exists(templatePath) ? File.ReadAllText(templatePath) : "";

            var finalReplacementsList = new List<string>();
            foreach (var item in context.PropertyItems.Where(property => !property.SkipFromShapeTemplate))
            {
                var finalReplacement = template.Replace("#propertyname#", item.Name);
                if (item.Type == "bool")
                {
                    finalReplacement = finalReplacement.Replace("#editortype#", "CheckBox");
                }
                else if (item.Type == "string")
                {
                    finalReplacement = finalReplacement.Replace("#editortype#", "TextBox");
                }
                else
                {
                    finalReplacement = finalReplacement.Replace("#editortype#", "Editor");
                }

                finalReplacementsList.Add(finalReplacement);
            }

            return string.Join(Environment.NewLine, finalReplacementsList);
        }
        private static string BuildShapePropertyDisplaysReplacement(ContentPartWizardContext context)
        {
            if (!context.PropertyItems.Any(property => !property.SkipFromShapeTemplate))
                return "";

            var templatePath = Path.Combine(CodeTemplateLocation, "shapepropertydisplay.template");

            var template = File.Exists(templatePath) ? File.ReadAllText(templatePath) : "";

            var finalReplacementsList = new List<string>();
            foreach (var item in context.PropertyItems.Where(property => !property.SkipFromShapeTemplate))
            {
                var finalReplacement = template.Replace("#propertyname#", item.Name);

                finalReplacementsList.Add(finalReplacement);
            }

            return string.Join(Environment.NewLine, finalReplacementsList);
        }
        private static string BuildMigrationsRecordIndexesReplacement(ContentPartWizardContext context)
        {
            if (!context.PropertyItems.Any(property => property.HybridInfoset))
                return "";

            var templatePath = Path.Combine(CodeTemplateLocation, "migrationsrecordindex.template");

            var template = File.Exists(templatePath) ? File.ReadAllText(templatePath) : "";

            var finalReplacementsList = new List<string>();
            foreach (var item in context.PropertyItems.Where(property => property.HybridInfoset))
            {
                var finalReplacement = template
                    .Replace("#propertyname#", item.Name)
                    .Replace("#recordname#", context.FullContentPartName + "Record");

                finalReplacementsList.Add(finalReplacement);
            }

            return string.Join(Environment.NewLine, finalReplacementsList);
        }
        private static string BuildInfosetPropertiesReplacement(ContentPartWizardContext context)
        {
            if (!context.PropertyItems.Any())
                return "";

            var infosetPropertyTemplatePath = Path.Combine(CodeTemplateLocation, "infoset.template");
            var hybridInfosetPropertyTemplatePath = Path.Combine(CodeTemplateLocation, "hybridinfoset.template");

            var infosetPropertyTemplate = File.Exists(infosetPropertyTemplatePath) ? File.ReadAllText(infosetPropertyTemplatePath) : "";
            var hybridInfosetPropertyTemplate = File.Exists(hybridInfosetPropertyTemplatePath) ? File.ReadAllText(hybridInfosetPropertyTemplatePath) : "";

            var finalPropertiesList = new List<string>();
            foreach (var item in context.PropertyItems)
            {
                var finalProperty = item.HybridInfoset ? hybridInfosetPropertyTemplate.Replace("#propertyname#", item.Name) : infosetPropertyTemplate.Replace("#propertyname#", item.Name);
                finalProperty = finalProperty.Replace("#propertytype#", item.Type);

                finalPropertiesList.Add(finalProperty);
            }

            return string.Join(Environment.NewLine + Environment.NewLine, finalPropertiesList);
        }
        public void RunStarted(object automationObject, Dictionary <string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
        {
            try
            {
                // Create and initialize the context first.
                _context = new ContentPartWizardContext();

                var dte = automationObject as DTE;

                if (dte == null)
                {
                    MessageBox.Show("Something unexpected happened. Couldn't create the items.", "Create items", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    _context.Cancelled = true;

                    return;
                }

                var activeProjects = (Array)dte.ActiveSolutionProjects;

                if (activeProjects.Length > 0)
                {
                    var activeProject = (Project)activeProjects.GetValue(0);

                    _context.TargetProjectPath = Path.GetDirectoryName(activeProject.FullName);
                }

                var serviceProvider = new ServiceProvider(dte as Microsoft.VisualStudio.OLE.Interop.IServiceProvider);
                if (serviceProvider != null)
                {
                    var vsActivityLog = serviceProvider.GetService(typeof(SVsActivityLog)) as IVsActivityLog;

                    if (vsActivityLog != null)
                    {
                        _logger = new VisualStudioActivityLogger(vsActivityLog);
                    }
                }

                // Gather additional information from the user.
                using (var dialog = new ContentPartWizardDialog())
                {
                    var dialogResult = dialog.ShowDialog();

                    // The form has been filled.
                    if (dialogResult == DialogResult.OK)
                    {
                        _context.PropertyItems = dialog.PropertyItems;
                        _context.UpdatePlacementInfoIfExists = dialog.UpdatePlacementInfoIfExists;
                    }
                    // It was cancelled so don't create any file in the project.
                    else if (dialogResult == DialogResult.Cancel)
                    {
                        _context.Cancelled = true;

                        return;
                    }
                    // It was skipped so use default values.
                    else
                    {
                        _context.UpdatePlacementInfoIfExists = DefaultUpdatePlacementIfExists;
                    }
                }


                // Create the necessary replacements.
                var safeItemName = string.Empty;
                replacementsDictionary.TryGetValue("$safeitemname$", out safeItemName);

                _context.ContentPartNameWithoutSuffix = safeItemName.TrimEnd("Part");
                _context.FullContentPartName          = _context.ContentPartNameWithoutSuffix + "Part";

                var settingsPartName = _context.ContentPartNameWithoutSuffix.TrimEnd("Settings");

                // Add custom parameters.
                replacementsDictionary.Add("$contentpartname$", _context.ContentPartNameWithoutSuffix);
                replacementsDictionary.Add("$settingscontentpartname$", settingsPartName);

                replacementsDictionary.Add("$infosetproperties$", BuildInfosetPropertiesReplacement(_context));
                replacementsDictionary.Add("$virtualproperties$", BuildVirtualPropertiesReplacement(_context));
                replacementsDictionary.Add("$shapepropertyeditors$", BuildShapePropertyEditorsReplacement(_context));
                replacementsDictionary.Add("$shapepropertydisplays$", BuildShapePropertyDisplaysReplacement(_context));
                replacementsDictionary.Add("$migrationsrecordproperties$", BuildMigrationsRecordPropertiesReplacement(_context));
                replacementsDictionary.Add("$migrationsrecordindexes$", BuildMigrationsRecordIndexesReplacement(_context));
            }
            catch (Exception ex)
            {
                _logger.Error(this.ToString(), "Couldn't start generating items. Exception: " + ex.ToString());

                MessageBox.Show("Couldn't start generating items because of an unexpected exception. For further information check the activity log.", "Create items", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }