Esempio n. 1
0
        /// <summary>
        ///     Creates an actual Cake task from the internal <see cref="RegisteredTask"/> structure.
        /// </summary>
        private void RegisterPluginTasks()
        {
            foreach (RegisteredTask registeredTask in _registeredTasks)
            {
                CakeTaskBuilder builder = RegisterTask(registeredTask.Name)
                                          .Description(registeredTask.Description);

                if (registeredTask.ContinueOnError)
                {
                    builder.ContinueOnError();
                }

                if (registeredTask.AttributeType == typeof(TaskAttribute) && registeredTask.RequiresConfig)
                {
                    builder.IsDependentOn(TaskNames.Config);
                }

                ParameterInfo[] parameters = registeredTask.Method.GetParameters();
                switch (parameters.Length)
                {
                case 2:
                    var action2 = (Action <ICakeContext, TaskConfig>)registeredTask.Method.CreateDelegate(
                        typeof(Action <ICakeContext, TaskConfig>));
                    builder.Does(action2);
                    break;

                case 1:
                    if (parameters[0].ParameterType == typeof(TaskConfig))
                    {
                        Action <ICakeContext, TaskConfig> action1 = (_, cfg) =>
                        {
                            registeredTask.Method.Invoke(obj: null, new object[] { cfg });
                        };
                        builder.Does(action1);
                    }
                    else
                    {
                        var action1 = (Action <ICakeContext>)registeredTask.Method.CreateDelegate(typeof(Action <ICakeContext>));
                        builder.Does(action1);
                    }

                    break;

                default:
                    var action = (Action)registeredTask.Method.CreateDelegate(typeof(Action));
                    builder.Does(action);
                    break;
                }
            }
        }
Esempio n. 2
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);
            });
        }
Esempio n. 3
0
 /// <summary>
 /// A quick error function that will invoke errors using the event
 /// </summary>
 /// <param name="builder">The CakeTaskBuilder being extended</param>
 /// <param name="continueOnError">Continue on error if you want</param>
 public static void CompleteTask(this CakeTaskBuilder builder, bool continueOnError = false)
 {
     builder
     .Does((context) => {
         context.Information($"Task {builder.Task.Name} Completed!");
     })
     .QuickError(continueOnError);
 }
Esempio n. 4
0
                public void Should_Add_Action_To_Task()
                {
                    // Given
                    var task    = new ActionTask("task");
                    var builder = new CakeTaskBuilder <ActionTask>(task);

                    // When
                    builder.Does(c => { });

                    // Then
                    Assert.Equal(1, task.Actions.Count);
                }
                public void Should_Throw_If_Action_Is_Null()
                {
                    // Given
                    var task    = new ActionTask("task");
                    var builder = new CakeTaskBuilder <ActionTask>(task);

                    // When
                    var result = Record.Exception(() => builder.Does((Action)null));

                    // Then
                    Assert.IsArgumentNullException(result, "action");
                }
                public void Should_Add_Action_To_Task()
                {
                    // Given
                    var task    = new ActionTask("task");
                    var builder = new CakeTaskBuilder <ActionTask>(task);

                    // When
                    builder.Does(() => { });

                    // Then
                    Assert.Single(task.Actions);
                }
                        public void Should_Add_Action_To_Task()
                        {
                            // Given
                            var task    = new CakeTask("task");
                            var builder = new CakeTaskBuilder(task);

                            // When
                            builder.Does <string>(data => { });

                            // Then
                            Assert.Single(task.Actions);
                        }
                        public void Should_Add_Action_To_Task()
                        {
                            // Given
                            var task    = new CakeTask("task");
                            var builder = new CakeTaskBuilder(task);

                            // When
                            builder.Does(async() => { await Task.Delay(0); });

                            // Then
                            Assert.Single(task.Actions);
                        }
                        public void Should_Throw_If_Action_Is_Null()
                        {
                            // Given
                            var task    = new CakeTask("task");
                            var builder = new CakeTaskBuilder(task);

                            // When
                            var result = Record.Exception(() => builder.Does((Func <ICakeContext, Task>)null));

                            // Then
                            AssertEx.IsArgumentNullException(result, "func");
                        }
Esempio n. 10
0
                private void Should_Throw_If_Action_Is_Null()
                {
                    // Given
                    var task    = new ActionTask("task");
                    var builder = new CakeTaskBuilder <ActionTask>(task);


                    // When
                    var exception = Record.Exception(() => builder.Does((Action)null));

                    // Then
                    Assert.IsType <ArgumentNullException>(exception);
                    Assert.Equal("action", ((ArgumentNullException)exception).ParamName);
                }
Esempio n. 11
0
        private void GenerateTasks(string taskName, IEnumerable <Target> targets)
        {
            CakeTaskBuilder <ActionTask> buildTask   = _parameters.Context.Task($"build-{taskName}");
            CakeTaskBuilder <ActionTask> releaseTask = _parameters.Context.Task($"release-{taskName}");
            CakeTaskBuilder <ActionTask> deployTask  = _parameters.Context.Task($"deploy-{taskName}");

            foreach (Target target in targets)
            {
                buildTask.IsDependentOn(target.BuildTaskName);
                releaseTask.IsDependentOn(target.ReleaseTaskName);
                deployTask.IsDependentOn(target.DeployTaskName);
            }

            buildTask.Does(() => { });
            releaseTask.Does(() => { });
            deployTask.Does(() => { });
        }
