Skip to content
This repository was archived by the owner on Jul 6, 2022. It is now read-only.

Commit 29afcf3

Browse files
beolutzJérémie Bertrand
authored andcommitted
Not merge all scripts (#12)
ExecuteScript called for every csx file to support using alias directives.
1 parent 73a0e06 commit 29afcf3

4 files changed

Lines changed: 80 additions & 22 deletions

File tree

src/ScriptCs.ComponentModel.Composition.Test/ScriptCsCatalogFacts.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Moq;
1+
using Moq;
22
using ScriptCs.Contracts;
33
using Should;
44
using System;
@@ -70,6 +70,25 @@ public void ShouldWorkWithMultipleScripts()
7070
mefHost.Plugins[1].DoSomething().ShouldEqual("Double");
7171
}
7272

73+
[Fact]
74+
public void ShouldWorkWithMultipleScriptsNotMerged()
75+
{
76+
// arrange
77+
var scriptName = @"c:\workingdirectory\SimpleScript.csx";
78+
var scriptName2 = @"c:\workingdirectory\DoubleScript.csx";
79+
var scriptCsCatalog = new ScriptCsCatalog(new[] { scriptName, scriptName2 },
80+
GetOptions(fileSystem: GetMockFileSystem(new[] { scriptName, scriptName2 }, new[] { Scripts.SimpleScript, Scripts.DoubleScript }).Object, keepScriptsSeparated: true));
81+
82+
// act
83+
var mefHost = GetComposedMefHost(scriptCsCatalog);
84+
85+
// assert
86+
mefHost.Plugins.ShouldNotBeNull();
87+
mefHost.Plugins.Count.ShouldEqual(2);
88+
mefHost.Plugins[0].DoSomething().ShouldEqual("Simple");
89+
mefHost.Plugins[1].DoSomething().ShouldEqual("Double");
90+
}
91+
7392
[Fact]
7493
public void ShouldThrowExceptionIfNullPassedForScriptsFiles()
7594
{
@@ -732,13 +751,14 @@ private static MEFHost GetComposedMefHost(ScriptCsCatalog catalog)
732751
return mefHost;
733752
}
734753

735-
private static ScriptCsCatalogOptions GetOptions(string[] scriptArgs = null, Type[] references = null, IFileSystem fileSystem = null)
754+
private static ScriptCsCatalogOptions GetOptions(string[] scriptArgs = null, Type[] references = null, IFileSystem fileSystem = null, bool keepScriptsSeparated = false)
736755
{
737756
return new ScriptCsCatalogOptions
738757
{
739758
FileSystem = fileSystem,
740759
References = references,
741-
ScriptArgs = scriptArgs
760+
ScriptArgs = scriptArgs,
761+
KeepScriptsSeparated = keepScriptsSeparated
742762
};
743763
}
744764

@@ -787,4 +807,4 @@ private static void UpdateFileSystem(Mock<IFileSystem> fileSystem, string[] file
787807
}
788808
}
789809
}
790-
}
810+
}

src/ScriptCs.ComponentModel.Composition.Test/ScriptCsCatalogOptionsFacts.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public void ShouldOverridesNullScriptArgs()
5757
var options = new ScriptCsCatalogOptions { ScriptArgs = null };
5858

5959
// act
60+
6061
var result = options.OverridesNullByDefault();
6162

6263
// assert
@@ -119,6 +120,20 @@ public void ShouldNotOverridesValuedFileSystem()
119120
result.FileSystem.ShouldNotBeNull();
120121
result.FileSystem.ShouldEqual(fileSystemMock);
121122
}
123+
124+
125+
[Fact]
126+
public void ShouldNotOverridesValuedKeepScriptsSeparated()
127+
{
128+
// arrange
129+
var options = new ScriptCsCatalogOptions { KeepScriptsSeparated = true };
130+
131+
// act
132+
var result = options.OverridesNullByDefault();
133+
134+
// assert
135+
result.KeepScriptsSeparated.ShouldEqual(true);
136+
}
122137
}
123138
}
124-
}
139+
}

