Пример #1
0
            public void Returns_original_path_when_unresolvable()
            {
                const string path = "File.ext";
                ITextTemplatingEngineHost host = new EfTextTemplateHost();

                var resolvedPath = host.ResolvePath(path);

                Assert.Equal(path, resolvedPath);
            }
Пример #2
0
            public void Resolves_absolute_paths()
            {
                const string path = @"C:\File.ext";
                ITextTemplatingEngineHost host = new EfTextTemplateHost();

                var resolvedPath = host.ResolvePath(path);

                Assert.Equal(path, resolvedPath);
            }
Пример #3
0
        public void LogErrors_sets_Errors()
        {
            var efHost = new EfTextTemplateHost();
            var host   = (ITextTemplatingEngineHost)efHost;
            var errors = new CompilerErrorCollection();

            host.LogErrors(errors);

            Assert.Same(errors, efHost.Errors);
        }
Пример #4
0
        public void SetFileExtension_sets_FileExtension()
        {
            const string extension = ".ext";
            var          efHost    = new EfTextTemplateHost();
            var          host      = (ITextTemplatingEngineHost)efHost;

            host.SetFileExtension(extension);

            Assert.Equal(extension, efHost.FileExtension);
        }
        public void StandardImports_includes_basic_imports()
        {
            ITextTemplatingEngineHost host = new EfTextTemplateHost();
            var hostNamespace = typeof(EfTextTemplateHost).Namespace;

            var imports = host.StandardImports;

            Assert.Contains("System", imports);
            Assert.Contains(hostNamespace, imports);
        }
Пример #6
0
        public void StandardImports_includes_basic_imports()
        {
            ITextTemplatingEngineHost host = new EfTextTemplateHost();
            var hostNamespace = typeof(EfTextTemplateHost).Namespace;

            var imports = host.StandardImports;

            Assert.Contains("System", imports);
            Assert.Contains(hostNamespace, imports);
        }
        public string Process(string templatePath, EfTextTemplateHost host)
        {
            host.TemplateFile = templatePath;
            Engine engine = new Engine();
            var    output = engine.ProcessTemplate(
                GetTemplate(templatePath),
                host);


            return(output);
        }
            public void Resolves_simple_assembly_names()
            {
                var host = new EfTextTemplateHost();
                var assemblyName = typeof(Type).Assembly.GetName();

                var resolvedReference = host.ResolveAssemblyReference(
                    assemblyName.Name);

                Assert.True(File.Exists(resolvedReference));
                Assert.Equal("mscorlib.dll", Path.GetFileName(resolvedReference));
            }
Пример #9
0
            public void Resolves_simple_assembly_names()
            {
                var host         = new EfTextTemplateHost();
                var assemblyName = typeof(Type).Assembly.GetName();

                var resolvedReference = host.ResolveAssemblyReference(
                    assemblyName.Name);

                Assert.True(File.Exists(resolvedReference));
                Assert.Equal("mscorlib.dll", Path.GetFileName(resolvedReference));
            }
Пример #10
0
            public void Resolves_assembly_locations()
            {
                var host             = new EfTextTemplateHost();
                var assemblyLocation = typeof(Type).Assembly.Location;

                var resolvedReference = host.ResolveAssemblyReference(
                    assemblyLocation);

                Assert.True(File.Exists(resolvedReference));
                Assert.Equal("mscorlib.dll", Path.GetFileName(resolvedReference));
            }
Пример #11
0
            public void Resolves_relative_paths_when_TemplateFile_absolute()
            {
                ITextTemplatingEngineHost host = new EfTextTemplateHost
                {
                    TemplateFile = @"C:\Template.tt"
                };

                var resolvedPath = host.ResolvePath("File.ext");

                Assert.Equal(@"C:\File.ext", resolvedPath);
            }
            public void Resolves_assembly_locations()
            {
                var host = new EfTextTemplateHost();
                var assemblyLocation = typeof(Type).Assembly.Location;

                var resolvedReference = host.ResolveAssemblyReference(
                    assemblyLocation);

                Assert.True(File.Exists(resolvedReference));
                Assert.Equal("mscorlib.dll", Path.GetFileName(resolvedReference));
            }
