-1

I have a Gruntfile.js that I configured in the VS2015 through Task Runned Explorer to run Before Build. I need to add a new task in this Gruntfile, in this new task I should compare the DateLastModified of the 2 files in the project and in case are different I should run the specific .tt files in the project. How can I acquire this?

In other projects I applied the solution described in this question : https://stackoverflow.com/a/3381556/5816699

But since I could have several dozens or hundreds of .tt files in the project, could be a little aggressive compiling the project. And I just run specific .tt files from the grunfile. Thanks in advance.

Community
  • 1
  • 1
Galego
  • 1
  • 2

1 Answers1

0

I came along with this solution to solve my problem. I created a watch to automatically run the .tt files in case of a change of a xml file, using the task transformFiles.

In case of runnig the task XXX in the before build, attach the task by using the Task Runner Explorer, were how you can do this: https://visualstudiogallery.msdn.microsoft.com/8e1b4368-4afb-467a-bc13-9650572db708

module.exports = function (grunt) {
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        watch: {
            all: {
                files: ['**/*Config.xml', '**/*Other.xml'],
                tasks: ['XXX'],
            },
        },
    });
    var spawn = require('child_process').spawn;
    var exec = require('child_process').exec;
    var textTransformPossiblePaths = ['C:\\Program Files (x86)\\Common Files\\Microsoft Shared\\TextTemplating\\14.0\\TextTransform.exe',
                                    'C:\\Program Files (x86)\\Common Files\\Microsoft Shared\\TextTemplating\\12.0\\TextTransform.exe',
                                    'D:\\Program Files (x86)\\Common Files\\Microsoft Shared\\TextTemplating\\14.0\\TextTransform.exe',
                                    'D:\\Program Files (x86)\\Common Files\\Microsoft Shared\\TextTemplating\\12.0\\TextTransform.exe'];
    var patterns = {
        allT4: ['**/*.tt', '!**/bin/**', '!**/node_modules/**'],
        watchXml: ['**/*Config.xml', '**/*Other.xml']
    };

    grunt.loadNpmTasks('grunt-contrib-watch');
    require('time-grunt')(grunt);

    grunt.registerTask('XXX', [], function () {
        var fs = require('fs');
        const path = require('path');
        var filesXML = [];

        for (i = 0; i < patterns.watchXml.length ; i++) {
            var files = grunt.file.expand(patterns.watchXml[i]);
            files.forEach(function (element) {
                filesXML.push(element);
            });
        };        
        for (i = 0; i < filesXML.length ; i++) {
            var filesCS = grunt.file.expand(path.dirname(filesXML[i]) + "/" + "*.cs");
            var filesTT = grunt.file.expand(path.dirname(filesXML[i]) + "/" + "*.tt");
            for (j = 0; j < filesCS.length ; j++) {
                if (fs.lstatSync(filesXML[i]).mtime > fs.lstatSync(filesCS[j]).mtime) {
                    for (k = 0 ; k < filesTT.length; k++) {
                        var t4Path = textTransformPossiblePaths.filter(function (f) { return fs.existsSync(f); })[0];
                        exec('"' + t4Path + '" ' + [filesTT[k]] + '"');
                    }
                    break;
                }
            }
        }
    }());
};
Galego
  • 1
  • 2