private static void CheckOutputFiles(
            ScaffoldedModel scaffoldedModel,
            string outputDir,
            bool overwriteFiles)
        {
            var paths = scaffoldedModel.AdditionalFiles.Select(f => f.Path).ToList();

            paths.Insert(0, scaffoldedModel.ContextFile.Path);

            var existingFiles = new List <string>();
            var readOnlyFiles = new List <string>();

            foreach (var path in paths)
            {
                var fullPath = Path.Combine(outputDir, path);

                if (File.Exists(fullPath))
                {
                    existingFiles.Add(path);

                    if (File.GetAttributes(fullPath).HasFlag(FileAttributes.ReadOnly))
                    {
                        readOnlyFiles.Add(path);
                    }
                }
            }

            if (!overwriteFiles && existingFiles.Count != 0)
            {
                throw new OperationException(
                          DesignStrings.ExistingFiles(
                              outputDir,
                              string.Join(CultureInfo.CurrentCulture.TextInfo.ListSeparator, existingFiles)));
            }
            if (readOnlyFiles.Count != 0)
            {
                throw new OperationException(
                          DesignStrings.ReadOnlyFiles(
                              outputDir,
                              string.Join(CultureInfo.CurrentCulture.TextInfo.ListSeparator, readOnlyFiles)));
            }
        }
Пример #2
0
        public void Save_throws_when_readonly_files()
        {
            using (var directory = new TempDirectory())
            {
                var contextPath = Path.Combine(directory.Path, "TestContext.cs");
                File.WriteAllText(contextPath, "// Old");

                var entityTypePath = Path.Combine(directory.Path, "TestEntity.cs");
                File.WriteAllText(entityTypePath, "// Old");

                var originalAttributes = File.GetAttributes(contextPath);
                File.SetAttributes(contextPath, originalAttributes | FileAttributes.ReadOnly);
                File.SetAttributes(entityTypePath, originalAttributes | FileAttributes.ReadOnly);
                try
                {
                    var scaffolder      = CreateScaffolder();
                    var scaffoldedModel = new ScaffoldedModel
                    {
                        ContextFile = new ScaffoldedFile {
                            Path = "TestContext.cs", Code = "// TestContext"
                        },
                        AdditionalFiles = { new ScaffoldedFile {
                                                Path = "TestEntity.cs", Code = "// TestEntity"
                                            } }
                    };

                    var ex = Assert.Throws <OperationException>(
                        () => scaffolder.Save(scaffoldedModel, directory.Path, overwriteFiles: true));

                    Assert.Equal(
                        DesignStrings.ReadOnlyFiles(
                            directory.Path,
                            string.Join(CultureInfo.CurrentCulture.TextInfo.ListSeparator, "TestContext.cs", "TestEntity.cs")),
                        ex.Message);
                }
                finally
                {
                    File.SetAttributes(contextPath, originalAttributes);
                    File.SetAttributes(entityTypePath, originalAttributes);
                }
            }
        }
Пример #3
0
        /// <summary>
        ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
        ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
        ///     any release. You should only use it directly in your code with extreme caution and knowing that
        ///     doing so can result in application failures when updating to a new Entity Framework Core release.
        /// </summary>
        public override ScaffoldedModel GenerateModel(
            IModel model,
            ModelCodeGenerationOptions options)
        {
            Check.NotNull(model, nameof(model));
            Check.NotNull(options, nameof(options));

            var resultingFiles = new ScaffoldedModel();

            var generatedCode = CSharpDbContextGenerator.WriteCode(
                model,
                options.ContextNamespace ?? options.ModelNamespace,
                options.ContextName,
                options.ConnectionString,
                options.UseDataAnnotations,
                options.SuppressConnectionStringWarning);

            // output DbContext .cs file
            var dbContextFileName = options.ContextName + FileExtension;

            resultingFiles.ContextFile = new ScaffoldedFile
            {
                Path = options.ContextDir != null
                    ? Path.Combine(options.ContextDir, dbContextFileName)
                    : dbContextFileName,
                Code = generatedCode
            };

            foreach (var entityType in model.GetEntityTypes())
            {
                generatedCode = CSharpEntityTypeGenerator.WriteCode(entityType, options.ModelNamespace, options.UseDataAnnotations);

                // output EntityType poco .cs file
                var entityTypeFileName = entityType.DisplayName() + FileExtension;
                resultingFiles.AdditionalFiles.Add(
                    new ScaffoldedFile {
                    Path = entityTypeFileName, Code = generatedCode
                });
            }

            return(resultingFiles);
        }
Пример #4
0
        public void Save_works_when_overwriteFiles()
        {
            using (var directory = new TempDirectory())
            {
                var path = Path.Combine(directory.Path, "Test.cs");
                File.WriteAllText(path, "// Old");

                var scaffolder      = CreateScaffolder();
                var scaffoldedModel = new ScaffoldedModel {
                    ContextFile = new ScaffoldedFile {
                        Path = "Test.cs", Code = "// Test"
                    }
                };

                var result = scaffolder.Save(scaffoldedModel, directory.Path, overwriteFiles: true);

                Assert.Equal(path, result.ContextFile);
                Assert.Equal("// Test", File.ReadAllText(path));
            }
        }
        public void Save_throws_when_existing_files()
        {
            using (var directory = new TempDirectory())
            {
                var contextPath = Path.Combine(directory.Path, "TestContext.cs");
                File.WriteAllText(contextPath, "// Old");

                var entityTypePath = Path.Combine(directory.Path, "TestEntity.cs");
                File.WriteAllText(entityTypePath, "// Old");

                var scaffolder      = CreateScaffolder();
                var scaffoldedModel = new ScaffoldedModel
                {
                    ContextFile = new ScaffoldedFile
                    {
                        Path = "TestContext.cs",
                        Code = "// TestContext"
                    },
                    AdditionalFiles =
                    {
                        new ScaffoldedFile
                        {
                            Path = "TestEntity.cs",
                            Code = "// TestEntity"
                        }
                    }
                };

                var ex = Assert.Throws <OperationException>(
                    () => scaffolder.Save(scaffoldedModel, directory.Path, overwriteFiles: false));

                Assert.Equal(
                    DesignStrings.ExistingFiles(
                        directory.Path,
                        string.Join(CultureInfo.CurrentCulture.TextInfo.ListSeparator, "TestContext.cs", "TestEntity.cs")),
                    ex.Message);
            }
        }
