wp-generate 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ wp-generate.gemspec
2
+ coverage
data/README.rdoc ADDED
@@ -0,0 +1,22 @@
1
+ = wp-generate
2
+
3
+ script/generate for WordPress
4
+
5
+ == Installation
6
+
7
+ gem install wp-generate
8
+
9
+ == Usage
10
+
11
+ To create a new theme:
12
+
13
+ wp-generate theme wp-content/themes/mytheme
14
+
15
+ This will create a theme just the way we like it.
16
+
17
+ Then get started with the home page:
18
+
19
+ cd wp-content/themes/mytheme
20
+ wp-generate template home
21
+
22
+ This will create the appropriate page template, helper, and stylesheet.
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ require 'rake'
2
+
3
+ begin
4
+ require 'jeweler'
5
+ Jeweler::Tasks.new do |gem|
6
+ gem.name = "wp-generate"
7
+ gem.summary = %Q{script/generate for WordPress}
8
+ gem.description = %Q{}
9
+ gem.email = "tom@thedextrousweb.com"
10
+ gem.homepage = "http://github.com/dxw/wp-generate"
11
+ gem.authors = ["The Dextrous Web"]
12
+ gem.add_runtime_dependency "activesupport"
13
+ end
14
+ Jeweler::GemcutterTasks.new
15
+ rescue LoadError
16
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
17
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.0
data/bin/wp-generate ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'wp_generate'
4
+
5
+ WpGenerate.generate(ARGV)
@@ -0,0 +1,46 @@
1
+ require 'active_support'
2
+ require 'erb'
3
+ require 'fileutils'
4
+
5
+ class WpGenerate; end
6
+ require 'wp_generate/generator'
7
+
8
+ String.send :include, ActiveSupport::CoreExtensions::String::Inflections
9
+
10
+ # A hack. I don't really know how to go about this best.
11
+ begin
12
+ require 'wp_capistrano'
13
+ rescue LoadError
14
+ end
15
+
16
+ class WpGenerate
17
+ def self.generate args
18
+ options = []
19
+ while generator = args.shift
20
+ break unless generator.start_with? '-'
21
+ options << generator
22
+ end
23
+
24
+ base_path = 'wp_generate/generator'
25
+
26
+ if generator.nil?
27
+ STDERR.puts 'Usage: wp-generate [GLOBAL OPT...] [generator] [PATH|OPT]'
28
+ STDERR.puts 'Generators:'
29
+ $:.map{|path|File.join(path,base_path,'*.rb')}.each do |path|
30
+ Dir.glob(path).map{|generator| generator.match(%r&/([^/]+)\.rb$&)[1]}.each do |generator|
31
+ STDERR.puts ' '+generator
32
+ end
33
+ end
34
+ exit 1
35
+ end
36
+
37
+ generator_path = File.join(base_path,generator)
38
+ begin
39
+ require generator_path
40
+ rescue LoadError
41
+ true # This is for rcov
42
+ end
43
+
44
+ generator_path.camelize.constantize.new(args, options).generate
45
+ end
46
+ end
@@ -0,0 +1,59 @@
1
+ class WpGenerate::Generator
2
+ def initialize *args
3
+ raise NotImplementedError, "This class doesn't do anything on its own, subclass it!"
4
+ end
5
+
6
+ def templates_dir
7
+ File.join(File.dirname(__FILE__), 'templates')
8
+ end
9
+
10
+ def generate
11
+ opt_parse
12
+
13
+ template_name = self.class.to_s.demodulize.downcase
14
+ @templates.each_pair do |template_path,output|
15
+ output = "#{cwd}/#{output}"
16
+ input = "#{template_name}/#{template_path}"
17
+ full_path = File.join(templates_dir, input)
18
+ erb = false
19
+ if not File.exist? full_path
20
+ full_path = "#{full_path}.erb"
21
+ erb = true
22
+ end
23
+
24
+ raise IOError, "Will not overwrite existing files without global -f option" if File.exist? output and not @options[:force]
25
+
26
+ dir = File.dirname(output)
27
+ FileUtils.makedirs dir unless File.directory? dir
28
+
29
+ STDERR.puts "#{input} => #{output}" unless @options[:quiet]
30
+ name = @vars[:name]
31
+ open(output, 'w+') do |f|
32
+ if erb
33
+ f.write ERB.new(open(full_path).read).result(binding)
34
+ else
35
+ f.write open(full_path).read
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ def opt_parse
42
+ if @options.is_a? Array
43
+ opt = {}
44
+ @options.each do |o|
45
+ case o
46
+ when '-f'
47
+ opt[:force] = true
48
+ when '-q'
49
+ opt[:quiet] = true
50
+ end
51
+ end
52
+ @options = opt
53
+ end
54
+ end
55
+
56
+ def cwd
57
+ ENV['WPGEN_WORK_DIR'] || Dir.pwd
58
+ end
59
+ end
@@ -0,0 +1,14 @@
1
+ class WpGenerate::Generator::Template < WpGenerate::Generator
2
+ def initialize args, options
3
+ @options = options
4
+ name = args.shift
5
+ if name.nil? or name.empty?
6
+ STDERR.puts 'Usage: wp-generate template [name]'
7
+ STDERR.puts 'Example: cd wp-content/themes/mytheme && wp-generate template home'
8
+ exit 1
9
+ end
10
+
11
+ @templates = {"template.php" => "#{name}.php", "helper.php" => "helpers/#{name}.php", "style.sass" => "sass/#{name}.sass"}
12
+ @vars = {:name => name}
13
+ end
14
+ end
@@ -0,0 +1,25 @@
1
+ class WpGenerate::Generator::Theme < WpGenerate::Generator
2
+ def initialize args, options
3
+ @options = options
4
+ name = args.shift
5
+ if name.nil? or name.empty?
6
+ STDERR.puts 'Usage: wp-generate theme [path]'
7
+ STDERR.puts 'Example: cd wp-content/themes && wp-generate theme mytheme'
8
+ exit 1
9
+ end
10
+
11
+ templates = %w[
12
+ index.php
13
+ style.css
14
+ helpers/helper.php
15
+ partials/header.php
16
+ partials/footer.php
17
+ sass/sass_output.php
18
+ ]
19
+ @templates = {}
20
+ templates.each do |t|
21
+ @templates[t] = "#{name}/#{t}"
22
+ end
23
+ @vars = {:name => name}
24
+ end
25
+ end
@@ -0,0 +1,9 @@
1
+ <?php
2
+ include_once 'helper.php';
3
+
4
+ class <%= name.camelize %>Helper {
5
+ }
6
+
7
+ $<%= name %>_helper = new <%= name.camelize %>Helper;
8
+ the_post();
9
+ ?>
@@ -0,0 +1 @@
1
+ body.page-template-<%= name %>-php #container
@@ -0,0 +1,5 @@
1
+ <?php /* Template Name: <%= name.titleize %> */ ?>
2
+ <?php include 'helpers/<%= name %>.php' ?>
3
+ <?php render_partial('header') ?>
4
+ <?php the_content() ?>
5
+ <?php render_partial('footer') ?>
@@ -0,0 +1,20 @@
1
+ <?php
2
+
3
+ function render_partial($partial, $arguments = array()) {
4
+ if (!empty($arguments)) {
5
+ if(is_array($arguments)) {
6
+ extract($arguments);
7
+ } else {
8
+ extract(parse_str($arguments));
9
+ }
10
+ }
11
+
12
+ $helper_path = TEMPLATEPATH.'/helpers/'.$partial.'.php';
13
+
14
+ if (file_exists($helper_path))
15
+ include_once $helper_path;
16
+
17
+ include TEMPLATEPATH."/partials/$partial.php";
18
+ }
19
+
20
+ ?>
File without changes
@@ -0,0 +1,4 @@
1
+ </div>
2
+ <?php wp_footer() ?>
3
+ </body>
4
+ </html>
@@ -0,0 +1,14 @@
1
+ <?php header('content-type: text/html;charset=utf-8') ?>
2
+ <!DOCTYPE html>
3
+ <html>
4
+ <head>
5
+ <title><?php the_title() ?></title>
6
+ <link rel="stylesheet" href="<?php bloginfo('stylesheet_url') ?>" type="text/css">
7
+ <?php wp_head() ?>
8
+ </head>
9
+ <?php flush() ?>
10
+ <body <?php body_class() ?>>
11
+ <div id="container">
12
+ <div id="header">
13
+ <h1><a href="/"><?php the_title() ?></a></h1>
14
+ </div>
@@ -0,0 +1,64 @@
1
+ <?php
2
+
3
+ // Configuration
4
+
5
+ $pre = array('mixins', 'reset', 'common', 'layout', 'page');
6
+ $post = array('ie');
7
+
8
+ // The below are not the droids you are looking for
9
+
10
+ function array_delete($arr, $val){ // lacking?
11
+ foreach ($arr as $key => $value) // so
12
+ if ($arr[$key] == $val) // PHP
13
+ unset($arr[$key]); // is
14
+ return $arr = array_values($arr);// Why
15
+ }
16
+
17
+ header('content-type: text/css');
18
+
19
+ // Find the executable
20
+
21
+ $sass = 'sass';
22
+ if (file_exists('config.php')) include 'config.php';
23
+ if (defined('SASS_PATH')) $sass = SASS_PATH;
24
+
25
+ // Find the SASS files, and put them in some sort of order
26
+
27
+ $files = glob('*.sass');
28
+
29
+ $_pre = array();
30
+ foreach ($pre as $s) {
31
+ $s = $s.'.sass';
32
+ if (file_exists($s)) $_pre[] = $s;
33
+ array_delete(&$files, $s);
34
+ }
35
+ $_post = array();
36
+ foreach ($post as $s) {
37
+ $s = $s.'.sass';
38
+ if (file_exists($s)) $_post[] = $s;
39
+ array_delete(&$files, $s);
40
+ }
41
+
42
+ $files = array_merge($_pre, $files, $_post);
43
+
44
+ // Pipe things into sass
45
+
46
+ $descriptorspec = array(0 => array("pipe", "r"), 1 => array("pipe", "w"));
47
+ $process = proc_open($sass, $descriptorspec, $pipes);
48
+
49
+ if (is_resource($process)) {
50
+ foreach ($files as $s)
51
+ fwrite($pipes[0], "@import $s\n");
52
+ fclose($pipes[0]);
53
+
54
+ $output = stream_get_contents($pipes[1]);
55
+ fclose($pipes[1]);
56
+
57
+ $return_value = proc_close($process);
58
+ if ($return_value != 0)
59
+ echo '/* sass error */';
60
+ else
61
+ echo $output;
62
+ } else
63
+ echo '/* wtf error */';
64
+ ?>
@@ -0,0 +1,10 @@
1
+ /*
2
+ * Theme Name: <%= name.titleize %>
3
+ * Theme URI:
4
+ * Description:
5
+ * Version: 0.1
6
+ * Author:
7
+ * Author URI:
8
+ */
9
+
10
+ @import url(style/sass_output.php);
@@ -0,0 +1,92 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe WpGenerate::Generator do
4
+ it "should not be possible to instantiate directly" do
5
+ lambda { WpGenerate::Generator.new }.should raise_error NotImplementedError
6
+ end
7
+
8
+ it "should correctly determine the cwd" do
9
+ g = BlankGen.new
10
+ g.cwd.should == Dir.pwd
11
+ ENV['WPGEN_WORK_DIR'] = '/tmp'
12
+ g.cwd.should == '/tmp'
13
+ end
14
+
15
+ it "should parse options correctly" do
16
+ g = BlankGen.new
17
+ g.instance_eval { @options = %w[hello there -f -c -d] }
18
+ g.opt_parse
19
+ g.instance_eval { @options[:force].should == true }
20
+ end
21
+
22
+ it "should create appropriate files from templates" do
23
+ g = BlankGen.new
24
+ g.instance_eval { @templates = {'abc' => 'def'}; @vars = {:name => 'the_name'}; @options = %w[-q] }
25
+ full_path = %r[/templates/blankgen/abc$]
26
+ output = %r[/def$]
27
+ text = "Here is some <%= test %> text"
28
+
29
+ File.should_receive(:exist?).with(full_path).and_return(true)
30
+ File.should_receive(:exist?).with(output).and_return(false)
31
+ File.should_receive(:directory?).with(an_instance_of(String)).and_return(true)
32
+ f = mock
33
+ f.should_receive(:write).with(text)
34
+ Kernel.should_receive(:open).with(output,'w+').and_yield(f)
35
+ f2 = mock
36
+ f2.should_receive(:read).and_return(text)
37
+ Kernel.should_receive(:open).with(full_path).and_return(f2)
38
+ g.generate
39
+ end
40
+
41
+ it "should not overwrite files" do
42
+ g = BlankGen.new
43
+ g.instance_eval { @templates = {'abc' => 'def'}; @vars = {:name => 'the_name'}; @options = %w[-q] }
44
+ full_path = %r[/templates/blankgen/abc$]
45
+ output = %r[/def$]
46
+ text = "Here is some text"
47
+
48
+ File.should_receive(:exist?).with(full_path).and_return(true)
49
+ File.should_receive(:exist?).with(output).and_return(true)
50
+ lambda { g.generate }.should raise_error IOError
51
+ end
52
+
53
+ it "should overwrite files with -f option" do
54
+ g = BlankGen.new
55
+ g.instance_eval { @templates = {'abc' => 'def'}; @vars = {:name => 'the_name'}; @options = %w[-q -f] }
56
+ full_path = %r[/templates/blankgen/abc$]
57
+ output = %r[/def$]
58
+ text = "Here is some <%= test %> text"
59
+
60
+ File.should_receive(:exist?).with(full_path).and_return(true)
61
+ File.should_receive(:exist?).with(output).and_return(true)
62
+ File.should_receive(:directory?).with(an_instance_of(String)).and_return(true)
63
+ f = mock
64
+ f.should_receive(:write).with(text)
65
+ Kernel.should_receive(:open).with(output,'w+').and_yield(f)
66
+ f2 = mock
67
+ f2.should_receive(:read).and_return(text)
68
+ Kernel.should_receive(:open).with(full_path).and_return(f2)
69
+ g.generate
70
+ end
71
+
72
+ it "should use ERB" do
73
+ g = BlankGen.new
74
+ g.instance_eval { @templates = {'abc' => 'def'}; @vars = {:name => 'the_name'}; @options = %w[-q] }
75
+ full_path = %r[/templates/blankgen/abc$]
76
+ full_path_erb = %r[/templates/blankgen/abc.erb$]
77
+ output = %r[/def$]
78
+ text = "Here is some <%= name %> text"
79
+ text_out = "Here is some the_name text"
80
+
81
+ File.should_receive(:exist?).with(full_path).and_return(false)
82
+ File.should_receive(:exist?).with(output).and_return(false)
83
+ File.should_receive(:directory?).with(an_instance_of(String)).and_return(true)
84
+ f = mock
85
+ f.should_receive(:write).with(text_out)
86
+ Kernel.should_receive(:open).with(output,'w+').and_yield(f)
87
+ f2 = mock
88
+ f2.should_receive(:read).and_return(text)
89
+ Kernel.should_receive(:open).with(full_path_erb).and_return(f2)
90
+ g.generate
91
+ end
92
+ end
@@ -0,0 +1,12 @@
1
+ require File.dirname(__FILE__) + '/../lib/wp_generate'
2
+ require 'spec'
3
+
4
+ class BlankGen < WpGenerate::Generator
5
+ def initialize
6
+ end
7
+ end
8
+
9
+ # Use Kernel.open so we can stub it
10
+ def open *args, &block
11
+ Kernel.open *args, &block
12
+ end
@@ -0,0 +1,24 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe WpGenerate do
4
+ it "should parse options correctly" do
5
+ global_args = %w[-a -b --see]
6
+ local_args = %w[-o -opt test --p -a]
7
+ args = global_args + %w[spec_helper/generator] + local_args
8
+
9
+ class WpGenerate::Generator::SpecHelper; class Generator; end; end
10
+ g = WpGenerate::Generator::SpecHelper::Generator.new
11
+ g.should_receive(:generate)
12
+ WpGenerate::Generator::SpecHelper::Generator.should_receive(:new).with(local_args, global_args).and_return(g)
13
+
14
+ WpGenerate.generate args
15
+ end
16
+
17
+ it "should raise an exception with no args" do
18
+ lambda { WpGenerate.generate([]) }.should raise_error ArgumentError
19
+ end
20
+
21
+ it "should raise an exception with no generator name" do
22
+ lambda { WpGenerate.generate(%w[-hello --there]) }.should raise_error ArgumentError
23
+ end
24
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wp-generate
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
+ platform: ruby
11
+ authors:
12
+ - The Dextrous Web
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-05-25 00:00:00 +01:00
18
+ default_executable: wp-generate
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activesupport
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :runtime
31
+ version_requirements: *id001
32
+ description: ""
33
+ email: tom@thedextrousweb.com
34
+ executables:
35
+ - wp-generate
36
+ extensions: []
37
+
38
+ extra_rdoc_files:
39
+ - README.rdoc
40
+ files:
41
+ - .gitignore
42
+ - README.rdoc
43
+ - Rakefile
44
+ - VERSION
45
+ - bin/wp-generate
46
+ - lib/wp_generate.rb
47
+ - lib/wp_generate/generator.rb
48
+ - lib/wp_generate/generator/template.rb
49
+ - lib/wp_generate/generator/theme.rb
50
+ - lib/wp_generate/templates/template/helper.php.erb
51
+ - lib/wp_generate/templates/template/style.sass.erb
52
+ - lib/wp_generate/templates/template/template.php.erb
53
+ - lib/wp_generate/templates/theme/helpers/helper.php.erb
54
+ - lib/wp_generate/templates/theme/index.php
55
+ - lib/wp_generate/templates/theme/partials/footer.php
56
+ - lib/wp_generate/templates/theme/partials/header.php.erb
57
+ - lib/wp_generate/templates/theme/sass/sass_output.php
58
+ - lib/wp_generate/templates/theme/style.css.erb
59
+ - spec/generator_spec.rb
60
+ - spec/spec_helper.rb
61
+ - spec/wp_generate_spec.rb
62
+ has_rdoc: true
63
+ homepage: http://github.com/dxw/wp-generate
64
+ licenses: []
65
+
66
+ post_install_message:
67
+ rdoc_options:
68
+ - --charset=UTF-8
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ requirements: []
86
+
87
+ rubyforge_project:
88
+ rubygems_version: 1.3.6
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: script/generate for WordPress
92
+ test_files:
93
+ - spec/generator_spec.rb
94
+ - spec/wp_generate_spec.rb
95
+ - spec/spec_helper.rb