示例#1
0
        public void LoadAssemblies(params string[] assembliesFiles)
        {
            Strings assemblyFolders = new Strings();
            Strings references      = new Strings();

            Assembly[] assemblies = new Assembly[assembliesFiles.Length];
            for (int i = 0; i < assembliesFiles.Length; ++i)
            {
                var assemblyFile = assembliesFiles[i];
                try
                {
                    var assembly = Assembly.LoadFile(assemblyFile);
                    assemblies[i] = assembly;

                    assemblyFolders.Add(Path.GetDirectoryName(assemblyFile));

                    // get references from the assembly, if any
                    foreach (var module in assembly.GetModules())
                    {
                        foreach (var reference in module.GetCustomAttributes <Reference>())
                        {
                            references.Add(reference.FileName);
                        }
                    }
                }
                catch (Exception e)
                {
                    throw new Error(e, "Cannot load assembly file: {0}", assemblyFile);
                }
            }

            using (var extensionLoader = new ExtensionLoader())
            {
                foreach (var dllName in references)
                {
                    try
                    {
                        foreach (var assemblyFolder in assemblyFolders)
                        {
                            var candidatePath = Path.Combine(assemblyFolder, dllName);
                            if (File.Exists(candidatePath))
                            {
                                extensionLoader.LoadExtension(candidatePath, false);
                            }
                        }
                    }
                    catch (Error)
                    {
                        // skip non-sharpmake extension
                    }
                }
            }

            LoadAssemblies(assemblies);
        }
示例#2
0
 public ILoadInfo LoadExtension(string file)
 {
     // Load extensions if they were passed as references (platforms,
     // entry point execution to add new ISourceAttributeParser...)
     using (var extensionLoader = new ExtensionLoader())
     {
         var parserCount = _builder._attributeParsers.Count;
         var assembly    = extensionLoader.LoadExtension(file, false);
         return(new LoadInfo(assembly, _builder._attributeParsers.Skip(parserCount)));
     }
 }
示例#3
0
        // Expect a list of existing files with their full path
        public void LoadSharpmakeFiles(params string[] sharpmakeFiles)
        {
            Assembler assembler = new Assembler();

            // Add sharpmake assembly
            Assembly sharpmake = Assembly.GetAssembly(typeof(Builder));

            assembler.Assemblies.Add(sharpmake);

            // Add generators assembly to be able to reference them from .sharpmake files.
            DirectoryInfo entryDirectoryInfo = new DirectoryInfo(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location));
            string        generatorsAssembly = entryDirectoryInfo.FullName + Path.DirectorySeparatorChar + "Sharpmake.Generators.dll";
            Assembly      generators         = Assembly.LoadFrom(generatorsAssembly);

            assembler.Assemblies.Add(generators);

            _projectAssembly = assembler.BuildAssembly(sharpmakeFiles);

            if (_projectAssembly == null)
            {
                throw new InternalError();
            }

            // Keep track of assemblies explicitly referenced by compiled files
            _referenceList = assembler.References.ToDictionary(fullpath => AssemblyName.GetAssemblyName(fullpath).FullName.ToString(), fullpath => fullpath);

            // load platforms if they were passed as references
            using (var extensionLoader = new ExtensionLoader())
            {
                foreach (var referencePath in assembler.References)
                {
                    try
                    {
                        extensionLoader.LoadExtension(referencePath, false);
                    }
                    catch (Error)
                    {
                        // skip non-sharpmake extension
                    }
                }
            }

            AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

            LoadAssemblies(_projectAssembly);
        }
示例#4
0
        private Assembly BuildAndLoadAssembly(IList <string> sharpmakeFiles)
        {
            Assembler assembler = new Assembler();

            // Add sharpmake assembly
            assembler.Assemblies.Add(SharpmakeAssembly.Value);

            // Add generators assembly to be able to reference them from .sharpmake.cs files
            assembler.Assemblies.Add(SharpmakeGeneratorAssembly.Value);

            Assembly newAssembly = assembler.BuildAssembly(sharpmakeFiles.ToArray());

            if (newAssembly == null)
            {
                throw new InternalError();
            }

            // Keep track of assemblies explicitly referenced by compiled files
            foreach (var fullpath in assembler.References.Distinct())
            {
                var    assemblyName = AssemblyName.GetAssemblyName(fullpath).FullName;
                string assemblyPath;
                if (_references.TryGetValue(assemblyName, out assemblyPath) && !string.Equals(assemblyPath, fullpath, StringComparison.OrdinalIgnoreCase))
                {
                    throw new Error($"Assembly {assemblyName} present in two different locations: {fullpath} and {assemblyPath}.");
                }
                _references[assemblyName] = fullpath;
            }

            // Load platforms if they were passed as references
            using (var extensionLoader = new ExtensionLoader())
            {
                foreach (var referencePath in assembler.References)
                {
                    extensionLoader.LoadExtension(referencePath, false);
                }
            }

            _builtAssemblies.Add(newAssembly);
            return(newAssembly);
        }