Пример #13
0
        public string Process(string templatePath, EfTextTemplateHost host)
        {
            Contract.Requires(!string.IsNullOrWhiteSpace(templatePath));
            Contract.Requires(host != null);

            host.TemplateFile = templatePath;

            var output = GetEngine().ProcessTemplate(
                GetTemplate(templatePath),
                host);

            host.Errors.HandleErrors(Strings.ProcessTemplateError(Path.GetFileName(templatePath)));

            return(output);
        }
Пример #14
0
        public string Process(string templatePath, EfTextTemplateHost host)
        {
            DebugCheck.NotEmpty(templatePath);
            DebugCheck.NotNull(host);

            host.TemplateFile = templatePath;

            var output = GetEngine().ProcessTemplate(
                GetTemplate(templatePath),
                host);

            host.Errors.HandleErrors(Strings.ProcessTemplateError(Path.GetFileName(templatePath)));

            return output;
        }
        public string Process(string templatePath, EfTextTemplateHost host)
        {
            DebugCheck.NotEmpty(templatePath);
            DebugCheck.NotNull(host);

            host.TemplateFile = templatePath;

            var output = GetEngine().ProcessTemplate(
                GetTemplate(templatePath),
                host);

            host.Errors.HandleErrors(Strings.ProcessTemplateError(Path.GetFileName(templatePath)));

            return(output);
        }
        public string Process(string templatePath, EfTextTemplateHost host)
        {
            Contract.Requires(!string.IsNullOrWhiteSpace(templatePath));
            Contract.Requires(host != null);

            host.TemplateFile = templatePath;

            var output = GetEngine().ProcessTemplate(
                GetTemplate(templatePath),
                host);

            host.Errors.HandleErrors(Strings.ProcessTemplateError(Path.GetFileName(templatePath)));

            return output;
        }
        public void StandardAssemblyReferences_includes_basic_references()
        {
            ITextTemplatingEngineHost host = new EfTextTemplateHost();
            var powerToolsAssemblyName = typeof(EfTextTemplateHost)
                .Assembly
                .GetName()
                .Name;

            var references = host.StandardAssemblyReferences
                .Select(r => Path.GetFileNameWithoutExtension(r))
                .ToArray();

            Assert.Contains(powerToolsAssemblyName, references);
            Assert.Contains("System", references);
            Assert.Contains("System.Core", references);
            Assert.Contains("System.Data.Entity", references);
        }
