示例#1
0
        private static void Main(string[] args)
        {
            // Script args can be passed in so the script / script packs can access them.
            // For example the sample script, the logger script pack is used which depends on these args
            var scriptArgs = new string[] { "-loglevel", "INFO" };
            var options    = new ScriptCsCatalogOptions {
                ScriptArgs = scriptArgs, References = new[] { typeof(IGreeter) }
            };

            // You can add script by script
            //var catalog = new ScriptCsCatalog(new[] { "Scripts/Test.csx" }, options);

            // Or an entire folder
            var catalog = new ScriptCsCatalog("Scripts", options);

            // Script Packs can be used, for a sample you just have to:
            // - run command "scriptcs -install" in the Scripts folder of the folder, not the one in bin/Debug
            // - uncomment the commented code in Test.csx
            // If you want to test it with the "script by script" constructor, you need to copy the scriptcs_packages.config file
            // in the bin/Debug folder and run the "scriptcs -install" command

            var container = new CompositionContainer(catalog);
            var greeter   = container.GetExportedValue <IGreeter>();

            greeter.Greet("Hello MEF!");
            Console.ReadLine();
        }
            public void ShouldRefreshPartsIfScriptsAreAdded(bool keepScriptSeparated)
            {
                // arrange
                var scriptName      = @"c:\workingdirectory\_plugins\SimpleScript.csx";
                var scriptName2     = @"c:\workingdirectory\_plugins\DoubleScript.csx";
                var fileSystem      = GetMockFileSystem(new string[0], new string[0]);
                var scriptCsCatalog = new ScriptCsCatalog("_plugins", GetOptions(fileSystem: fileSystem.Object,
                                                                                 keepScriptsSeparated: keepScriptSeparated));

                // act
                var mefHost = GetComposedMefHost(scriptCsCatalog);

                // assert
                scriptCsCatalog.LoadedFiles.Count.ShouldEqual(0);
                mefHost.Plugins.Count.ShouldEqual(0);

                // arrange
                UpdateFileSystem(fileSystem, new[] { scriptName, scriptName2 }, new[] { Scripts.SimpleScript, Scripts.DoubleScript });

                // act
                scriptCsCatalog.Refresh();

                // assert
                scriptCsCatalog.LoadedFiles.Count.ShouldEqual(2);
                mefHost.Plugins.Count.ShouldEqual(2);
            }
            public void ShouldRefreshPartsIfScriptIsModified()
            {
                // arrange
                var scriptName      = @"c:\workingdirectory\_plugins\SimpleScript.csx";
                var fileSystem      = GetMockFileSystem(scriptName, Scripts.SimpleScript);
                var scriptCsCatalog = new ScriptCsCatalog("_plugins", GetOptions(fileSystem: fileSystem.Object));

                // act
                var mefHost = GetComposedMefHost(scriptCsCatalog);

                // assert
                scriptCsCatalog.LoadedFiles.Count.ShouldEqual(1);
                mefHost.Plugins.Count.ShouldEqual(1);
                mefHost.Plugins[0].DoSomething().ShouldEqual("Simple");

                // arrange
                UpdateFileSystem(fileSystem, new[] { scriptName }, new[] { Scripts.DoubleScript });

                // act
                scriptCsCatalog.Refresh();

                // assert
                scriptCsCatalog.LoadedFiles.Count.ShouldEqual(1);
                mefHost.Plugins.Count.ShouldEqual(1);
                mefHost.Plugins[0].DoSomething().ShouldEqual("Double");
            }
            public void ShouldReturnPathIfFolderIsProvided()
            {
                // arrange
                var scriptName = @"c:\workingdirectory\_plugins\SimpleScript.csx";

                // act
                var scriptCsCatalog = new ScriptCsCatalog("_plugins",
                                                          GetOptions(fileSystem: GetMockFileSystem(scriptName, Scripts.SimpleScript).Object));

                // assert
                scriptCsCatalog.Path.ShouldEqual("_plugins");
            }
            public void ShouldReturnNameAndPath_ScriptsFolder()
            {
                // arrange
                var scriptName = @"c:\workingdirectory\_plugins\SimpleScript.csx";

                // act
                var scriptCsCatalog = new ScriptCsCatalog("_plugins",
                                                          GetOptions(fileSystem: GetMockFileSystem(scriptName, Scripts.SimpleScript).Object));

                // assert
                scriptCsCatalog.DisplayName.ShouldEqual("ScriptCsCatalog (Path=\"_plugins\")");
            }
            public void ShouldReturnNameAndPath_ScriptsFiles()
            {
                // arrange
                var scriptName = @"c:\workingdirectory\SimpleScript.csx";

                // act
                var scriptCsCatalog = new ScriptCsCatalog(new[] { scriptName },
                                                          GetOptions(fileSystem: GetMockFileSystem(scriptName, Scripts.SimpleScript).Object));

                // assert
                scriptCsCatalog.ToString().ShouldEqual("ScriptCsCatalog (Path=\"\")");
            }
            public void ShouldReturnNullIfScriptsFilesAreProvided()
            {
                // arrange
                var scriptName = @"c:\workingdirectory\SimpleScript.csx";

                // act
                var scriptCsCatalog = new ScriptCsCatalog(new[] { scriptName },
                                                          GetOptions(fileSystem: GetMockFileSystem(scriptName, Scripts.SimpleScript).Object));

                // assert
                scriptCsCatalog.SearchPattern.ShouldBeNull();
            }
            public void ShouldReturnSearchPatternIfFolderAndSearchPatternAreProvided()
            {
                // arrange
                var scriptName = @"c:\workingdirectory\_plugins\SimpleScript.csx";

                // act
                var scriptCsCatalog = new ScriptCsCatalog("_plugins", "*.script",
                                                          GetOptions(fileSystem: GetMockFileSystem(scriptName, Scripts.SimpleScript).Object));

                // assert
                scriptCsCatalog.SearchPattern.ShouldEqual("*.script");
            }
        private static MEFHost GetComposedMefHost(ScriptCsCatalog catalog)
        {
            // arrange
            var container = new CompositionContainer(catalog);
            var batch     = new CompositionBatch();
            var mefHost   = new MEFHost();

            batch.AddPart(mefHost);
            // act
            container.Compose(batch);

            return(mefHost);
        }
            public void ShouldNotThrowExceptionIfFolderProvidedIsEmpty()
            {
                // act
                var scriptCsCatalog = new ScriptCsCatalog("_plugins",
                                                          GetOptions(fileSystem: GetMockFileSystem(new string[0], new string[0]).Object));

                // assert
                var enumerator = scriptCsCatalog.GetEnumerator();

                enumerator.ShouldNotBeNull();
                enumerator.Current.ShouldBeNull();
                enumerator.MoveNext().ShouldBeFalse();
            }
            public void ShouldWorkWithSimpleScript()
            {
                // arrange
                var scriptName      = @"c:\workingdirectory\SimpleScript.csx";
                var scriptCsCatalog = new ScriptCsCatalog(new[] { scriptName },
                                                          GetOptions(fileSystem: GetMockFileSystem(scriptName, Scripts.SimpleScript).Object));

                // act
                var mefHost = GetComposedMefHost(scriptCsCatalog);

                // assert
                mefHost.Plugins.ShouldNotBeNull();
                mefHost.Plugins.Count.ShouldEqual(1);
                mefHost.Plugins[0].DoSomething().ShouldEqual("Simple");
            }
            public void ShouldThrowExceptionIfAlreadyDisposed()
            {
                // arrange
                var scriptName      = @"c:\workingdirectory\_plugins\SimpleScript.csx";
                var scriptName2     = @"c:\workingdirectory\_plugins\DoubleScript.csx";
                var scriptCsCatalog = new ScriptCsCatalog("_plugins",
                                                          GetOptions(fileSystem: GetMockFileSystem(new[] { scriptName, scriptName2 }, new[] { Scripts.SimpleScript, Scripts.DoubleScript }).Object));

                scriptCsCatalog.Dispose();

                // act
                var exception = Assert.Throws <ObjectDisposedException>(() => scriptCsCatalog.Refresh());

                exception.ObjectName.ShouldEqual("ScriptCsCatalog");
            }
            public void ShouldReturnAllScriptsFilesIfFolderIsProvided()
            {
                // arrange
                var scriptName  = @"c:\workingdirectory\_plugins\SimpleScript.csx";
                var scriptName2 = @"c:\workingdirectory\_plugins\DoubleScript.csx";

                // act
                var scriptCsCatalog = new ScriptCsCatalog("_plugins",
                                                          GetOptions(fileSystem: GetMockFileSystem(new string[] { scriptName, scriptName2 }, new[] { Scripts.SimpleScript, Scripts.DoubleScript }).Object));

                // assert
                scriptCsCatalog.LoadedFiles.Count.ShouldEqual(2);
                scriptCsCatalog.LoadedFiles[0].ShouldEqual(@"c:\workingdirectory\_plugins\SimpleScript.csx");
                scriptCsCatalog.LoadedFiles[1].ShouldEqual(@"c:\workingdirectory\_plugins\DoubleScript.csx");
            }
            public void ShouldWorkWithReferences()
            {
                // arrange
                var scriptName      = @"c:\workingdirectory\_plugins\SimpleScript.csx";
                var scriptCsCatalog = new ScriptCsCatalog("_plugins",
                                                          GetOptions(fileSystem: GetMockFileSystem(scriptName, Scripts.SimpleScriptWithoutReference).Object,
                                                                     references: new[] { typeof(IDoSomething) }));

                // act
                var mefHost = GetComposedMefHost(scriptCsCatalog);

                // assert
                mefHost.Plugins.ShouldNotBeNull();
                mefHost.Plugins.Count.ShouldEqual(1);
                mefHost.Plugins[0].DoSomething().ShouldEqual("Simple");
            }
            public void ShouldWorkWithMultipleScriptsNotMerged()
            {
                // arrange
                var scriptName      = @"c:\workingdirectory\SimpleScript.csx";
                var scriptName2     = @"c:\workingdirectory\DoubleScript.csx";
                var scriptCsCatalog = new ScriptCsCatalog(new[] { scriptName, scriptName2 },
                                                          GetOptions(fileSystem: GetMockFileSystem(new[] { scriptName, scriptName2 }, new[] { Scripts.SimpleScript, Scripts.DoubleScript }).Object, keepScriptsSeparated: true));

                // act
                var mefHost = GetComposedMefHost(scriptCsCatalog);

                // assert
                mefHost.Plugins.ShouldNotBeNull();
                mefHost.Plugins.Count.ShouldEqual(2);
                mefHost.Plugins[0].DoSomething().ShouldEqual("Simple");
                mefHost.Plugins[1].DoSomething().ShouldEqual("Double");
            }
            public void ShouldWorkWithAbsoluteFolder()
            {
                // arrange
                var scriptName      = @"c:\workingdirectory\_plugins\SimpleScript.csx";
                var scriptName2     = @"c:\workingdirectory\_plugins\DoubleScript.csx";
                var scriptCsCatalog = new ScriptCsCatalog(@"c:\workingdirectory\_plugins",
                                                          GetOptions(fileSystem: GetMockFileSystem(new string[] { scriptName, scriptName2 }, new[] { Scripts.SimpleScript, Scripts.DoubleScript }).Object));

                // act
                var mefHost = GetComposedMefHost(scriptCsCatalog);

                // assert
                mefHost.Plugins.ShouldNotBeNull();
                mefHost.Plugins.Count.ShouldEqual(2);
                mefHost.Plugins[0].DoSomething().ShouldEqual("Simple");
                mefHost.Plugins[1].DoSomething().ShouldEqual("Double");
            }
            public void ShouldEnumerateAllPartsFilesIfFolderIsProvided()
            {
                // arrange
                var scriptName  = @"c:\workingdirectory\_plugins\SimpleScript.csx";
                var scriptName2 = @"c:\workingdirectory\_plugins\DoubleScript.csx";

                // act
                var scriptCsCatalog = new ScriptCsCatalog("_plugins",
                                                          GetOptions(fileSystem: GetMockFileSystem(new string[] { scriptName, scriptName2 }, new[] { Scripts.SimpleScript, Scripts.DoubleScript }).Object));

                // assert
                var enumerator = scriptCsCatalog.GetEnumerator();

                enumerator.ShouldNotBeNull();
                enumerator.Current.ShouldBeNull();
                enumerator.MoveNext().ShouldBeTrue();
                enumerator.Current.ShouldNotBeNull();
                enumerator.Current.ToString().ShouldEqual("Submission#0+SimpleSomething");
                enumerator.MoveNext().ShouldBeTrue();
                enumerator.Current.ShouldNotBeNull();
                enumerator.Current.ToString().ShouldEqual("Submission#0+DoubleSomething");
                enumerator.MoveNext().ShouldBeFalse();
                enumerator.Current.ShouldBeNull();
            }
            public void ShouldRaiseOnchangedAndOnChanging()
            {
                // arrange
                bool onChangedCalled  = false;
                bool onChangingCalled = false;
                ComposablePartCatalogChangeEventArgs onChangedEventArgs  = null;
                ComposablePartCatalogChangeEventArgs onChangingEventArgs = null;
                var scriptName      = @"c:\workingdirectory\_plugins\SimpleScript.csx";
                var fileSystem      = GetMockFileSystem(scriptName, Scripts.SimpleScript);
                var scriptCsCatalog = new ScriptCsCatalog("_plugins", GetOptions(fileSystem: fileSystem.Object));

                scriptCsCatalog.Changing += (object sender, ComposablePartCatalogChangeEventArgs e) =>
                {
                    onChangingCalled    = true;
                    onChangingEventArgs = e;
                };

                scriptCsCatalog.Changed += (object sender, ComposablePartCatalogChangeEventArgs e) =>
                {
                    onChangedCalled    = true;
                    onChangedEventArgs = e;
                };

                // act
                var mefHost = GetComposedMefHost(scriptCsCatalog);

                // assert
                scriptCsCatalog.LoadedFiles.Count.ShouldEqual(1);
                mefHost.Plugins.Count.ShouldEqual(1);
                mefHost.Plugins[0].DoSomething().ShouldEqual("Simple");

                // arrange
                UpdateFileSystem(fileSystem, new[] { scriptName }, new[] { Scripts.DoubleScript });

                // act
                scriptCsCatalog.Refresh();

                // assert
                scriptCsCatalog.LoadedFiles.Count.ShouldEqual(1);
                mefHost.Plugins.Count.ShouldEqual(1);
                mefHost.Plugins[0].DoSomething().ShouldEqual("Double");

                onChangingCalled.ShouldBeTrue();
                onChangingEventArgs.ShouldNotBeNull();
                onChangingEventArgs.AtomicComposition.ShouldNotBeNull();
                onChangingEventArgs.AddedDefinitions.ShouldNotBeNull();
                onChangingEventArgs.AddedDefinitions.ShouldNotBeEmpty();
                onChangingEventArgs.AddedDefinitions.Count().ShouldEqual(1);
                onChangingEventArgs.AddedDefinitions.First().ToString().ShouldEqual("Submission#0+DoubleSomething");
                onChangingEventArgs.RemovedDefinitions.ShouldNotBeNull();
                onChangingEventArgs.RemovedDefinitions.ShouldNotBeEmpty();
                onChangingEventArgs.RemovedDefinitions.Count().ShouldEqual(1);
                onChangingEventArgs.RemovedDefinitions.First().ToString().ShouldEqual("Submission#0+SimpleSomething");

                onChangedCalled.ShouldBeTrue();
                onChangedEventArgs.ShouldNotBeNull();
                onChangedEventArgs.AtomicComposition.ShouldBeNull();
                onChangedEventArgs.AddedDefinitions.ShouldNotBeNull();
                onChangedEventArgs.AddedDefinitions.ShouldNotBeEmpty();
                onChangedEventArgs.AddedDefinitions.Count().ShouldEqual(1);
                onChangedEventArgs.AddedDefinitions.First().ToString().ShouldEqual("Submission#0+DoubleSomething");
                onChangedEventArgs.RemovedDefinitions.ShouldNotBeNull();
                onChangedEventArgs.RemovedDefinitions.ShouldNotBeEmpty();
                onChangedEventArgs.RemovedDefinitions.Count().ShouldEqual(1);
                onChangedEventArgs.RemovedDefinitions.First().ToString().ShouldEqual("Submission#0+SimpleSomething");
            }