Пример #6
0
        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        public override ScaffoldedModel GenerateModel(
            IModel model,
            string @namespace,
            string contextDir,
            string contextName,
            string connectionString,
            bool useDataAnnotations)
        {
            Check.NotNull(model, nameof(model));
            Check.NotEmpty(@namespace, nameof(@namespace));
            Check.NotNull(contextDir, nameof(contextDir));
            Check.NotEmpty(contextName, nameof(contextName));
            Check.NotEmpty(connectionString, nameof(connectionString));

            var resultingFiles = new ScaffoldedModel();

            var generatedCode = CSharpDbContextGenerator.WriteCode(model, @namespace, contextName, connectionString, useDataAnnotations);

            // output DbContext .cs file
            var dbContextFileName = contextName + FileExtension;

            resultingFiles.ContextFile = new ScaffoldedFile {
                Path = Path.Combine(contextDir, dbContextFileName), Code = generatedCode
            };

            foreach (var entityType in model.GetEntityTypes())
            {
                generatedCode = CSharpEntityTypeGenerator.WriteCode(entityType, @namespace, useDataAnnotations);

                // output EntityType poco .cs file
                var entityTypeFileName = entityType.DisplayName() + FileExtension;
                resultingFiles.AdditionalFiles.Add(new ScaffoldedFile {
                    Path = entityTypeFileName, Code = generatedCode
                });
            }

            return(resultingFiles);
        }
        public void Save_works()
        {
            using (var directory = new TempDirectory())
            {
                var scaffolder      = CreateScaffolder();
                var scaffoldedModel = new ScaffoldedModel
                {
                    ContextFile = new ScaffoldedFile
                    {
                        Path = Path.Combine("..", "Data", "TestContext.cs"),
                        Code = "// TestContext"
                    },
                    AdditionalFiles =
                    {
                        new ScaffoldedFile
                        {
                            Path = "TestEntity.cs",
                            Code = "// TestEntity"
                        }
                    }
                };

                var result = scaffolder.Save(
                    scaffoldedModel,
                    Path.Combine(directory.Path, "Models"),
                    overwriteFiles: false);

                var contextPath = Path.Combine(directory.Path, "Data", "TestContext.cs");
                Assert.Equal(contextPath, result.ContextFile);
                Assert.Equal("// TestContext", File.ReadAllText(contextPath));

                Assert.Equal(1, result.AdditionalFiles.Count);
                var entityTypePath = Path.Combine(directory.Path, "Models", "TestEntity.cs");
                Assert.Equal(entityTypePath, result.AdditionalFiles[0]);
                Assert.Equal("// TestEntity", File.ReadAllText(entityTypePath));
            }
        }
Пример #8
0
        /// <summary>
        ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
        ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
        ///     any release. You should only use it directly in your code with extreme caution and knowing that
        ///     doing so can result in application failures when updating to a new Entity Framework Core release.
        /// </summary>
        public override ScaffoldedModel GenerateModel(
            IModel model,
            ModelCodeGenerationOptions options)
        {
            Check.NotNull(model, nameof(model));
            Check.NotNull(options, nameof(options));

            if (options.ContextName == null)
            {
                throw new ArgumentException(
                          CoreStrings.ArgumentPropertyNull(nameof(options.ContextName), nameof(options)), nameof(options));
            }

            if (options.ConnectionString == null)
            {
                throw new ArgumentException(
                          CoreStrings.ArgumentPropertyNull(nameof(options.ConnectionString), nameof(options)), nameof(options));
            }

            if (options.ModelNamespace == null)
            {
                throw new ArgumentException(
                          CoreStrings.ArgumentPropertyNull(nameof(options.ModelNamespace), nameof(options)), nameof(options));
            }

            var generatedCode = CSharpDbContextGenerator.WriteCode(
                model,
                options.ContextName,
                options.ConnectionString,
                options.ContextNamespace,
                options.ModelNamespace,
                options.UseDataAnnotations,
                options.SuppressConnectionStringWarning,
                options.SuppressOnConfiguring);

            // output DbContext .cs file
            var dbContextFileName = options.ContextName + FileExtension;
            var resultingFiles    = new ScaffoldedModel
            {
                ContextFile = new ScaffoldedFile
                {
                    Path = options.ContextDir != null
                        ? Path.Combine(options.ContextDir, dbContextFileName)
                        : dbContextFileName,
                    Code = generatedCode
                }
            };

            foreach (var entityType in model.GetEntityTypes())
            {
                generatedCode = CSharpEntityTypeGenerator.WriteCode(entityType, options.ModelNamespace, options.UseDataAnnotations);

                // output EntityType poco .cs file
                var entityTypeFileName = entityType.Name + FileExtension;
                resultingFiles.AdditionalFiles.Add(
                    new ScaffoldedFile {
                    Path = entityTypeFileName, Code = generatedCode
                });
            }

            return(resultingFiles);
        }