public void ScaffoldingCommandInterpreterShouldDetectContentParts()
        {
            var commands = _generator.GetCreateFeatureCommands("Feature1", false).ToList();
            Assert.That(commands, Is.Not.Null);
            Assert.That(commands.Count(), Is.EqualTo(3));

            var sw = new StringWriter();
            var interpreter = new CodeGenerationCommandInterpreter(sw);

            var blogRecord = commands.Where(c => c.Name == "TEST_Feature1_BlogRecord").First();
            var blogArchiveRecord = commands.Where(c => c.Name == "TEST_Feature1_BlogArchiveRecord").First();
            var bodyRecord = commands.Where(c => c.Name == "TEST_Feature1_BodyRecord").First();

            sw.GetStringBuilder().Clear();
            interpreter.Visit(blogRecord);
            Assert.That(sw.ToString().Contains("SchemaBuilder.CreateTable(\"TEST_Feature1_BlogRecord"));
            Assert.That(sw.ToString().Contains(".ContentPartRecord()"));
            Assert.That(sw.ToString().Contains(".Column(\"Description\", DbType.String)"));
            Assert.That(sw.ToString().Contains(".Column(\"PostCount\", DbType.Int32)"));

            sw.GetStringBuilder().Clear();
            interpreter.Visit(blogArchiveRecord);
            Assert.That(sw.ToString().Contains("SchemaBuilder.CreateTable(\"TEST_Feature1_BlogArchiveRecord"));
            Assert.That(sw.ToString().Contains(".Column(\"Id\", DbType.Int32, column => column.PrimaryKey().Identity())"));
            Assert.That(sw.ToString().Contains(".Column(\"Year\", DbType.Int32)"));
            Assert.That(sw.ToString().Contains(".Column(\"Month\", DbType.Int32)"));
            Assert.That(sw.ToString().Contains(".Column(\"PostCount\", DbType.Int32)"));
            Assert.That(sw.ToString().Contains(".Column(\"Blog_id\", DbType.Int32)"));

            sw.GetStringBuilder().Clear();
            interpreter.Visit(bodyRecord);
            Assert.That(sw.ToString().Contains("SchemaBuilder.CreateTable(\"TEST_Feature1_BodyRecord"));
            Assert.That(sw.ToString().Contains(".ContentPartVersionRecord()"));
            Assert.That(sw.ToString().Contains(".Column(\"Text\", DbType.String, column => column.Unlimited())"));
            Assert.That(sw.ToString().Contains(".Column(\"Format\", DbType.String, column => column.WithLength(42))"));
            Assert.That(!sw.ToString().Contains("ContentItemRecord_id"));
        }
        public void CreateDataMigration(string featureName)
        {
            Context.Output.WriteLine(T("Creating Data Migration for {0}", featureName));
            ExtensionDescriptor extensionDescriptor = _extensionManager.AvailableExtensions().FirstOrDefault(extension => DefaultExtensionTypes.IsModule(extension.ExtensionType) &&
                                                                                                             extension.Features.Any(feature => String.Equals(feature.Id, featureName, StringComparison.OrdinalIgnoreCase)));

            if (extensionDescriptor == null) {
                Context.Output.WriteLine(T("Creating data migration failed: target Feature {0} could not be found.", featureName));
                return;
            }

            string dataMigrationFolderPath = HostingEnvironment.MapPath("~/Modules/" + extensionDescriptor.Id + "/");
            string dataMigrationFilePath = dataMigrationFolderPath + "Migrations.cs";
            string moduleCsProjPath = HostingEnvironment.MapPath(string.Format("~/Modules/{0}/{0}.csproj", extensionDescriptor.Id));

            if (!Directory.Exists(dataMigrationFolderPath)) {
                Directory.CreateDirectory(dataMigrationFolderPath);
            }

            if (File.Exists(dataMigrationFilePath)) {
                Context.Output.WriteLine(T("Data migration already exists in target Module {0}.", extensionDescriptor.Id));
                return;
            }

            List<SchemaCommand> commands = _schemaCommandGenerator.GetCreateFeatureCommands(featureName, false).ToList();
            string dataMigrationText;
            using (var stringWriter = new StringWriter()) {
                var interpreter = new CodeGenerationCommandInterpreter(stringWriter);

                foreach (var command in commands) {
                    interpreter.Visit(command);
                    stringWriter.WriteLine();
                }
                var migrationTemplate = new DataMigration {Session = new Dictionary<string, object>()};
                migrationTemplate.Session["FeatureName"] = featureName;
                migrationTemplate.Session["Commands"] = stringWriter.ToString();
                migrationTemplate.Initialize();
                dataMigrationText = migrationTemplate.TransformText();
            }
            File.WriteAllText(dataMigrationFilePath, dataMigrationText);

            string projectFileText = File.ReadAllText(moduleCsProjPath);

            // The string searches in solution/project files can be made aware of comment lines.
            if (projectFileText.Contains("<Compile Include")) {
                string compileReference = string.Format("<Compile Include=\"{0}\" />\r\n    ", "Migrations.cs");
                projectFileText = projectFileText.Insert(projectFileText.LastIndexOf("<Compile Include"), compileReference);
            }
            else {
                string itemGroupReference = string.Format("</ItemGroup>\r\n  <ItemGroup>\r\n    <Compile Include=\"{0}\" />\r\n  ", "Migrations.cs");
                projectFileText = projectFileText.Insert(projectFileText.LastIndexOf("</ItemGroup>"), itemGroupReference);
            }

            File.WriteAllText(moduleCsProjPath, projectFileText);
            TouchSolution(Context.Output);
            Context.Output.WriteLine(T("Data migration created successfully in Module {0}", extensionDescriptor.Id));
        }