Esempio n. 12
0
        public void Build()
        {
            List <Target> targets = MergeTargets(_parameters);

            //generate application-target-platform
            foreach (Target target in targets)
            {
                //generate application-target-platform
                _parameters.Context.Task(target.BuildTaskName).Does(() =>
                {
                    foreach (IStep preBuild in target.Configuration.StepsOf <PreBuildStepAttribute>())
                    {
                        preBuild.Execute(target.Configuration, StepType.PreBuild);
                    }

                    foreach (IStep build in target.Configuration.StepsOf <BuildStepAttribute>())
                    {
                        build.Execute(target.Configuration, StepType.Build);
                    }

                    foreach (IStep postBuild in target.Configuration.StepsOf <PostBuildStepAttribute>())
                    {
                        postBuild.Execute(target.Configuration, StepType.PostBuild);
                    }
                });

                _parameters.Context.Task(target.ReleaseTaskName)
                .IsDependentOn(CleanTaskBuilder.TASK_NAME)
                .IsDependentOn(target.BuildTaskName)
                .Does(() =>
                {
                    foreach (IStep preRelease in target.Configuration.StepsOf <PreReleaseStepAttribute>())
                    {
                        preRelease.Execute(target.Configuration, StepType.PreRelease);
                    }

                    foreach (IStep release in target.Configuration.StepsOf <ReleaseStepAttribute>())
                    {
                        release.Execute(target.Configuration, StepType.Release);
                    }

                    foreach (IStep postRelease in target.Configuration.StepsOf <PostReleaseStepAttribute>())
                    {
                        postRelease.Execute(target.Configuration, StepType.PostRelease);
                    }
                });

                _parameters.Context.Task(target.DeployTaskName)
                .IsDependentOn(CleanTaskBuilder.TASK_NAME)
                .IsDependentOn(target.ReleaseTaskName)
                .Does(() =>
                {
                    foreach (IStep preDeploy in target.Configuration.StepsOf <PreDeployStepAttribute>())
                    {
                        preDeploy.Execute(target.Configuration, StepType.PreDeploy);
                    }

                    foreach (IStep deploy in target.Configuration.StepsOf <DeployStepAttribute>())
                    {
                        deploy.Execute(target.Configuration, StepType.Deploy);
                    }

                    foreach (IStep postDeploy in target.Configuration.StepsOf <PostDeployStepAttribute>())
                    {
                        postDeploy.Execute(target.Configuration, StepType.PostDeploy);
                    }
                });
            }

            //generate application-target
            foreach (IList <Target> grouping in targets.GroupBy(x => x.ApplicationName).SelectMany(x => x.GroupBy(y => y.TargetName).Select(y => y.ToList())))
            {
                Target sample = grouping.First();
                GenerateTasks($"{sample.ApplicationName}-{sample.TargetName}", grouping);
            }

            //generate application-platform
            foreach (IList <Target> grouping in targets.GroupBy(x => x.ApplicationName).SelectMany(x => x.GroupBy(y => y.PlatformName).Select(y => y.ToList())))
            {
                Target sample = grouping.First();
                GenerateTasks($"{sample.ApplicationName}-{sample.PlatformName}", grouping);
            }

            //generate target-platform
            foreach (IList <Target> grouping in targets.GroupBy(x => x.TargetName).SelectMany(x => x.GroupBy(y => y.PlatformName).Select(y => y.ToList())))
            {
                Target sample = grouping.First();
                GenerateTasks($"{sample.TargetName}-{sample.PlatformName}", grouping);
            }

            //generate application
            foreach (IList <Target> grouping in targets.GroupBy(x => x.ApplicationName).Select(y => y.ToList()))
            {
                Target sample = grouping.First();
                GenerateTasks($"{sample.ApplicationName}", grouping);
            }

            //generate target
            foreach (IList <Target> grouping in targets.GroupBy(x => x.TargetName).Select(y => y.ToList()))
            {
                Target sample = grouping.First();
                GenerateTasks($"{sample.TargetName}", grouping);
            }

            //generate platform
            foreach (IList <Target> grouping in targets.GroupBy(x => x.PlatformName).Select(y => y.ToList()))
            {
                Target sample = grouping.First();
                GenerateTasks($"{sample.PlatformName}", grouping);
            }

            CakeTaskBuilder <ActionTask> buildTask   = _parameters.Context.Task("build");
            CakeTaskBuilder <ActionTask> releaseTask = _parameters.Context.Task("release");
            CakeTaskBuilder <ActionTask> deployTask  = _parameters.Context.Task("deploy");

            foreach (Target target in targets)
            {
                buildTask.IsDependentOn(target.BuildTaskName);
                releaseTask.IsDependentOn(target.ReleaseTaskName);
                deployTask.IsDependentOn(target.DeployTaskName);
            }

            buildTask.Does(() => { });
            releaseTask.Does(() => { });
            deployTask.Does(() => { });
        }