Changeset 924

Show
Ignore:
Timestamp:
05/10/07 17:00:03 (2 years ago)
Author:
sandal
Message:

rope now more AAR friendly, with README generated for projects

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • ruport-util/trunk/lib/ruport/util/generator.rb

    r862 r924  
    3333    build_config 
    3434    build_utils          
    35     build_rakefile   
     35    build_rakefile  
     36    build_readme 
     37      
    3638    puts "\nSuccessfully generated project: #{proj}" 
    37   end 
    38  
    39  
     39  end  
     40   
     41  def self.write_file(path,options={}) 
     42    options = {:io => STDOUT}.merge(options) 
     43    m = "#{project}/#{path}" 
     44    options[:io].puts "  #{m}" 
     45    if options[:file] 
     46       options[:file] << yield    
     47    else              
     48      File.open(m,"w") { |f| f << yield } 
     49    end 
     50  end                                 
     51   
    4052  def self.build_init 
    41     m = "#{project}/lib/init.rb"  
    42     puts "  #{m}" 
    43     File.open(m,"w") { |f| f << INIT } 
     53    write_file("lib/init.rb") { INIT } 
    4454  end 
    4555 
    4656  # Generates a trivial rakefile for use with Ruport. 
    4757  def self.build_rakefile 
    48     m = "#{project}/Rakefile" 
    49     puts "  #{m}" 
    50     File.open(m,"w") { |f| f << RAKEFILE } 
     58    write_file("Rakefile") { RAKEFILE } 
     59  end   
     60   
     61  def self.build_readme 
     62     write_file("README") { README } 
    5163  end 
    5264 
    5365  # Generates the build.rb and sql_exec.rb utilities 
    5466  def self.build_utils            
    55      
    5667    m = "#{project}/util/build"    
    5768    puts "  #{m}" 
     
    6980    mkdir project         
    7081    puts "creating directories.." 
    71     %w[ test config output data lib lib/reports  
    72         lib/renderers templates sql log util].each do |d| 
     82    %w[ test config output data data/models lib lib/reports  
     83        lib/renderers templates sql log util ].each do |d| 
    7384      m="#{project}/#{d}"  
    7485      puts "  #{m}" 
     
    8596 
    8697  def self.build_config 
    87     m = "#{project}/config/environment.rb" 
    88     puts "  #{m}" 
    89     File.open(m,"w") { |f| f << CONFIG } 
     98    write_file("config/environment.rb") { CONFIG } 
    9099  end 
    91100 
     
    110119  elsif ENV['renderer'] 
    111120    sh "ruby util/build renderer #{ENV['renderer']}" 
     121  elsif ENV['model'] 
     122    sh "ruby util/build model #{ENV['model']}" 
    112123  end 
    113124end 
     
    121132require "ruport" 
    122133 
    123 Ruport::Query.add_source :default, :user => "root", 
    124                                    :dsn  => "dbi:mysql:mydb" 
     134# Uncomment and modify the lines below if you want to use query.rb 
     135
     136# Ruport::Query.add_source :default, :user => "root", 
     137#                                    :dsn  => "dbi:mysql:mydb"      
     138 
     139# Uncomment and modify the lines below if you want to use AAR 
     140#   
     141# require "active_record" 
     142# require "ruport/acts_as_reportable" 
     143# ActiveRecord::Base.establish_connection( 
     144#      :adapter  => 'mysql', 
     145#      :host     => 'localhost', 
     146#      :username => 'name', 
     147#      :password => 'password', 
     148#      :database => 'mydb' 
     149# ) 
     150 
    125151END_CONFIG 
    126152 
     
    218244 
    219245  puts "class name: #{class_name}" 
    220   File.open("test/test_#{ARGV[1]}.rb","w") { |f| f << TEST } 
     246  File.open("test/test_#{ARGV[1]}.rb","w") { |f| f << TEST }  
     247elsif ARGV[0].eql? "model" 
     248  if File.exist?("data/models/#{ARGV[1]}.rb") 
     249    raise "Model #{class_name} exists!" 
     250  end 
     251  File.open("data/models.rb","a") { |f| 
     252    f.puts("require \"data/models/#{ARGV[1]}\"") 
     253  } 
     254REP = <<EOR 
     255class #{class_name} < ActiveRecord::Base 
     256   
     257  acts_as_reportable 
     258   
     259end 
     260EOR 
     261  puts "model file: data/models/#{ARGV[1]}.rb" 
     262  File.open("data/models/#{ARGV[1]}.rb", "w") { |f| f << REP } 
     263  puts "class name: #{class_name}" 
    221264else 
    222265  puts "Incorrect usage." 
     
    243286require "ruport/util" 
    244287require "lib/helpers" 
    245 require "config/environment" 
    246  
    247 class String 
    248   def /(other) 
    249    self + "/" + other 
    250   end 
    251 end 
    252  
    253 class Ruport::Report 
    254    
    255   def output_dir 
    256     config.output_dir or dir('output') 
    257   end 
    258  
    259   def data_dir 
    260     config.data_dir or dir('data') 
    261   end 
    262  
    263   def query_dir 
    264     config.query_dir or dir('sql') 
    265   end 
    266  
    267   def template_dir 
    268     config.template_dir or dir('templates') 
    269   end 
    270  
    271   private 
    272   def dir(name) 
    273     "#{FileUtils.pwd}/#{ARGV[0]}/\#{name}" 
    274   end 
    275 end 
     288require "config/environment"  
    276289END_INIT 
    277290 
    278  
    279   end 
    280 end 
     291README = <<END_README 
     292 
     293== rope : A Code Generation Tool for Ruby Reports == 
     294 
     295# Overview 
     296 
     297Rope provides you with a number of simple utilities that script away 
     298much of your boilerplate code, and also provide useful tools for 
     299development, such as automatic test running and a way to check your 
     300SQL queries for validity. Additionally, you'll get logging for free 
     301and you can share a common configuration file between applications  
     302in the same project. 
     303 
     304Though each tool on it's own isn't complicated, having them all 
     305working together can be a major win. 
     306 
     307# The Basics 
     308 
     309-- Starting a new rope project 
     310 
     311$ rope labyrith 
     312creating directories.. 
     313  labyrith/test 
     314  labyrith/config 
     315  labyrith/output 
     316  labyrith/data 
     317  labyrith/data/models 
     318  labyrith/lib 
     319  labyrith/lib/reports 
     320  labyrith/lib/renderers 
     321  labyrith/templates 
     322  labyrith/sql 
     323  labyrith/log 
     324  labyrith/util 
     325creating files.. 
     326  labyrith/lib/reports.rb 
     327  labyrith/lib/helpers.rb 
     328  labyrith/lib/renderers.rb 
     329  labyrith/lib/init.rb 
     330  labyrith/config/environment.rb 
     331  labyrith/util/build 
     332  labyrith/util/sql_exec 
     333  labyrith/Rakefile 
     334  labyrith/README 
     335 
     336Successfully generated project: labyrith 
     337 
     338Once this is complete, you'll have a large number of mostly empty 
     339folders laying around, along with some helpful tools at your disposal. 
     340 
     341-- utilities 
     342 
     343 * build : A tool for generating reports and formatting system extensions 
     344 * sql_exec: A simple tool for getting a result set from a SQL file 
     345             (possibly with ERb) 
     346 * Rakefile: Script for project automation tasks.   
     347 
     348-- directories 
     349 
     350 * test : unit tests stored here can be auto-run 
     351 * config : holds a configuration file which is shared across your applications 
     352 * reports : when reports are autogenerated, they are stored here 
     353 * renderers : autogenerated formatting system extensions are stored here 
     354 * models : stores autogenerated activerecord models  
     355 * templates : erb templates may be stored here 
     356 * sql : SQL files can be stored here, which are pre-processed by erb 
     357 * log : The logger will automatically store your logfiles here by default 
     358 * util : contains rope related tools 
     359 
     360# Generating a Report definition with rope 
     361 
     362 $ ./util/build report ghosts 
     363 report file: lib/reports/ghosts.rb 
     364 test file: test/test_ghosts.rb 
     365 class name: Ghosts 
     366 
     367 $ rake 
     368 (in /home/sandal/labyrinth) 
     369 /usr/bin/ruby -Ilib:test 
     370"/usr/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake/rake_test_loader.rb" 
     371"test/test_ghosts.rb" 
     372 Loaded suite /usr/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake/rake_test_loader 
     373 Started 
     374 F 
     375 Finished in 0.001119 seconds. 
     376 
     377   1) Failure: 
     378   test_flunk(TestGhosts) [./test/test_ghosts.rb:6]: 
     379   Write your real tests here or in any test/test_* file. 
     380 
     381   1 tests, 1 assertions, 1 failures, 0 errors 
     382   rake aborted! 
     383   Command failed with status (1): [/usr/bin/ruby -Ilib:test 
     384"/usr/lib/ruby/ge...] 
     385 
     386   (See full trace by running task with --trace) 
     387 
     388You can now edit lib/reports/ghosts.rb as needed and write tests for 
     389it in test/test_ghosts.rb without having to hook up any underplumbing. 
     390 
     391# Rope's Auto-generated Configuration File 
     392 
     393-- Basic Details 
     394 
     395roped projects will automatically make use of the configuration details in 
     396config/environment.rb , which can be used to set up database 
     397connections, Ruport's mailer, and other project information. 
     398 
     399The default file is shown below. 
     400 
     401require "ruport" 
     402 
     403# Uncomment and modify the lines below if you want to use query.rb 
     404
     405# Ruport::Query.add_source :default, :user => "root", 
     406#                                    :dsn  => "dbi:mysql:mydb"      
     407 
     408# Uncomment and modify the lines below if you want to use AAR 
     409#   
     410# require "active_record" 
     411# require "ruport/acts_as_reportable" 
     412# ActiveRecord::Base.establish_connection( 
     413#      :adapter  => 'mysql', 
     414#      :host     => 'localhost', 
     415#      :username => 'name', 
     416#      :password => 'password', 
     417#      :database => 'mydb' 
     418# ) 
     419 
     420You'll need to tweak this as needed to fit your database configuration needs. 
     421If you need to require any third party libraries which are shared across your 
     422project, you should do it in this file. 
     423 
     424# Custom rendering with rope generators. 
     425 
     426-- By Example 
     427 
     428 $ rope my_reverser 
     429 $ cd my_reverser 
     430 $ rake build renderer=reverser 
     431 
     432Edit test/test_reverser.rb to look like the code below: 
     433 
     434 require "test/unit" 
     435 require "lib/renderers/reverser" 
     436 
     437 class TestReverser < Test::Unit::TestCase 
     438   def test_reverser 
     439     assert_equal "baz", Reverser.render_text("zab") 
     440   end 
     441 end 
     442 
     443Now edit lib/renderers/reverser.rb to look like this: 
     444 
     445 require "lib/init" 
     446 
     447 class Reverser < Ruport::Renderer 
     448   stage :reverser 
     449 end 
     450 
     451 class ReverserFormatter < Ruport::Formatter 
     452 
     453   renders :text, :for => Reverser 
     454 
     455   def build_reverser 
     456     output << data.reverse 
     457   end 
     458 
     459 end 
     460 
     461The tests should pass.  You can now generate a quick report using this renderer 
     462 
     463 $ rake build report=reversed_report 
     464 
     465Edit test/test_reversed_report.rb as such: 
     466 
     467 require "test/unit" 
     468 require "lib/reports/reversed_report" 
     469 
     470 class TestReversedReport < Test::Unit::TestCase 
     471   def test_reversed_report 
     472     report = ReversedReport.new 
     473     report.message = "hello" 
     474     assert_equal "olleh", report.to_text 
     475   end 
     476 end 
     477 
     478edit lib/reports/reversed_report.rb as below and run the tests. 
     479 
     480 require "lib/init"   
     481 require "lib/renderers/reverser" 
     482 class ReversedReport < Ruport::Report 
     483 
     484   renders_with Reverser 
     485   attr_accessor :message 
     486 
     487   def generate 
     488     message 
     489   end 
     490 
     491 end 
     492 
     493# ActiveRecord integration the lazy way. 
     494 
     495Ruport now has built in support for acts_as_reportable, which provides 
     496ActiveRecord integration with ruport. 
     497 
     498-- Setup details 
     499 
     500Edit the following code in config/environment.rb  
     501(change as needed to match your config information) 
     502 
     503 ActiveRecord::Base.establish_connection( 
     504       :adapter  => 'mysql', 
     505       :host     => 'localhost', 
     506       :username => 'name', 
     507       :password => 'password', 
     508       :database => 'mydb' 
     509 ) 
     510 
     511-- Generating a model  
     512 
     513Here is an example of generating the model file: 
     514 
     515$ util/build model my_model 
     516model file: data/models/my_model.rb              
     517class name: MyModel                              
     518   
     519This will create a barebones model that looks like this: 
     520 
     521class MyModel < ActiveRecord::Base 
     522 
     523  acts_as_reportable 
     524 
     525end 
     526 
     527The data/models.rb file will require all generated models, 
     528but you can of course require specific models in your reports.   
     529 
     530This should be enought to get you started, but for more complex needs,  
     531check the appropriate acts_as_reportable / ActiveRecord documentation.    
     532 
     533# Getting Help / Reporting Problems 
     534 
     535rope is an officially supported utility for the Ruby Reports project, in 
     536the ruport-util package.   If you run into problems or have feature requests, 
     537please contact us at http://list.rubyreports.org 
     538END_README 
     539 
     540  end 
     541end