Skip to content

Commit

Permalink
update custom search pattern implementation
Browse files Browse the repository at this point in the history
plus write error message with old config style
  • Loading branch information
chemix committed Oct 12, 2014
1 parent a7fe0f8 commit 85b95e1
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 128 deletions.
36 changes: 29 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## Getting Started
This plugin requires Grunt `~0.4.4`
This plugin requires Grunt `~0.4.4` and use-min `^2.4.0`

If you haven't used [Grunt](http://gruntjs.com/) before, be sure to check out the [Getting Started](http://gruntjs.com/getting-started) guide, as it explains how to create a [Gruntfile](http://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

Expand All @@ -29,9 +29,11 @@ full example on [github.com/chemix/Nette-Grunt](https://github.com/chemix/Nette-
grunt.initConfig({
...
netteBasePath: {
basePath: 'www',
options: {
removeFromPath: ['app/templates/']
task: {
basePath: 'www',
options: {
removeFromPath: ['app/templates/']
}
}
},
...
Expand All @@ -41,12 +43,32 @@ grunt.initConfig({
```coffee
...
netteBasePath:
basePath: 'www'
options:
removeFromPath: ['app/template/']
task:
basePath: 'www'
options:
removeFromPath: ['app/template/']
...
```


Multiple replacements

```coffee
...
netteBasePath:
taskCore:
basePath: 'www'
options:
removeFromPath: ['app/template/']
taskLibs:
basePath: 'www/components'
options:
searchPattern: '{$incPath}'
...
```


## Release History
- 0.3.0: Support for another replace path search string, multiple replaces
- 0.2.0: Update for grunt-usemin 2.1.0
- 0.1.0: Initial release
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "grunt-nette-basepath",
"description": "Grunt plugin to update paths for concat, uglify and cssmin where is nette variable $basePath",
"version": "0.3.0",
"version": "0.3.1",
"homepage": "https://github.com/chemix/grunt-nette-basepath",
"author": {
"name": "Honza Černý",
Expand Down
251 changes: 131 additions & 120 deletions tasks/netteBasePath.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,151 +10,162 @@
*
* Use:
grunt.initConfig({
grunt.initConfig({
...
netteBasePath: {
basePath: {
task: {
basePath: 'www',
options: {
removeFromPath: ['app/templates/']
}
},
other: {
search: '{$inc}',
basePath: '/login',
otherTask: {
basePath: 'www',
options: {
searchPattern: '{$inc}',
removeFromPath: ['app/templates/']
}
}
},
...
});
*/

var fs = require('fs'),
path = require('path'),
util = require('util');
path = require('path'),
util = require('util');

var inspect = function (obj) {
return util.inspect(obj, false, 4, true);
return util.inspect(obj, false, 4, true);
};

module.exports = function(grunt) {
"use strict";

grunt.registerMultiTask('netteBasePath', 'In list of files to prepare replace $basePath with real path', function() {
var options = this.options();
var basePath = this.data.basePath;
var search = this.data.search;
var replacePath, dataFixed, clearItem;
var concatFixed = {}, uglifyFixed = {}, cssminFixed = {};
var concat = grunt.config('concat') || {},
uglify = grunt.config('uglify') || {},
cssmin = grunt.config('cssmin') || { compress: { files: {} }};
if(concat.options) {
concatFixed.options = concat.options;
}
if(uglify.options) {
uglifyFixed.options = uglify.options;
}
if(cssmin.options) {
cssminFixed.options = cssmin.options;
}

// default
if (search == "") {
search = '{$basePath}';
}

var clearBasePath = function(path,search){
return path.replace(search, basePath);
};

var index;

grunt.log.writeln('[Replace Nette {$basePath} => '+ basePath +']');
grunt.log.writeln('[Nette fix usemin usage real folders]');

// concat
grunt.log.debug('- step concat');
grunt.log.debug(inspect(concat));

if (concat.generated && concat.generated.files){
concatFixed.generated = {files:[]};
for(index in concat.generated.files){
// grunt.log.debug(inspect(concat.generated.files[index].dest));
// grunt.log.debug(inspect(concat.generated.files[index].src));
concatFixed.generated.files[index] = {};
concatFixed.generated.files[index].dest = clearBasePath(concat.generated.files[index].dest,search)
dataFixed = [];
concat.generated.files[index].src.forEach(function(item){
clearItem = clearBasePath(item,search);
if (options.removeFromPath){
options.removeFromPath.forEach(function(whatRemove){
whatRemove = whatRemove.replace('/', path.sep);
clearItem = clearItem.replace(whatRemove, '');
});
}
dataFixed.push(clearItem);
});

concatFixed.generated.files[index].src = dataFixed;
}
grunt.config('concat', concatFixed);
}

// uglify
// todo: refacotr to function
grunt.log.debug('- step uglify');
grunt.log.debug(inspect(uglify));

if (uglify.generated && uglify.generated.files){
uglifyFixed.generated = {files:[]};
for(index in uglify.generated.files){
uglifyFixed.generated.files[index] = {};
uglifyFixed.generated.files[index].dest = clearBasePath(uglify.generated.files[index].dest,search)
dataFixed = [];
uglify.generated.files[index].src.forEach(function(item){
clearItem = clearBasePath(item,search);
dataFixed.push(clearItem);
});
module.exports = function (grunt) {
"use strict";

uglifyFixed.generated.files[index].src = dataFixed;
}
grunt.config('uglify', uglifyFixed);
}

// css min
// todo: refacotr to function
grunt.log.debug('- step cssmin');
grunt.log.debug(inspect(cssmin));

if (cssmin.generated && cssmin.generated.files){
cssminFixed.generated = {files:[]};
for(index in cssmin.generated.files){
cssminFixed.generated.files[index] = {};
cssminFixed.generated.files[index].dest = clearBasePath(cssmin.generated.files[index].dest,search)
dataFixed = [];
cssmin.generated.files[index].src.forEach(function(item){
clearItem = clearBasePath(item,search);
dataFixed.push(clearItem);
grunt.registerMultiTask('netteBasePath', 'In list of files to prepare replace $basePath with real path', function () {
var options = this.options({
searchPattern : '{$basePath}'
});

cssminFixed.generated.files[index].src = dataFixed;
}
grunt.config('cssmin', cssminFixed);
}

// log a bit what was update in config
grunt.log.subhead('Configuration is now:')
.subhead('concat:')
.writeln(' ' + inspect(concatFixed))
.subhead('uglify:')
.writeln(' ' + inspect(uglifyFixed))
.subhead('cssmin:')
.writeln(' ' + inspect(cssminFixed));
});
var basePath = this.data.basePath;
var searchPattern = options.searchPattern;
var dataFixed, clearItem, index;
var concatFixed = {}, uglifyFixed = {}, cssminFixed = {};
var concat = grunt.config('concat') || {},
uglify = grunt.config('uglify') || {},
cssmin = grunt.config('cssmin') || {compress: {files: {}}};

// set origin configs for task
if (concat.options) {
concatFixed.options = concat.options;
}
if (uglify.options) {
uglifyFixed.options = uglify.options;
}
if (cssmin.options) {
cssminFixed.options = cssmin.options;
}

/**
* replace search patter for defined real base path
* @param string
* @returns string
*/
var clearBasePath = function (path) {
return path.replace(searchPattern, basePath);
};

// update for 0.3.x
if (!searchPattern || !basePath){
grunt.warn(
'UPDATE YOUR CONFIG FOR NETTE-BASE-PATH. '
);
}

grunt.log.writeln('[Replace '+ searchPattern +' => ' + basePath + ']');
grunt.log.writeln('[Nette fix usemin usage real folders]');

// concat
grunt.log.debug('- step concat');
grunt.log.debug(inspect(concat));

if (concat.generated && concat.generated.files) {
concatFixed.generated = {files: []};
for (index in concat.generated.files) {
// grunt.log.debug(inspect(concat.generated.files[index].dest));
// grunt.log.debug(inspect(concat.generated.files[index].src));
concatFixed.generated.files[index] = {};
concatFixed.generated.files[index].dest = clearBasePath(concat.generated.files[index].dest)
dataFixed = [];
concat.generated.files[index].src.forEach(function (item) {
clearItem = clearBasePath(item);
if (options.removeFromPath) {
options.removeFromPath.forEach(function (whatRemove) {
whatRemove = whatRemove.replace('/', path.sep);
clearItem = clearItem.replace(whatRemove, '');
});
}
dataFixed.push(clearItem);
});

concatFixed.generated.files[index].src = dataFixed;
}
grunt.config('concat', concatFixed);
}

// uglify
// todo: refacotr to function
grunt.log.debug('- step uglify');
grunt.log.debug(inspect(uglify));

if (uglify.generated && uglify.generated.files) {
uglifyFixed.generated = {files: []};
for (index in uglify.generated.files) {
uglifyFixed.generated.files[index] = {};
uglifyFixed.generated.files[index].dest = clearBasePath(uglify.generated.files[index].dest)
dataFixed = [];
uglify.generated.files[index].src.forEach(function (item) {
clearItem = clearBasePath(item);
dataFixed.push(clearItem);
});

uglifyFixed.generated.files[index].src = dataFixed;
}
grunt.config('uglify', uglifyFixed);
}

// css min
// todo: refacotr to function
grunt.log.debug('- step cssmin');
grunt.log.debug(inspect(cssmin));

if (cssmin.generated && cssmin.generated.files) {
cssminFixed.generated = {files: []};
for (index in cssmin.generated.files) {
cssminFixed.generated.files[index] = {};
cssminFixed.generated.files[index].dest = clearBasePath(cssmin.generated.files[index].dest)
dataFixed = [];
cssmin.generated.files[index].src.forEach(function (item) {
clearItem = clearBasePath(item);
dataFixed.push(clearItem);
});

cssminFixed.generated.files[index].src = dataFixed;
}
grunt.config('cssmin', cssminFixed);
}

// log a bit what was update in config
grunt.log.subhead('Configuration is now:')
.subhead('concat:')
.writeln(' ' + inspect(concatFixed))
.subhead('uglify:')
.writeln(' ' + inspect(uglifyFixed))
.subhead('cssmin:')
.writeln(' ' + inspect(cssminFixed));
});

};

0 comments on commit 85b95e1

Please sign in to comment.