diff --git a/Gruntfile.js b/Gruntfile.js new file mode 100644 index 0000000..79de511 --- /dev/null +++ b/Gruntfile.js @@ -0,0 +1,73 @@ +/* + * grunt-gitInfo + * https://github.com/damkraw/grunt-gitInfo + * + * Copyright (c) 2013 Damian Krawczyk + * Licensed under the MIT license. + */ + +'use strict'; + +module.exports = function(grunt) { + + // Project configuration. + grunt.initConfig({ + jshint: { + all: [ + 'Gruntfile.js', + 'tasks/*.js', + '<%= nodeunit.tests %>', + ], + options: { + jshintrc: '.jshintrc', + }, + }, + + // Before generating any new files, remove any previously-created files. + clean: { + tests: ['tmp'], + }, + + // Configuration to be run (and then tested). + gitInfo: { + default_options: { + options: { + }, + files: { + 'tmp/default_options': ['test/fixtures/testing', 'test/fixtures/123'], + }, + }, + custom_options: { + options: { + separator: ': ', + punctuation: ' !!!', + }, + files: { + 'tmp/custom_options': ['test/fixtures/testing', 'test/fixtures/123'], + }, + }, + }, + + // Unit tests. + nodeunit: { + tests: ['test/*_test.js'], + }, + + }); + + // Actually load this plugin's task(s). + grunt.loadTasks('tasks'); + + // These plugins provide necessary tasks. + grunt.loadNpmTasks('grunt-contrib-jshint'); + grunt.loadNpmTasks('grunt-contrib-clean'); + grunt.loadNpmTasks('grunt-contrib-nodeunit'); + + // Whenever the "test" task is run, first clean the "tmp" dir, then run this + // plugin's task(s), then test the result. + grunt.registerTask('test', ['clean', 'gitInfo', 'nodeunit']); + + // By default, lint and run all tests. + grunt.registerTask('default', ['jshint', 'test']); + +}; diff --git a/LICENSE-MIT b/LICENSE-MIT new file mode 100644 index 0000000..4344534 --- /dev/null +++ b/LICENSE-MIT @@ -0,0 +1,22 @@ +Copyright (c) 2013 Damian Krawczyk + +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. diff --git a/README.md b/README.md index c946d6c..0eca6e2 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,89 @@ -grunt-gitInfo -============= +# grunt-gitInfo -Get Git info from a working copy and populate grunt.config with the data +> Get Git info from a working copy and populate grunt.config with the data + +## Getting Started +This plugin requires Grunt `~0.4.1` + +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: + +```shell +npm install grunt-gitInfo --save-dev +``` + +Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript: + +```js +grunt.loadNpmTasks('grunt-gitInfo'); +``` + +## The "gitInfo" task + +### Overview +In your project's Gruntfile, add a section named `gitInfo` to the data object passed into `grunt.initConfig()`. + +```js +grunt.initConfig({ + gitInfo: { + options: { + // Task-specific options go here. + }, + your_target: { + // Target-specific file lists and/or options go here. + }, + }, +}) +``` + +### Options + +#### options.separator +Type: `String` +Default value: `', '` + +A string value that is used to do something with whatever. + +#### options.punctuation +Type: `String` +Default value: `'.'` + +A string value that is used to do something else with whatever else. + +### Usage Examples + +#### Default Options +In this example, the default options are used to do something with whatever. So if the `testing` file has the content `Testing` and the `123` file had the content `1 2 3`, the generated result would be `Testing, 1 2 3.` + +```js +grunt.initConfig({ + gitInfo: { + options: {}, + files: { + 'dest/default_options': ['src/testing', 'src/123'], + }, + }, +}) +``` + +#### Custom Options +In this example, custom options are used to do something else with whatever else. So if the `testing` file has the content `Testing` and the `123` file had the content `1 2 3`, the generated result in this case would be `Testing: 1 2 3 !!!` + +```js +grunt.initConfig({ + gitInfo: { + options: { + separator: ': ', + punctuation: ' !!!', + }, + files: { + 'dest/default_options': ['src/testing', 'src/123'], + }, + }, +}) +``` + +## Contributing +In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/). + +## Release History +_(Nothing yet)_ diff --git a/atlassian-ide-plugin.xml b/atlassian-ide-plugin.xml new file mode 100644 index 0000000..c7826a8 --- /dev/null +++ b/atlassian-ide-plugin.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..3b4c13a --- /dev/null +++ b/package.json @@ -0,0 +1,42 @@ +{ + "name": "grunt-gitInfo", + "description": "Get Git info from a working copy and populate grunt.config with the data", + "version": "0.1.0", + "homepage": "https://github.com/damkraw/grunt-gitInfo", + "author": { + "name": "Damian Krawczyk", + "email": "mail@damiankrawczyk.pl" + }, + "repository": { + "type": "git", + "url": "git://github.com/damkraw/grunt-gitInfo.git" + }, + "bugs": { + "url": "https://github.com/damkraw/grunt-gitInfo/issues" + }, + "licenses": [ + { + "type": "MIT", + "url": "https://github.com/damkraw/grunt-gitInfo/blob/master/LICENSE-MIT" + } + ], + "main": "Gruntfile.js", + "engines": { + "node": ">= 0.8.0" + }, + "scripts": { + "test": "grunt test" + }, + "devDependencies": { + "grunt-contrib-jshint": "~0.1.1", + "grunt-contrib-clean": "~0.4.0", + "grunt-contrib-nodeunit": "~0.1.2", + "grunt": "~0.4.1" + }, + "peerDependencies": { + "grunt": "~0.4.1" + }, + "keywords": [ + "gruntplugin" + ] +} \ No newline at end of file diff --git a/tasks/gitInfo.js b/tasks/gitInfo.js new file mode 100644 index 0000000..86f740e --- /dev/null +++ b/tasks/gitInfo.js @@ -0,0 +1,83 @@ +/* + * grunt-gitInfo + * https://github.com/damkraw/grunt-gitInfo + * + * Copyright (c) 2013 Damian Krawczyk + * Licensed under the MIT license. + */ + +'use strict'; + +module.exports = function(grunt) { + grunt.registerTask('gitInfo', 'Your task description goes here.', function () { + var done = this.async(), + gitInfo = {}, + + getCurrentBranchName = function () { + grunt.util.spawn({ + cmd : 'git', + args : ['rev-parse', '--abbrev-ref', 'HEAD'] + }, function (err, result) { + if (err) { + done(); + } else { + gitInfo.local.branch.current.name = result.stdout; + grunt.config.set('gitInfo', gitInfo); + done(); + } + }); + }, + + getRemoteOriginUrl = function () { + grunt.util.spawn({ + cmd : 'git', + args : ['config', '--get-all', 'remote.origin.url'] + }, function (err, result) { + if (err) { + done(); + } else { + gitInfo.remote = { + origin : { + url : result.stdout + } + }; + getCurrentBranchName(); + } + }); + }, + + getShortSHA = function () { + grunt.util.spawn({ + cmd : 'git', + args : ['rev-parse', '--short', 'HEAD'] + }, function (err, result) { + if (err) { + done(); + } else { + gitInfo.local.branch.current.shortSHA = result.stdout; + getRemoteOriginUrl(); + } + }); + }, + getSHA = function () { + grunt.util.spawn({ + cmd : 'git', + args : ['rev-parse', 'HEAD'] + }, function (err, result) { + if (err) { + done(); + } else { + gitInfo.local = { + branch : { + current : { + SHA : result.stdout + } + } + }; + getShortSHA(); + } + }); + }; + getSHA(); + }); +}; diff --git a/test/expected/custom_options b/test/expected/custom_options new file mode 100644 index 0000000..e597128 --- /dev/null +++ b/test/expected/custom_options @@ -0,0 +1 @@ +Testing: 1 2 3 !!! \ No newline at end of file diff --git a/test/expected/default_options b/test/expected/default_options new file mode 100644 index 0000000..5f8b72f --- /dev/null +++ b/test/expected/default_options @@ -0,0 +1 @@ +Testing, 1 2 3. \ No newline at end of file diff --git a/test/fixtures/123 b/test/fixtures/123 new file mode 100644 index 0000000..703ca85 --- /dev/null +++ b/test/fixtures/123 @@ -0,0 +1 @@ +1 2 3 \ No newline at end of file diff --git a/test/fixtures/testing b/test/fixtures/testing new file mode 100644 index 0000000..0a90125 --- /dev/null +++ b/test/fixtures/testing @@ -0,0 +1 @@ +Testing \ No newline at end of file diff --git a/test/gitInfo_test.js b/test/gitInfo_test.js new file mode 100644 index 0000000..f0f5eb2 --- /dev/null +++ b/test/gitInfo_test.js @@ -0,0 +1,48 @@ +'use strict'; + +var grunt = require('grunt'); + +/* + ======== A Handy Little Nodeunit Reference ======== + https://github.com/caolan/nodeunit + + Test methods: + test.expect(numAssertions) + test.done() + Test assertions: + test.ok(value, [message]) + test.equal(actual, expected, [message]) + test.notEqual(actual, expected, [message]) + test.deepEqual(actual, expected, [message]) + test.notDeepEqual(actual, expected, [message]) + test.strictEqual(actual, expected, [message]) + test.notStrictEqual(actual, expected, [message]) + test.throws(block, [error], [message]) + test.doesNotThrow(block, [error], [message]) + test.ifError(value) +*/ + +exports.gitInfo = { + setUp: function(done) { + // setup here if necessary + done(); + }, + default_options: function(test) { + test.expect(1); + + var actual = grunt.file.read('tmp/default_options'); + var expected = grunt.file.read('test/expected/default_options'); + test.equal(actual, expected, 'should describe what the default behavior is.'); + + test.done(); + }, + custom_options: function(test) { + test.expect(1); + + var actual = grunt.file.read('tmp/custom_options'); + var expected = grunt.file.read('test/expected/custom_options'); + test.equal(actual, expected, 'should describe what the custom option(s) behavior is.'); + + test.done(); + }, +};