Exemple #1
0
        public FrostingConfiguration(IEnumerable <FrostingConfigurationValue> values, IFileSystem fileSystem, ICakeEnvironment environment, IRemainingArguments remainingArguments)
        {
            if (values is null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            if (fileSystem is null)
            {
                throw new ArgumentNullException(nameof(fileSystem));
            }

            if (environment is null)
            {
                throw new ArgumentNullException(nameof(environment));
            }

            var baseConfiguration = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            foreach (var value in values)
            {
                baseConfiguration[value.Key] = value.Value;
            }

            var provider = new CakeConfigurationProvider(fileSystem, environment);
            var args     = remainingArguments.Parsed.ToDictionary(x => x.Key, x => x.FirstOrDefault() ?? string.Empty);

            _cakeConfiguration = provider.CreateConfiguration(environment.WorkingDirectory, baseConfiguration, args);
        }
Exemple #2
0
        public FakeCakeContext()
        {
            var testsDir = new DirectoryPath(System.IO.Path.GetFullPath(AppContext.BaseDirectory));

            var fileSystem  = new FileSystem();
            var environment = new FakeEnvironment(PlatformFamily.Windows);
            var globber     = new Globber(fileSystem, environment);
            var log         = new FakeLog();
            var args        = new FakeCakeArguments();
            var toolRepo    = new ToolRepository(environment);
            var config      =
                new CakeConfigurationProvider(fileSystem, environment)
                .CreateConfiguration(testsDir,
                                     new Dictionary <string, string>());
            var toolResolutionStrategy = new ToolResolutionStrategy(fileSystem, environment, globber, config);
            var toolLocator            = new ToolLocator(environment, toolRepo, toolResolutionStrategy);
            var processRunner          = new ProcessRunner(fileSystem, environment, log, toolLocator, config);
            var registry    = new WindowsRegistry();
            var dataService = new FakeDataService();

            _context = new CakeContext(
                fileSystem,
                environment,
                globber,
                log,
                args,
                processRunner,
                registry,
                toolLocator,
                dataService,
                config);

            _context.Environment.WorkingDirectory = testsDir;
        }
Exemple #3
0
        protected ICakeConfiguration ReadConfiguration(
            IRemainingArguments remaining, DirectoryPath root)
        {
            var provider = new CakeConfigurationProvider(_fileSystem, _environment);
            var args     = remaining.Parsed.ToDictionary(x => x.Key, x => x.FirstOrDefault() ?? string.Empty);

            return(provider.CreateConfiguration(root, args));
        }
 public Configuration(
     CakeConfigurationProvider provider,
     CakeHostOptions options,
     ICakeEnvironment environment,
     IEnumerable <ConfigurationValue> values)
 {
     _provider      = provider;
     _options       = options;
     _environment   = environment;
     _values        = values;
     _lock          = new object();
     _configuration = null;
 }
Exemple #5
0
            public void Should_Throw_If_Registrar_Are_Null()
            {
                // Given
                var fileSystem  = Substitute.For <IFileSystem>();
                var environment = FakeEnvironment.CreateUnixEnvironment();
                var provider    = new CakeConfigurationProvider(fileSystem, environment);
                var options     = new CakeOptions();
                var module      = new ConfigurationModule(provider, options);

                // When
                var result = Record.Exception(() => module.Register(null));

                // Then
                AssertEx.IsArgumentNullException(result, "registrar");
            }
Exemple #6
0
            public void Should_Use_The_Script_Directory_As_Root_For_Configuration_File()
            {
                // Given
                var fileSystem  = Substitute.For <IFileSystem>();
                var environment = FakeEnvironment.CreateUnixEnvironment();
                var provider    = new CakeConfigurationProvider(fileSystem, environment);
                var registry    = new ContainerRegistrar();
                var options     = new CakeOptions {
                    Script = "./foo/bar/build.cake"
                };
                var module = new ConfigurationModule(provider, options);

                // When
                module.Register(registry);

                // Then
                fileSystem.Received(1).Exist(Arg.Is <FilePath>(f => f.FullPath == "/Working/foo/bar/cake.config"));
            }
Exemple #7
0
        /// <summary>
        /// Runs the application.
        /// </summary>
        /// <param name="args">Arguments.</param>
        /// <param name="appRoot">Application root folder</param>
        /// <returns>The result of the run.</returns>
        public async Task <RunResult> RunAsync(IEnumerable <string> args, string appRoot = null)
        {
            var console = new CakeConsole();
            var logger  = new SafeCakeLog(console);
            ICakeDataService dataService = new CodeCakeDataService();
            var engine = new CakeEngine(dataService, logger);

            ICakePlatform          platform    = new CakePlatform();
            ICakeRuntime           runtime     = new CakeRuntime();
            IFileSystem            fileSystem  = new FileSystem();
            MutableCakeEnvironment environment = new MutableCakeEnvironment(platform, runtime, appRoot);

            console.SupportAnsiEscapeCodes = AnsiDetector.SupportsAnsi(environment);

            IGlobber globber = new Globber(fileSystem, environment);

            IRegistry windowsRegistry = new WindowsRegistry();
            // Parse options.
            var         argumentParser = new ArgumentParser(logger, fileSystem);
            CakeOptions options        = argumentParser.Parse(args);

            Debug.Assert(options != null);
            CakeConfigurationProvider configProvider = new CakeConfigurationProvider(fileSystem, environment);
            ICakeConfiguration        configuration  = configProvider.CreateConfiguration(environment.ApplicationRoot, options.Arguments);
            IToolRepository           toolRepo       = new ToolRepository(environment);
            IToolResolutionStrategy   toolStrategy   = new ToolResolutionStrategy(fileSystem, environment, globber, configuration, logger);
            IToolLocator   locator       = new ToolLocator(environment, toolRepo, toolStrategy);
            IToolLocator   toolLocator   = new ToolLocator(environment, toolRepo, toolStrategy);
            IProcessRunner processRunner = new ProcessRunner(fileSystem, environment, logger, toolLocator, configuration);

            logger.SetVerbosity(options.Verbosity);
            ICakeArguments arguments = new CakeArguments(options.Arguments);
            var            context   = new CakeContext(
                fileSystem,
                environment,
                globber,
                logger,
                arguments,
                processRunner,
                windowsRegistry,
                locator,
                dataService,
                configuration);

            CodeCakeBuildTypeDescriptor choosenBuild;

            if (!AvailableBuilds.TryGetValue(options.Script, out choosenBuild))
            {
                logger.Error("Build script '{0}' not found.", options.Script);
                return(new RunResult(-1, context.InteractiveMode()));
            }

            // Set the working directory: the solution directory.
            logger.Information($"Working in Solution directory: '{_solutionDirectory}'.");
            environment.WorkingDirectory = new DirectoryPath(_solutionDirectory);

            try
            {
                SetEnvironmentVariablesFromCodeCakeBuilderKeyVault(logger, context);

                // Instantiates the script object.
                CodeCakeHost._injectedActualHost = new BuildScriptHost(engine, context);
                CodeCakeHost c = (CodeCakeHost)Activator.CreateInstance(choosenBuild.Type);


                var target                  = context.Arguments.GetArgument("target") ?? "Default";
                var execSettings            = new ExecutionSettings().SetTarget(target);
                var exclusiveTargetOptional = context.Arguments.HasArgument("exclusiveOptional");
                var exclusiveTarget         = exclusiveTargetOptional | context.Arguments.HasArgument("exclusive");
                var strategy                = new CodeCakeExecutionStrategy(logger, exclusiveTarget ? target : null);
                if (exclusiveTargetOptional && !engine.Tasks.Any(t => t.Name == target))
                {
                    logger.Warning($"No task '{target}' defined. Since -exclusiveOptional is specified, nothing is done.");
                    return(new RunResult(0, context.InteractiveMode()));
                }
                var report = await engine.RunTargetAsync(context, strategy, execSettings);

                if (report != null && !report.IsEmpty)
                {
                    var printerReport = new CakeReportPrinter(console);
                    printerReport.Write(report);
                }
            }
            catch (CakeTerminateException ex)
            {
                switch (ex.Option)
                {
                case CakeTerminationOption.Error:
                    logger.Error("Termination with Error: '{0}'.", ex.Message);
                    return(new RunResult(-2, context.InteractiveMode()));

                case CakeTerminationOption.Warning:
                    logger.Warning("Termination with Warning: '{0}'.", ex.Message);
                    break;

                default:
                    Debug.Assert(ex.Option == CakeTerminationOption.Success);
                    logger.Information("Termination with Success: '{0}'.", ex.Message);
                    break;
                }
            }
            catch (TargetInvocationException ex)
            {
                logger.Error("Error occurred: '{0}'.", ex.InnerException?.Message ?? ex.Message);
                return(new RunResult(-3, context.InteractiveMode()));
            }
            catch (AggregateException ex)
            {
                logger.Error("Error occurred: '{0}'.", ex.Message);
                foreach (var e in ex.InnerExceptions)
                {
                    logger.Error("  -> '{0}'.", e.Message);
                }
                return(new RunResult(-4, context.InteractiveMode()));
            }
            catch (Exception ex)
            {
                logger.Error("Error occurred: '{0}'.", ex.Message);
                return(new RunResult(-5, context.InteractiveMode()));
            }
            return(new RunResult(0, context.InteractiveMode()));
        }
        /// <summary>
        /// Runs the application.
        /// </summary>
        /// <param name="args">Arguments.</param>
        /// <returns>0 on success.</returns>
        public int Run( string[] args )
        {
            var console = new CakeConsole();
            var logger = new SafeCakeLog( console );
            var engine = new CakeEngine( logger );

            ICakePlatform platform = new CakePlatform();
            ICakeRuntime runtime = new CakeRuntime();
            IFileSystem fileSystem = new FileSystem();
            MutableCakeEnvironment environment = new MutableCakeEnvironment( platform, runtime );
            IGlobber globber = new Globber( fileSystem, environment );
            environment.Initialize( globber );
            IProcessRunner processRunner = new ProcessRunner( environment, logger );
            IRegistry windowsRegistry = new WindowsRegistry();
            // Parse options.
            var argumentParser = new ArgumentParser( logger, fileSystem );
            CakeOptions options = argumentParser.Parse( args );
            Debug.Assert( options != null );
            CakeConfigurationProvider configProvider = new CakeConfigurationProvider( fileSystem, environment );
            ICakeConfiguration configuration = configProvider.CreateConfiguration( environment.ApplicationRoot, options.Arguments );
            IToolRepository toolRepo = new ToolRepository( environment );
            IToolResolutionStrategy toolStrategy = new ToolResolutionStrategy( fileSystem, environment, globber, configuration );
            IToolLocator locator = new ToolLocator( environment, toolRepo, toolStrategy );
            IToolLocator toolLocator = new ToolLocator( environment, toolRepo, toolStrategy  );
            logger.SetVerbosity( options.Verbosity );
            CodeCakeBuildTypeDescriptor choosenBuild;
            if( !AvailableBuilds.TryGetValue( options.Script, out choosenBuild ) )
            {
                logger.Error( "Build script '{0}' not found.", options.Script );
                return -1;
            }

            ICakeArguments arguments = new CakeArguments(options.Arguments);

            var context = new CakeContext( fileSystem, environment, globber, logger, arguments, processRunner, windowsRegistry, locator );

            // Copy the arguments from the options.

            // Set the working directory: the solution directory.
            environment.WorkingDirectory = new DirectoryPath( _solutionDirectory );

            // Adds additional paths from chosen build.
            foreach( var p in choosenBuild.AdditionnalPatternPaths )
            {
                environment.AddPath( p );
            }
            logger.Information( "Path(s) added: " + string.Join( ", ", environment.EnvironmentAddedPaths ) );
            logger.Information( "Dynamic pattern path(s) added: " + string.Join( ", ", environment.EnvironmentDynamicPaths ) );

            try
            {
                // Instanciates the script object.
                CodeCakeHost._injectedActualHost = new BuildScriptHost( engine, context );
                CodeCakeHost c = (CodeCakeHost)Activator.CreateInstance( choosenBuild.Type );

                var strategy = new DefaultExecutionStrategy( logger );
                var report = engine.RunTarget( context, strategy, context.Arguments.GetArgument( "target" ) ?? "Default" );
                if( report != null && !report.IsEmpty )
                {
                    var printerReport = new CakeReportPrinter( console );
                    printerReport.Write( report );
                }
            }
            catch( CakeTerminateException ex )
            {
                switch( ex.Option )
                {
                    case CakeTerminationOption.Error:
                        logger.Error( "Termination with Error: '{0}'.", ex.Message );
                        return -1;
                    case CakeTerminationOption.Warning:
                        logger.Warning( "Termination with Warning: '{0}'.", ex.Message );
                        break;
                    default:
                        Debug.Assert( ex.Option == CakeTerminationOption.Success );
                        logger.Information( "Termination with Success: '{0}'.", ex.Message );
                        break;
                }
            }
            catch( TargetInvocationException ex )
            {
                logger.Error( "Error occurred: '{0}'.", ex.InnerException?.Message ?? ex.Message );
                return -1;
            }
            catch( Exception ex )
            {
                logger.Error( "Error occurred: '{0}'.", ex.Message );
                return -1;
            }
            return 0;
        }
        /// <summary>
        /// Runs the application.
        /// </summary>
        /// <param name="args">Arguments.</param>
        /// <param name="appRoot">Application root folder</param>
        /// <returns>0 on success.</returns>
        public int Run(string[] args, string appRoot = null)
        {
            var console = new CakeConsole();
            var logger  = new SafeCakeLog(console);
            var engine  = new CakeEngine(logger);

            ICakePlatform          platform    = new CakePlatform();
            ICakeRuntime           runtime     = new CakeRuntime();
            IFileSystem            fileSystem  = new FileSystem();
            MutableCakeEnvironment environment = new MutableCakeEnvironment(platform, runtime, appRoot);
            IGlobber globber = new Globber(fileSystem, environment);

            environment.Initialize(globber);
            IProcessRunner processRunner   = new ProcessRunner(environment, logger);
            IRegistry      windowsRegistry = new WindowsRegistry();
            // Parse options.
            var         argumentParser = new ArgumentParser(logger, fileSystem);
            CakeOptions options        = argumentParser.Parse(args);

            Debug.Assert(options != null);
            CakeConfigurationProvider configProvider = new CakeConfigurationProvider(fileSystem, environment);
            ICakeConfiguration        configuration  = configProvider.CreateConfiguration(environment.ApplicationRoot, options.Arguments);
            IToolRepository           toolRepo       = new ToolRepository(environment);
            IToolResolutionStrategy   toolStrategy   = new ToolResolutionStrategy(fileSystem, environment, globber, configuration);
            IToolLocator locator     = new ToolLocator(environment, toolRepo, toolStrategy);
            IToolLocator toolLocator = new ToolLocator(environment, toolRepo, toolStrategy);

            logger.SetVerbosity(options.Verbosity);
            CodeCakeBuildTypeDescriptor choosenBuild;

            if (!AvailableBuilds.TryGetValue(options.Script, out choosenBuild))
            {
                logger.Error("Build script '{0}' not found.", options.Script);
                return(-1);
            }

            ICakeArguments arguments = new CakeArguments(options.Arguments);

            var context = new CakeContext(fileSystem, environment, globber, logger, arguments, processRunner, windowsRegistry, locator);

            // Copy the arguments from the options.

            // Set the working directory: the solution directory.
            environment.WorkingDirectory = new DirectoryPath(_solutionDirectory);

            // Adds additional paths from chosen build.
            foreach (var p in choosenBuild.AdditionnalPatternPaths)
            {
                environment.AddPath(p);
            }
            logger.Information("Path(s) added: " + string.Join(", ", environment.EnvironmentAddedPaths));
            logger.Information("Dynamic pattern path(s) added: " + string.Join(", ", environment.EnvironmentDynamicPaths));

            try
            {
                // Instanciates the script object.
                CodeCakeHost._injectedActualHost = new BuildScriptHost(engine, context);
                CodeCakeHost c = (CodeCakeHost)Activator.CreateInstance(choosenBuild.Type);

                var strategy = new DefaultExecutionStrategy(logger);
                var report   = engine.RunTargetAsync(context, strategy, context.Arguments.GetArgument("target") ?? "Default").GetAwaiter().GetResult();
                if (report != null && !report.IsEmpty)
                {
                    var printerReport = new CakeReportPrinter(console);
                    printerReport.Write(report);
                }
            }
            catch (CakeTerminateException ex)
            {
                switch (ex.Option)
                {
                case CakeTerminationOption.Error:
                    logger.Error("Termination with Error: '{0}'.", ex.Message);
                    return(-1);

                case CakeTerminationOption.Warning:
                    logger.Warning("Termination with Warning: '{0}'.", ex.Message);
                    break;

                default:
                    Debug.Assert(ex.Option == CakeTerminationOption.Success);
                    logger.Information("Termination with Success: '{0}'.", ex.Message);
                    break;
                }
            }
            catch (TargetInvocationException ex)
            {
                logger.Error("Error occurred: '{0}'.", ex.InnerException?.Message ?? ex.Message);
                return(-1);
            }
            catch (Exception ex)
            {
                logger.Error("Error occurred: '{0}'.", ex.Message);
                return(-1);
            }
            return(0);
        }
Exemple #10
0
        public ICakeConfiguration Create()
        {
            var provider = new CakeConfigurationProvider(FileSystem, Environment);

            return(provider.CreateConfiguration(Path, Arguments));
        }
Exemple #11
0
 public ConfigurationModule(IContainer container, CakeOptions options)
 {
     _provider = container.Resolve <CakeConfigurationProvider>();
     _options  = options;
 }
Exemple #12
0
 public ConfigurationModule(CakeConfigurationProvider provider, CakeOptions options)
 {
     _provider = provider;
     _options  = options;
 }