-
-
Save jgrenat/5adc479428361ae73e03 to your computer and use it in GitHub Desktop.
Grunt, un lanceur de tâche (3/3)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
grunt.task.registerTask(nomDeLaTache, listeDeTaches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
grunt.task.registerTask(nomDeLaTache, description, function(argument1, argument2, ...) { | |
// Notre tâche | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function (grunt) { /* Création de nos tâches */ }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"prod": { | |
"serverUrl": "http://mon-domaine.com" | |
}, | |
"preprod": { | |
"serverUrl": "http://preprod.mon-domaine.com" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<p>Adresse du serveur : @@serverUrl</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getConfigReplacements(configFile, environment, openString, closeString) { | |
var i, configVars, envVars, replaces = []; | |
// Protège les chaînes à convertir en regex | |
var escapeRegexp = /([.*+?^=!:${}()|\[\]\/\\])/g; | |
grunt.log.writeln('Checking the environment... '); | |
if(!(configVars = grunt.file.readJSON(configFile))) { | |
grunt.fail.warn('Unable to replace config vars : file ' + configFile + ' could not be opened'); | |
return null; | |
} | |
// Si l'environnement cible est inconnu, on envoie un message d'erreur et on arrête la tâche | |
if(!(envVars = configVars[environment])) { | |
grunt.fail.warn('Unable to replace config vars : unknown environment.'); | |
return null; | |
} | |
grunt.log.ok('Environment checked!'); | |
// On convertit les variables en regex pour les remplacements multiples. | |
for(i in envVars) { | |
if(envVars.hasOwnProperty(i)) { | |
regexp = new RegExp((openString + i + closeString).replace(escapeRegexp, '$1'), 'g'); | |
replaces.push([regexp, envVars[i]]); | |
grunt.verbose.writeln('Replacement of *' + i + '* by *' + envVars[i] + '* found'); | |
} | |
} | |
return replaces; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function replaceInFile(sourcePath, destPath, replaces) { | |
// On remplace les variables dans le fichier | |
grunt.file.copy(sourcePath, destPath, { | |
process: function(content) { | |
// On remplace les variables une à une dans le fichier | |
replaces.forEach(function(replace) { | |
content = content.replace(replace[0], replace[1]); | |
} | |
return content; | |
} | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
grunt.task.registerTask('replace-config-vars', 'Remplace les variables de configuration selon l\'environnement', function(environment) { | |
var replaces; | |
// Fichier JSON contenant les variables de configuration | |
var configFile = 'config.json'; | |
// Fichier JSON contenant les variables de configuration | |
var sourceDirectory = 'dist'; | |
// Paramètre la forme des remplacements, ici : @@var | |
var openString = '@@', closeString = ''; | |
// On vérifie qu'on a bien un argument | |
if(this.args.length !== 1) { | |
grunt.fail.warn('Unable to replace config vars : one argument expected, got ' + this.args.length + '.'); | |
return false; | |
} | |
// On récupère les remplacements à effectuer | |
replaces = getConfigReplacements(configFile, environment, openString, closeString); | |
grunt.log.writeln('Replacing config vars for *' + environment + '* env... '); | |
// Cette fonction parcourt récursivement tous les fichiers du répertoire spécifié | |
grunt.file.recurse(sourceDirectory, function(absPath, rootDir, subDir, filename) { | |
grunt.verbose.writeln('Processing file *' + (subDir || '.') + '/' + filename + '*... '); | |
// Dans la ligne suivante, la destination est actuellement la même que le fichier source, on "réécrit" notre fichier | |
var result = replaceInFile(absPath, absPath, replaces); | |
return result; | |
}); | |
grunt.log.ok('Every files have been processed!'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function(grunt) { | |
grunt.registerTask('build', function(environment) { | |
environment = environment || 'preprod'; | |
grunt.task.run([ | |
'clean:dist', | |
'copy:dist', | |
'useminPrepare', | |
'cssmin', | |
'uglify', | |
'usemin', | |
'replace-config-vars:' + environment | |
]); | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
grunt build | |
grunt build:prod | |
grunt build:preprod |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
grunt.registerMultiTask('replace-config-vars', 'Remplace les variables de configuration selon l\'environnement', function() { | |
// Le code précédent n'a pour l'instant pas changé | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var environment = grunt.option('env'); | |
// On vérifie qu'on a bien l'environnement cible | |
if(!environment) { | |
grunt.fail.warn('Unable to replace config vars : env option missing.'); | |
return false; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var options = this.options({ | |
'configFile': 'config.json', | |
'openString': '@@', | |
'closeString': '' | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function replaceInFile(sourcePath, destPath, replaces) { | |
// Vérification de la destination | |
if(grunt.file.isDir(destPath)) { | |
destPath = destPath + '/' + sourcePath.split('/').pop(); | |
} | |
// On remplace les variables dans le fichier | |
grunt.file.copy(sourcePath, destPath, { | |
process: function(content) { | |
// On remplace les variables une à une dans le fichier | |
replaces.forEach(function(replace) { | |
content = content.replace(replace[0], replace[1]); | |
}); | |
return content; | |
} | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
grunt.registerMultiTask('replace-config-vars', 'Remplace les variables de configuration selon l\'environnement', function() { | |
var replaces; | |
var environment = grunt.option('env'); | |
// On vérifie qu'on a bien l'environnement cible | |
if(!environment) { | |
grunt.fail.warn('Unable to replace config vars : env option missing.'); | |
return false; | |
} | |
var options = this.options({ | |
'configFile': 'config.json', | |
'openString': '@@', | |
'closeString': '' | |
}); | |
// On récupère les remplacements à effectuer selon l'environnement | |
replaces = getConfigReplacements(options['configFile'], environment, options['openString'], options['closeString']); | |
grunt.log.writeln('Replacing config vars for *' + environment + '* environment... '); | |
this.files.forEach(function(file) { | |
// Si on n'a aucun fichier source pour une destination, on passe à la suite | |
if(file.src.length < 1) { | |
return; | |
} | |
// On retourne une erreur si on a plusieurs fichiers sources pour un seul fichier cible | |
if(file.src.length !== 1 && !grunt.file.isDir(file.dest)) { | |
grunt.fail.warn('replace-config-vars doesn\'t handle files concatenation.'); | |
return; | |
} | |
file.src.forEach(function(path) { | |
grunt.verbose.writeln('Processing file *' + path + '*... '); | |
replaceInFile(path, file.dest, replaces); | |
}); | |
}); | |
grunt.log.ok('Every files have been processed!'); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
grunt.registerTask('build', function(target) { | |
target = target || ''; | |
// L'environnement | |
if(!grunt.option('env')) { | |
grunt.option('env', 'preprod'); | |
} | |
grunt.task.run([ | |
'clean:dist', | |
'copy:dist', | |
'useminPrepare', | |
'cssmin', | |
'uglify', | |
'usemin', | |
'replace-config-vars:' + target | |
]); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
grunt.initConfig({ | |
// [...] | |
'replace-config-vars': { | |
html: { | |
src: ['*.html'], | |
dest: 'dist', | |
options: { | |
configFile: 'configHTML.json' | |
openString: '<%=', | |
closeString: '%>' | |
} | |
}, | |
js: { | |
src: ['dist/public/*.js'], | |
dest: 'dist/public', | |
options: { | |
configFile: 'configJS.json' | |
} | |
} | |
}, | |
// [...] | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
grunt build:notreTarget --env=notreEnvironnement |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment