
'use strict';

var util   = require('util')
  , path   = require('path')
  , fs     = require('fs')
  , yeoman = require('yeoman-generator')
  , config = require('./../config.js')

module.exports = Generator

function Generator() {
  yeoman.generators.Base.apply(this, arguments)

  this.sourceRoot(path.join(__dirname, 'templates'))
}

util.inherits(Generator, yeoman.generators.NamedBase)

Generator.prototype.getConfig = function getConfig() {
  var cb   = this.async()
    , self = this

  config.getConfig(function(err, data) {
    self.defaultAuthor = data.authorName
    cb()
  })
}

Generator.prototype.askFor = function askFor() {
  var cb   = this.async()
    , self = this

  var prompts = [{
          name: 'pluginName',
          message: 'Name of the plugin: ',
          default: 'myPlugin'
      },
      {
          name: 'pluginAuthor',
          message: 'Author Name: ',
          default: self.defaultAuthor
      }]

  this.prompt(prompts, function(props) {
    self.pluginName   = props.pluginName
    self.pluginAuthor = props.pluginAuthor

    cb()
  })
}

Generator.prototype.createPlugin = function createPlugin() {
  var cb = this.async()

  this.tarball('https://github.com/tommcfarlin/WordPress-Plugin-Boilerplate/tarball/master', 'app/wp-content/plugins', cb)
}

Generator.prototype.editFiles = function editFiles() {
  var cb       = this.async()
    , self     = this
    , safeName = self.pluginName.replace(/\ /g, '');

  fs.rename('app/wp-content/plugins/plugin-boilerplate', 'app/wp-content/plugins/'+safeName, function() {
    var pluginFile = 'app/wp-content/plugins/'+safeName+'/plugin.php'

    fs.readFile(pluginFile, 'utf8', function (err, data) {
      if (err) throw err

      data = data.replace(/^.*Plugin Name: .*$/mg, 'Plugin Name: ' + self.pluginName)
      data = data.replace(/^.*Author: .*$/mg, 'Author: ' + self.pluginAuthor)

      fs.writeFile(pluginFile, data)
      fs.unlink('app/wp-content/plugins/README.md', function() {
        cb()
      })
    })
  })
}

Generator.prototype.goodbye = function goodbye() {
  this.log.writeln('Plugin created successfully')
  this.log.writeln('')
}

@@ -1497,19 +1497,130 @@
 gin-
-boilerplate
+name', 'app/wp-content/plugins/'+safeName, function() %7B%0A    fs.rename('app/wp-content/plugins/'+safeName+'/plugin-name.php
 ', '
@@ -1644,32 +1644,52 @@
 ugins/'+safeName
++'/'+safeName+'.php'
 , function() %7B%0A 
@@ -1691,16 +1691,18 @@
 ) %7B%0A    
+ 
+ 
 var plug
@@ -1751,21 +1751,29 @@
 e+'/
-plugin
+'+safeName+'
 .php'%0A%0A
+  
     
@@ -1812,17 +1812,16 @@
 function
- 
 (err, da
@@ -1832,16 +1832,18 @@
 %7B%0A      
+ 
+ 
 if (err)
@@ -1854,16 +1854,18 @@
 ow err%0A%0A
+  
       da
@@ -1944,24 +1944,26 @@
 Name)%0A      
+  
 data = data.
@@ -2029,16 +2029,18 @@
 %0A%0A      
+ 
+ 
 fs.write
@@ -2062,16 +2062,18 @@
 , data)%0A
+  
       fs
@@ -2125,24 +2125,26 @@
 unction() %7B%0A
+  
         cb()
@@ -2144,16 +2144,27 @@
    cb()%0A
+        %7D)%0A
       %7D)

Fix plugin repo url and filenames