Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit d877c8873a16595d7dc305cdc7c799e8ce2c2dd1 @vanetix committed Mar 7, 2013
@@ -0,0 +1,3 @@
+tmp/
+node_modules/
+npm-debug.log
@@ -0,0 +1,15 @@
+{
+ "curly": true,
+ "eqeqeq": true,
+ "immed": true,
+ "latedef": true,
+ "newcap": true,
+ "noarg": true,
+ "sub": true,
+ "undef": true,
+ "boss": true,
+ "eqnull": true,
+ "node": true,
+ "es5": true,
+ "strict": false
+}
@@ -0,0 +1,5 @@
+tmp
+test
+.npmignore
+.travis.yml
+Gruntfile.js
@@ -0,0 +1,5 @@
+language: node_js
+node_js:
+ - 0.8
+before_script:
+ - npm install -g grunt-cli
@@ -0,0 +1,49 @@
+module.exports = function(grunt) {
+
+ // Project configuration.
+ grunt.initConfig({
+
+ jshint: {
+ all: [
+ 'Gruntfile.js',
+ 'tasks/mapper.js',
+ '<%= nodeunit.tests %>'
+ ],
+ options: {
+ jshintrc: '.jshintrc'
+ }
+ },
+
+ nodeunit: {
+ tests: ['test/index.js']
+ },
+
+ // Remove test staging directory
+ clean: {
+ tests: ['tmp']
+ },
+
+ // Build the test cases
+ mapper: {
+ basic: {
+ cwd: 'test/cases',
+ assets: '../assets/*',
+ src: ['*']
+ }
+ }
+ });
+
+ // Load this task
+ grunt.loadTasks('tasks');
+
+ // Load plugins used by this task gruntfile
+ grunt.loadNpmTasks('grunt-contrib-clean');
+ grunt.loadNpmTasks('grunt-contrib-jshint');
+ grunt.loadNpmTasks('grunt-contrib-nodeunit');
+
+ // Task cleans `tmp` and builds mapper, then runs tests
+ grunt.registerTask('test', ['clean', 'mapper', 'nodeunit']);
+
+ // Default task
+ grunt.registerTask('default', ['jshint', 'test']);
+};
@@ -0,0 +1,5 @@
+Copyright (c) 2013 Matt McFarland
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,47 @@
+# grunt-asset-mapper [![Build Status](https://travis-ci.org/vanetix/grunt-asset-mapper.png?branch=master)](https://travis-ci.org/vanetix/grunt-asset-mapper)
+
+> A grunt plugin for mapping assets.
+
+## Getting Started
+***This task only works with grunt >= 0.4.0***
+
+Install this grunt plugin next to your project's `Gruntfile.js` with:
+
+```shell
+npm install grunt-asset-mapper
+```
+
+Then add this line to your project's `Gruntfile.js`:
+
+```javascript
+grunt.loadNpmTasks('grunt-asset-mapper');
+```
+
+## Usage
+**Basic**
+
+```javascript
+mapper: {
+ basic: {
+ src: ['path/to/assets/**'],
+ dest: 'path/to/dest'
+ }
+}
+```
+
+**Flatten**
+
+```javascript
+flatten: {
+ flatten: true,
+ src: ['path/to/assets/**'],
+ dest: 'path/to/dest'
+}
+```
+
+## License (MIT)
+Copyright (c) 2013 Matt McFarland
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,2 @@
+#!/usr/bin/env node
+require('grunt').npmTasks('grunt-asset-mapper').cli();
@@ -0,0 +1,34 @@
+{
+ "name": "grunt-asset-mapper",
+ "description": "Automate the mapping of assets.",
+ "version": "0.0.1",
+ "author": "vanetix <matmcfarland@gmail.com>",
+ "main": "Gruntfile.js",
+ "bin": "bin/grunt-asset-mapper",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/vanetix/grunt-asset-mapper.git"
+ },
+ "scripts": {
+ "test": "grunt test"
+ },
+ "devDependencies": {
+ "grunt": "~0.4.0",
+ "grunt-contrib-copy": "~0.4.0",
+ "grunt-contrib-clean": "~0.4.0",
+ "grunt-contrib-jshint": "~0.1.1",
+ "grunt-contrib-nodeunit": "~0.1.2"
+ },
+ "peerDependencies": {
+ "grunt": "~0.4.0"
+ },
+ "keywords": [
+ "assets",
+ "mapping",
+ "gruntplugin",
+ "asset-mapper"
+ ],
+ "engines": {
+ "node": ">= 0.8.0"
+ }
+}
@@ -0,0 +1,158 @@
+/*
+ * grunt-asset-mapper
+ * https://github.com/vanetix/grunt-asset-mapper
+ *
+ * Copyright (c) 2013 Matt McFarland
+ * Licensed under the MIT license.
+ */
+
+module.exports = function(grunt) {
+
+ /**
+ * Dependencies
+ */
+
+ var path = require('path');
+
+ /**
+ * Core `grunt-asset-mapper` task
+ */
+
+ grunt.registerMultiTask('mapper', 'Automate asset mapping', function() {
+
+ /**
+ * Loop over all file arguments replacing any asset paths with assets
+ * in `f.assets`
+ */
+
+ this.files.forEach(function(f) {
+ var src, assets, cwd = f.cwd;
+
+ /**
+ * Expand assets
+ */
+
+ assets = grunt.file.expand({cwd: cwd}, f.assets);
+ console.dir(assets);
+
+ /**
+ * Filter out non-existent files then iterate over all files
+ * in `f.src` if an asset reference is found check if the asset
+ * is in `f.assets`, if so, replace with the new path
+ */
+
+ src = f.src.filter(function(p) {
+ if(cwd) {
+ p = path.join(cwd, p);
+ }
+
+ if(grunt.file.exists(p)) {
+ return true;
+ }
+
+ grunt.log.error('File: ' + p + ' not found.');
+ return false;
+ });
+
+ src.forEach(function(p) {
+ var s = f.dest ? f.dest : p;
+
+ if(cwd) {
+ p = path.join(cwd, p);
+ }
+
+ grunt.log.oklns(map(p, assets));
+
+ /**
+ * If `f.dest` is set, save to `f.dest` otherwize
+ * overwrite the original
+ */
+
+ //grunt.file.write(map(p, assets), s);
+ });
+
+ });
+ });
+
+ /**
+ * Try to match a filename in `assets` against `f`
+ *
+ * @param {String} f
+ * @param {Array} assets
+ * @return {String}
+ */
+
+ function matchAsset(f, assets) {
+ var i, len;
+ var ext = path.extname(f);
+ var base = path.basename(f, ext);
+ var regexp = new RegExp(base + '\\.[A-Za-z0-9]+' + ext + '$');
+
+ for(i = 0, len = assets.length; i < len; i = i + 1) {
+ if(assets[i].match(regexp)) {
+ return assets[i];
+ }
+ }
+
+ return '';
+ }
+
+ /**
+ * Returns the regular expression used for matching the filetype
+ *
+ * @param {String} p
+ * @return {RegExp}
+ */
+
+ function fileRegexp(p) {
+ var types = {
+ css: /url\(['"](\S+)['"]\);/,
+ html: /(?:src|href)="(\S+)"/
+ };
+
+ return types[fileType(p)] || types.css;
+ }
+
+ /**
+ * Returns the filetype of path `p`
+ *
+ * @param {String} p
+ * @return {String}
+ */
+
+ function fileType(p) {
+ return path.extname(p).slice(1);
+ }
+
+
+ /**
+ * Map any `asset` references in `src` to the given asset
+ *
+ * @param {String} p
+ * @param {Array} assets
+ * @return {String}
+ */
+
+ function map(p, assets) {
+ var asset, match, regex, lines;
+
+ regex = fileRegexp(p);
+ lines = grunt.file.read(p).split(grunt.util.linefeed);
+
+ lines = lines.map(function(line) {
+ if(match = line.match(regex)) {
+ asset = matchAsset(match[1], assets);
+
+ if(asset) {
+ grunt.verbose.oklns('Replacing: ' + match[1] + ' with ' + asset);
+ return line.replace(match[1], asset);
+ }
+ }
+
+ return line;
+ });
+
+ return lines.join(grunt.util.linefeed);
+ }
+
+};
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,7 @@
+body {
+ background: url('../assets/cat1.jpg');
+}
+
+.cat-rap {
+ background: url('../assets/unknowncat.jpg');
+}
@@ -0,0 +1,14 @@
+<html>
+<head>
+ <title>Crazy cat test</title>
+</head>
+<body>
+
+ <div class="cat-rap">
+ <img src="../assets/cat1.jpg">
+ <img src="../assets/cat2.jpg">
+ <img src="../assets/cat3.jpg">
+ </div>
+
+</body>
+</html>
@@ -0,0 +1,9 @@
+var grunt = require('grunt');
+
+exports.mapper = {
+ basic: function(test) {
+ test.expect(1);
+
+ test.done();
+ }
+};

0 comments on commit d877c88

Please sign in to comment.