Permalink
Browse files

Template and working generator

  • Loading branch information...
1 parent a31b8d7 commit f55492066949a40348c1cc16ead91d45a6794a92 @wycats committed Jan 18, 2008
View
@@ -0,0 +1,20 @@
+Copyright (c) 2007 Yehuda Katz
+
+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.
View
@@ -0,0 +1,10 @@
+==== merb-gen
+
+merb-gen is a part of the Merb More package. It provides application and plugin generators for Merb.
+
+==== Usage
+
+To generate a new merb app, run merb-gen &lt;path&gt;. For instance, <tt>merb-gen foo</tt> would create
+a new merb app inside of the foo directory.
+
+To generate a new merb plugin, run merb-gen plugin &lt;path&gt;.
View
@@ -0,0 +1,45 @@
+require 'rubygems'
+require 'rake/gempackagetask'
+
+GEM = "merb-gen"
+VERSION = "0.9.0"
+AUTHOR = "Yehuda Katz"
+EMAIL = "wycats@gmail.com"
+HOMEPAGE = "http://www.merbivore.com"
+SUMMARY = "Merb More: Merb's Application and Plugin Generators"
+
+spec = Gem::Specification.new do |s|
+ s.name = GEM
+ s.version = VERSION
+ s.platform = Gem::Platform::RUBY
+ s.has_rdoc = true
+ s.extra_rdoc_files = ["README", "LICENSE", 'TODO']
+ s.summary = SUMMARY
+ s.description = s.summary
+ s.author = AUTHOR
+ s.email = EMAIL
+ s.homepage = HOMEPAGE
+ s.bindir = "bin"
+ s.executables = %w( merb-gen )
+
+ # Uncomment this to add a dependency
+ s.add_dependency "merb-core", ">= 0.9"
+
+ s.require_path = 'lib'
+ s.autorequire = GEM
+ s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,spec,app_generators}/**/*")
+end
+
+Rake::GemPackageTask.new(spec) do |pkg|
+ pkg.gem_spec = spec
+end
+
+namespace :jruby do
+ task :install do
+ sh %{sudo jruby -S gem install pkg/#{GEM}-#{VERSION}}
+ end
+end
+
+task :install => [:package] do
+ sh %{sudo gem install pkg/#{GEM}-#{VERSION}}
+end
View
@@ -0,0 +1,4 @@
+TODO:
+Fix LICENSE with your name
+Fix Rakefile with your name and contact info
+Add your code to lib/<%= name %>.rb
No changes.
@@ -0,0 +1,94 @@
+class MerbGenerator < RubiGen::Base
+ DEFAULT_SHEBANG = File.join(Config::CONFIG['bindir'],
+ Config::CONFIG['ruby_install_name']) unless defined? DEFAULT_SHEBANG
+
+ default_options :shebang => DEFAULT_SHEBANG
+
+ attr_reader :name
+
+ def initialize(runtime_args, runtime_options = {})
+ super
+ usage if args.empty?
+ @name = args.shift
+ @destination_root = File.expand_path(@name)
+ extract_options
+ end
+
+ def relative(path, dir = nil)
+ path.gsub(/^#{File.dirname(__FILE__)}\/templates\/#{dir}/, "")
+ end
+
+ def manifest
+ script_options = { :chmod => 0755, :shebang => options[:shebang] == DEFAULT_SHEBANG ? nil : options[:shebang] }
+
+ record do |m|
+ m.directory ''
+
+ options["spec"] = true unless options["test"]
+
+ # Things that are optional and should only be included if options[choice] is true
+ choices = %w( spec test )
+
+ # Make sure the appropriate folders exist
+ basedirs = Dir["#{File.dirname(__FILE__)}/templates/**/*"].
+ select {|x| File.directory?(x) && (!choices.include?(relative(x)) || options[relative(x)]) }.
+ map {|x| relative(x) }
+
+ basedirs.each { |path| m.directory path }
+
+ # copy skeleton
+ # Grab everything under templates
+ Dir["#{File.dirname(__FILE__)}/templates/**/*"].each do |file|
+ # Only continue if the file is a directory
+ next unless File.directory?(file)
+ next if (choices.include?(relative(file)) && !options[relative(file)])
+ # Remove "template/" from the glob filename
+ dir = relative(file)
+ # Get all the files under the directory that are not directories or in the list of excluded templates above.
+ # Then, remove "template/" from the resulting filenames
+ files = Dir["#{file}/*"].reject {|f| File.directory?(f)}.map {|f| relative(f, "#{dir}/") }
+ # Copy the files over
+ m.file_copy_each(files, dir) unless files.empty?
+ end
+
+ # m.template "config/merb.yml", "config/merb.yml", :assigns => {:key => "#{@name.upcase}#{rand(9999)}"}
+ end
+ end
+
+ def windows
+ (RUBY_PLATFORM =~ /dos|win32|cygwin/i) || (RUBY_PLATFORM =~ /(:?mswin|mingw)/)
+ end
+
+ protected
+ def banner
+ <<-EOS.split("\n").map{|x| x.strip}.join("\n")
+ Creates a Merb application stub.
+
+ USAGE: #{spec.name} -g path"
+ EOS
+ end
+
+ def add_options!(opts)
+ opts.on("-S", "--[no-]spec", "Generate with RSpec") {|s| @options["spec"] = true}
+ opts.on("-T", "--[no-]test", "Generate with Test::Unit") {|t| @options["test"] = true}
+ end
+
+ def extract_options
+ end
+
+ def parse!(args, runtime_options = {})
+ self.options = {}
+
+ @option_parser = OptionParser.new do |opt|
+ opt.banner = banner
+ add_options!(opt)
+ add_general_options!(opt)
+ opt.parse!(args)
+ end
+
+ return args
+ ensure
+ self.options = full_options(runtime_options)
+ end
+
+end
@@ -0,0 +1,123 @@
+require 'rubygems'
+Gem.clear_paths
+Gem.path.unshift(File.join(File.dirname(__FILE__), "gems"))
+
+require 'rake'
+require 'rake/rdoctask'
+require 'rake/testtask'
+require 'spec/rake/spectask'
+require 'fileutils'
+
+Merb.environment = ENV['Merb.environment'] if ENV['Merb.environment']
+$RAKE_ENV = true
+
+require File.dirname(__FILE__)+'/config/boot.rb'
+require Merb::framework_root+'/tasks'
+Merb.root = File.dirname(__FILE__)
+include FileUtils
+
+# Get Merb plugins and dependencies
+require File.dirname(__FILE__)+'/config/dependencies.rb'
+Merb::Plugins.rakefiles.each {|r| require r }
+
+#desc "Packages up Merb."
+#task :default => [:package]
+
+desc "load merb_init.rb"
+task :merb_init do
+ require 'merb'
+ require File.dirname(__FILE__)+'/config/merb_init.rb'
+end
+
+task :uninstall => [:clean] do
+ sh %{sudo gem uninstall #{NAME}}
+end
+
+desc 'Run unit tests'
+Rake::TestTask.new('test_unit') do |t|
+ t.libs << 'test'
+ t.pattern = 'test/unit/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Run functional tests'
+Rake::TestTask.new('test_functional') do |t|
+ t.libs << 'test'
+ t.pattern = 'test/functional/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Run all tests'
+Rake::TestTask.new('test') do |t|
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc "Run all specs"
+Spec::Rake::SpecTask.new('specs') do |t|
+ t.spec_opts = ["--format", "specdoc", "--colour"]
+ t.spec_files = Dir['spec/**/*_spec.rb'].sort
+end
+
+desc "Run all model specs"
+Spec::Rake::SpecTask.new('model_specs') do |t|
+ t.spec_opts = ["--format", "specdoc", "--colour"]
+ t.spec_files = Dir['spec/models/**/*_spec.rb'].sort
+end
+
+desc "Run all controller specs"
+Spec::Rake::SpecTask.new('controller_specs') do |t|
+ t.spec_opts = ["--format", "specdoc", "--colour"]
+ t.spec_files = Dir['spec/controllers/**/*_spec.rb'].sort
+end
+
+desc "Run a specific spec with TASK=xxxx"
+Spec::Rake::SpecTask.new('spec') do |t|
+ t.spec_opts = ["--format", "specdoc", "--colour"]
+ t.libs = ['lib', 'server/lib' ]
+ t.spec_files = ["spec/merb/#{ENV['TASK']}_spec.rb"]
+end
+
+desc "Run all specs output html"
+Spec::Rake::SpecTask.new('specs_html') do |t|
+ t.spec_opts = ["--format", "html"]
+ t.libs = ['lib', 'server/lib' ]
+ t.spec_files = Dir['spec/**/*_spec.rb'].sort
+end
+
+desc "RCov"
+Spec::Rake::SpecTask.new('rcov') do |t|
+ t.spec_opts = ["--format", "specdoc", "--colour"]
+ t.spec_files = Dir['spec/**/*_spec.rb'].sort
+ t.libs = ['lib', 'server/lib' ]
+ t.rcov = true
+end
+
+desc 'Run all tests, specs and finish with rcov'
+task :aok do
+ sh %{rake rcov}
+ sh %{rake spec}
+end
+
+unless Gem.cache.search("haml").empty?
+ namespace :haml do
+ desc "Compiles all sass files into CSS"
+ task :compile_sass do
+ gem 'haml'
+ require 'sass'
+ puts "*** Updating stylesheets"
+ Sass::Plugin.update_stylesheets
+ puts "*** Done"
+ end
+ end
+end
+
+##############################################################################
+# SVN
+##############################################################################
+
+desc "Add new files to subversion"
+task :svn_add do
+ system "svn status | grep '^\?' | sed -e 's/? *//' | sed -e 's/ /\ /g' | xargs svn add"
+end
@@ -0,0 +1,2 @@
+class Application < Merb::Controller
+end
@@ -0,0 +1,13 @@
+class Exceptions < Application
+
+ # handle NotFound exceptions (404)
+ def not_found
+ render :format => :html
+ end
+
+ # handle NotAcceptable exceptions (406)
+ def not_acceptable
+ render :format => :html
+ end
+
+end
@@ -0,0 +1,5 @@
+module Merb
+ module GlobalHelper
+ # helpers defined here available to all views.
+ end
+end
@@ -0,0 +1,11 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
+ <head>
+ <title>Fresh Merb App</title>
+ <meta http-equiv="content-type" content="text/html; charset=utf-8" />
+ <link rel="stylesheet" href="/stylesheets/master.css" type="text/css" media="screen" charset="utf-8">
+ </head>
+ <body>
+ <%= catch_content :for_layout %>
+ </body>
+</html>
@@ -0,0 +1,41 @@
+# Make the app's "gems" directory a place where gems are loaded from
+Gem.clear_paths
+Gem.path.unshift(Merb.root / "gems")
+
+# Make the app's "lib" directory a place where ruby files get "require"d from
+$LOAD_PATH.unshift(Merb.root / "lib")
+
+### Merb doesn't come with database support by default. You need
+### an ORM plugin. Install one, and uncomment one of the following lines,
+### if you need a database.
+
+### Uncomment for DataMapper ORM
+# use_orm :datamapper
+
+### Uncomment for ActiveRecord ORM
+# use_orm :activerecord
+
+### Uncomment for Sequel ORM
+# use_orm :sequel
+
+
+### This defines which test framework the generators will use
+### rspec is turned on by default
+# use_test :test_unit
+use_test :rspec
+
+### Add your other dependencies here
+
+# These are some examples of how you might specify dependencies.
+#
+# dependencies "RedCloth", "merb_helpers"
+# OR
+# dependency "RedCloth", "> 3.0"
+# OR
+# dependencies "RedCloth" => "> 3.0", "ruby-aes-cext" => "= 1.0"
+
+Merb::BootLoader.after_app_loads do
+ ### Add dependencies here that must load after the application loads:
+
+ # dependency "magic_admin" # this gem uses the app's model classes
+end
@@ -0,0 +1 @@
+puts "Loaded DEVELOPMENT Environment..."
@@ -0,0 +1 @@
+puts "Loaded PRODUCTION Environment..."
@@ -0,0 +1 @@
+puts "Loaded TEST Environment..."
Oops, something went wrong.

0 comments on commit f554920

Please sign in to comment.