Class: Ruport::Generator
- Inherits:
-
Object
- Object
- Ruport::Generator
- Extended by:
- FileUtils
- Defined in:
- lib/ruport/util/generator.rb
Overview
:nodoc:
Defined Under Namespace
Modules: Helpers
Constant Summary collapse
- RAKEFILE =
"begin; require \"rubygems\"; rescue LoadError; end\nrequire \"rake/testtask\"\n\ntask :default => [:test]\n\nRake::TestTask.new do |test|\n test.libs << \"test\"\n test.pattern = 'test/**/test_*.rb'\n test.verbose = true\nend\n\ntask :build do\n if ENV['report']\n sh \"ruby util/build report \#{ENV['report']}\"\n elsif ENV['controller']\n sh \"ruby util/build controller \#{ENV['controller']}\"\n elsif ENV['model']\n sh \"ruby util/build model \#{ENV['model']}\"\n end\nend\n\ntask :run do\n sh \"ruby lib/reports/\#{ENV['report']}.rb\"\nend\n"
- CONFIG =
"require \"ruport\"\n\n# Uncomment and modify the lines below if you want to use query.rb\n#\n# Ruport::Query.add_source :default, :user => \"root\",\n# :dsn => \"dbi:mysql:mydb\" \n\n# Uncomment and modify the lines below if you want to use AAR\n# \n# require \"active_record\"\n# require \"ruport/acts_as_reportable\"\n# ActiveRecord::Base.establish_connection(\n# :adapter => 'mysql',\n# :host => 'localhost',\n# :username => 'name',\n# :password => 'password',\n# :database => 'mydb'\n# )\n\n"
- BUILD =
"#!/usr/bin/env ruby\n\nrequire 'fileutils'\nrequire 'lib/init.rb'\nrequire \"ruport/util\"\ninclude FileUtils\ninclude Ruport::Generator::Helpers\n\nunless ARGV.length > 1\n puts \"usage: build [command] [options]\"\n exit\nend\n\nclass_name = format_class_name(ARGV[1])\n\nif ARGV[0].eql? \"report\"\n check_for_files\n File.open(\"lib/reports.rb\", \"a\") { |f| \n f.puts(\"require \\\"lib/reports/\#{ARGV[1]}\\\"\")\n }\nREP = <<EOR\nrequire \"lib/init\"\nclass \#{class_name} < Ruport::Report\n\n def renderable_data(format)\n\n end\n \nend\n\nEOR\n\nTEST = <<EOR\nrequire \"test/unit\"\nrequire \"lib/reports/\#{ARGV[1]}\"\n\nclass Test\#{class_name} < Test::Unit::TestCase\n def test_flunk\n flunk \"Write your real tests here or in any test/test_* file\"\n end\nend\nEOR\n\n File.open(\"lib/reports/\#{ARGV[1]}.rb\", \"w\") { |f| f << REP }\n puts \"reports file: lib/reports/\#{ARGV[1]}.rb\"\n puts \"test file: test/test_\#{ARGV[1]}.rb\"\n puts \"class name: \#{class_name}\" \n File.open(\"test/test_\#{ARGV[1]}.rb\",\"w\") { |f| f << TEST } \n\nelsif ARGV[0].eql? \"controller\"\n\n check_for_files\n File.open(\"lib/controllers.rb\",\"a\") { |f|\n f.puts(\"require \\\"lib/controllers/\#{ARGV[1]}\\\"\")\n }\nREP = <<EOR\nrequire \"lib/init\"\n\nclass \#{class_name} < Ruport::Controller\n stage :\#{class_name.downcase}\nend\n\nclass \#{class_name}Formatter < Ruport::Formatter\n\n # change to your format name, or add additional formats\n renders :my_format, :for => \#{class_name}\n\n def build_\#{class_name.downcase}\n \n end\n\nend\nEOR\n\nTEST = <<EOR\nrequire \"test/unit\"\nrequire \"lib/controllers/\#{ARGV[1]}\"\n\nclass Test\#{class_name} < Test::Unit::TestCase\n def test_flunk\n flunk \"Write your real tests here or in any test/test_* file\"\n end\nend\nEOR\n puts \"controller file: lib/contollers/\#{ARGV[1]}.rb\"\n File.open(\"lib/controllers/\#{ARGV[1]}.rb\", \"w\") { |f| f << REP }\n puts \"test file: test/test_\#{ARGV[1]}.rb\"\n\n puts \"class name: \#{class_name}\"\n File.open(\"test/test_\#{ARGV[1]}.rb\",\"w\") { |f| f << TEST } \nelsif ARGV[0].eql? \"model\"\n if File.exist?(\"data/models/\#{ARGV[1]}.rb\")\n raise \"Model \#{class_name} exists!\"\n end\n File.open(\"data/models.rb\",\"a\") { |f|\n f.puts(\"require \\\"data/models/\#{ARGV[1]}\\\"\")\n }\nREP = <<EOR\nclass \#{class_name} < ActiveRecord::Base\n \n acts_as_reportable\n \nend\nEOR\n puts \"model file: data/models/\#{ARGV[1]}.rb\"\n File.open(\"data/models/\#{ARGV[1]}.rb\", \"w\") { |f| f << REP }\n puts \"class name: \#{class_name}\"\nelse\n puts \"Incorrect usage.\"\nend\n"
- SQL_EXEC =
"#!/usr/bin/env ruby\n\nrequire \"lib/init\"\n\nputs Ruport::Query.new(ARGF.read).result\n"
- INIT =
"begin\n require \"rubygems\"\n gem \"ruport\",\"=\#{Ruport::VERSION}\"\n gem \"ruport-util\",\"=\#{Ruport::Util::VERSION}\"\nrescue LoadError \n nil\nend\nrequire \"ruport\"\nrequire \"ruport/util\"\nrequire \"lib/helpers\"\nrequire \"config/environment\" \nrequire \"lib/templates\"\n"
- README =
"\n== rope : A Code Generation Tool for Ruby Reports ==\n\n# Overview\n\nRope provides you with a number of simple utilities that script away\nmuch of your boilerplate code, and also provide useful tools for\ndevelopment\n\n# The Basics\n\n-- Starting a new rope project\n\n$ rope labyrith\ncreating directories..\n labyrith/test\n labyrith/config\n labyrith/output\n labyrith/data\n labyrith/data/models\n labyrith/lib\n labyrith/lib/reports\n labyrith/lib/controllers\n labyrith/templates\n labyrith/sql\n labyrith/log\n labyrith/util\ncreating files..\n labyrith/lib/reports.rb\n labyrith/lib/helpers.rb\n labyrith/lib/controllers.rb\n labyrith/lib/init.rb\n labyrith/config/environment.rb\n labyrith/util/build\n labyrith/util/sql_exec\n labyrith/Rakefile\n labyrith/README\n\nSuccessfully generated project: labyrith\n\nOnce this is complete, you'll have a large number of mostly empty\nfolders laying around, along with some helpful tools at your disposal.\n\n-- utilities\n\n * build : A tool for generating reports and formatting system extensions\n * sql_exec: A simple tool for getting a result set from a SQL file\n (possibly with ERb)\n * Rakefile: Script for project automation tasks. \n\n-- directories\n\n * test : unit tests stored here can be auto-run\n * config : holds a configuration file which is shared across your applications\n * reports : when reports are autogenerated, they are stored here\n * controllers : autogenerated formatting system extensions are stored here\n * models : stores autogenerated activerecord models \n * templates : erb templates may be stored here\n * sql : SQL files can be stored here, which are pre-processed by erb\n * log : The logger will automatically store your logfiles here by default\n * util : contains rope related tools\n\n# Generating a Report definition with rope\n\n $ ./util/build report ghosts\n report file: lib/reports/ghosts.rb\n test file: test/test_ghosts.rb\n class name: Ghosts\n\n $ rake\n (in /home/sandal/labyrinth)\n /usr/bin/ruby -Ilib:test\n\"/usr/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake/rake_test_loader.rb\"\n\"test/test_ghosts.rb\"\n Loaded suite /usr/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake/rake_test_loader\n Started\n F\n Finished in 0.001119 seconds.\n\n 1) Failure:\n test_flunk(TestGhosts) [./test/test_ghosts.rb:6]:\n Write your real tests here or in any test/test_* file.\n\n 1 tests, 1 assertions, 1 failures, 0 errors\n rake aborted!\n Command failed with status (1): [/usr/bin/ruby -Ilib:test\n\"/usr/lib/ruby/ge...]\n\n (See full trace by running task with --trace)\n\nYou can now edit lib/reports/ghosts.rb as needed and write tests for\nit in test/test_ghosts.rb without having to hook up any underplumbing.\n\n# Rope's Auto-generated Configuration File\n\n-- Basic Details\n\nroped projects will automatically make use of the configuration details in\nconfig/environment.rb , which can be used to set up database\nconnections, Ruport's mailer, and other project information.\n\nThe default file is shown below.\n\nrequire \"ruport\"\n\n# Uncomment and modify the lines below if you want to use query.rb\n#\n# Ruport::Query.add_source :default, :user => \"root\",\n# :dsn => \"dbi:mysql:mydb\" \n\n# Uncomment and modify the lines below if you want to use AAR\n# \n# require \"active_record\"\n# require \"ruport/acts_as_reportable\"\n# ActiveRecord::Base.establish_connection(\n# :adapter => 'mysql',\n# :host => 'localhost',\n# :username => 'name',\n# :password => 'password',\n# :database => 'mydb'\n# )\n\nYou'll need to tweak this as needed to fit your database configuration needs.\nIf you need to require any third party libraries which are shared across your\nproject, you should do it in this file.\n\n# Custom rendering with rope generators.\n\n-- By Example\n\n $ rope my_reverser\n $ cd my_reverser\n $ rake build controller=reverser\n\nEdit test/test_reverser.rb to look like the code below:\n\n require \"test/unit\"\n require \"lib/controllers/reverser\"\n\n class TestReverser < Test::Unit::TestCase\n def test_reverser\n assert_equal \"baz\", Reverser.render_text(\"zab\")\n end\n end\n\nNow edit lib/controllers/reverser.rb to look like this:\n\n require \"lib/init\"\n\n class Reverser < Ruport::Controller\n stage :reverser\n end\n\n class ReverserFormatter < Ruport::Formatter\n\n renders :text, :for => Reverser\n\n def build_reverser\n output << data.reverse\n end\n\n end\n\nThe tests should pass. You can now generate a quick report using this controller\n\n $ rake build report=reversed_report\n\nEdit test/test_reversed_report.rb as such:\n\n require \"test/unit\"\n require \"lib/reports/reversed_report\"\n\n class TestReversedReport < Test::Unit::TestCase\n def test_reversed_report\n report = ReversedReport.new\n report.message = \"hello\"\n assert_equal \"olleh\", report.to_text\n end\n end\n\nedit lib/reports/reversed_report.rb as below and run the tests.\n\n require \"lib/init\" \n require \"lib/controllers/reverser\"\n class ReversedReport < Ruport::Report\n\n renders_with Reverser\n attr_accessor :message\n\n def generate\n message\n end\n\n end\n\n# ActiveRecord integration the lazy way.\n\nRuport now has built in support for acts_as_reportable, which provides\nActiveRecord integration with ruport.\n\n-- Setup details\n\nEdit the following code in config/environment.rb \n(change as needed to match your config information)\n\n ActiveRecord::Base.establish_connection(\n :adapter => 'mysql',\n :host => 'localhost',\n :username => 'name',\n :password => 'password',\n :database => 'mydb'\n )\n\n-- Generating a model \n\nHere is an example of generating the model file:\n\n$ util/build model my_model\nmodel file: data/models/my_model.rb \nclass name: MyModel \n \nThis will create a barebones model that looks like this:\n\nclass MyModel < ActiveRecord::Base\n\n acts_as_reportable\n\nend\n\nThe data/models.rb file will require all generated models,\nbut you can of course require specific models in your reports. \n\nThis should be enought to get you started, but for more complex needs, \ncheck the appropriate acts_as_reportable / ActiveRecord documentation. \n\n# Getting Help / Reporting Problems\n\nrope is an officially supported utility for the Ruby Reports project, in\nthe ruport-util package. If you run into problems or have feature requests,\nplease contact us at http://list.rubyreports.org\n"
Class Method Summary collapse
- .build(proj) ⇒ Object
- .build_config ⇒ Object
-
.build_directory_structure ⇒ Object
sets up the basic directory layout for a Ruport application.
- .build_init ⇒ Object
-
.build_rakefile ⇒ Object
Generates a trivial rakefile for use with Ruport.
- .build_readme ⇒ Object
-
.build_utils ⇒ Object
Generates the build.rb and sql_exec.rb utilities.
-
.project ⇒ Object
returns the project’s name.
- .write_file(path, options = {}) ⇒ Object
Class Method Details
.build(proj) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/ruport/util/generator.rb', line 29 def self.build(proj) @project = proj build_directory_structure build_init build_config build_utils build_rakefile build_readme puts "\nSuccessfully generated project: #{proj}" end |
.build_config ⇒ Object
97 98 99 |
# File 'lib/ruport/util/generator.rb', line 97 def self.build_config write_file("config/environment.rb") { CONFIG } end |
.build_directory_structure ⇒ Object
sets up the basic directory layout for a Ruport application
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/ruport/util/generator.rb', line 79 def self.build_directory_structure mkdir project puts "creating directories.." %w[ test config output data data/models lib lib/reports lib/controllers sql util ].each do |d| m="#{project}/#{d}" puts " #{m}" mkdir(m) end puts "creating files.." %w[reports helpers controllers templates].each { |f| m = "#{project}/lib/#{f}.rb" puts " #{m}" touch(m) } end |
.build_init ⇒ Object
52 53 54 |
# File 'lib/ruport/util/generator.rb', line 52 def self.build_init write_file("lib/init.rb") { INIT } end |
.build_rakefile ⇒ Object
Generates a trivial rakefile for use with Ruport.
57 58 59 |
# File 'lib/ruport/util/generator.rb', line 57 def self.build_rakefile write_file("Rakefile") { RAKEFILE } end |
.build_readme ⇒ Object
61 62 63 |
# File 'lib/ruport/util/generator.rb', line 61 def self.build_readme write_file("README") { README } end |
.build_utils ⇒ Object
Generates the build.rb and sql_exec.rb utilities
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/ruport/util/generator.rb', line 66 def self.build_utils m = "#{project}/util/build" puts " #{m}" File.open(m,"w") { |f| f << BUILD } chmod(0755, m) m = "#{project}/util/sql_exec" puts " #{m}" File.open(m,"w") { |f| f << SQL_EXEC } chmod(0755, m) end |
.project ⇒ Object
returns the project’s name
102 |
# File 'lib/ruport/util/generator.rb', line 102 def self.project; @project; end |
.write_file(path, options = {}) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/ruport/util/generator.rb', line 41 def self.write_file(path,={}) = {:io => STDOUT}.merge() m = "#{project}/#{path}" [:io].puts " #{m}" if [:file] [:file] << yield else File.open(m,"w") { |f| f << yield } end end |