Skip to content

Commit b36b1b6

Browse files
committed
Added CompositeCommand for combining commands, -restore should now be provided with a script name to restore before executing the script
1 parent e553a92 commit b36b1b6

15 files changed

Lines changed: 136 additions & 49 deletions

src/ScriptCs/Command/CommandFactory.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,40 @@ public ICommand CreateCommand(ScriptCsArgs args)
1313
{
1414
if (args.ScriptName != null)
1515
{
16-
return new ScriptExecuteCommand(
16+
var executeCommand = new ExecuteScriptCommand(
1717
args.ScriptName,
1818
_scriptServiceRoot.FileSystem,
1919
_scriptServiceRoot.Executor,
2020
_scriptServiceRoot.ScriptPackResolver);
21+
22+
if (args.Restore)
23+
{
24+
var restoreCommand = new RestoreCommand(
25+
args.ScriptName,
26+
_scriptServiceRoot.FileSystem,
27+
_scriptServiceRoot.PackageAssemblyResolver);
28+
29+
return new CompositeCommand(restoreCommand, executeCommand);
30+
}
31+
32+
return executeCommand;
2133
}
2234

2335
if (args.Install != null)
2436
{
25-
return new InstallCommand(
37+
var installCommand = new InstallCommand(
2638
args.Install,
2739
args.AllowPreReleaseFlag,
2840
_scriptServiceRoot.FileSystem,
2941
_scriptServiceRoot.PackageAssemblyResolver,
3042
_scriptServiceRoot.PackageInstaller);
31-
}
3243

33-
if (args.Restore)
34-
{
35-
return new RestoreCommand(_scriptServiceRoot.FileSystem, _scriptServiceRoot.PackageAssemblyResolver);
44+
var restoreCommand = new RestoreCommand(
45+
args.Install,
46+
_scriptServiceRoot.FileSystem,
47+
_scriptServiceRoot.PackageAssemblyResolver);
48+
49+
return new CompositeCommand(installCommand, restoreCommand);
3650
}
3751

3852
return new InvalidCommand();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace ScriptCs.Command
2+
{
3+
public enum CommandResult
4+
{
5+
Success,
6+
Error
7+
}
8+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
namespace ScriptCs.Command
5+
{
6+
public class CompositeCommand : ICompositeCommand
7+
{
8+
public CompositeCommand(params ICommand[] commands)
9+
{
10+
Commands = commands.ToList();
11+
}
12+
13+
public List<ICommand> Commands { get; private set; }
14+
15+
public CommandResult Execute()
16+
{
17+
var result = CommandResult.Success;
18+
foreach (var command in Commands)
19+
{
20+
result = command.Execute();
21+
22+
if (result != CommandResult.Success)
23+
return result;
24+
}
25+
26+
return result;
27+
}
28+
}
29+
}

src/ScriptCs/Command/ScriptExecuteCommand.cs renamed to src/ScriptCs/Command/ExecuteScriptCommand.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@
55

66
namespace ScriptCs.Command
77
{
8-
internal class ScriptExecuteCommand : IScriptCommand
8+
internal class ExecuteScriptCommand : IScriptCommand
99
{
1010
private readonly string _script;
1111
private readonly IFileSystem _fileSystem;
1212
private readonly IScriptExecutor _scriptExecutor;
1313
private readonly IScriptPackResolver _scriptPackResolver;
1414

15-
public ScriptExecuteCommand(string script, IFileSystem fileSystem, IScriptExecutor scriptExecutor, IScriptPackResolver scriptPackResolver)
15+
public ExecuteScriptCommand(string script, IFileSystem fileSystem, IScriptExecutor scriptExecutor, IScriptPackResolver scriptPackResolver)
1616
{
1717
_script = script;
1818
_fileSystem = fileSystem;
1919
_scriptExecutor = scriptExecutor;
2020
_scriptPackResolver = scriptPackResolver;
2121
}
2222

23-
public int Execute()
23+
public CommandResult Execute()
2424
{
2525
try
2626
{
@@ -33,12 +33,12 @@ public int Execute()
3333
}
3434

3535
_scriptExecutor.Execute(_script, assemblyPaths, _scriptPackResolver.GetPacks());
36-
return 0;
36+
return CommandResult.Success;
3737
}
3838
catch (Exception ex)
3939
{
4040
Console.WriteLine(ex.Message);
41-
return -1;
41+
return CommandResult.Error;
4242
}
4343
}
4444

src/ScriptCs/Command/ICommand.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace ScriptCs.Command
1+
using System.Collections.Generic;
2+
3+
namespace ScriptCs.Command
24
{
35
public interface IScriptCommand : ICommand { }
46

@@ -8,8 +10,13 @@ public interface IInstallCommand : ICommand { }
810

911
public interface IInvalidCommand : ICommand { }
1012

13+
public interface ICompositeCommand : ICommand
14+
{
15+
List<ICommand> Commands { get; }
16+
}
17+
1118
public interface ICommand
1219
{
13-
int Execute();
20+
CommandResult Execute();
1421
}
1522
}

src/ScriptCs/Command/InstallCommand.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace ScriptCs.Command
88
{
9-
internal class InstallCommand : RestoreCommand, IInstallCommand
9+
internal class InstallCommand : IInstallCommand
1010
{
1111
private readonly string _name;
1212

@@ -23,7 +23,7 @@ public InstallCommand(
2323
bool allowPre,
2424
IFileSystem fileSystem,
2525
IPackageAssemblyResolver packageAssemblyResolver,
26-
IPackageInstaller packageInstaller) : base(fileSystem, packageAssemblyResolver)
26+
IPackageInstaller packageInstaller)
2727
{
2828
_name = name;
2929
_allowPre = allowPre;
@@ -32,7 +32,7 @@ public InstallCommand(
3232
_packageInstaller = packageInstaller;
3333
}
3434

35-
public override int Execute()
35+
public CommandResult Execute()
3636
{
3737
Console.WriteLine("Installing packages...");
3838

@@ -44,12 +44,12 @@ public override int Execute()
4444
_packageInstaller.InstallPackages(packages, _allowPre, Console.WriteLine);
4545

4646
Console.WriteLine("Installation completed successfully.");
47-
return base.Execute();
47+
return CommandResult.Success;
4848
}
4949
catch (Exception e)
5050
{
5151
Console.WriteLine("Installation failed: {0}.", e.Message);
52-
return -1;
52+
return CommandResult.Error;
5353
}
5454
}
5555

src/ScriptCs/Command/InvalidCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ namespace ScriptCs.Command
55
{
66
internal class InvalidCommand : IInvalidCommand
77
{
8-
public int Execute()
8+
public CommandResult Execute()
99
{
1010
Console.WriteLine(ArgUsage.GetUsage<ScriptCsArgs>());
11-
return -1;
11+
return CommandResult.Error;
1212
}
1313
}
1414
}

src/ScriptCs/Command/RestoreCommand.cs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,64 +3,68 @@
33

44
namespace ScriptCs.Command
55
{
6-
public class RestoreCommand : IRestoreCommand
6+
internal class RestoreCommand : IRestoreCommand
77
{
8+
private readonly string _scriptName;
9+
810
private readonly IFileSystem _fileSystem;
911

1012
private readonly IPackageAssemblyResolver _packageAssemblyResolver;
1113

12-
public RestoreCommand(IFileSystem fileSystem, IPackageAssemblyResolver packageAssemblyResolver)
14+
public RestoreCommand(string scriptName, IFileSystem fileSystem, IPackageAssemblyResolver packageAssemblyResolver)
1315
{
16+
_scriptName = scriptName;
1417
_fileSystem = fileSystem;
1518
_packageAssemblyResolver = packageAssemblyResolver;
1619
}
1720

18-
public virtual int Execute()
21+
public CommandResult Execute()
1922
{
20-
Console.WriteLine("Moving assemblies to bin folder...");
23+
Console.WriteLine("Copying assemblies to bin folder...");
2124

22-
var binFolder = Path.Combine(_fileSystem.CurrentDirectory, "bin");
25+
var workingDirectory = _fileSystem.GetWorkingDirectory(_scriptName);
26+
var binFolder = Path.Combine(workingDirectory, "bin");
2327

2428
try
2529
{
2630
if (!_fileSystem.DirectoryExists(binFolder))
2731
_fileSystem.CreateDirectory(binFolder);
2832

29-
var packages = _packageAssemblyResolver.GetAssemblyNames(_fileSystem.CurrentDirectory);
33+
var packages = _packageAssemblyResolver.GetAssemblyNames(workingDirectory);
3034
foreach (var package in packages)
3135
{
32-
MoveFile(package, binFolder);
36+
CopyFile(package, binFolder);
3337
}
3438

3539
Console.WriteLine("Restore completed successfully.");
36-
return 0;
40+
return CommandResult.Success;
3741
}
3842
catch (Exception e)
3943
{
4044
Console.WriteLine("Restore failed: {0}.", e.Message);
41-
return -1;
45+
return CommandResult.Error;
4246
}
4347
}
4448

45-
private void MoveFile(string package, string binFolder)
49+
private void CopyFile(string package, string binFolder)
4650
{
47-
var packageFileName = Path.GetFileName(package);
48-
if (packageFileName == null) return;
51+
var assemblyFileName = Path.GetFileName(package);
52+
if (assemblyFileName == null) return;
4953

50-
var destFile = Path.Combine(binFolder, packageFileName);
54+
var destFile = Path.Combine(binFolder, assemblyFileName);
5155

5256
var sourceFileLastWriteTime = _fileSystem.GetLastWriteTime(package);
5357
var destFileLastWriteTime = _fileSystem.GetLastWriteTime(destFile);
5458

5559
if (sourceFileLastWriteTime == destFileLastWriteTime)
5660
{
57-
Console.WriteLine("Skipped {0}.", packageFileName);
61+
Console.WriteLine("Skipped: {0}.", assemblyFileName);
5862
return;
5963
}
6064

6165
_fileSystem.Copy(package, destFile, true);
6266

63-
Console.WriteLine("Moved {0}.", packageFileName);
67+
Console.WriteLine("Copied: {0}.", assemblyFileName);
6468
}
6569
}
6670
}

src/ScriptCs/Program.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@ private static int Main(string[] args)
1717
var commandFactory = new CommandFactory(scriptServiceRoot);
1818
var command = commandFactory.CreateCommand(commandArgs);
1919

20-
return command.Execute();
20+
var result = command.Execute();
21+
22+
switch (result)
23+
{
24+
case CommandResult.Success:
25+
return 0;
26+
default:
27+
return -1;
28+
}
2129
}
2230
}
2331
}

src/ScriptCs/ScriptCs.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@
4242
<Link>Properties\CommonVersionInfo.cs</Link>
4343
</Compile>
4444
<Compile Include="Command\CommandFactory.cs" />
45+
<Compile Include="Command\CommandResult.cs" />
46+
<Compile Include="Command\CompositeCommand.cs" />
4547
<Compile Include="Command\ICommand.cs" />
4648
<Compile Include="Command\InstallCommand.cs" />
4749
<Compile Include="Command\InvalidCommand.cs" />
4850
<Compile Include="Command\RestoreCommand.cs" />
49-
<Compile Include="Command\ScriptExecuteCommand.cs" />
51+
<Compile Include="Command\ExecuteScriptCommand.cs" />
5052
<Compile Include="CompositionRoot.cs" />
5153
<Compile Include="ScriptServiceRoot.cs" />
5254
<Compile Include="Program.cs" />

0 commit comments

Comments
 (0)