Example #1
0
 public WorkspaceConfiguration(
     TargetCompilationConfiguration compilationConfiguration,
     InteractiveDependencyResolver dependencyResolver)
 {
     CompilationConfiguration = compilationConfiguration;
     DependencyResolver       = dependencyResolver;
 }
Example #2
0
        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 WorkspaceConfiguration(
     TargetCompilationConfiguration compilationConfiguration,
     InteractiveDependencyResolver dependencyResolver,
     bool includePEImagesInDependencyResolution,
     Type hostObjectType = null)
 {
     CompilationConfiguration = compilationConfiguration;
     DependencyResolver       = dependencyResolver;
     IncludePEImagesInDependencyResolution = includePEImagesInDependencyResolution;
     HostObjectType = hostObjectType;
 }
Example #5
0
        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;
        }
        internal static async Task LoadFormsAgentExtensions(
            Version formsVersion,
            TargetCompilationConfiguration configuration,
            IEvaluationContextManager evaluationContextManager,
            DependencyResolver dependencyResolver)
        {
            var formsAssembly = InteractiveInstallation.Default.LocateFormsAssembly(configuration.Sdk?.Id);

            if (string.IsNullOrWhiteSpace(formsAssembly))
            {
                return;
            }

            var deps             = dependencyResolver.Resolve(new [] { new FilePath(formsAssembly) });
            var includePeImage   = configuration.IncludePEImagesInDependencyResolution;
            var assembliesToLoad = deps.Select(dep => {
                var peImage = includePeImage ? GetFileBytes(dep.Path) : null;
                var syms    = includePeImage ? GetDebugSymbolsFromAssemblyPath(dep.Path) : null;
                return(new AssemblyDefinition(
                           dep.AssemblyName,
                           dep.Path,
                           peImage: peImage,
                           debugSymbols: syms
                           ));
            }).ToArray();

            var results = await evaluationContextManager.LoadAssembliesAsync(
                configuration.EvaluationContextId,
                assembliesToLoad);

            var failed = results.Where(p => !p.Success);

            if (failed.Any())
            {
                var failedLoads = string.Join(", ", failed.Select(p => p.AssemblyName.Name));
                Log.Warning(
                    TAG,
                    $"Xamarin.Forms reference detected, but integration may not have" +
                    $" loaded properly. Assemblies that did not load: {failedLoads}");
            }
        }
Example #7
0
        internal static async Task LoadFormsAgentExtensions(
            Version formsVersion,
            TargetCompilationConfiguration configuration,
            IEvaluationContextManager evaluationContextManager,
            DependencyResolver dependencyResolver)
        {
            var formsAssembly = InteractiveInstallation.Default.LocateFormsAssembly(configuration.Sdk?.Id);

            if (string.IsNullOrWhiteSpace(formsAssembly))
            {
                return;
            }

            var deps = dependencyResolver.Resolve(new [] { new FilePath(formsAssembly) });

            // Now dig out the resolved assembly that is Xamarin.Forms.Core, and compare it to the
            // default ref.
            var resolvedFormsAssembly = deps.FirstOrDefault(d => d.AssemblyName.Name == "Xamarin.Forms.Core");

            if (resolvedFormsAssembly == null)
            {
                Log.Warning(TAG,
                            "Cannot enable Forms integration because Forms cannot be " +
                            "resolved. Check log for assembly search path issues.");
                return;
            }
            var ourVersion   = resolvedFormsAssembly.AssemblyName.Version;
            var theirVersion = formsVersion;

            if (ourVersion.Major != theirVersion.Major)
            {
                Log.Warning(
                    TAG,
                    "Assembly version mismatch between app's Xamarin.Forms.Core and" +
                    $"our referenced Xamarin.Forms.Core. Our version: {ourVersion}, " +
                    $"their version: {theirVersion}. Won't load Xamarin.Forms agent " +
                    "integration, as it probably won't work."
                    );
                return;
            }

            var includePeImage   = configuration.IncludePEImagesInDependencyResolution;
            var assembliesToLoad = deps.Select(dep => {
                var peImage = includePeImage ? GetFileBytes(dep.Path) : null;
                var syms    = includePeImage ? GetDebugSymbolsFromAssemblyPath(dep.Path) : null;
                return(new AssemblyDefinition(
                           dep.AssemblyName,
                           dep.Path,
                           peImage: peImage,
                           debugSymbols: syms
                           ));
            }).ToArray();

            var results = await evaluationContextManager.LoadAssembliesAsync(
                configuration.EvaluationContextId,
                assembliesToLoad);

            var failed = results.Where(p => !p.Success);

            if (failed.Any())
            {
                var failedLoads = string.Join(", ", failed.Select(p => p.AssemblyName.Name));
                Log.Warning(
                    TAG,
                    $"Xamarin.Forms reference detected, but integration may not have" +
                    $" loaded properly. Assemblies that did not load: {failedLoads}");
            }
        }
Example #8
0
        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));
            }
        }