Skip to content

Commit

Permalink
+ picky-generators gem project
Browse files Browse the repository at this point in the history
  • Loading branch information
floere committed Dec 12, 2010
1 parent b4e61d3 commit 73433cd
Show file tree
Hide file tree
Showing 51 changed files with 8,564 additions and 0 deletions.
5 changes: 5 additions & 0 deletions generators/lib/picky-generators.rb
@@ -0,0 +1,5 @@
require File.expand_path '../picky-generators/not_found_error', __FILE__

require File.expand_path '../picky-generators/server/unicorn', __FILE__

require File.expand_path '../picky-generators/selector', __FILE__
119 changes: 119 additions & 0 deletions generators/lib/picky-generators/generators/base.rb
@@ -0,0 +1,119 @@
# encoding: utf-8
#
require 'fileutils'

module Picky

# This is a very simple generator.
# Not at all like Padrino's or Rails'.
# (No diss, just by way of a faster explanation)
#
# Basically copies a prototype Unicorn into a newly generated directory.
#
module Generators # :nodoc:all

# Simple Base generator.
#
class Base

attr_reader :identifier, :name, :prototype_basedir

def initialize identifier, name, prototype_path, *args
@identifier = identifier
@name = name
@prototype_basedir = File.expand_path "../../../prototypes/#{prototype_path}", __FILE__
end

#
#
def create_target_directory
if File.exists?(target_directory)
exists target_directory
else
FileUtils.mkdir target_directory
created target_directory
end
end

#
#
def copy_all_files
all_prototype_files.each do |filename|
next if filename.match(/\.textile$/)
copy_single_file filename
end
end

