Beispiel #1
0
        public Generator()
        {
            Logger.CatchAll();
            Logger.Trace($"KY Generator v{Assembly.GetCallingAssembly().GetName().Version}");
            Logger.Trace("Current Directory: " + Environment.CurrentDirectory);
            Logger.Trace("Log Directory: " + Logger.File.Path);

            NugetPackageDependencyLoader.Activate();

            this.output   = new FileOutput(AppDomain.CurrentDomain.BaseDirectory);
            this.resolver = new DependencyResolver();
            this.resolver.Bind <ITypeMapping>().ToSingleton <TypeMapping>();
            this.resolver.Bind <CommandRunner>().ToSelf();
            this.resolver.Bind <ModuleFinder>().ToSelf();
            this.resolver.Bind <IConfigurationReaderVersion>().To <ConfigurationReaderVersion2>();
            this.resolver.Bind <ReaderConfigurationMapping>().ToSingleton();
            this.resolver.Bind <WriterConfigurationMapping>().ToSingleton();
            this.resolver.Bind <ConfigurationRunner>().ToSelf();
            this.resolver.Bind <ModelWriter>().ToSelf();
            StaticResolver.Resolver = this.resolver;

            ModuleFinder moduleFinder = this.resolver.Get <ModuleFinder>();

            moduleFinder.LoadFromAssemblies();
            this.Modules = moduleFinder.Modules;
            foreach (ModuleBase module in this.Modules)
            {
                Logger.Trace($"{module.GetType().Name.Replace("Module", "")}-{module.GetType().Assembly.GetName().Version} module loaded");
            }
            this.Modules.ForEach(module => this.resolver.Bind <ModuleBase>().To(module));
            this.Modules.ForEach(module => module.Initialize());
        }
        public void LoadAnAlphaVersion()
        {
            NugetAssemblyLocator locator = NugetPackageDependencyLoader.CreateLocator();
            // Assembly assembly = locator.Locate("Polly, Version=6.0.0.0, Culture=neutral, PublicKeyToken=c8a3ffc3f8f825cc");
            Assembly assembly = locator.Locate("amotiq.base.api, Version=0.0.1.1, Culture=neutral, PublicKeyToken=c8a3ffc3f8f825cc");

            Assert.IsNotNull(assembly);
        }
        public static Assembly Locate(string assemblyName, params SearchLocation[] locations)
        {
            NugetAssemblyLocator locator = NugetPackageDependencyLoader.CreateLocator();

            locator.Locations.InsertRange(0, locations);
            Version defaultVersion = typeof(CoreModule).Assembly.GetName().Version;

            return(locator.Locate(assemblyName, defaultVersion));
        }
Beispiel #4
0
        public static bool Run(string[] args)
        {
#if DEBUG
            if (args.Length > 0 && args[0] != "statistics")
            {
                System.Diagnostics.Debugger.Launch();
            }
#endif
            Generator.InitializeLogger(args);
            NugetPackageDependencyLoader.Activate();
            NugetPackageDependencyLoader.Locations.Insert(0, new SearchLocation(SharedPath));
            return(Generator.Initialize()
                   .PreloadModules(SharedPath, "KY.Generator.*.dll")
                   .SetParameters(args)
                   .Run());
        }
        public static LocateAssemblyResult Locate(string assemblyName, bool isBeforeBuild, params SearchLocation[] locations)
        {
            foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies().Where(x => !x.IsDynamic))
            {
                if (assembly.Location.Equals(assemblyName, StringComparison.CurrentCultureIgnoreCase) || assembly.GetName().Name.Equals(assemblyName, StringComparison.CurrentCultureIgnoreCase))
                {
                    return(new LocateAssemblyResult(assembly));
                }
            }

            Assembly entryAssembly = Assembly.GetEntryAssembly();
            ProcessorArchitecture entryArchitecture = entryAssembly.GetName().ProcessorArchitecture;

            try
            {
                ProcessorArchitecture assemblyArchitecture = AssemblyName.GetAssemblyName(assemblyName).ProcessorArchitecture;
                bool isCompatible64 = (entryArchitecture == ProcessorArchitecture.Amd64 || entryArchitecture == ProcessorArchitecture.MSIL) &&
                                      (assemblyArchitecture == ProcessorArchitecture.Amd64 || assemblyArchitecture == ProcessorArchitecture.MSIL);
                bool isCompatible86 = entryArchitecture == ProcessorArchitecture.X86 && assemblyArchitecture == ProcessorArchitecture.X86;
                if (!isCompatible64 && !isCompatible86)
                {
                    return(new LocateAssemblyResult(assemblyArchitecture));
                }
            }
            catch (FileNotFoundException)
            {
                if (isBeforeBuild)
                {
                    return(new LocateAssemblyResult());
                }
                throw;
            }

            try
            {
                SwitchableFramework? assemblyFramework   = null;
                string[]             frameworkFiles      = FileSystem.GetFiles(RuntimeEnvironment.GetRuntimeDirectory(), "*.dll");
                IEnumerable <string> loadedAssemblies    = AppDomain.CurrentDomain.GetAssemblies().Where(x => !x.IsDynamic).Select(x => x.Location);
                PathAssemblyResolver resolver            = new PathAssemblyResolver(loadedAssemblies.Concat(frameworkFiles));
                MetadataLoadContext  metadataLoadContext = new MetadataLoadContext(resolver);

                using (metadataLoadContext)
                {
                    Assembly assemblyData = metadataLoadContext.LoadFromAssemblyPath(assemblyName);
                    IList <CustomAttributeData> customAttributeData = assemblyData.GetCustomAttributesData();

                    foreach (CustomAttributeData attributeData in customAttributeData)
                    {
                        try
                        {
                            assemblyFramework = assemblyFramework ?? attributeData.ConstructorArguments.Select(x => x.Value as string)
                                                .Where(x => x != null)
                                                .Select(TryParseFrameworkName)
                                                .FirstOrDefault()?
                                                .GetSwitchableFramework();
                        }
                        catch
                        {
                            // Some unnecessary attributes can not be read by a assembly with the wrong framework version, so ignore them
                        }
                    }
                    assemblyFramework = assemblyFramework ?? SwitchableFramework.None;
                }

                SwitchableFramework entryFramework = entryAssembly.GetSwitchableFramework();
                if (entryFramework != assemblyFramework && assemblyFramework != SwitchableFramework.None)
                {
                    return(new LocateAssemblyResult(assemblyFramework.Value));
                }
            }
            catch (TypeLoadException exception)
            {
                Logger.Warning($"Could not check framework compatibility, because {exception.TypeName} could not be loaded\n{exception.Message}");
            }
            catch (FileNotFoundException exception)
            {
                Logger.Warning($"Could not check framework compatibility, because an assembly could not be found\n{exception.Message}");
            }
            catch (Exception exception)
            {
                Logger.Warning($"Could not check framework compatibility, because an error occurred\n{exception.Message}");
            }
            NugetAssemblyLocator locator = NugetPackageDependencyLoader.CreateLocator();

            locator.Locations.InsertRange(0, locations);
            Version defaultVersion = typeof(CoreModule).Assembly.GetName().Version;

            return(new LocateAssemblyResult(locator.Locate(assemblyName, defaultVersion)));
        }