Пример #1
0
        public Assembly LoadExternalAssembly(ExtensionDescriptor descriptor)
        {
            var projectContext = ProjectContext.CreateContextForEachFramework(Path.Combine(descriptor.Location, descriptor.Id)).FirstOrDefault();

            if (projectContext == null)
            {
                return(null);
            }

            var assemblyNames = new HashSet <string>(ApplicationAssemblyNames(), StringComparer.OrdinalIgnoreCase);

            // TODO: find a way to select the right configuration
            var libraryExporter = projectContext.CreateExporter("Debug");

            Assembly assembly = null;

            foreach (var libraryExport in libraryExporter.GetAllExports())
            {
                foreach (var asset in libraryExport.RuntimeAssemblyGroups.GetDefaultAssets())
                {
                    if (assemblyNames.Add(asset.Name))
                    {
                        try
                        {
                            if (asset.Name == descriptor.Id)
                            {
                                if (!File.Exists(asset.ResolvedPath))
                                {
                                    var location = Path.Combine(descriptor.Location, descriptor.Id);


                                    // We can do this but it relies on the dotnet cli sdk
                                    //Command.CreateDotNet("build", new [] { location }).Execute();


                                    // With an adapted compiler we only need to embed "csc.dll" and "csc.runtimeconfig.json"
                                    var success = new CSharpExtensionCompiler().Compile(projectContext, "Debug", projectContext.RootDirectory);
                                }
                            }

                            var loadedAssembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(asset.ResolvedPath);
                            if (loadedAssembly.GetName().Name == projectContext.ProjectFile.Name)
                            {
                                assembly = loadedAssembly;
                            }
                        }
                        catch
                        {
                        }
                    }
                }
            }

            return(assembly);
        }
Пример #2
0
        internal void CompileProject(ProjectContext context)
        {
            var compiler    = new CSharpExtensionCompiler();
            var success     = compiler.Compile(context, Configuration, _probingFolderPath);
            var diagnostics = compiler.Diagnostics;

            if (success && diagnostics.Count == 0)
            {
                if (_logger.IsEnabled(LogLevel.Information))
                {
                    _logger.LogInformation("{0} was successfully compiled", context.ProjectName());
                }
            }
            else if (success && diagnostics.Count > 0)
            {
                if (_logger.IsEnabled(LogLevel.Warning))
                {
                    _logger.LogWarning("{0} was compiled but has warnings", context.ProjectName());

                    foreach (var diagnostic in diagnostics)
                    {
                        _logger.LogWarning(diagnostic);
                    }
                }
            }
            else
            {
                if (_logger.IsEnabled(LogLevel.Error))
                {
                    _logger.LogError($"{0} compilation failed", context.ProjectName());

                    foreach (var diagnostic in diagnostics)
                    {
                        _logger.LogError(diagnostic);
                    }
                }
            }
        }