Explorar o código

Added dev-plugin create grunt task.

Szymon Kupś %!s(int64=10) %!d(string=hai) anos
pai
achega
c446d67a7f

+ 17 - 4
dev/tasks/dev.js

@@ -6,6 +6,8 @@
 'use strict';
 'use strict';
 
 
 const initTask = require( './utils/dev-init' );
 const initTask = require( './utils/dev-init' );
+const pluginCreateTask = require( './utils/dev-plugin-create' );
+
 var ckeditor5Path = process.cwd();
 var ckeditor5Path = process.cwd();
 
 
 module.exports = ( grunt ) => {
 module.exports = ( grunt ) => {
@@ -13,11 +15,22 @@ module.exports = ( grunt ) => {
 
 
 	grunt.registerTask( 'dev-init', function() {
 	grunt.registerTask( 'dev-init', function() {
 		// Get workspace root relative path from configuration and convert it to absolute path.
 		// Get workspace root relative path from configuration and convert it to absolute path.
-		let	options = {
-			workspaceRoot: '..'
-		};
-		options = this.options( options );
+		const options = getOptions( this );
 		initTask( ckeditor5Path, packageJSON, options, grunt.log.writeln, grunt.log.error );
 		initTask( ckeditor5Path, packageJSON, options, grunt.log.writeln, grunt.log.error );
 	} );
 	} );
+
+	grunt.registerTask( 'dev-plugin-create', function() {
+		const done = this.async();
+		const options = getOptions( this );
+		pluginCreateTask( ckeditor5Path, options, grunt.log.writeln, grunt.log.error ).then( done );
+	} );
+
+	function getOptions( context ) {
+		const options = {
+			workspaceRoot: '..'
+		};
+
+		return context.options( options );
+	}
 };
 };
 
 

+ 82 - 0
dev/tasks/utils/dev-plugin-create.js

@@ -0,0 +1,82 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+const inquiries = require( './inquiries' );
+const git = require( './git' );
+const tools = require( './tools' );
+const path = require( 'path' );
+
+/**
+ * 1. Ask for new plugin name.
+ * 2. Ask for initial version.
+ * 3. Ask for GitHub URL.
+ * 4. Initialize repository
+ * 		4.1. Initialize GIT repository.
+ * 		4.2. Fetch and merge boilerplate project.
+ * 5. Update package.json file in new plugin's repository.
+ * 6. Update package.json file in CKEditor5 repository.
+ * 7. Link new plugin.
+ *
+ * @param {String} ckeditor5Path Path to main CKEditor5 repository.
+ * @param {Object} options grunt options.
+ * @param {Function} writeln Function for log output.
+ * @param {Function} writeError Function of error output
+ * @returns {Promise} Returns promise fulfilled after task is done.
+ */
+module.exports = ( ckeditor5Path, options, writeln, writeError ) => {
+	return new Promise( ( resolve, reject ) => {
+		const workspaceAbsolutePath = path.join( ckeditor5Path, options.workspaceRoot );
+		let pluginName;
+		let repositoryPath;
+		let pluginVersion;
+		let gitHubUrl;
+
+		inquiries.getPluginName()
+			.then( result => {
+				pluginName = result;
+				repositoryPath = path.join( workspaceAbsolutePath, pluginName );
+
+				return inquiries.getPluginVersion();
+			} )
+			.then( result => {
+				pluginVersion = result;
+
+				return inquiries.getPluginGitHubUrl( pluginName );
+			} )
+			.then( ( result ) => {
+				try {
+					gitHubUrl = result;
+
+					writeln( `Initializing repository ${ repositoryPath }...` );
+					git.initializeRepository( repositoryPath );
+
+					writeln( `Updating package.json files...` );
+					tools.updateJSONFile( path.join( repositoryPath, 'package.json' ), ( json ) => {
+						json.name = pluginName;
+						json.version = pluginVersion;
+
+						return json;
+					} );
+
+					tools.updateJSONFile( path.join( ckeditor5Path, 'package.json' ), ( json ) => {
+						if ( !json.dependencies ) {
+							json.dependencies = {};
+						}
+						json.dependencies[ pluginName ] = gitHubUrl;
+
+						return json;
+					} );
+
+					writeln( `Linking ${ pluginName } to node_modules...` );
+					tools.linkDirectories( repositoryPath, path.join( ckeditor5Path, 'node_modules', pluginName ) );
+				} catch ( error ) {
+					writeError( error );
+				}
+			} )
+			.catch( reject );
+	} );
+};

+ 14 - 0
dev/tasks/utils/git.js

@@ -6,6 +6,8 @@
 'use strict';
 'use strict';
 
 
 const tools = require( './tools' );
 const tools = require( './tools' );
+const BOILERPLATE_REPOSITORY = 'git@github.com:ckeditor/ckeditor-boilerplate.git';
+const BOILERPLATE_BRANCH = 'ckeditor5';
 
 
 module.exports = {
 module.exports = {
 	/**
 	/**
@@ -71,5 +73,17 @@ module.exports = {
 		];
 		];
 
 
 		tools.shExec( checkoutCommands.join( ' && ' ) );
 		tools.shExec( checkoutCommands.join( ' && ' ) );
+	},
+
+	initializeRepository( repositoryPath ) {
+		const initializeCommands = [
+			`git init ${ repositoryPath }`,
+			`cd ${ repositoryPath }`,
+			`git remote add boilerplate ${ BOILERPLATE_REPOSITORY }`,
+			`git fetch boilerplate ${ BOILERPLATE_BRANCH }`,
+			`git merge boilerplate/${ BOILERPLATE_BRANCH }`
+		];
+
+		tools.shExec( initializeCommands.join( ' && ' ) );
 	}
 	}
 };
 };

+ 55 - 0
dev/tasks/utils/inquiries.js

@@ -0,0 +1,55 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+const inquirer = require( 'inquirer' );
+const DEFAULT_PLUGIN_NAME_PREFIX = 'ckeditor5-plugin-';
+const DEFAULT_PLUGIN_VERSION = '0.0.1';
+const DEFAULT_GITHUB_URL_PREFIX = 'ckeditor/';
+
+module.exports = {
+	getPluginName() {
+		return new Promise( ( resolve ) => {
+			inquirer.prompt( [ {
+				name: 'pluginName',
+				message: 'Enter plugin name without ' + DEFAULT_PLUGIN_NAME_PREFIX + ' prefix:',
+				validate: ( input ) => {
+					var regexp = /^[a-zA-Z0-9-_]+$/;
+
+					return regexp.test( input ) ? true : 'Please provide a valid plugin name.';
+				}
+			} ], ( answers ) => {
+				resolve( DEFAULT_PLUGIN_NAME_PREFIX + answers.pluginName );
+			} );
+		} );
+	},
+
+	getPluginVersion( ) {
+		return new Promise( ( resolve ) => {
+			inquirer.prompt( [ {
+				name: 'version',
+				message: 'Enter plugin\'s initial version:',
+				default: DEFAULT_PLUGIN_VERSION
+			} ], ( answers ) => {
+				resolve( answers.version );
+			} );
+		} );
+	},
+
+	getPluginGitHubUrl( pluginName ) {
+		var defaultGitHubUrl = DEFAULT_GITHUB_URL_PREFIX + pluginName;
+
+		return new Promise( ( resolve ) => {
+			inquirer.prompt( [ {
+				name: 'gitHubUrl',
+				message: 'Enter plugin\'s GitHub URL:',
+				default: defaultGitHubUrl
+			} ], ( answers ) => {
+				resolve( answers.gitHubUrl );
+			} );
+		} );
+	}
+};

+ 10 - 0
dev/tasks/utils/tools.js

@@ -219,5 +219,15 @@ module.exports = {
 		return this.getDirectories( path ).filter( dir => {
 		return this.getDirectories( path ).filter( dir => {
 			return dependencyRegExp.test( dir );
 			return dependencyRegExp.test( dir );
 		} );
 		} );
+	},
+
+	updateJSONFile( path, updateFunction ) {
+		const fs = require( 'fs' );
+
+		const contents = fs.readFileSync( path, 'utf-8' );
+		let json = JSON.parse( contents );
+		json = updateFunction( json );
+
+		fs.writeFileSync( path, JSON.stringify( json, null, 2 ), 'utf-8' );
 	}
 	}
 };
 };