file_task.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. var fs = require('fs')
  2. , Task = require('./task').Task
  3. , FileTask
  4. , FileBase
  5. , DirectoryTask
  6. , utils = require('../utils');
  7. FileBase = new (function () {
  8. var isFileOrDirectory = function (t) {
  9. return (t instanceof FileTask ||
  10. t instanceof DirectoryTask);
  11. }
  12. , isFile = function (t) {
  13. return (t instanceof FileTask && !(t instanceof DirectoryTask));
  14. };
  15. this.isNeeded = function () {
  16. var runAction = false
  17. , prereqs = this.prereqs
  18. , prereqName
  19. , prereqTask;
  20. // No repeatsies
  21. if (this.taskStatus === Task.runStatuses.DONE) {
  22. return false;
  23. }
  24. // The always-make override
  25. else if (jake.program.opts['always-make']) {
  26. // Run if there actually is an action
  27. if (typeof this.action == 'function') {
  28. return true;
  29. }
  30. else {
  31. return false;
  32. }
  33. }
  34. // Default case
  35. else {
  36. // We need either an existing file, or an action to create one.
  37. // First try grabbing the actual mod-time of the file
  38. try {
  39. this.updateModTime();
  40. }
  41. // Then fall back to looking for an action
  42. catch(e) {
  43. if (typeof this.action == 'function') {
  44. return true;
  45. }
  46. else {
  47. throw new Error('File-task ' + this.fullName + ' has no ' +
  48. 'existing file, and no action to create one.');
  49. }
  50. }
  51. // Compare mod-time of all the prereqs with its mod-time
  52. // If any prereqs are newer, need to run the action to update
  53. if (prereqs && prereqs.length) {
  54. for (var i = 0, ii = prereqs.length; i < ii; i++) {
  55. prereqName = prereqs[i];
  56. prereqTask = this.namespace.resolveTask(prereqName) ||
  57. jake.createPlaceholderFileTask(prereqName, this.namespace);
  58. // Run the action if:
  59. // 1. The prereq is a normal task (not file/dir)
  60. // 2. The prereq is a file-task with a mod-date more recent than
  61. // the one for this file/dir
  62. if (prereqTask) {
  63. if (!isFileOrDirectory(prereqTask) ||
  64. (isFile(prereqTask) && prereqTask.modTime > this.modTime)) {
  65. return true;
  66. }
  67. }
  68. }
  69. }
  70. // File/dir has no prereqs, and exists -- no need to run
  71. else {
  72. return false;
  73. }
  74. }
  75. };
  76. this.updateModTime = function () {
  77. var stats = fs.statSync(this.name);
  78. this.modTime = stats.mtime;
  79. };
  80. this.complete = function () {
  81. jake._invocationChain.splice(jake._invocationChain.indexOf(this),1);
  82. if (!this.dummy) {
  83. this.updateModTime();
  84. }
  85. this._currentPrereqIndex = 0;
  86. this.taskStatus = Task.runStatuses.DONE;
  87. this.emit('complete');
  88. };
  89. })();
  90. /**
  91. @name jake
  92. @namespace jake
  93. */
  94. /**
  95. @name jake.FileTask
  96. @constructor
  97. @augments EventEmitter
  98. @augments jake.Task
  99. @description A Jake FileTask
  100. @param {String} name The name of the Task
  101. @param {Array} [prereqs] Prerequisites to be run before this task
  102. @param {Function} [action] The action to perform to create this file
  103. @param {Object} [opts]
  104. @param {Array} [opts.asyc=false] Perform this task asynchronously.
  105. If you flag a task with this option, you must call the global
  106. `complete` method inside the task's action, for execution to proceed
  107. to the next task.
  108. */
  109. FileTask = function (name, prereqs, action, opts) {
  110. this.modTime = null;
  111. this.dummy = false;
  112. // Do constructor-work only on actual instances, not when used
  113. // for inheritance
  114. if (arguments.length) {
  115. this.init.apply(this, arguments);
  116. }
  117. };
  118. FileTask.prototype = new Task();
  119. FileTask.prototype.constructor = FileTask;
  120. utils.mixin(FileTask.prototype, FileBase);
  121. exports.FileTask = FileTask;
  122. // DirectoryTask is a subclass of FileTask, depends on it
  123. // being defined
  124. DirectoryTask = require('./directory_task').DirectoryTask;