public static int Main(string[] args) { try { IFileExistsService fileExistsService = new FileExistsService(); IConsoleLogger consoleLogger = new ConsoleLogger(); IScriptLoader scriptLoader = new ScriptLoader(); List <string> arguments = new List <string>(args); BuildScriptLocator buildScriptLocator = new BuildScriptLocator(fileExistsService, consoleLogger, scriptLoader); IBuildScript script = buildScriptLocator.FindBuildScript(arguments); //Parser p = new Parser(); //p.ParseArguments(args); return(script.Run(arguments)); } catch (Exception ex) { System.Console.WriteLine("Error executing script the script"); System.Console.WriteLine(ex.ToString()); return(1); } }
public async Task LoadSimpleScript() { CommandArguments args = new CommandArguments(); _scriptLocator.Setup(x => x.FindBuildScript(args)).Returns("e3.cs"); _fileLoader.Setup(i => i.ReadAllLines("e3.cs")) .Returns(new List <string> { "using FlubuCore.Scripting;", "using System;", "using FlubuCore.Context;", "using Microsoft.Extensions.DependencyInjection;", "using Microsoft.Extensions.Logging;", string.Empty, "public class MyBuildScript : IBuildScript", "{", " public int Run(IFlubuSession session)", " {", " Console.WriteLine(\"11\");", " return 0;", " }", " public void ConfigureServices(IServiceCollection services)", " {", " }", " public void Configure(ILoggerFactory loggerFactory)", " {", " }", "}", }); _analyzer.Setup(i => i.Analyze(It.IsAny <List <string> >())) .Returns(new ScriptAnalyzerResult() { ClassName = "MyBuildScript" }); IBuildScript t = await _loader.FindAndCreateBuildScriptInstanceAsync(args); var provider = new ServiceCollection() .AddSingleton <IScriptProperties, ScriptProperties>() .AddSingleton <ITargetCreator, TargetCreator>() .BuildServiceProvider(); var targetTree = new TargetTree(provider, new CommandArguments()); t.Run(new FlubuSession( _loggerTaskSession.Object, targetTree, new CommandArguments(), new ScriptFactory(provider), new DotnetTaskFactory(provider), new FluentInterfaceFactory(provider), new BuildPropertiesSession(targetTree), new BuildSystem())); }
public async System.Threading.Tasks.Task LoadDefaultScriptWithAnotherClass() { CommandArguments args = new CommandArguments(); _scriptLocator.Setup(x => x.FindBuildScript(args)).Returns("e2.cs"); _fileLoader.Setup(i => i.ReadAllLines("e2.cs")) .Returns(new List <string> { "using System;", "using FlubuCore.Context;", "using FlubuCore.Scripting;", string.Empty, "public class MyBuildScript : DefaultBuildScript", "{", " protected override void ConfigureBuildProperties(IBuildPropertiesContext context)", " {", " System.Console.WriteLine(\"2222\");", " }", string.Empty, " protected override void ConfigureTargets(ITaskContext context)", " {", " var test = new Test();", " }", " }", "public class Test", "{", "}" }); _analyzer.Setup(i => i.Analyze(It.IsAny <List <string> >())) .Returns(new ScriptAnalyzerResult() { ClassName = "MyBuildScript" }); IBuildScript t = await _loader.FindAndCreateBuildScriptInstanceAsync(args); var provider = new ServiceCollection() .AddSingleton <IScriptProperties, ScriptProperties>() .AddSingleton <ITargetCreator, TargetCreator>() .BuildServiceProvider(); var targetTree = new TargetTree(provider, new CommandArguments()); t.Run(new FlubuSession( _loggerTaskSession.Object, targetTree, new CommandArguments(), new ScriptFactory(provider), new DotnetTaskFactory(provider), new FluentInterfaceFactory(provider), new BuildPropertiesSession(targetTree), new BuildSystem())); }
public async Task LoadSimpleScript() { CommandArguments args = new CommandArguments(); _scriptLocator.Setup(x => x.FindBuildScript(args)).Returns("e3.cs"); _fileLoader.Setup(i => i.ReadAllLines("e3.cs")) .Returns(new List <string> { "using FlubuCore.Scripting;", "using System;", "using FlubuCore.Context;", string.Empty, "public class MyBuildScript : IBuildScript", "{", " public int Run(ITaskSession session)", " {", " Console.WriteLine(\"11\");", " return 0;", " }", "}", }); _analyser.Setup(i => i.Analyze(It.IsAny <List <string> >())) .Returns(new AnalyserResult() { ClassName = "MyBuildScript" }); IBuildScript t = await _loader.FindAndCreateBuildScriptInstanceAsync(args); var provider = new ServiceCollection().BuildServiceProvider(); t.Run(new TaskSession( null, new TargetTree(provider, new CommandArguments()), new CommandArguments(), new DotnetTaskFactory(provider), new FluentInterfaceFactory(provider), new BuildPropertiesSession(), new BuildSystem())); }
public async Task <int> ExecuteAsync() { var version = Assembly.GetEntryAssembly().GetCustomAttribute <AssemblyFileVersionAttribute>().Version; if (_args.DisableColoredLogging) { FlubuConsoleLogger.DisableColloredLogging = true; } _log.LogInformation($"Flubu v.{version}"); if (_args.Help) { return(1); } if (_args.MainCommands.Count == 1 && _args.MainCommands.First().Equals("setup", StringComparison.OrdinalIgnoreCase)) { TargetTree.SetupFlubu(); return(0); } try { int result = 0; do { IBuildScript script = null; try { if (!_flubuSession.InteractiveMode) { script = await _scriptLoader.FindAndCreateBuildScriptInstanceAsync(_args); } else { script = await _scriptLoader.FindAndCreateBuildScriptInstanceAsync(_flubuSession .InteractiveArgs); } } catch (BuildScriptLocatorException) { if (!_args.InteractiveMode && !_flubuSession.InteractiveMode) { throw; } await SimpleFlubuInteractiveMode(script); } _flubuSession.TargetTree.BuildScript = script; _flubuSession.FlubuHelpText = FlubuHelpText; _flubuSession.Properties.Set(BuildProps.IsWebApi, _args.IsWebApi); _flubuSession.ScriptArgs = _args.ScriptArguments; _flubuSession.TargetTree.ResetTargetTree(); //// ReSharper disable once PossibleNullReferenceException if (script != null) { result = script.Run(_flubuSession); } }while (_flubuSession.InteractiveMode && ReloadCommands.Contains(_flubuSession.InteractiveArgs.MainCommands[0], StringComparer.OrdinalIgnoreCase)); return(result); } catch (TaskExecutionException e) { if (_args.RethrowOnException) { throw; } _log.Log(LogLevel.Error, 1, $"EXECUTION FAILED:\r\n{e.ToString()}", null, (t, ex) => t); return(StatusCodes.BuildScriptNotFound); } catch (FlubuException e) { if (_args.RethrowOnException) { throw; } var str = _args.Debug ? e.ToString() : e.Message; _log.Log(LogLevel.Error, 1, $"EXECUTION FAILED:\r\n{str}", null, (t, ex) => t); return(StatusCodes.BuildScriptNotFound); } catch (Exception e) { if (_args.RethrowOnException) { throw; } var str = _args.Debug ? e.ToString() : e.Message; _log.Log(LogLevel.Error, 1, $"EXECUTION FAILED:\r\n{str}", null, (t, ex) => t); return(3); } }
public virtual async Task <int> ExecuteAsync() { if (Args.DisableColoredLogging) { FlubuConsoleLogger.DisableColloredLogging = true; } if (Args.Help) { return(1); } if (Args.IsInternalCommand()) { await ExecuteInternalCommand(); return(0); } try { int result = 0; do { IBuildScript script = null; try { if (!_flubuSession.InteractiveMode) { script = await _scriptProvider.GetBuildScriptAsync(Args); } else { script = await _scriptProvider.GetBuildScriptAsync(_flubuSession .InteractiveArgs, true); } } catch (BuildScriptLocatorException) { if (Args.InteractiveMode && !_flubuSession.InteractiveMode) { _flubuSession.LogInfo("Build script not found."); script = await SimpleFlubuInteractiveMode(script); } else { throw; } } _flubuSession.ScriptArgs = Args.ScriptArguments; _flubuSession.InteractiveMode = Args.InteractiveMode; _flubuSession.InteractiveArgs = Args; _flubuSession.TargetTree.BuildScript = script; _flubuSession.Properties.Set(BuildProps.IsWebApi, Args.IsWebApi); _flubuSession.TargetTree.ResetTargetTree(); if (script != null) { if (_flubuSession.InteractiveMode) { FlubuInteractiveMode(_flubuSession, script); } else { result = script.Run(_flubuSession); } } }while (_flubuSession.InteractiveMode && InternalTerminalCommands.ReloadCommands.Contains(_flubuSession.InteractiveArgs.MainCommands[0], StringComparer.OrdinalIgnoreCase)); return(result); } catch (TaskExecutionException e) { if (Args.RethrowOnException) { throw; } _log.Log(LogLevel.Error, 1, $"EXECUTION FAILED:\r\n{e}", null, (t, ex) => t); return(StatusCodes.BuildScriptNotFound); } catch (FlubuException e) { if (Args.RethrowOnException) { throw; } var str = Args.Debug ? e.ToString() : e.Message; _log.Log(LogLevel.Error, 1, $"EXECUTION FAILED:\r\n{str}", null, (t, ex) => t); return(StatusCodes.BuildScriptNotFound); } catch (Exception e) { if (Args.RethrowOnException) { throw; } var str = Args.Debug ? e.ToString() : e.Message; _log.Log(LogLevel.Error, 1, $"EXECUTION FAILED:\r\n{str}", null, (t, ex) => t); return(3); } }
protected virtual void FlubuInteractiveMode(IFlubuSession flubuSession, IBuildScript script) { flubuSession.InteractiveMode = true; var flubuConsole = InitializeFlubuConsole(flubuSession, script); flubuSession.TargetTree.ScriptArgsHelp = _scriptProperties.GetPropertiesHelp(script); flubuSession.TargetTree.RunTarget(flubuSession, FlubuTargets.HelpOnlyTargets); flubuSession.LogInfo(" "); do { if (flubuSession.InteractiveMode) { var commandLine = flubuConsole.ReadLine(); if (string.IsNullOrEmpty(commandLine)) { continue; } var cmdApp = new CommandLineApplication() { UnrecognizedArgumentHandling = UnrecognizedArgumentHandling.CollectAndContinue }; var parser = new FlubuCommandParser(cmdApp, null, null, null); var args = parser.Parse(commandLine.Split(' ') .Where(x => !string.IsNullOrWhiteSpace(x)) .Select(x => x.Trim()).ToArray()); var targetsInfo = DefaultBuildScript.ParseCmdLineArgs(args.MainCommands, flubuSession.TargetTree); if (args.MainCommands.Count == 0) { continue; } if (!args.MainCommands[0].Equals("l", StringComparison.OrdinalIgnoreCase) && !args.MainCommands[0].Equals("load", StringComparison.OrdinalIgnoreCase)) { args.Script = flubuSession.InteractiveArgs.Script; } flubuSession.InteractiveArgs = args; flubuSession.ScriptArgs = args.ScriptArguments; _scriptProperties.InjectProperties(script, flubuSession); if (InternalTerminalCommands.InteractiveExitAndReloadCommands.Contains(args.MainCommands[0], StringComparer.OrdinalIgnoreCase)) { break; } if (targetsInfo.unknownTarget) { var internalCommandExecuted = flubuConsole.ExecuteInternalCommand(commandLine); if (internalCommandExecuted) { continue; } var splitedLine = commandLine.Split(' ').ToList(); var command = splitedLine.First(); var runProgram = flubuSession.Tasks().RunProgramTask(command).DoNotLogTaskExecutionInfo() .WorkingFolder(Directory.GetCurrentDirectory()); splitedLine.RemoveAt(0); try { runProgram.WithArguments(splitedLine.ToArray()).Execute(flubuSession); } catch (CommandUnknownException) { flubuSession.LogError( $"'{command}' is not recognized as a flubu target, internal or external command, operable program or batch file."); } catch (TaskExecutionException e) { flubuSession.LogError($"ERROR: {(flubuSession.Args.Debug ? e.ToString() : e.Message)}", Color.Red); } catch (ArgumentException) { } catch (Win32Exception) { } continue; } } try { script.Run(flubuSession); flubuSession.LogInfo(" "); } catch (Exception e) { flubuSession.LogError($"ERROR: {(flubuSession.Args.Debug ? e.ToString() : e.Message)}", Color.Red); } }while (flubuSession.InteractiveMode); }