如何同步(编程地)运行Grunt任务?

时间:2021-07-11 19:14:08

I want a task that runs sub-tasks synchonously in a loop, and such loop is broken when a sub-task changes the value of a flag.

我想要一个在循环中同步运行子任务的任务,当子任务更改标志的值时,这样的循环就会被破坏。

Here is a sample of what I'm trying to do:

这是我想做的一个例子:

grunt.registerTask('complexTask', 'Run sub-tasks synchronously.', function () {
    var value;
    do {
        // 'changeValueTask' task sets 'importantValue'
        grunt.task.run(['preTask', 'changeValueTask', 'postTask']);
        value = grunt.config('importantValue');
    } while (!value);
    // ... end task
});

What I want to get out of this is

我想从中得到的是

  • make sure that each set of tasks (['preTask', 'changeValueTask', 'postTask']) is run in order (sequentially)
  • 确保每一组任务(['preTask', 'changeValueTask', 'postTask'])按顺序运行(顺序)
  • and the best way to break out of the loop
  • 以及跳出循环的最佳方式

Is it possible to achive this?

有可能做到这一点吗?

Note: the closest thing I could find after some research was that Grunt allows to define that a given task B fails if task A is not done (with grunt.task.requires: grunt.task.requires('A')).

注意:在一些研究之后,我能找到的最接近的东西是Grunt(咕哝)可以定义如果任务a没有完成,给定任务B就会失败。要求:grunt.task.requires(A))。

1 个解决方案

#1


2  

This is how we fixed it (following in the footsteps of Bergi's comment):

这就是我们如何修复它(跟随Bergi的评论):

Instead of trying to explicitly loop and run tasks on Grunt, it is best to use the natural task-chaining mechanism of Grunt to ensure one task is executed after the other, like so

与其尝试在Grunt上显式地循环和运行任务,不如使用Grunt的自然任务链接机制来确保一个任务接一个地执行,就像这样

grunt.task.run(['preTask', 'changeValueTask', 'postTask']);

Assuming this, we used recursion at the end of each postTask to check if we need to run it again (by checking the importantValue flag) - we achieve this with a task on its own

假设如此,我们在每个postTask的末尾使用递归检查是否需要再次运行它(通过检查importantValue标志)——我们通过单独的任务来实现这一点

grunt.task.run(['preTask', 'changeValueTask', 'postTask', 'taskCheck']);

grunt.registerTask('taskCheck', 'Recursive task.', function() {
  var value = grunt.config('importantValue');
  if (value) {
    // done... work with value
  } else {
    // keep running
    grunt.task.run(['preTask', 'changeValueTask', 'postTask', 'taskCheck']);
  }
});

As an improvement we can bootstrap this whole recursion on a task to avoid unnecessary cluttering and isolate this looping behavior:

作为改进,我们可以在任务上引导整个递归,以避免不必要的混乱和隔离这种循环行为:

grunt.registerTask('runValueTask', 'Bootstrap to start recursion.', function() {
  grunt.task.run(['preTask', 'changeValueTask', 'postTask', 'taskCheck']);
});

grunt.registerTask('taskCheck', 'Recursive task.', function() {
  var value = grunt.config('importantValue');
  if (value) {
    // done... work with value
  } else {
    // keep running
    grunt.task.run('runValueTask');
  }
});

#1


2  

This is how we fixed it (following in the footsteps of Bergi's comment):

这就是我们如何修复它(跟随Bergi的评论):

Instead of trying to explicitly loop and run tasks on Grunt, it is best to use the natural task-chaining mechanism of Grunt to ensure one task is executed after the other, like so

与其尝试在Grunt上显式地循环和运行任务,不如使用Grunt的自然任务链接机制来确保一个任务接一个地执行,就像这样

grunt.task.run(['preTask', 'changeValueTask', 'postTask']);

Assuming this, we used recursion at the end of each postTask to check if we need to run it again (by checking the importantValue flag) - we achieve this with a task on its own

假设如此,我们在每个postTask的末尾使用递归检查是否需要再次运行它(通过检查importantValue标志)——我们通过单独的任务来实现这一点

grunt.task.run(['preTask', 'changeValueTask', 'postTask', 'taskCheck']);

grunt.registerTask('taskCheck', 'Recursive task.', function() {
  var value = grunt.config('importantValue');
  if (value) {
    // done... work with value
  } else {
    // keep running
    grunt.task.run(['preTask', 'changeValueTask', 'postTask', 'taskCheck']);
  }
});

As an improvement we can bootstrap this whole recursion on a task to avoid unnecessary cluttering and isolate this looping behavior:

作为改进,我们可以在任务上引导整个递归,以避免不必要的混乱和隔离这种循环行为:

grunt.registerTask('runValueTask', 'Bootstrap to start recursion.', function() {
  grunt.task.run(['preTask', 'changeValueTask', 'postTask', 'taskCheck']);
});

grunt.registerTask('taskCheck', 'Recursive task.', function() {
  var value = grunt.config('importantValue');
  if (value) {
    // done... work with value
  } else {
    // keep running
    grunt.task.run('runValueTask');
  }
});