src/ScriptCs.ComponentModel.Composition/ScriptCsCatalog.cs

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using ScriptCs.Contracts;
1+
using ScriptCs.Contracts;
22
using ScriptCs.Hosting;
33
using System;
44
using System.Collections.Generic;
@@ -406,8 +406,33 @@ private AssemblyCatalog ExecuteScripts(IEnumerable<string> scriptFiles)
406406
{
407407
var services = CreateScriptServices();
408408

409-
var loader = GetLoader(scriptFiles);
409+
ScriptResult result = null;
410+
if (_options.KeepScriptsSeparated)
411+
{
412+
foreach (var scriptFile in scriptFiles)
413+
{
414+
var loader = GetLoader(new[] { scriptFile });
415+
result = ExecuteScript(services, loader);
416+
}
417+
}
418+
else
419+
{
420+
var loader = GetLoader(scriptFiles);
421+
result = ExecuteScript(services, loader);
422+
}
410423

424+
AssemblyCatalog catalog = null;
425+
var marker = result.ReturnValue as Type;
426+
if (marker != null)
427+
{
428+
catalog = new AssemblyCatalog(marker.Assembly, this);
429+
}
430+
431+
return catalog;
432+
}
433+
434+
private ScriptResult ExecuteScript(ScriptServices services, string loader)
435+
{
411436
var result = services.Executor.ExecuteScript(loader);
412437

413438
if (result.CompileExceptionInfo != null)
@@ -420,15 +445,7 @@ private AssemblyCatalog ExecuteScripts(IEnumerable<string> scriptFiles)
420445
result.ExecuteExceptionInfo.Throw();
421446
}
422447

423-
var marker = result.ReturnValue as Type;
424-
425-
AssemblyCatalog catalog = null;
426-
if (marker != null)
427-
{
428-
catalog = new AssemblyCatalog(marker.Assembly, this);
429-
}
430-
431-
return catalog;
448+
return result;
432449
}
433450

434451
private string GetFullPath(string path)
@@ -459,11 +476,11 @@ private string GetDisplayName()
459476
private string GetLoader(IEnumerable<string> scriptFiles)
460477
{
461478
var builder = new StringBuilder();
462-
foreach (var script in scriptFiles)
479+
480+
foreach (var scriptFile in scriptFiles)
463481
{
464-
builder.AppendFormat("#load {0}{1}", script, Environment.NewLine);
482+
builder.AppendFormat("#load {0}{1}", scriptFile, Environment.NewLine);
465483
}
466-
467484
builder.AppendLine("public class Marker {}");
468485
builder.AppendLine("typeof(Marker)");
469486

@@ -499,4 +516,4 @@ private ScriptServices CreateScriptServices()
499516
return scriptServices;
500517
}
501518
}
502-
}
519+
}

src/ScriptCs.ComponentModel.Composition/ScriptCsCatalogOptions.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,21 @@ public class ScriptCsCatalogOptions
2828
/// </summary>
2929
public string[] Modules { get; set; }
3030

31+
/// <summary>
32+
/// Prevents the scripts from beeing merged.
33+
/// </summary>
34+
public bool KeepScriptsSeparated { get; set; }
35+
3136
internal ScriptCsCatalogOptions OverridesNullByDefault()
3237
{
3338
return new ScriptCsCatalogOptions
3439
{
3540
References = References,
3641
ScriptArgs = ScriptArgs ?? new string[0],
3742
FileSystem = FileSystem ?? new FileSystem(),
38-
Modules = Modules ?? new string[0]
43+
Modules = Modules ?? new string[0],
44+
KeepScriptsSeparated = KeepScriptsSeparated
3945
};
4046
}
4147
}
42-
}
48+
}

0 commit comments

Comments
 (0)