Example #1
0
        private static void Generate(string projectFolder, string configPath, bool verbose)
        {
            // get config

            configPath = !string.IsNullOrEmpty(configPath)
                ? Path.Combine(projectFolder, configPath)
                : Path.Combine(projectFolder, "tgconfig.json");

            TgConfig config = _configProvider.GetConfig(configPath, projectFolder, verbose);

            // register assembly resolver

            _assemblyResolver.Directories = config.ExternalAssemblyPaths;
            _assemblyResolver.Register();

            IEnumerable <Assembly> assemblies = GetAssemblies(config.GetAssemblies());

            // create generator

            GeneratorOptions generatorOptions = _generatorOptionsProvider.GetGeneratorOptions(config, assemblies, projectFolder, verbose);

            generatorOptions.BaseOutputDirectory = Path.Combine(projectFolder, config.OutputPath);
            generatorOptions.CreateIndexFile     = config.CreateIndexFile ?? GeneratorOptions.DefaultCreateIndexFile;
            var generator = new Generator {
                Options = generatorOptions
            };

            // generate

            IEnumerable <string> generatedFiles = generator.Generate(assemblies).GeneratedFiles;

            if (config.AddFilesToProject ?? TgConfig.DefaultAddFilesToProject)
            {
                AddFilesToProject(projectFolder, generatedFiles);
            }

            // unregister assembly resolver

            _assemblyResolver.Unregister();
        }
Example #2
0
        private static void Generate(string projectFolder, string configPath)
        {
            // get config

            configPath = !string.IsNullOrEmpty(configPath)
                ? Path.Combine(projectFolder, configPath)
                : Path.Combine(projectFolder, "tgconfig.json");

            TgConfig config = _configProvider.GetConfig(configPath, projectFolder);

            LogConfigWarnings(config);

            // register assembly resolver

            _assemblyResolver.Directories = config.ExternalAssemblyPaths;
            _assemblyResolver.Register();

            IEnumerable <Assembly> assemblies = GetAssemblies(config.GetAssemblies()).ToArray();

            // create generator

            GeneratorOptions generatorOptions = _generatorOptionsProvider.GetGeneratorOptions(config, assemblies, projectFolder);

            generatorOptions.BaseOutputDirectory = Path.Combine(projectFolder, config.OutputPath);
            var generator = new Generator(generatorOptions, _logger);

            // generate

            if (config.ClearOutputDirectory == true)
            {
                _fileSystem.ClearDirectory(generatorOptions.BaseOutputDirectory);
            }
            if (config.BuildProject == true)
            {
                _projectBuilder.Build(projectFolder);
            }

            _logger.Log($"Generating files for project \"{projectFolder}\"...", LogLevel.Info);

            var generatedFiles = new List <string>();

            if (!config.GenerationSpecs.Any() || config.GenerateFromAssemblies == true)
            {
                generatedFiles.AddRange(generator.Generate(assemblies));
            }

            if (config.GenerationSpecs.Any())
            {
                var typeResolver = new TypeResolver(_logger, _fileSystem, projectFolder, assemblies);

                IEnumerable <GenerationSpec> generationSpecs = config.GenerationSpecs
                                                               .Select(name => typeResolver.Resolve(name, "GenerationSpec"))
                                                               .Where(t => t != null)
                                                               .Select(t => (GenerationSpec)Activator.CreateInstance(t))
                                                               .ToArray();

                generatedFiles.AddRange(generator.Generate(generationSpecs));
            }

            foreach (string file in generatedFiles)
            {
                _logger.Log($"Generated {file}", LogLevel.Info);
            }

            if (config.AddFilesToProject ?? TgConfig.DefaultAddFilesToProject)
            {
                AddFilesToProject(projectFolder, generatedFiles);
            }

            // unregister assembly resolver

            _assemblyResolver.Unregister();

            _logger.Log($"Files for project \"{projectFolder}\" generated successfully.{Environment.NewLine}", LogLevel.Info);
        }
Example #3
0
        private static void Generate(string projectFolder, string configPath)
        {
            // get config

            configPath = !string.IsNullOrEmpty(configPath)
                ? Path.Combine(projectFolder, configPath)
                : Path.Combine(projectFolder, "tgconfig.json");

            TgConfig config = _configProvider.GetConfig(configPath, projectFolder);

            // register assembly resolver

            _assemblyResolver.Directories = config.ExternalAssemblyPaths;
            _assemblyResolver.Register();

            IEnumerable <Assembly> assemblies = GetAssemblies(config.GetAssemblies()).ToArray();

            // create generator

            GeneratorOptions generatorOptions = _generatorOptionsProvider.GetGeneratorOptions(config, assemblies, projectFolder);

            generatorOptions.BaseOutputDirectory = Path.Combine(projectFolder, config.OutputPath);
            var generator = new Generator(generatorOptions, _logger);

            // generate

            if (config.ClearOutputDirectory == true)
            {
                _fileSystem.ClearDirectory(generatorOptions.BaseOutputDirectory);
            }

            IEnumerable <string> generatedFiles = Enumerable.Empty <string>();

            if (!config.GenerationSpecs.Any() || config.GenerateFromAssemblies == true)
            {
                generatedFiles = generator.Generate(assemblies);
            }

            if (config.GenerationSpecs.Any())
            {
                var typeResolver = new TypeResolver(_logger, _fileSystem, projectFolder, assemblies);

                generatedFiles = config.GenerationSpecs
                                 .Select(name => typeResolver.Resolve(name, "GenerationSpec"))
                                 .Where(t => t != null)
                                 .Select(t => (GenerationSpec)Activator.CreateInstance(t))
                                 .Aggregate(generatedFiles, (acc, spec) => acc.Concat(generator.Generate(spec)));
            }

            _logger.Log("");
            _logger.Log(generatedFiles.Select(x => $"Generated {x}").ToArray());
            _logger.Log("");

            if (config.AddFilesToProject ?? TgConfig.DefaultAddFilesToProject)
            {
                AddFilesToProject(projectFolder, generatedFiles);
            }

            // unregister assembly resolver

            _assemblyResolver.Unregister();
        }