public WorkspaceConfiguration( TargetCompilationConfiguration compilationConfiguration, InteractiveDependencyResolver dependencyResolver) { CompilationConfiguration = compilationConfiguration; DependencyResolver = dependencyResolver; }
static InteractiveDependencyResolver CreateDependencyResolver( AgentType agentType, IEnumerable <string> assemblySearchPaths) { var dependencyResolver = new InteractiveDependencyResolver(agentType: agentType); var consoleOrWpf = agentType == AgentType.WPF || agentType == AgentType.Console; foreach (var strPath in assemblySearchPaths) { var path = new FilePath(strPath); if (path.DirectoryExists) { Log.Info(TAG, $"Searching assembly path {path}"); dependencyResolver.AddAssemblySearchPath( path, scanRecursively: consoleOrWpf); if (!consoleOrWpf) { path = path.Combine("Facades"); if (path.DirectoryExists) { dependencyResolver.AddAssemblySearchPath(path); } } } else { Log.Warning(TAG, $"Assembly search path {strPath} does not exist"); } } return(dependencyResolver); }
static InteractiveDependencyResolver CreateDependencyResolver( TargetCompilationConfiguration configuration) { var dependencyResolver = new InteractiveDependencyResolver(configuration); var scanRecursively = configuration.Sdk?.TargetFramework.Identifier == ".NETFramework"; foreach (FilePath path in configuration.AssemblySearchPaths) { if (path.DirectoryExists) { Log.Info(TAG, $"Searching assembly path {path} (recursive: {scanRecursively})"); dependencyResolver.AddAssemblySearchPath( path, scanRecursively); if (!scanRecursively) { var facadesPath = path.Combine("Facades"); if (facadesPath.DirectoryExists) { Log.Info(TAG, $"Searching assembly path {facadesPath}"); dependencyResolver.AddAssemblySearchPath(facadesPath); } } } else { Log.Warning(TAG, $"Assembly search path {path} does not exist"); } } return(dependencyResolver); }
static Type ResolveHostObjectType( InteractiveDependencyResolver dependencyResolver, TargetCompilationConfiguration configuration, AgentType agentType) { if (System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription.StartsWith( ".NET Core", StringComparison.OrdinalIgnoreCase)) { return(typeof(object)); } using (var assemblyContext = new EvaluationAssemblyContext()) { string globalStateAssemblyCachePath = null; if (configuration.GlobalStateAssembly.Content.PEImage != null) { globalStateAssemblyCachePath = dependencyResolver.CacheRemoteAssembly( configuration.GlobalStateAssembly); } var resolvedAssemblies = dependencyResolver .Resolve(new [] { configuration.GlobalStateAssembly }) .Select(r => new AssemblyDefinition(r.AssemblyName, r.Path)); assemblyContext.AddRange(resolvedAssemblies); var globalStateAssemblyDef = resolvedAssemblies.First( assembly => ResolvedAssembly.NameEqualityComparer.Default.Equals( assembly.Name, configuration.GlobalStateAssembly.Name)); netStandardAssembly = netStandardAssembly ?? Assembly.ReflectionOnlyLoadFrom( new FilePath(Assembly.GetExecutingAssembly().Location) .ParentDirectory .Combine("netstandard.dll")); xiAssembly = xiAssembly ?? Assembly.ReflectionOnlyLoadFrom( new FilePath(Assembly.GetExecutingAssembly().Location) .ParentDirectory .Combine("Xamarin.Interactive.dll")); Assembly globalStateAssembly; if (globalStateAssemblyDef.Name.Name == "Xamarin.Interactive") { globalStateAssembly = xiAssembly; } else { globalStateAssembly = Assembly.ReflectionOnlyLoadFrom( globalStateAssemblyCachePath ?? globalStateAssemblyDef.Content.Location); } return(globalStateAssembly.GetType(configuration.GlobalStateTypeName)); } }
public InteractiveSourceReferenceResolver(InteractiveDependencyResolver dependencyResolver) { if (dependencyResolver == null) { throw new ArgumentNullException(nameof(dependencyResolver)); } this.dependencyResolver = dependencyResolver; }
public WorkspaceConfiguration( TargetCompilationConfiguration compilationConfiguration, InteractiveDependencyResolver dependencyResolver, bool includePEImagesInDependencyResolution, Type hostObjectType = null) { CompilationConfiguration = compilationConfiguration; DependencyResolver = dependencyResolver; IncludePEImagesInDependencyResolution = includePEImagesInDependencyResolution; HostObjectType = hostObjectType; }
public static void Initialize() { InitializingTask = Task.Run(() => { var gacPaths = GacPaths; // AgentType.Unknown will result in the GAC cache not being used. var dependencyResolver = new InteractiveDependencyResolver(AgentType.Unknown); foreach (var gacPath in gacPaths) { ProcessGacPath(gacPath, dependencyResolver); } }); }
public WorkspaceConfiguration( TargetCompilationConfiguration compilationConfiguration, InteractiveDependencyResolver dependencyResolver, bool includePEImagesInDependencyResolution, Type hostObjectType = null) { CompilationConfiguration = compilationConfiguration ?? throw new ArgumentNullException(nameof(compilationConfiguration)); DependencyResolver = dependencyResolver ?? throw new ArgumentNullException(nameof(dependencyResolver)); IncludePEImagesInDependencyResolution = includePEImagesInDependencyResolution; HostObjectType = hostObjectType; }
public RoslynCompilationWorkspace( InteractiveDependencyResolver dependencyResolver, TargetCompilationConfiguration compilationConfiguration, AgentType agentType, Type hostObjectType = null, bool includePeImagesInResolution = false) { if (dependencyResolver == null) { throw new ArgumentNullException(nameof(dependencyResolver)); } if (compilationConfiguration == null) { throw new ArgumentNullException(nameof(compilationConfiguration)); } workspace = new InteractiveWorkspace(); sourceReferenceResolver = new InteractiveSourceReferenceResolver(dependencyResolver); metadataReferenceResolver = new InteractiveMetadataReferenceResolver(dependencyResolver); monoScriptCompilationPatcher = new MonoScriptCompilationPatcher( assemblyNamePrefixBytes); DependencyResolver = dependencyResolver; this.hostObjectType = hostObjectType; EvaluationContextId = compilationConfiguration.EvaluationContextId; initialImports = compilationConfiguration.DefaultUsings.ToImmutableArray(); initialWarningSuppressions = compilationConfiguration.DefaultWarningSuppressions.ToImmutableArray(); initialDiagnosticOptions = initialWarningSuppressions.ToImmutableDictionary( warningId => warningId, warningId => ReportDiagnostic.Suppress); initialReferences = dependencyResolver.ResolveDefaultReferences(); var byNameImplicitReferences = new Tuple <string, Func <bool> > [] { Tuple.Create <string, Func <bool> > ("Microsoft.CSharp", () => true), // Add an implicit by name reference to System.ValueTuple if and only if the // agent is Console or WPF, and the current runtime does not have SVT in corlib. // This check makes sense because the Console and WPF agents run on the same // runtime as the current client. The other agents are on runtimes controlled // by us, and we can be sure that System.ValueTuple will be part of corlib on // those agents. A bug in .NET 4.7 means we can't just always implicitly add // the reference and have the runtime figure out what should be done. This bug // is fixed in .NET 4.7.1. Tuple.Create <string, Func <bool> > ("System.ValueTuple", () => { return((agentType == AgentType.Console || agentType == AgentType.WPF) && typeof(object).Assembly.GetType("System.ValueTuple") == null); }) }; foreach (var implicitReference in byNameImplicitReferences) { if (!implicitReference.Item2()) { continue; } var assembly = DependencyResolver.ResolveWithoutReferences( new AssemblyName(implicitReference.Item1)); if (assembly != null) { initialReferences = initialReferences.Add( MetadataReference.CreateFromFile(assembly.Path)); } } this.includePeImagesInResolution = includePeImagesInResolution; CompletionService = workspace .Services .GetLanguageServices(LanguageNames.CSharp) .GetService <CompletionService> (); }
static Type ResolveHostObjectType( InteractiveDependencyResolver dependencyResolver, TargetCompilationConfiguration configuration) { if (configuration.GlobalStateType?.Name == null) { return(typeof(object)); } if (System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription.StartsWith( ".NET Core", StringComparison.OrdinalIgnoreCase)) { // .NET Core does not support reflection-only-load and due to a bad design decision // in Roslyn's scripting support, we cannot pass SRM or Roslyn type symbols to // the compiler configuration for it to know the global API shape. // cf. https://github.com/dotnet/corefx/issues/2800 // cf. https://github.com/dotnet/roslyn/issues/20920 // // Employ a hack here to get our base globals type working by checking the name // of the type and assuming it is or dervives from EvaluationContextGlobalObject. // // Unfortunately this still makes agent-specific API such as "MainWindow" unavailable // in the Web UX (since Roslyn is running under ASP.NET Core). if (dncGlobalStateTypeHackRegex.IsMatch(configuration.GlobalStateType.Name)) { return(typeof(EvaluationContextGlobalObject)); } return(typeof(object)); } using (var assemblyContext = new EvaluationAssemblyContext()) { string globalStateAssemblyCachePath = null; if (configuration.GlobalStateType.Assembly.Content.PEImage != null) { globalStateAssemblyCachePath = dependencyResolver.CacheRemoteAssembly( configuration.GlobalStateType.Assembly); } var resolvedAssemblies = dependencyResolver .Resolve(new [] { configuration.GlobalStateType.Assembly }) .Select(r => new AssemblyDefinition(r.AssemblyName, r.Path)); assemblyContext.AddRange(resolvedAssemblies); var globalStateAssemblyDef = resolvedAssemblies.First( assembly => ResolvedAssembly.NameEqualityComparer.Default.Equals( assembly.Name, configuration.GlobalStateType.Assembly.Name)); netStandardAssembly = netStandardAssembly ?? Assembly.ReflectionOnlyLoadFrom( new FilePath(Assembly.GetExecutingAssembly().Location) .ParentDirectory .Combine("netstandard.dll")); xiAssembly = xiAssembly ?? Assembly.ReflectionOnlyLoadFrom( new FilePath(Assembly.GetExecutingAssembly().Location) .ParentDirectory .Combine("Xamarin.Interactive.dll")); Assembly globalStateAssembly; if (globalStateAssemblyDef.Name.Name == "Xamarin.Interactive") { globalStateAssembly = xiAssembly; } else { globalStateAssembly = Assembly.ReflectionOnlyLoadFrom( globalStateAssemblyCachePath ?? globalStateAssemblyDef.Content.Location); } return(globalStateAssembly.GetType(configuration.GlobalStateType.Name)); } }