/// <summary> /// The application entry point. /// </summary> /// <returns>The application exit code.</returns> public static int Main() { ICakeLog log = null; try { // Parse arguments. var args = QuoteAwareStringSplitter .Split(Environment.CommandLine) .Skip(1) // Skip executable. .ToArray(); var builder = new ContainerRegistrar(); builder.RegisterModule(new CakeModule()); builder.RegisterModule(new CoreModule()); builder.RegisterModule(new CommonModule()); // Build the container. using (var container = builder.Build()) { // Resolve the log. log = container.Resolve <ICakeLog>(); // Parse the options. var parser = container.Resolve <IArgumentParser>(); var options = parser.Parse(args); // Set verbosity. log.Verbosity = options.Verbosity; // Rebuild the container. builder = new ContainerRegistrar(); var provider = container.Resolve <CakeConfigurationProvider>(); builder.RegisterModule(new ConfigurationModule(provider, options)); builder.RegisterModule(new ArgumentsModule(options)); builder.RegisterModule(new ScriptingModule(options, log)); builder.Update(container); // Register the NuGetModule builder = new ContainerRegistrar(); var configuration = container.Resolve <ICakeConfiguration>(); builder.RegisterModule(new NuGetModule(configuration)); builder.Update(container); // Load all modules. var loader = container.Resolve <ModuleLoader>(); loader.LoadModules(container, options); // Resolve and run the application. var application = container.Resolve <CakeApplication>(); return(application.Run(options)); } } catch (Exception ex) { return(LogException(log, ex)); } }
/// <summary> /// Splits the specified line into tokens. /// </summary> /// <param name="line">The line to split.</param> /// <returns>The parts that make up the line.</returns> protected static string[] Split(string line) { if (line == null) { throw new ArgumentNullException(nameof(line)); } return(QuoteAwareStringSplitter.Split(line).ToArray()); }
public void Should_Return_Zero_Results_If_Input_String_Is_Null() { // Given var input = string.Empty; // When var result = QuoteAwareStringSplitter.Split(input).ToArray(); // Then Assert.Equal(0, result.Length); }
public void Should_Parse_Single_Quoted_Argument_With_Space_In_It() { // Given const string input = "\"C:\\cake walk\\cake.exe\""; // When var result = QuoteAwareStringSplitter.Split(input).ToArray(); // Then Assert.Equal(1, result.Length); Assert.Equal("\"C:\\cake walk\\cake.exe\"", result[0]); }
public void Should_Parse_Part_That_Contains_Quotes_With_Space_In_It() { // Given const string input = @"cake.exe build.cake -target=""te st"""; // When var result = QuoteAwareStringSplitter.Split(input).ToArray(); // Then Assert.Equal(3, result.Length); Assert.Equal("cake.exe", result[0]); Assert.Equal("build.cake", result[1]); Assert.Equal("-target=\"te st\"", result[2]); }
public void Should_Parse_Multiple_Quoted_Arguments() { // Given const string input = "\"C:\\cake-walk\\cake.exe\" \"build.cake\" \"-dryrun\""; // When var result = QuoteAwareStringSplitter.Split(input).ToArray(); // Then Assert.Equal(3, result.Length); Assert.Equal("\"C:\\cake-walk\\cake.exe\"", result[0]); Assert.Equal("\"build.cake\"", result[1]); Assert.Equal("\"-dryrun\"", result[2]); }
public void Should_Parse_Multiple_Mixed_Arguments() { // Given const string input = "\"C:\\cake-walk\\cake.exe\" build.cake -verbosity \"diagnostic\""; // When var result = QuoteAwareStringSplitter.Split(input).ToArray(); // Then Assert.Equal(4, result.Length); Assert.Equal("\"C:\\cake-walk\\cake.exe\"", result[0]); Assert.Equal("build.cake", result[1]); Assert.Equal("-verbosity", result[2]); Assert.Equal("\"diagnostic\"", result[3]); }
public static void RunCake(ICakeContext context, string script, CakeSettings settings = null) { var rawArgs = QuoteAwareStringSplitter.Split(GetCommandLine()).Skip(1) // Skip executable. .ToArray(); settings = settings ?? new CakeSettings(); var ar = new InternalArgumentParser(context.Log); var baseOptions = ar.Parse(rawArgs); var mergedOptions = new Dictionary <string, string>(); foreach (var optionsArgument in baseOptions) { if (optionsArgument.Key == "target" || string.IsNullOrEmpty(optionsArgument.Value)) { continue; } mergedOptions[optionsArgument.Key] = optionsArgument.Value; } if (settings.Arguments != null) { foreach (var optionsArgument in settings.Arguments) { if (string.IsNullOrEmpty(optionsArgument.Value)) { mergedOptions.Remove(optionsArgument.Key); continue; } mergedOptions[optionsArgument.Key] = optionsArgument.Value; } } mergedOptions.Remove("script"); settings.Arguments = mergedOptions; script = script ?? "./build.cake"; var cakeRunner = new FixedCakeRunner(context.FileSystem, context.Environment, context.Globber, context.ProcessRunner, context.Tools, context.Log); cakeRunner.ExecuteScript(script, settings); }
public static int Main() { // Parse arguments. var args = ArgumentParser.Parse( QuoteAwareStringSplitter .Split(EnvironmentHelper.GetCommandLine()) .Skip(1)); if (args.ContainsKey(Constants.CommandLine.Debug)) { Console.WriteLine($"Attach debugger to process {Process.GetCurrentProcess().Id} to continue. .."); while (!Debugger.IsAttached) { Thread.Sleep(100); } } // Validate the port argument. if (!args.ContainsKey(Constants.CommandLine.Port) || !int.TryParse(args[Constants.CommandLine.Port], out int port)) { throw new ArgumentException("Port not specified or invalid."); } var loggerFactory = new LoggerFactory(); if (args.ContainsKey(Constants.CommandLine.Verbose)) { loggerFactory.AddProvider(new LoggerProvider()); } var registrar = new ContainerRegistrar(); registrar.RegisterModule(new CoreModule()); registrar.RegisterModule(new BakeryModule(loggerFactory)); // Build the container. using (var container = registrar.Build()) { var fileSystem = container.Resolve <IFileSystem>(); var log = container.Resolve <ICakeLog>(); var configurationProvider = container.Resolve <CakeConfigurationProvider>(); var workingDirectory = new DirectoryPath(System.IO.Directory.GetCurrentDirectory()); var configuration = configurationProvider.CreateConfiguration(workingDirectory, args); // Rebuild the container for NuGet and Buffered File System. registrar = new ContainerRegistrar(); registrar.RegisterInstance(configuration); registrar.RegisterModule(new NuGetModule(configuration)); registrar.RegisterModule(new ScriptingModule(fileSystem, log)); registrar.Builder.Update(container); var environment = container.Resolve <ICakeEnvironment>(); var aliasFinder = container.Resolve <IScriptAliasFinder>(); var processor = container.Resolve <Core.Scripting.IScriptProcessor>(); var aliasGenerator = container.Resolve <ICakeAliasGenerator>(); var loadDirectiveProviders = container.Resolve <IEnumerable <ILoadDirectiveProvider> >(); // Rebuild the container for Cached Alias Finder. registrar = new ContainerRegistrar(); registrar.RegisterModule(new CacheModule(aliasFinder, processor, environment, aliasGenerator, loadDirectiveProviders)); registrar.Builder.Update(container); // Get Script generator. var scriptGenerator = container.Resolve <IScriptGenerationService>(); environment.WorkingDirectory = workingDirectory; try { using (var server = new ScriptGenerationServer(scriptGenerator, port, loggerFactory)) { server.Start(); var cancel = new ManualResetEvent(false); server.OnDisconnected += (sender, e) => { cancel.Set(); }; Console.CancelKeyPress += (sender, e) => { cancel.Set(); e.Cancel = true; }; cancel.WaitOne(); server.Stop(); } } catch (Exception e) { loggerFactory.CreateLogger <Program>().LogCritical(0, e, "Unhandled Exception"); throw; } } return(0); }