Пример #18
0
        public void StandardAssemblyReferences_includes_basic_references()
        {
            ITextTemplatingEngineHost host = new EfTextTemplateHost();
            var powerToolsAssemblyName     = typeof(EfTextTemplateHost)
                                             .Assembly
                                             .GetName()
                                             .Name;

            var references = host.StandardAssemblyReferences
                             .Select(r => Path.GetFileNameWithoutExtension(r))
                             .ToArray();

            Assert.Contains(powerToolsAssemblyName, references);
            Assert.Contains("System", references);
            Assert.Contains("System.Core", references);
            Assert.Contains("System.Data.Entity", references);
        }
        public void LogErrors_sets_Errors()
        {
            var efHost = new EfTextTemplateHost();
            var host = (ITextTemplatingEngineHost)efHost;
            var errors = new CompilerErrorCollection();

            host.LogErrors(errors);

            Assert.Same(errors, efHost.Errors);
        }
        public void ReverseEngineerCodeFirst(Project project)
        {
            DebugCheck.NotNull(project);

            try
            {
                // Show dialog with SqlClient selected by default
                var dialogFactory = _package.GetService<IVsDataConnectionDialogFactory>();
                var dialog = dialogFactory.CreateConnectionDialog();
                dialog.AddAllSources();
                dialog.SelectedSource = new Guid("067ea0d9-ba62-43f7-9106-34930c60c528");
                var dialogResult = dialog.ShowDialog(connect: true);

                if (dialogResult != null)
                {
                    // Find connection string and provider
                    _package.DTE2.StatusBar.Text = Strings.ReverseEngineer_LoadSchema;
                    var connection = (DbConnection)dialogResult.GetLockedProviderObject();
                    var connectionString = connection.ConnectionString;
                    var providerManager = (IVsDataProviderManager)Package.GetGlobalService(typeof(IVsDataProviderManager));
                    IVsDataProvider dp;
                    providerManager.Providers.TryGetValue(dialogResult.Provider, out dp);
                    var providerInvariant = (string)dp.GetProperty("InvariantName");

                    // Load store schema
                    var storeGenerator = new EntityStoreSchemaGenerator(providerInvariant, connectionString, "dbo");
                    storeGenerator.GenerateForeignKeyProperties = true;
                    var errors = storeGenerator.GenerateStoreMetadata(_storeMetadataFilters).Where(e => e.Severity == EdmSchemaErrorSeverity.Error);
                    errors.HandleErrors(Strings.ReverseEngineer_SchemaError);

                    // Generate default mapping
                    _package.DTE2.StatusBar.Text = Strings.ReverseEngineer_GenerateMapping;
                    var contextName = connection.Database.Replace(" ", string.Empty).Replace(".", string.Empty) + "Context";
                    var modelGenerator = new EntityModelSchemaGenerator(storeGenerator.EntityContainer, "DefaultNamespace", contextName);
                    modelGenerator.PluralizationService = PluralizationService.CreateService(new CultureInfo("en"));
                    modelGenerator.GenerateForeignKeyProperties = true;
                    modelGenerator.GenerateMetadata();

                    // Pull out info about types to be generated
                    var entityTypes = modelGenerator.EdmItemCollection.OfType<EntityType>().ToArray();
                    var mappings = new EdmMapping(modelGenerator, storeGenerator.StoreItemCollection);

                    // Find the project to add the code to
                    var vsProject = (VSLangProj.VSProject)project.Object;
                    var projectDirectory = new FileInfo(project.FileName).Directory;
                    var projectNamespace = (string)project.Properties.Item("RootNamespace").Value;
                    var references = vsProject.References.Cast<VSLangProj.Reference>();

                    if (!references.Any(r => r.Name == "EntityFramework"))
                    {
                        // Add EF References
                        _package.DTE2.StatusBar.Text = Strings.ReverseEngineer_InstallEntityFramework;

                        try
                        {
                            project.InstallPackage("EntityFramework");
                        }
                        catch (Exception ex)
                        {
                            _package.LogError(Strings.ReverseEngineer_InstallEntityFrameworkError, ex);
                        }
                    }

                    // Generate Entity Classes and Mappings
                    _package.DTE2.StatusBar.Text = Strings.ReverseEngineer_GenerateClasses;
                    var templateProcessor = new TemplateProcessor(project);
                    var modelsNamespace = projectNamespace + ".Models";
                    var modelsDirectory = Path.Combine(projectDirectory.FullName, "Models");
                    var mappingNamespace = modelsNamespace + ".Mapping";
                    var mappingDirectory = Path.Combine(modelsDirectory, "Mapping");
                    var entityFrameworkVersion = GetEntityFrameworkVersion(references);

                    foreach (var entityType in entityTypes)
                    {
                        // Generate the code file
                        var entityHost = new EfTextTemplateHost
                            {
                                EntityType = entityType,
                                EntityContainer = modelGenerator.EntityContainer,
                                Namespace = modelsNamespace,
                                ModelsNamespace = modelsNamespace,
                                MappingNamespace = mappingNamespace,
                                EntityFrameworkVersion = entityFrameworkVersion,
                                TableSet = mappings.EntityMappings[entityType].Item1,
                                PropertyToColumnMappings = mappings.EntityMappings[entityType].Item2,
                                ManyToManyMappings = mappings.ManyToManyMappings
                            };
                        var entityContents = templateProcessor.Process(Templates.EntityTemplate, entityHost);

                        var filePath = Path.Combine(modelsDirectory, entityType.Name + entityHost.FileExtension);
                        project.AddNewFile(filePath, entityContents);

                        var mappingHost = new EfTextTemplateHost
                            {
                                EntityType = entityType,
                                EntityContainer = modelGenerator.EntityContainer,
                                Namespace = mappingNamespace,
                                ModelsNamespace = modelsNamespace,
                                MappingNamespace = mappingNamespace,
                                EntityFrameworkVersion = entityFrameworkVersion,
                                TableSet = mappings.EntityMappings[entityType].Item1,
                                PropertyToColumnMappings = mappings.EntityMappings[entityType].Item2,
                                ManyToManyMappings = mappings.ManyToManyMappings
                            };
                        var mappingContents = templateProcessor.Process(Templates.MappingTemplate, mappingHost);

                        var mappingFilePath = Path.Combine(mappingDirectory, entityType.Name + "Map" + mappingHost.FileExtension);
                        project.AddNewFile(mappingFilePath, mappingContents);
                    }

                    // Generate Context
                    _package.DTE2.StatusBar.Text = Strings.ReverseEngineer_GenerateContext;
                    var contextHost = new EfTextTemplateHost
                        {
                            EntityContainer = modelGenerator.EntityContainer,
                            Namespace = modelsNamespace,
                            ModelsNamespace = modelsNamespace,
                            MappingNamespace = mappingNamespace,
                            EntityFrameworkVersion = entityFrameworkVersion
                        };
                    var contextContents = templateProcessor.Process(Templates.ContextTemplate, contextHost);

                    var contextFilePath = Path.Combine(modelsDirectory, modelGenerator.EntityContainer.Name + contextHost.FileExtension);
                    var contextItem = project.AddNewFile(contextFilePath, contextContents);
                    AddConnectionStringToConfigFile(project, connectionString, providerInvariant, modelGenerator.EntityContainer.Name);

                    if (contextItem != null)
                    {
                        // Open context class when done
                        _package.DTE2.ItemOperations.OpenFile(contextFilePath);
                    }

                    _package.DTE2.StatusBar.Text = Strings.ReverseEngineer_Complete;
                }
            }
            catch (Exception exception)
            {
                _package.LogError(Strings.ReverseEngineer_Error, exception);
            }
        }
            public void Resolves_absolute_paths()
            {
                const string path = @"C:\File.ext";
                ITextTemplatingEngineHost host = new EfTextTemplateHost();

                var resolvedPath = host.ResolvePath(path);

                Assert.Equal(path, resolvedPath);
            }
        public void SetFileExtension_sets_FileExtension()
        {
            const string extension = ".ext";
            var efHost = new EfTextTemplateHost();
            var host = (ITextTemplatingEngineHost)efHost;

            host.SetFileExtension(extension);

            Assert.Equal(extension, efHost.FileExtension);
        }
            public void Returns_original_path_when_unresolvable()
            {
                const string path = "File.ext";
                ITextTemplatingEngineHost host = new EfTextTemplateHost();

                var resolvedPath = host.ResolvePath(path);

                Assert.Equal(path, resolvedPath);
            }
            public void Resolves_relative_paths_when_TemplateFile_absolute()
            {
                ITextTemplatingEngineHost host = new EfTextTemplateHost
                    {
                        TemplateFile = @"C:\Template.tt"
                    };

                var resolvedPath = host.ResolvePath("File.ext");

                Assert.Equal(@"C:\File.ext", resolvedPath);
            }