Esempio n. 1
0
        public async Task CanCreatePluginNameWithGenerator()
        {
            // Arrange
            var code = @"public class MyClass
                   {
                       public void RunThings()
                       {
                           var y = 0;
                           var a = 1;
           
                           a = y + 10;
                       
                           Debug.WriteLine(y + a);
                       }
                   }";

            var options = new RoslynPluginCatalogOptions()
            {
                PluginNameOptions = new PluginNameOptions()
                {
                    PluginNameGenerator = (nameOptions, type) => "HelloThereFromGenerator"
                },
            };

            var catalog = new RoslynPluginCatalog(code, options);

            // Act
            await catalog.Initialize();

            // Assert
            var plugin = catalog.Get("HelloThereFromGenerator", Version.Parse("1.0.0.0"));

            Assert.NotNull(plugin);
        }
Esempio n. 2
0
        public async Task PluginDefaultsToVersion100()
        {
            // Arrange
            var code = @"public class MyClass
                   {
                       public void RunThings()
                       {
                           var y = 0;
                           var a = 1;
           
                           a = y + 10;
                       
                           Debug.WriteLine(y + a);
                       }
                   }";

            var options = new RoslynPluginCatalogOptions()
            {
                PluginName = "MyPlugin",
            };

            var catalog = new RoslynPluginCatalog(code, options);

            // Act
            await catalog.Initialize();

            var plugin = catalog.Get("MyPlugin", new Version(1, 0, 0, 0));

            // Assert
            Assert.Equal(new Version(1, 0, 0, 0), plugin.Version);
        }
Esempio n. 3
0
        public async Task CanCreatePluginWithNameAndVersion()
        {
            // Arrange
            var code = @"public class MyClass
                   {
                       public void RunThings()
                       {
                           var y = 0;
                           var a = 1;
           
                           a = y + 10;
                       
                           Debug.WriteLine(y + a);
                       }
                   }";

            var options = new RoslynPluginCatalogOptions()
            {
                PluginName = "MyPlugin", PluginVersion = new Version(1, 1)
            };

            var catalog = new RoslynPluginCatalog(code, options);

            // Act
            await catalog.Initialize();

            var plugin = catalog.Get("MyPlugin", new Version(1, 1));

            // Assert
            Assert.NotNull(plugin);
        }
Esempio n. 4
0
        public static async Task <List <Type> > CreateCatalog(string code, RoslynPluginCatalogOptions options = null)
        {
            var catalog = new RoslynPluginCatalog(code, options);
            await catalog.Initialize();

            return(catalog.GetPlugins().Select(x => x.Type).ToList());
        }
        public async Task CanAddNamespace()
        {
            // Arrange
            var code = @"public class MyClass
                   {
                       public void RunThings()
                       {
                            JsonConvert.SerializeObject(15);
                       }
                   }";

            var options = new RoslynPluginCatalogOptions()
            {
                AdditionalReferences = new List <Assembly>()
                {
                    typeof(Newtonsoft.Json.JsonConvert).Assembly
                },
                AdditionalNamespaces = new List <string>()
                {
                    "Newtonsoft.Json"
                }
            };

            await TestHelpers.CompileRegular(code, options);
        }
Esempio n. 6
0
        public async Task CanConfigureToHaveUniqueTypeNames()
        {
            // Arrange
            var code  = "var i = 15; var x = \"hello\"; return x;";
            var code2 = "var i = 15; var x = \"hello\"; return x;";

            var randomGenerator = new Random(Guid.NewGuid().GetHashCode());

            var options = new RoslynPluginCatalogOptions()
            {
                TypeNameGenerator = catalogOptions =>
                {
                    var defaultTypeName = catalogOptions.TypeName;
                    var result          = $"{defaultTypeName}{randomGenerator.Next(int.MaxValue)}";

                    return(result);
                }
            };

            // Act
            var types = await TestHelpers.CompileScript(code, options);

            var types2 = await TestHelpers.CompileScript(code2, options);

            // Assert
            var firstType  = types.First();
            var secondType = types2.First();

            Assert.NotEqual(firstType.Name, secondType.Name);
        }
Esempio n. 7
0
        public async Task CanCreatePluginVersionWithGenerator()
        {
            // Arrange
            var code = @"public class MyClass
                   {
                       public void RunThings()
                       {
                           var y = 0;
                           var a = 1;
           
                           a = y + 10;
                       
                           Debug.WriteLine(y + a);
                       }
                   }";

            var options = new RoslynPluginCatalogOptions()
            {
                PluginNameOptions = new PluginNameOptions()
                {
                    PluginVersionGenerator = (nameOptions, type) => new Version(2, 0, 0)
                }
            };

            var catalog = new RoslynPluginCatalog(code, options);

            // Act
            await catalog.Initialize();

            var plugin = catalog.Single();

            // Assert
            Assert.Equal(new Version(2, 0, 0), plugin.Version);
        }
Esempio n. 8
0
        public static async Task <List <Type> > CompileRegular(string code, RoslynPluginCatalogOptions options = null)
        {
            var catalog  = new RegularInitializer(code, options);
            var assembly = await catalog.CreateAssembly();

            var result = assembly.GetTypes().Where(x => x.GetCustomAttribute(typeof(CompilerGeneratedAttribute), true) == null).ToList();

            return(result);
        }
