Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/ScriptCs.Contracts/IScriptLibraryComposer.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System.Collections.Generic;
using System.Text;
using System.Text;

namespace ScriptCs.Contracts
{
public interface IScriptLibraryComposer
{
void Compose(string workingDirectory, StringBuilder builder = null);

string ScriptLibrariesFile { get; }
}
}
}
17 changes: 17 additions & 0 deletions src/ScriptCs.Core/NullScriptLibraryComposer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using ScriptCs.Contracts;
using System.Text;

namespace ScriptCs
{
public class NullScriptLibraryComposer : IScriptLibraryComposer
{
public void Compose(string workingDirectory, StringBuilder builder = null)
{
}

public string ScriptLibrariesFile
{
get { return string.Empty; }
}
}
}
1 change: 1 addition & 0 deletions src/ScriptCs.Core/ScriptCs.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<Compile Include="AssemblyResolver.cs" />
<Compile Include="AssemblyUtility.cs" />
<Compile Include="FileSystemMigrator.cs" />
<Compile Include="NullScriptLibraryComposer.cs" />
<Compile Include="ScriptLibraryComposer.cs" />
<Compile Include="ScriptLibraryWrapper.cs" />
<Compile Include="ReplCommands\AliasCommand.cs" />
Expand Down
16 changes: 16 additions & 0 deletions src/ScriptCs.Core/ScriptExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ public class ScriptExecutor : IScriptExecutor

public IScriptLibraryComposer ScriptLibraryComposer { get; protected set; }

public ScriptExecutor(
IFileSystem fileSystem, IFilePreProcessor filePreProcessor, IScriptEngine scriptEngine, ILog logger)
: this(fileSystem, filePreProcessor, scriptEngine, logger, new NullScriptLibraryComposer())
{
}

