Exemple #1
0
        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);
            }
        }
        private static void CheckOutputFiles(
            IScaffoldingCodeGenerator codeGenerator,
            string outputPath,
            string dbContextClassName,
            IModel metadataModel,
            bool overwriteFiles)
        {
            var readOnlyFiles = codeGenerator.GetReadOnlyFilePaths(
                outputPath, dbContextClassName, metadataModel.GetEntityTypes());

            if (readOnlyFiles.Count > 0)
            {
                throw new InvalidOperationException(
                          DesignStrings.ReadOnlyFiles(
                              outputPath,
                              string.Join(
                                  CultureInfo.CurrentCulture.TextInfo.ListSeparator, readOnlyFiles)));
            }

            if (!overwriteFiles)
            {
                var existingFiles = codeGenerator.GetExistingFilePaths(
                    outputPath, dbContextClassName, metadataModel.GetEntityTypes());
                if (existingFiles.Count > 0)
                {
                    throw new InvalidOperationException(
                              DesignStrings.ExistingFiles(
                                  outputPath,
                                  string.Join(
                                      CultureInfo.CurrentCulture.TextInfo.ListSeparator, existingFiles)));
                }
            }
        }
Exemple #3
0
        private static void CheckOutputFiles(
            ScaffoldedModel scaffoldedModel,
            string outputDir,
            bool overwriteFiles)
        {
            var paths = scaffoldedModel.AdditionalFiles.Select(f => f.Path).ToList();

            if (scaffoldedModel.ContextFile != null &&
                !string.IsNullOrWhiteSpace(scaffoldedModel.ContextFile.Path))
            {
                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)));
            }
        }
        private void CheckOutputFiles(
            [NotNull] string outputPath,
            [NotNull] string dbContextClassName,
            [NotNull] IModel metadataModel,
            bool overwriteFiles)
        {
            Check.NotEmpty(outputPath, nameof(outputPath));
            Check.NotEmpty(dbContextClassName, nameof(dbContextClassName));
            Check.NotNull(metadataModel, nameof(metadataModel));

            var readOnlyFiles = ScaffoldingCodeGenerator.GetReadOnlyFilePaths(
                outputPath, dbContextClassName, metadataModel.GetEntityTypes());

            if (readOnlyFiles.Count > 0)
            {
                throw new InvalidOperationException(
                          DesignStrings.ReadOnlyFiles(
                              outputPath,
                              string.Join(
                                  CultureInfo.CurrentCulture.TextInfo.ListSeparator, readOnlyFiles)));
            }

            if (!overwriteFiles)
            {
                var existingFiles = ScaffoldingCodeGenerator.GetExistingFilePaths(
                    outputPath, dbContextClassName, metadataModel.GetEntityTypes());
                if (existingFiles.Count > 0)
                {
                    throw new InvalidOperationException(
                              DesignStrings.ExistingFiles(
                                  outputPath,
                                  string.Join(
                                      CultureInfo.CurrentCulture.TextInfo.ListSeparator, existingFiles)));
                }
            }
        }