1
mirror of https://github.com/pure-css/pure synced 2024-11-30 11:29:23 +01:00
pure/Gruntfile.js
2013-05-09 18:05:18 -04:00

220 lines
7.3 KiB
JavaScript

var path = require('path'),
parserlib = require('parserlib');
module.exports = function (grunt) {
// -- Config -------------------------------------------------------------------
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
// -- Constants ------------------------------------------------------------
NORMALIZE_LIB: path.join(process.cwd(), '../', 'normalize.css'),
BASE_DIR : 'src/base/',
PREFIX : '.k',
COMMENT: '/* THIS FILE IS GENERATED BY A BUILD SCRIPT - DO NOT EDIT! */\n',
LICENSE: [
'/*! normalize.css v1.1.1 | MIT License | git.io/normalize */',
'/*! Copyright (c) Nicolas Gallagher and Jonathan Neal */',
'\n'
].join('\n'),
// -- CSSMin Config --------------------------------------------------------
cssmin: {
options: {
report: 'gzip'
},
base: {
files: {
'build/base/base-min.css' : ['src/base/css/normalize.css'],
'build/base/base-context-min.css': ['src/base/css/normalize-context.css']
}
},
forms: {
files: {
'build/forms/forms-min.css' : ['src/forms/css/*.css'],
'build/forms/forms-nr-min.css': ['src/forms/css/forms-core.css',
'src/forms/css/forms.css']
}
},
grids: {
files: {
'build/grids/grids-min.css' : ['src/grids/css/*.css'],
'build/grids/grids-nr-min.css': ['src/grids/css/cssgrids-base.css',
'src/grids/css/cssgrids-units.css']
}
},
menus: {
files: {
'build/menus/menus-min.css' : ['src/menus/css/*.css'],
'build/menus/menus-nr-min.css': ['src/menus/css/list-core.css',
'src/menus/css/list.css',
'src/menus/css/list-paginator.css']
}
},
tables: {
files: {
'build/tables/tables-min.css': ['src/tables/css/*.css']
}
},
buttons: {
files: {
'build/buttons/buttons-min.css': ['src/buttons/css/*.css']
}
}
}
});
// -- Main Tasks ---------------------------------------------------------------
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.registerTask('default', ['cssmin']);
grunt.registerTask('base', ['cssmin:base']);
grunt.registerTask('buttons', ['cssmin:buttons']);
grunt.registerTask('forms', ['cssmin:forms']);
grunt.registerTask('grids', ['cssmin:grids']);
grunt.registerTask('menus', ['cssmin:menus']);
grunt.registerTask('tables', ['cssmin:tables']);
// -- Import Tasks -------------------------------------------------------------
grunt.registerTask('base-clean', 'Clean Source Tree', function () {
var files = grunt.file.expand(grunt.config('BASE_DIR') + 'css/*.css');
files.forEach(function (file) {
grunt.log.writeln('Deleting: '.red + file.cyan);
grunt.file['delete'](file);
});
});
grunt.registerTask('base-import-css', 'Import Normalize CSS Files', function () {
var file = 'normalize.css',
src = path.join(grunt.config('NORMALIZE_LIB'), file),
dest = path.join(grunt.config('BASE_DIR'), 'css', file);
if (!grunt.file.exists(src)) {
grunt.fail.fatal('Did you clone normalize.css yet?');
}
grunt.log.writeln('Copying: '.green + file.cyan + ' to ' + dest.cyan);
grunt.file.write(dest, grunt.config('COMMENT') + grunt.file.read(src));
});
grunt.registerTask('base-import-tests', 'Import Normalize Tests', function () {
var file = 'test.html',
src = path.join(grunt.config('NORMALIZE_LIB'), file),
dest = path.join(grunt.config('BASE_DIR'), 'tests', 'manual', file);
if (!grunt.file.exists(src)) {
grunt.fail.fatal('Did you clone normalize.css yet?');
}
grunt.log.writeln('Copying: '.green + file.cyan + ' to ' + dest.cyan);
grunt.file.copy(src, dest);
});
grunt.registerTask('base-create-context', 'Make context version', function () {
var context = grunt.config('COMMENT') + grunt.config('LICENSE'),
done = this.async(),
parser = new parserlib.css.Parser(),
raw = grunt.file.read(grunt.config('BASE_DIR') + 'css/normalize.css');
parser.addListener('startstylesheet', function () {
grunt.log.ok('Starting to parse style sheet...');
});
parser.addListener('endstylesheet', function () {
var contextFile = path.join(grunt.config('BASE_DIR'), 'css', 'normalize-context.css');
grunt.log.ok('Finished parsing style sheet...');
grunt.file.write(contextFile, context);
grunt.log.ok('Done creating context build!');
done();
});
// Fired right before CSS properties are parsed for a certain rule. Go
// through and add all the selectors to the `css` string.
parser.addListener('startrule', function (event) {
var prefix = grunt.config('PREFIX');
event.selectors.forEach(function (selector, i) {
var nextSelector = event.selectors[i + 1];
// If the selector does not contain the html selector, we can go
// ahead and prepend .k in front of it.
if (selector.text.indexOf('html') === -1) {
context += prefix + ' ' + selector.text;
} else if (selector.text.indexOf('html') !== -1) {
// If it contains `html`, replace the `html` with `.k`. Replace
// multiple spaces with a single space. This is for the case
// where `html input[type='button']` comes through as
// `html input[type='button']`.
context += selector.text.replace('html', prefix).replace(/ +/g, ' ');
}
// If theres another selector, add a comma.
if (nextSelector) {
context += ',\n';
} else {
// Otherwise, add an opening bracket for properties
context += ' {\n';
}
});
});
// Fired right after CSS properties are parsed for a certain rule. Add the
// closing bracket to end the CSS Rule.
parser.addListener('endrule', function (event) {
context += '}\n';
});
// Fired for each property that the parser encounters. Add these properties
// to the `css` string with 4 spaces.
parser.addListener('property', function (event) {
// Add 4 spaces tab.
context += ' ' + event.property + ': ' + event.value + ';\n';
});
// Do the parsing.
parser.parse(raw);
});
grunt.registerTask('base-prep', 'Prep Normalize.css import', function () {
var normalize = grunt.config('NORMALIZE_LIB');
grunt.log.write('Looking for Normalize.css'.green);
if (!grunt.file.exists(normalize)) {
grunt.log.writeln('');
grunt.fail.fatal('Could not locate Normalize.css repo: ' + normalize + '\nDid you clone it as a sibling of the Kimono');
}
grunt.log.writeln('...OK'.white);
});
grunt.registerTask('base-all', [
'base-prep',
'base-clean',
'base-import',
'base-create-context'
]);
grunt.registerTask('base-import', [
'base-import-css',
'base-import-tests'
]);
};