Example #1
0
        public static async Task InitializeGeneratorsAsync(bool writeOutputMessages = false)
        {
            Outputs.Clear();

            Debug.WriteLine($@"Scanning local directory for Generator libraries");

            var assemblies = new List <Assembly>();

            foreach (var file in Directory.EnumerateFiles(Environment.CurrentDirectory, "*" + GenesisDefaults.LibraryExtension, SearchOption.TopDirectoryOnly))
            {
                assemblies.Add(Assembly.LoadFile(file)); //doesn't seem to mind loading everything... for now
            }
            var conventions = new ConventionBuilder();

            conventions.ForTypesDerivedFrom <IGenerator>()
            .Export <IGenerator>()
            .Shared();

            var configuration = new ContainerConfiguration().WithAssemblies(assemblies, conventions);

            using (var container = configuration.CreateContainer())
            {
                var generators = container.GetExports <IGenerator>();

                Outputs.Clear();

                foreach (var generator in generators)
                {
                    Outputs.Add(generator);

                    var configType = generator.GetType().GetRuntimeProperty("Config").PropertyType ?? typeof(GeneratorConfiguration);
                    var config     = configType.IsInstanceOfType(typeof(GeneratorConfiguration));
                    var cfgWarning = false;
                    try
                    {
                        //cmdtext was located from friendlyname and is available if it succeedes
                        if (typeof(GeneratorConfiguration).IsAssignableFrom(configType)) //Make sure we can use it
                        {
                            generator.Configuration = (IGeneratorConfiguration)Activator.CreateInstance(configType, true);
                        }

                        //bind the configuration json to the config instance
                        if (writeOutputMessages && !GenesisConfiguration.ApplyFromJson(configType, $"{configType.Name}.json", generator.Configuration))
                        {
                            cfgWarning = true;
                            Text.RedLine($"Unable to configure from {configType.Name}.json for {generator.GetType().Name}");
                        }

                        try
                        {
                            generator.Template = TemplateLoader.LoadTemplateFor(generator);
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine(ex.Message);
                            Text.DarkYellow($"Could not load template for ");
                            Text.Command(generator.CommandText);
                            Text.Line();

                            cfgWarning = true;
                        }

                        try
                        {
                            await generator.AttachDependencies(DependencyManager.LoadDependencies(generator));
                        }
                        catch (Exception dx)
                        {
                            Text.DarkYellow($"Could not load or parse dependencies for");
                            Text.Command(generator.CommandText);
                            Text.Line();

                            Debug.WriteLine(dx.Message);

                            cfgWarning = true;
                        }
                        await generator.Initialize();

                        if (!writeOutputMessages)
                        {
                            continue;
                        }

                        Text.White($"'"); Text.Green(generator.CommandText); Text.White("' ("); Text.Cyan(generator.FriendlyName); Text.White(") was found in '"); Text.Magenta(generator.GetType().Name); Text.White("'... ");

                        if (cfgWarning)
                        {
                            Text.YellowLine("warning");
                        }
                        else
                        {
                            Text.GreenLine("OK");
                        }

                        Console.ResetColor();
                    }
                    catch (Exception exc)
                    {
                        if (!writeOutputMessages)
                        {
                            continue;
                        }

                        Console.Write($"'{generator.FriendlyName}': ");
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine(exc.Message);
                        Console.ResetColor();
                    }
                }
            }
        }
Example #2
0
        //TODO: Why is InputManager 'writing' messages to the Console?

        /// <summary>
        /// Load Populator extensions from the current directory
        /// </summary>
        //slow down son
        public static async Task InitializePopulatorsAsync(bool writeOutputMessages = false)
        {
            Inputs.Clear();

            var assemblies = new List <Assembly>();

            foreach (var file in Directory.EnumerateFiles(Environment.CurrentDirectory, "*" + GenesisDefaults.LibraryExtension, SearchOption.TopDirectoryOnly))
            {
                assemblies.Add(Assembly.LoadFile(file));
            }

            var conventions = new ConventionBuilder();

            conventions.ForTypesDerivedFrom <IPopulator>()
            .Export <IPopulator>()
            .Shared();

            var configuration = new ContainerConfiguration().WithAssemblies(assemblies, conventions);

            //TODO: Post-build steps for Populator assemblies

            using (var container = configuration.CreateContainer())
            {
                var populators = container.GetExports <IPopulator>();

                Inputs.Clear();

                foreach (var populator in populators)
                {
                    Inputs.Add(populator);

                    var configType = populator.GetType().GetRuntimeProperty("Config").PropertyType ?? typeof(PopulatorConfiguration);
                    var config     = configType.IsInstanceOfType(typeof(PopulatorConfiguration));
                    var cfgWarning = false;
                    try
                    {
                        //cmdtext was located from friendlyname and is available if it succeedes
                        if (typeof(PopulatorConfiguration).IsAssignableFrom(configType)) //Make sure we can use it
                        {
                            populator.Configuration = (IPopulatorConfiguration)Activator.CreateInstance(configType, true);
                        }

                        //bind the configuration json to the config instance
                        if (writeOutputMessages && !GenesisConfiguration.ApplyFromJson(configType, $"{configType.Name}.json", populator.Configuration))
                        {
                            cfgWarning = true;
                            Text.RedLine($"Unable to configure from {configType.Name}.json for {populator.GetType().Name}");
                        }

                        await populator.Initialize();

                        if (!writeOutputMessages)
                        {
                            continue;
                        }

                        Text.White($"'"); Text.Green(populator.CommandText); Text.White("' ("); Text.Cyan(populator.FriendlyName); Text.White(") was found in '"); Text.Magenta(populator.GetType().Name); Text.White("'... ");

                        if (cfgWarning)
                        {
                            Text.YellowLine("warning");
                        }
                        else
                        {
                            Text.GreenLine("OK");
                        }

                        Console.ResetColor();
                    }
                    catch (Exception exc)
                    {
                        if (!writeOutputMessages)
                        {
                            continue;
                        }

                        Console.Write($"'{populator.FriendlyName}': ");
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine(exc.Message);
                        Console.ResetColor();
                    }
                }
            }
        }