public ScriptExecutor(
IFileSystem fileSystem,
IFilePreProcessor filePreProcessor,
Expand All @@ -63,6 +69,11 @@ public ScriptExecutor(
Guard.AgainstNullArgument("fileSystem", fileSystem);
Guard.AgainstNullArgumentProperty("fileSystem", "BinFolder", fileSystem.BinFolder);
Guard.AgainstNullArgumentProperty("fileSystem", "DllCacheFolder", fileSystem.DllCacheFolder);
Guard.AgainstNullArgument("filePreProcessor", filePreProcessor);
Guard.AgainstNullArgument("scriptEngine", scriptEngine);
Guard.AgainstNullArgument("logger", logger);
Guard.AgainstNullArgument("composer", composer);

References = new AssemblyReferences(DefaultReferences);
Namespaces = new Collection<string>();
ImportNamespaces(DefaultNamespaces);
Expand Down Expand Up @@ -202,6 +213,11 @@ IDictionary<string, object> state

protected internal virtual FilePreProcessorResult LoadScriptLibraries(string workingDirectory)
{
if (string.IsNullOrWhiteSpace(ScriptLibraryComposer.ScriptLibrariesFile))
{
return null;
}

var scriptLibrariesPath = Path.Combine(workingDirectory, FileSystem.PackagesFolder,
ScriptLibraryComposer.ScriptLibrariesFile);

Expand Down
29 changes: 18 additions & 11 deletions src/ScriptCs.Core/ScriptLibraryComposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common.Logging;
using ScriptCs.Contracts;
using System.IO;
Expand Down Expand Up @@ -43,18 +42,18 @@ internal string GetMainScript(IPackageObject package)
if (count == 1)
{
script = content[0];
}
}
else if (content.Count() > 1)
{
_logger.WarnFormat("Script Libraries in '{0}' ignored due to multiple Main files being present", package.FullName);
return null;
}

if (script != null)
{
_logger.DebugFormat("Found main script: {0}", script);
_logger.DebugFormat("Found main script: {0}", script);
}

return script;
}

Expand All @@ -65,13 +64,13 @@ public virtual string ScriptLibrariesFile

public void Compose(string workingDirectory, StringBuilder builder = null)
{
var namespaces = new List<string>();
var references = new List<string>();
if (string.IsNullOrWhiteSpace(ScriptLibrariesFile))
{
return;
}

var packagesPath = Path.Combine(workingDirectory, _fileSystem.PackagesFolder);
var packageReferences = _packageAssemblyResolver.GetPackages(workingDirectory);
var packageScriptsPath = Path.Combine(packagesPath, ScriptLibrariesFile);

if (!_fileSystem.DirectoryExists(packagesPath) || _fileSystem.FileExists(packageScriptsPath))
{
return;
Expand All @@ -82,6 +81,9 @@ public void Compose(string workingDirectory, StringBuilder builder = null)
builder = new StringBuilder();
}

var namespaces = new List<string>();
var references = new List<string>();
var packageReferences = _packageAssemblyResolver.GetPackages(workingDirectory);
foreach (var reference in packageReferences)
{
ProcessPackage(packagesPath, reference, builder, references, namespaces);
Expand All @@ -96,15 +98,20 @@ public void Compose(string workingDirectory, StringBuilder builder = null)
{
builder.Insert(0, String.Format("#r {0}{1}", reference, Environment.NewLine));
}

_fileSystem.WriteToFile(packageScriptsPath, builder.ToString());
}

protected internal virtual void ProcessPackage(string packagesPath, IPackageReference reference, StringBuilder builder, List<string> references,
protected internal virtual void ProcessPackage(
string packagesPath,
IPackageReference reference,
StringBuilder builder,
List<string> references,
List<string> namespaces)
{
_logger.DebugFormat("Finding package:{0}", reference.PackageId);
var package = _packageContainer.FindPackage(packagesPath, reference);

if (package == null)
{
_logger.WarnFormat("Package missing: {0}", reference.PackageId);
Expand Down
13 changes: 8 additions & 5 deletions src/ScriptCs/Command/InstallCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@ public CommandResult Execute()
_logger.Info("Installing packages...");

var packagesFolder = Path.Combine(_fileSystem.CurrentDirectory, _fileSystem.PackagesFolder);
var scriptLibrariesFile = Path.Combine(packagesFolder, _composer.ScriptLibrariesFile);

if (_fileSystem.DirectoryExists(packagesFolder))
if (!string.IsNullOrWhiteSpace(_composer.ScriptLibrariesFile))
{
_logger.DebugFormat("Deleting: {0}", scriptLibrariesFile);
_fileSystem.FileDelete(scriptLibrariesFile);
var scriptLibrariesFile = Path.Combine(packagesFolder, _composer.ScriptLibrariesFile);

if (_fileSystem.FileExists(scriptLibrariesFile))
{
_logger.DebugFormat("Deleting: {0}", scriptLibrariesFile);
_fileSystem.FileDelete(scriptLibrariesFile);
}
}

var packages = GetPackages(_fileSystem.CurrentDirectory);
Expand Down
32 changes: 24 additions & 8 deletions test/ScriptCs.Core.Tests/ReplCommands/AliasCommandTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System.Collections.Generic;
using System.IO;
using Common.Logging;
using Moq;
using ScriptCs.Contracts;
using ScriptCs.ReplCommands;
using Should;
using Xunit;
using Xunit.Extensions;

namespace ScriptCs.Tests.ReplCommands
{
Expand All @@ -25,20 +27,34 @@ public void ShouldReturnAlias()

public class ExecuteMethod
{
[Fact]
public void ShouldAliasCommandWithNewName()
[Theory, ScriptCsAutoData]
public void ShouldAliasCommandWithNewName(
Mock<IFileSystem> fileSystem,
Mock<IScriptEngine> engine,
Mock<IObjectSerializer> serializer,
Mock<ILog> logger,
Mock<IScriptLibraryComposer> composer,
Mock<IConsole> console,
Mock<IFilePreProcessor> filePreProcessor)
{
// arrange
var currentDir = @"C:\";
var dummyCommand = new Mock<IReplCommand>();
dummyCommand.Setup(x => x.CommandName).Returns("foo");

var fs = new Mock<IFileSystem>();
fs.Setup(x => x.BinFolder).Returns(Path.Combine(currentDir, "bin"));
fs.Setup(x => x.DllCacheFolder).Returns(Path.Combine(currentDir, "cache"));
fileSystem.Setup(x => x.BinFolder).Returns(Path.Combine(currentDir, "bin"));
fileSystem.Setup(x => x.DllCacheFolder).Returns(Path.Combine(currentDir, "cache"));

var console = new Mock<IConsole>();
var executor = new Repl(null, fs.Object, null, null, null, null, null, null, new List<IReplCommand> { dummyCommand.Object });
var executor = new Repl(
new string[0],
fileSystem.Object,
engine.Object,
serializer.Object,
logger.Object,
composer.Object,
console.Object,
filePreProcessor.Object,
new List<IReplCommand> { dummyCommand.Object });

var cmd = new AliasCommand(console.Object);

Expand All @@ -64,4 +80,4 @@ public void ShouldNotThrowAnExceptionWhenAnUnknownCommandIsPassed()
}
}
}
}
}
8 changes: 4 additions & 4 deletions test/ScriptCs.Core.Tests/ReplTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public Mocks()
FileSystem.SetupGet(x => x.PackagesFolder).Returns("scriptcs_packages");
ScriptEngine = new Mock<IScriptEngine>();
Logger = new Mock<ILog>();
PackageScriptsComposer = new Mock<IScriptLibraryComposer>();
PackageScriptsComposer.SetupGet(p => p.ScriptLibrariesFile).Returns("PackageScripts.csx");
ScriptLibraryComposer = new Mock<IScriptLibraryComposer>();
ScriptLibraryComposer.SetupGet(p => p.ScriptLibrariesFile).Returns("PackageScripts.csx");
Console = new Mock<IConsole>();
ScriptPack = new Mock<IScriptPack>();
FilePreProcessor = new Mock<IFilePreProcessor>();
Expand All @@ -46,7 +46,7 @@ public Mocks()

public Mock<IFilePreProcessor> FilePreProcessor { get; private set; }

public Mock<IScriptLibraryComposer> PackageScriptsComposer { get; private set; }
public Mock<IScriptLibraryComposer> ScriptLibraryComposer { get; private set; }

public Mock<IReplCommand>[] ReplCommands { get; set; }
}
Expand All @@ -59,7 +59,7 @@ public static Repl GetRepl(Mocks mocks)
mocks.ScriptEngine.Object,
mocks.ObjectSerializer.Object,
mocks.Logger.Object,
mocks.PackageScriptsComposer.Object,
mocks.ScriptLibraryComposer.Object,
mocks.Console.Object,
mocks.FilePreProcessor.Object,
mocks.ReplCommands.Select(x => x.Object));
Expand Down
15 changes: 12 additions & 3 deletions test/ScriptCs.Core.Tests/ScriptExecutorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Common.Logging;
using Moq;
using Moq.Protected;
using Ploeh.AutoFixture.Xunit;
Expand Down Expand Up @@ -504,13 +505,21 @@ public class TheLoadScriptLibrariesMethod
{
[Theory, ScriptCsAutoData]
public void ShouldPreProcessTheScriptLibrariesFileIfPresent(
[Frozen] Mock<IFilePreProcessor> preProcessor,
[Frozen] Mock<IFileSystem> fileSystem,
ScriptExecutor executor)
[Frozen] Mock<IFilePreProcessor> preProcessor,
[Frozen] Mock<IScriptEngine> engine,
[Frozen] Mock<ILog> logger,
[Frozen] Mock<IScriptLibraryComposer> composer)
{
preProcessor.Setup(p => p.ProcessFile(It.IsAny<string>())).Returns(new FilePreProcessorResult());
// arrange
fileSystem.Setup(fs => fs.FileExists(It.IsAny<string>())).Returns(true);
var executor = new ScriptExecutor(
fileSystem.Object, preProcessor.Object, engine.Object, logger.Object,composer.Object);

// act
executor.LoadScriptLibraries("");

// assert
preProcessor.Verify(p => p.ProcessFile(It.IsAny<string>()));
}
}
Expand Down
8 changes: 4 additions & 4 deletions test/ScriptCsMoqCustomization.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.IO;
using Moq;
using Moq;
using Ploeh.AutoFixture;
using Ploeh.AutoFixture.AutoMoq;
using ScriptCs.Contracts;
Expand All @@ -12,7 +11,7 @@ void ICustomization.Customize(IFixture fixture)
{
this.Customize(fixture);

fixture.Register<Mock<IFileSystem>>(() =>
fixture.Register(() =>
{
var fileSystem = new Mock<IFileSystem>();
fileSystem.SetupGet(f => f.PackagesFile).Returns("scriptcs_packages.config");
Expand All @@ -26,7 +25,8 @@ void ICustomization.Customize(IFixture fixture)
fileSystem.Setup(f => f.GetWorkingDirectory(It.IsAny<string>())).Returns("workingdirectory");
return fileSystem;
});
fixture.Register<Mock<IScriptLibraryComposer>>(() =>

fixture.Register(() =>
{
var composer = new Mock<IScriptLibraryComposer>();
composer.SetupGet(c => c.ScriptLibrariesFile).Returns("ScriptLibraries.csx");
Expand Down