Esempio n. 9
0
        public async Task ThrowsWithMissingNamespaceNameGenerator()
        {
            var code    = "var x = \"hello\"; return x;";
            var options = new RoslynPluginCatalogOptions()
            {
                NamespaceNameGenerator = null
            };

            await Assert.ThrowsAsync <ArgumentNullException>(async() => await TestHelpers.CompileScript(code, options));
        }
Esempio n. 10
0
        public static async Task <List <Type> > CreateCatalog(string code, RoslynPluginCatalogOptions options = null)
        {
            var catalog = new RoslynPluginCatalog(code, options);
            await catalog.Initialize();

            var plugin = catalog.Single();

            // var result = plugin.GetTypes().Where(x => x.GetCustomAttribute(typeof(CompilerGeneratedAttribute), true) == null).ToList();

            return(catalog.GetPlugins().Select(x => x.Type).ToList());
            // return result;
        }
Esempio n. 11
0
        public async Task HasDefaultNamespace()
        {
            // Arrange
            var code = "var x = \"hello\"; return x;";

            // Act
            var type = (await TestHelpers.CompileScript(code)).First();

            // Assert
            var defaultOptions = new RoslynPluginCatalogOptions();

            Assert.Equal(defaultOptions.NamespaceName, type.Namespace);
        }
Esempio n. 12
0
        public RoslynPluginCatalog(string code, RoslynPluginCatalogOptions options = null, string description = null, string productVersion = null)
        {
            if (string.IsNullOrWhiteSpace(code))
            {
                throw new ArgumentOutOfRangeException(nameof(code), code, "Code can not be null or empty");
            }

            _code    = code;
            _options = options ?? new RoslynPluginCatalogOptions();

            _options.PluginNameOptions.PluginDescriptionGenerator    = (nameOptions, type) => description;
            _options.PluginNameOptions.PluginProductVersionGenerator = (nameOptions, type) => productVersion;
        }
Esempio n. 13
0
        public async Task CanConfigureNamespace()
        {
            // Arrange
            var code    = "var x = \"hello\"; return x;";
            var options = new RoslynPluginCatalogOptions()
            {
                NamespaceName = "MyTestNamespace"
            };

            // Act
            var type = (await TestHelpers.CompileScript(code, options)).First();

            // Assert
            Assert.Equal("MyTestNamespace", type.Namespace);
        }
Esempio n. 14
0
        public async Task CanConfigureMethodNameGenerator()
        {
            // Arrange
            var code    = "var x = \"hello\"; return x;";
            var options = new RoslynPluginCatalogOptions()
            {
                MethodNameGenerator = catalogOptions => "MyMethod"
            };

            // Act
            var type   = (await TestHelpers.CompileScript(code, options)).First();
            var method = type.GetMethods().First();

            // Assert
            Assert.Equal("MyMethod", method.Name);
        }
Esempio n. 15
0
        public async Task CanConfigureReturnsTask()
        {
            // Arrange
            var code    = "var x = \"hello\"; return x;";
            var options = new RoslynPluginCatalogOptions()
            {
                ReturnsTask = false
            };

            // Act
            var type   = (await TestHelpers.CompileScript(code, options)).First();
            var method = type.GetMethods().First();

            // Assert
            Assert.Equal(typeof(string), method.ReturnParameter.ParameterType);
        }
Esempio n. 16
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // First we create a Roslyn Plugin Catalog which uses the scripting version of C#/Roslyn
            var script = "var x = \"Hello from Roslyn Plugin\"; return x;";
            var roslynScriptCatalog = new RoslynPluginCatalog(script);

            // We also create an another Roslyn Plugin Catalog to show how to create a plugin from a class.
            // This catalog also uses dependency injection, external references and additional namespaces.
            var code = @"public class MyClass
                   {
                       private ExternalService _service;
                       public MyClass(ExternalService service)
                       {
                            _service = service;
                       } 

                       public string RunThings()
                       {
                            var result = JsonConvert.SerializeObject(15);
                            result += _service.DoWork();
                            return result; 
                       }
                   }";

            var options = new RoslynPluginCatalogOptions()
            {
                PluginName           = "MyPlugin",
                PluginVersion        = new Version("1.5.0.0"),
                AdditionalReferences = new List <Assembly>()
                {
                    typeof(Newtonsoft.Json.JsonConvert).Assembly, typeof(ExternalService).Assembly
                },
                AdditionalNamespaces = new List <string>()
                {
                    "Newtonsoft.Json", "WebAppWithRoslyn"
                }
            };

            var roslynCodeCatalog = new RoslynPluginCatalog(code, options);

            services.AddPluginFramework()
            .AddPluginCatalog(new CompositePluginCatalog(roslynScriptCatalog, roslynCodeCatalog));

            services.AddSingleton <ExternalService>();
            services.AddControllers();
        }
Esempio n. 17
0
        public async Task CanConfigureTypeNameGenerator()
        {
            // Arrange
            var code = "var x = \"hello\"; return x;";

            var typeName = "MyTestTypeName";
            var options  = new RoslynPluginCatalogOptions()
            {
                TypeNameGenerator = catalogOptions => typeName + "15"
            };

            // Act
            var type = (await TestHelpers.CompileScript(code, options)).First();

            // Assert
            Assert.Equal("MyTestTypeName15", type.Name);
        }