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
53 changes: 31 additions & 22 deletions src/ScriptCs/Command/CommandFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,6 @@ public ICommand CreateCommand(ScriptCsArgs args, string[] scriptArgs)

if (args.ScriptName != null)
{
scriptServices = _scriptServicesBuilder.Build();
var executeCommand = args.Watch
? (ICommand)new WatchScriptCommand(
args,
scriptArgs,
scriptServices.Console,
scriptServices.FileSystem,
scriptServices.Logger)
: (ICommand)new ExecuteScriptCommand(
args.ScriptName,
scriptArgs,
scriptServices.FileSystem,
scriptServices.Executor,
scriptServices.ScriptPackResolver,
scriptServices.Logger,
scriptServices.AssemblyResolver);

var currentDirectory = fileSystem.CurrentDirectory;
var packageFile = Path.Combine(currentDirectory, Constants.PackagesFile);
var packagesFolder = Path.Combine(currentDirectory, Constants.PackagesFolder);
Expand All @@ -89,16 +72,22 @@ public ICommand CreateCommand(ScriptCsArgs args, string[] scriptArgs)
var installCommand = new InstallCommand(
null,
null,
false,
true,
fileSystem,
scriptServices.PackageAssemblyResolver,
scriptServices.PackageInstaller,
scriptServices.Logger);
packageAssemblyResolver,
_initializationServices.GetPackageInstaller(),
logger);

var executeCommand = new DeferredCreationCommand<IScriptCommand>(() =>
{
scriptServices = ScriptServicesBuilderFactory.Create(args, scriptArgs).Build();
return CreateScriptCommand(args, scriptArgs, scriptServices);
});

return new CompositeCommand(installCommand, executeCommand);
}

return executeCommand;
return CreateScriptCommand(args, scriptArgs, _scriptServicesBuilder.Build());
}

if (args.Clean)
Expand Down Expand Up @@ -151,5 +140,25 @@ public ICommand CreateCommand(ScriptCsArgs args, string[] scriptArgs)

return new ShowUsageCommand(logger, isValid: false);
}

private static IScriptCommand CreateScriptCommand(
ScriptCsArgs args, string[] scriptArgs, ScriptServices scriptServices)
{
return args.Watch
? (IScriptCommand)new WatchScriptCommand(
args,
scriptArgs,
scriptServices.Console,
scriptServices.FileSystem,
scriptServices.Logger)
: (IScriptCommand)new ExecuteScriptCommand(
args.ScriptName,
scriptArgs,
scriptServices.FileSystem,
scriptServices.Executor,
scriptServices.ScriptPackResolver,
scriptServices.Logger,
scriptServices.AssemblyResolver);
}
}
}
21 changes: 21 additions & 0 deletions src/ScriptCs/Command/DeferredCreationCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;

namespace ScriptCs.Command
{
internal class DeferredCreationCommand<TCommand> : IDeferredCreationCommand<TCommand> where TCommand : ICommand
{
private readonly Func<TCommand> _factory;

public DeferredCreationCommand(Func<TCommand> factory)
{
Guard.AgainstNullArgument("factory", factory);

_factory = factory;
}

public CommandResult Execute()
{
return _factory().Execute();
}
}
}
4 changes: 4 additions & 0 deletions src/ScriptCs/Command/ICommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public interface IVersionCommand : ICommand
{
}

public interface IDeferredCreationCommand<TCommand> : ICommand where TCommand : ICommand
{
}

public interface ICommand
{
CommandResult Execute();
Expand Down
1 change: 1 addition & 0 deletions src/ScriptCs/ScriptCs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
<Compile Include="Command\CompositeCommand.cs" />
<Compile Include="Command\CrossAppDomainExecuteScriptCommand.cs" />
<Compile Include="Command\FileWatcher.cs" />
<Compile Include="Command\DeferredCreationCommand.cs" />
<Compile Include="Command\WatchScriptCommand.cs" />
<Compile Include="Command\ExecuteReplCommand.cs" />
<Compile Include="Command\ShowUsageCommand.cs" />
Expand Down
2 changes: 1 addition & 1 deletion test/ScriptCs.Tests/CommandFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void ShouldInstallAndExecuteWhenScriptNameIsPassedAndPackagesFolderDoesNo

compositeCommand.Commands.Count.ShouldEqual(2);
compositeCommand.Commands[0].ShouldImplement<IInstallCommand>();
compositeCommand.Commands[1].ShouldImplement<IScriptCommand>();
compositeCommand.Commands[1].ShouldImplement<IDeferredCreationCommand<IScriptCommand>>();
}

[Fact]
Expand Down