Пример #1
0
        /// <summary>
        ///     Registers a task that performs final setup of configuration after all plugin-specific
        ///     config tasks have been run.
        ///     <para/>
        ///     This includes:
        ///     1. Runs all configuration lambdas from the <c>ConfigureTask</c> methods in the
        ///     build.cake file.
        ///     2. Override configurations with any matching values from the environment.
        ///     3. Override configurations with any matching values from the command line.
        /// </summary>
        /// <param name="envTasks">List of all plugin tasks for the current CI environment.</param>
        private void RegisterConfigTask(IReadOnlyList <RegisteredTask> envTasks)
        {
            CakeTaskBuilder task = RegisterTask(TaskNames.Config)
                                   .Description("Finalizes configurations and displays final configuration values.");

            // Create dependency on all plugin configuration tasks.
            IEnumerable <RegisteredTask> configTasks = envTasks
                                                       .Where(t => t.AttributeType == typeof(ConfigAttribute))
                                                       .OrderBy(t => t.Order);

            foreach (RegisteredTask configTask in configTasks)
            {
                task.IsDependentOn(configTask.Name);
            }

            task.Does(ctx =>
            {
                TaskConfig config = TaskConfig.Current;

                // Run configurations specified in the build.cake file
                config.PerformDeferredSetup();

                // Override configurations from environment variables
                IDictionary <string, string> envVars = ctx.Environment.GetEnvironmentVariables();
                foreach (KeyValuePair <string, string> envVar in envVars)
                {
                    if (config.Data.ContainsKey(envVar.Key))
                    {
                        config.Data[envVar.Key] = envVar.Value;
                    }
                }

                // Override configurations from command line arguments
                var keys = config.Data.Keys.ToList();
                foreach (string key in keys)
                {
                    if (ctx.Arguments.HasArgument(key))
                    {
                        config.Data[key] = ctx.Arguments.GetArgument(key);
                    }
                }

                // Display the final configuration values
                ctx.LogHighlight("Final Configurations");
                ctx.LogHighlight("--------------------");
                foreach (KeyValuePair <string, object> data in config.Data.OrderBy(kvp => kvp.Key))
                {
                    ctx.LogHighlight($"{data.Key} = {data.Value?.Dump() ?? "[NULL]"}");
                }

                EnvConfig env = config.Load <EnvConfig>();

                // Clean out output directories or create them
                //TODO: Can these directories be created on-demand? For some project types like Angular,
                //these folders are ignored and the dist folder is used.
                ctx.EnsureDirectoryExists(env.Directories.Artifacts);
                ctx.EnsureDirectoryExists(env.Directories.BinaryOutput);
                ctx.EnsureDirectoryExists(env.Directories.TestOutput);
            });
        }