#
#
def target_filename_for filename
filename.gsub(%r{#{prototype_basedir}}, target_directory)
end
#
#
def copy_single_file filename
target = target_filename_for filename
if File.exists? target
exists target
else
smart_copy filename, target
end
end

# Well, "smart" ;)
#
def smart_copy filename, target
# p "Trying to copy #{filename} -> #{target}"
FileUtils.copy_file filename, target
created target
rescue Errno::EISDIR
# p "EISDIR #{filename} -> #{target}"
FileUtils.rm target
FileUtils.mkdir_p target unless Dir.exists?(target)
created target
rescue Errno::EEXIST
# p "EEXIST #{filename} -> #{target}"
exists target
rescue Errno::ENOTDIR
# p "ENOTDIR #{filename} -> #{target}"
FileUtils.mkdir_p File.dirname(target) rescue nil
retry
rescue Errno::ENOENT => e
# p "ENOENT #{filename} -> #{target}"
if File.exists? filename
FileUtils.mkdir_p File.dirname(target)
retry
else
raise e
end
end

#
#
def all_prototype_files
Dir[File.join(prototype_basedir, '**', '*')]
end

#
#
def target_directory
File.expand_path name, Dir.pwd
end

def created entry
exclaim "#{entry} \x1b[32mcreated\x1b[m."
end

def exists entry
exclaim "#{entry} \x1b[31mexists\x1b[m, skipping."
end

def exclaim something
puts something
end

end

end

end
39 changes: 39 additions & 0 deletions generators/lib/picky-generators/generators/client/sinatra.rb
@@ -0,0 +1,39 @@
module Picky

module Generators

module Server

# Generates a new Picky Sinatra Client Example.
#
# Example:
# > picky-generate sinatra my_lovely_sinatra
#
class Sinatra < Base

def initialize identifier, name, *args
super indentifier, name, 'client/sinatra', *args
end

#
#
def generate
exclaim "Setting up Picky project \"#{name}\"."
create_target_directory
copy_all_files
exclaim "\"#{name}\" is a great project name! Have fun :)\n"
exclaim ""
exclaim "Next steps:"
exclaim "cd #{name}"
exclaim "bundle install"
exclaim "unicorn -p 3000 # (optional) Or use your favorite web server."
exclaim ""
end

end

end

end

end
35 changes: 35 additions & 0 deletions generators/lib/picky-generators/generators/not_found_error.rb
@@ -0,0 +1,35 @@
module Picky

module Generators

# Thrown when no generator for the command
# picky <command> <options>
# is found.
#
class NotFoundError < StandardError # :nodoc:all

def initialize selector
super usage + possible_commands(selector.types)
end

def usage
"\nUsage:\n" +
"picky <command> <params>\n" +
?\n
end

def possible_commands types
"Possible commands:\n" +
types.map do |name, klass_params|
result = "picky #{name}"
_, params = *klass_params
result << ' ' << [*params].map { |param| "<#{param}>" }.join(' ') if params
result
end.join(?\n) + ?\n
end

end

end

end
45 changes: 45 additions & 0 deletions generators/lib/picky-generators/generators/selector.rb
@@ -0,0 +1,45 @@
module Picky

module Generators

# Selects the right generator.
#
class Selector

attr_reader :types

def initialize
@types = {
sinatra_client: [Client::Sinatra, :sinatra_project_name]
unicorn_server: [Server::Unicorn, :unicorn_project_name]
}
end

# Run the generators with this command.
#
# This will "route" the commands to the right specific generator.
#
def generate args
generator = generator_for *args
generator.generate
end

#
#
def generator_for identifier, *args
generator_info = types[identifier.to_sym]
raise NotFoundError.new(self) unless generator_info
generator_class = generator_info.first
generator_for_class generator_class, identifier, *args
end

#
#
def generator_for_class klass, *args
klass.new *args
end
end

end

end
41 changes: 41 additions & 0 deletions generators/lib/picky-generators/generators/server/unicorn.rb
@@ -0,0 +1,41 @@
module Picky

module Generators

module Server

# Generates a new Picky Unicorn Server.
#
# Example:
# > picky-generate unicorn my_lovely_unicorn
#
class Unicorn < Base

def initialize identifier, name, *args
super indentifier, name, 'server/unicorn', *args
end

#
#
def generate
exclaim "Setting up Picky Unicorn Server \"#{name}\"."
create_target_directory
copy_all_files
exclaim "\"#{name}\" is a great project name! Have fun :)\n"
exclaim ""
exclaim "Next steps:"
exclaim "1. cd #{name}"
exclaim "2. bundle install"
exclaim "3. rake index"
exclaim "4. rake start"
exclaim "5. rake # (optional) shows you where Picky needs input from you"
exclaim " # if you want to define your own search."
end

end

end

end

end
23 changes: 23 additions & 0 deletions generators/picky-generators.gemspec
@@ -0,0 +1,23 @@
require File.expand_path '../../version', __FILE__

Gem::Specification.new do |s|
s.platform = Gem::Platform::RUBY

s.name = 'picky-generators'
s.version = Picky::VERSION
s.author = 'Florian Hanke'
s.email = 'florian.hanke+picky-generators@gmail.com'
s.homepage = 'http://floere.github.com/picky'
s.rubyforge_project = 'http://rubyforge.org/projects/picky'

s.description = 'Generators for Picky.'
s.summary = 'Generators for Picky the Ruby Search Engine.'

s.executables = ['picky-generator']
s.default_executable = "picky-generator"

s.files = Dir["lib/**/*.rb"]

s.test_files = Dir["spec/**/*_spec.rb"]
s.add_development_dependency 'rspec'
end
13 changes: 13 additions & 0 deletions generators/prototypes/client/sinatra/Gemfile
@@ -0,0 +1,13 @@
source :gemcutter

# Gems required by the Picky client.
#
gem 'picky-client'
gem 'i18n'
gem 'activesupport', :require => 'active_support/core_ext'
gem 'sinatra'
gem 'haml'

group :development do
gem 'sinatra-reloader'
end

0 comments on commit 73433cd

Please sign in to comment.