caterpillar 0.9.8 → 0.9.15

Sign up to get free protection for your applications and to get access to all the features.
@@ -56,24 +56,33 @@ module Caterpillar
56
56
  # portlet.xml template.
57
57
  def template(portlet)
58
58
  xml = " <!-- %s -->\n" % portlet[:title]
59
+
59
60
  xml << " <portlet>\n"
61
+ ### identification
60
62
  xml << " <portlet-name>%s</portlet-name>\n" % portlet[:name]
61
63
  xml << " <portlet-class>%s</portlet-class>\n" % self.portlet_class
64
+ ### supported portlet modes
62
65
  xml << " <supports>\n"
63
66
  xml << " <mime-type>text/html</mime-type>\n"
64
67
  xml << " <portlet-mode>view</portlet-mode>\n"
65
- ### edit mode
66
- xml << " <portlet-mode>edit</portlet-mode>\n" if portlet[:edit]==true
68
+ # edit mode is not used. this is for development.
69
+ if portlet[:edit]==true
70
+ xml << " <portlet-mode>edit</portlet-mode>\n"
71
+ end
67
72
  xml << " </supports>\n"
73
+ ### title for portlet container
68
74
  xml << " <portlet-info>\n"
69
75
  xml << " <title>%s</title>\n" % portlet[:title]
70
76
  xml << " </portlet-info>\n"
71
- xml << " </portlet>\n"
72
- xml << ""
77
+ xml << " </portlet>\n\n"
78
+
79
+ ### portlet filters
73
80
  xml << " <filter>\n"
81
+ # the filter reads the settings and sets the portlet session
74
82
  xml << " <filter-name>%s_filter</filter-name>\n" % portlet[:name]
75
83
  xml << " <filter-class>%s</filter-class>\n" % self.portlet_filter_class
76
84
  xml << " <lifecycle>RENDER_PHASE</lifecycle>\n"
85
+ # define host, servlet and route (path to be more precise)
77
86
  xml << " <init-param>\n"
78
87
  xml << " <name>host</name>\n"
79
88
  xml << " <value>%s</value>\n" % portlet[:host] || ""
@@ -86,8 +95,8 @@ module Caterpillar
86
95
  xml << " <name>route</name>\n"
87
96
  xml << " <value>%s</value>\n" % portlet[:path].gsub(/&/,"&amp;")
88
97
  xml << " </init-param>\n"
89
- xml << " </filter>\n"
90
- xml << ""
98
+ xml << " </filter>\n\n"
99
+
91
100
  xml << " <filter-mapping>\n"
92
101
  xml << " <filter-name>%s_filter</filter-name>\n" % portlet[:name]
93
102
  xml << " <portlet-name>%s</portlet-name>\n" % portlet[:name]
@@ -39,14 +39,16 @@ module Caterpillar
39
39
  # The main task.
40
40
  # Reads the configuration file and launches appropriate tasks.
41
41
  def initialize(name = :usage, config = nil, tasks = :define_tasks)
42
+ STDOUT.puts 'Caterpillar v.%s (c) Copyright 2008,2009 Mikael Lammentausta' % VERSION
43
+ STDOUT.puts 'Provided under the terms of the MIT license.'
44
+ STDOUT.puts
42
45
  @name = name
43
46
  @config = Util.eval_configuration(config)
47
+ @logger = @config.logger
48
+
44
49
  yield self if block_given?
45
50
  @xml_files = []
46
- STDOUT.puts 'Caterpillar v.%s (c) Copyright 2008,2009 Mikael Lammentausta' % VERSION
47
- #STDOUT.puts 'Caterpillar v.%s' % Caterpillar::VERSION
48
- STDOUT.puts 'Provided under the terms of the MIT license.'
49
- STDOUT.puts
51
+
50
52
  send tasks
51
53
  end
52
54
 
@@ -55,6 +57,7 @@ module Caterpillar
55
57
  def define_tasks
56
58
  define_xml_task
57
59
  define_usage_task
60
+ define_config_task
58
61
  define_pluginize_task
59
62
  define_environment_task
60
63
  define_portletxml_task
@@ -64,8 +67,9 @@ module Caterpillar
64
67
  define_portlets_task
65
68
  define_fixtures_task
66
69
  define_liferayportlets_task
67
- define_migrate_task
68
- define_rollback_task
70
+ define_db_migrate_task
71
+ define_db_rollback_task
72
+ define_db_update_task
69
73
  define_jar_install_task
70
74
  define_jar_uninstall_task
71
75
  define_jar_version_task
@@ -75,28 +79,19 @@ module Caterpillar
75
79
  define_deploy_war_task
76
80
  end
77
81
 
78
- # the main XML generator task
79
- def define_xml_task
80
- @name = :xml
81
- desc 'Create all XML files according to configuration'
82
- tasks = [:parse,"#{@name}:portlet"]
83
- if @config.container.kind_of? Liferay
84
- tasks << "#{@name}:liferayportletapp"
85
- tasks << "#{@name}:liferaydisplay"
86
- end
87
-
88
- # print produced portlets
89
- tasks << :portlets
90
-
91
- task :xml => tasks
92
- end
93
-
94
82
  def define_usage_task
95
83
  task :usage do
96
84
  Usage.show
97
85
  end
98
86
  end
99
87
 
88
+ # TODO: copy and diff config file
89
+ def define_config_task
90
+ task :config do
91
+ STDOUT.puts 'TODO'
92
+ end
93
+ end
94
+
100
95
  def define_pluginize_task
101
96
  desc "Unpack Caterpillar as a plugin in your Rails application"
102
97
  task :pluginize do
@@ -115,41 +110,65 @@ module Caterpillar
115
110
  end
116
111
  end
117
112
 
118
- # navigation debugging task
113
+
114
+ ### MAIN TASKS
115
+
116
+ # Main XML generator task
117
+ def define_xml_task
118
+ @name = :xml
119
+ desc 'Create all XML files according to configuration'
120
+ tasks = [:parse,"#{@name}:portlet"]
121
+ if @config.container.kind_of? Liferay
122
+ tasks << "#{@name}:liferayportletapp"
123
+ tasks << "#{@name}:liferaydisplay"
124
+ end
125
+
126
+ # print produced portlets
127
+ tasks << :portlets
128
+
129
+ task :xml => tasks
130
+ end
131
+
132
+ # Prints the list of portlets.
119
133
  def define_portlets_task
120
134
  desc 'Prints portlet configuration'
121
135
  task :portlets => :parse do
136
+ portal_info
122
137
  info 'Portlet configuration ***********************'
123
138
  print_portlets(@portlets)
124
139
  end
125
140
  end
126
141
 
127
- def define_fixtures_task
128
- desc 'Creates YAML fixtures from live data for testing.'
129
- task :fixtures => :environment do
130
- require 'active_record'
142
+ # Creates live fixtures from the RAILS_ENV database for testing.
143
+ def define_fixtures_task
144
+ desc 'Creates YAML fixtures from live data for testing.'
145
+ task :fixtures => :environment do
131
146
 
132
- sql = "SELECT * from %s"
147
+ sql = "SELECT * from %s"
133
148
 
134
- skip_tables = []
135
- begin
136
- skip_tables = @config.container.skip_fixture_tables
137
- end
149
+ skip_tables = []
150
+ begin
151
+ skip_tables = @config.container.skip_fixture_tables
152
+ end
138
153
 
139
- ActiveRecord::Base.establish_connection
140
- (ActiveRecord::Base.connection.tables - skip_tables).each do |table|
141
- i = "000"
142
- File.open("#{RAILS_ROOT}/test/fixtures/#{table}.yml", 'w') do |file|
143
- puts "* %s" % file.inspect
144
- data = ActiveRecord::Base.connection.select_all(sql % table)
145
- file.write data.inject({}) { |hash, record|
146
- hash["#{table}_#{i.succ!}"] = record
147
- hash
148
- }.to_yaml
154
+ ActiveRecord::Base.establish_connection
155
+ info 'Creating YAML fixtures from %s database' % RAILS_ENV
156
+
157
+ (ActiveRecord::Base.connection.tables - skip_tables).each do |table|
158
+ i = "000"
159
+ File.open(
160
+ File.join(RAILS_ROOT,'test','fixtures',table+'.yml'), 'w'
161
+ ) do |file|
162
+ info file.inspect
163
+ data = ActiveRecord::Base.connection.select_all(sql % table)
164
+ file.write data.inject({}) { |hash, record|
165
+ hash["#{table}_#{i.succ!}"] = record
166
+ hash
167
+ }.to_yaml
168
+ end
149
169
  end
150
170
  end
151
171
  end
152
- end
153
172
 
154
173
  ### SUB-TASKS
155
174
 
@@ -158,6 +177,10 @@ module Caterpillar
158
177
  task :default => :test
159
178
  task :environment do
160
179
  require(File.join(@config.rails_root, 'config', 'environment'))
180
+ if @config.container.is_a?(Caterpillar::Liferay)
181
+ @config.container.version ||= Lportal::Schema.version
182
+ portal_info
183
+ end
161
184
  end
162
185
  end
163
186
 
@@ -182,11 +205,9 @@ module Caterpillar
182
205
  with_namespace_and_config do |name, config|
183
206
  desc 'Create JSR286 portlet XML'
184
207
  task :portlet do
185
- info 'Configured to use %s version %s' % [
186
- @config.container.class.to_s.sub('Caterpillar::',''), @config.container.version
187
- ]
208
+ portal_info
188
209
 
189
- system('touch %s' % file)
210
+ exit 1 unless system('touch %s' % file)
190
211
  f=File.open(file,'w')
191
212
  f.write Portlet.xml(@portlets)
192
213
  f.close
@@ -203,7 +224,7 @@ module Caterpillar
203
224
  with_namespace_and_config do |name, config|
204
225
  desc 'Create Liferay portlet XML'
205
226
  task :liferayportletapp do
206
- system('touch %s' % file)
227
+ exit 1 unless system('touch %s' % file)
207
228
  f=File.open(file,'w')
208
229
  f.write config.container.portletapp_xml(@portlets)
209
230
  f.close
@@ -217,12 +238,13 @@ module Caterpillar
217
238
  @name = :xml
218
239
  file = File.join('tmp','liferay-display.xml')
219
240
  @xml_files << file
220
- raise 'This version of Liferay is broken!' if @config.container.version == '5.1.2'
221
241
 
222
242
  with_namespace_and_config do |name, config|
223
243
  desc 'Create Liferay display XML'
224
244
  task :liferaydisplay do
225
- system('touch %s' % file)
245
+ raise 'Version 5.1.2 of Liferay is broken!' if config.container.version == '5.1.2'
246
+
247
+ exit 1 unless system('touch %s' % file)
226
248
  f=File.open(file,'w')
227
249
  f.write config.container.display_xml(@portlets)
228
250
  f.close
@@ -243,67 +265,116 @@ module Caterpillar
243
265
  end
244
266
 
245
267
 
246
- def define_migrate_task
268
+ ### MIGRATIONS AND JAR-INSTALL
269
+
270
+ def define_db_migrate_task
247
271
  @name = :db
248
272
  with_namespace_and_config do |name, config|
249
- desc "Migrates Caterpillar database tables"
273
+ desc "Migrates lportal and Caterpillar database tables"
250
274
  task :migrate => :environment do
251
- require 'active_record'
252
- ActiveRecord::Migrator.migrate(
253
- File.expand_path(
254
- File.dirname(__FILE__) + "/../../db/migrate"))
255
- STDOUT.puts 'Now, run rake db:schema:dump'
275
+
276
+ # first run lportal sequence migrations (TODO)
277
+ info('Running lportal migrations')
278
+ ActiveRecord::Migrator.migrate(LPORTAL_MIGRATIONS)
279
+
280
+ # info('running Caterpillar migrations')
281
+ # ActiveRecord::Migrator.migrate(
282
+ # File.expand_path(
283
+ # File.join(CATERPILLAR_LIBS,'..','db','migrate')))
284
+
256
285
  #Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
286
+ info 'You need to manually run rake db:schema:dump'
257
287
 
258
- @portlets = config.container.analyze(:native)
259
- @portlets.each do |portlet|
260
- Web::PortletName.create(
261
- :portletid => portlet[:id],
262
- :name => portlet[:name],
263
- :title => portlet[:title]
264
- )
265
- end
288
+ Rake::Task['db:update'].invoke
266
289
  end
267
290
  end
268
291
  end
269
292
 
270
- def define_rollback_task
293
+ def define_db_rollback_task
271
294
  @name = :db
272
295
  with_namespace_and_config do |name, config|
273
296
  desc "Wipes out Caterpillar database tables"
274
297
  task :rollback => :environment do
275
- require 'active_record'
298
+
276
299
  version = ENV['VERSION'].to_i || 0
277
- ActiveRecord::Migrator.migrate(
278
- File.expand_path(
279
- File.dirname(__FILE__) + "/../../db/migrate"), version)
280
- STDOUT.puts 'Now, run rake db:schema:dump'
300
+
301
+ info('Reverting lportal migrations')
302
+ ActiveRecord::Migrator.migrate(LPORTAL_MIGRATIONS, version)
303
+
304
+ # info('Reverting Caterpillar migrations')
305
+ # ActiveRecord::Migrator.migrate(
306
+ # File.expand_path(
307
+ # File.join(CATERPILLAR_LIBS,'..','db','migrate')), version)
308
+
281
309
  #Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
310
+ info 'You need to manually run rake db:schema:dump'
311
+ end
312
+ end
313
+ end
314
+
315
+ def define_db_update_task
316
+ @name = :db
317
+ with_namespace_and_config do |name, config|
318
+ desc 'Updates the portletproperties table'
319
+ task :update => :environment do
320
+
321
+ info 'analyzing portlet XML configuration'
322
+ @portlets = config.container.analyze(:native)
323
+
324
+ info 'updating database'
325
+ Web::PortletProperties.all.each(&:destroy)
326
+ @portlets.each do |portlet|
327
+ Web::PortletProperties.create(
328
+ :portletid => portlet[:id],
329
+ :name => portlet[:name],
330
+ :title => portlet[:title],
331
+ :instanceable => portlet[:instanceable]
332
+ )
333
+ end
282
334
  end
283
335
  end
284
336
  end
285
337
 
338
+ # Install the Rails-portlet JAR
286
339
  def define_jar_install_task
287
340
  @name = :jar
288
341
  with_namespace_and_config do |name, config|
289
342
  desc 'Installs Rails-portlet JAR into the portlet container'
290
- task :install do
291
- raise 'Only Liferay is supported' unless @config.container.kind_of? Liferay
292
- require 'find'
343
+ task :install => :environment do
344
+ source = File.join(CATERPILLAR_LIBS,'java')
345
+
346
+ unless @config.container.kind_of? Liferay
347
+ info 'Installation of the JAR is only supported on Liferay. Patches are welcome.'
348
+ info 'Copy the JAR from this directory into the CLASSPATH of the portlet container.'
349
+ info source
350
+ exit 1
351
+ end
352
+
353
+ container_v = @config.container.version
354
+
355
+ unless container_v
356
+ info 'Unable to detect the version of the portlet container. Installing the latest version.'
357
+ end
293
358
 
294
359
  version = (
295
- if @config.container.version[/^5.2/]
296
- '0.6.1'
360
+ if container_v and container_v[/^5.1/]
361
+ '0.6.0' #'0.5.2' # FIXME: branch properly
297
362
  else
298
- version = '0.5.2' # think of a way to branch properly
363
+ '0.6.1'
299
364
  end
300
365
  )
301
366
 
302
367
  portlet_jar = nil
303
368
  old_jar = nil
304
- source = File.join(CATERPILLAR_LIBS,'java')
305
369
  target = File.join(@config.container.WEB_INF,'lib')
306
370
 
371
+ # check that target exists
372
+ unless File.exists?(target)
373
+ info 'JAR directory %s does not exist' % target
374
+ exit 1
375
+ end
376
+
377
+ require 'find'
307
378
  Find.find(source) do |file|
308
379
  if File.basename(file) =~ /rails-portlet-#{version}/
309
380
  portlet_jar = file
@@ -325,10 +396,10 @@ module Caterpillar
325
396
  end
326
397
  end
327
398
 
328
- system('cp %s %s' % [portlet_jar,target])
399
+ exit 1 unless system('cp %s %s' % [portlet_jar,target])
329
400
  info 'installed Rails-portlet version %s to %s' % [version, target]
330
401
  if old_jar
331
- system('rm -f %s' % old_jar)
402
+ exit 1 unless system('rm -f %s' % old_jar)
332
403
  info '..removed the old version %s' % old_jar
333
404
  end
334
405
 
@@ -342,14 +413,20 @@ module Caterpillar
342
413
  desc 'Uninstalls Rails-portlet JAR from the portlet container'
343
414
  task :uninstall do
344
415
  raise 'Only Liferay is supported' unless @config.container.kind_of? Liferay
345
- require 'find'
346
416
  target = File.join(@config.container.WEB_INF,'lib')
347
417
 
418
+ # check that target exists
419
+ unless File.exists?(target)
420
+ info 'JAR directory %s does not exist' % target
421
+ exit 1
422
+ end
423
+
424
+ require 'find'
348
425
  Find.find(target) do |file|
349
426
  if File.basename(file) =~ /rails-portlet/
350
427
  version = file[/(\d.\d.\d).jar/,1]
351
428
  info 'Uninstalling Rails-portlet version %s from %s' % [version, target]
352
- system('rm -f %s' % file)
429
+ exit 1 unless system('rm -f %s' % file)
353
430
  exit 0
354
431
  end
355
432
  end
@@ -389,44 +466,41 @@ module Caterpillar
389
466
  def define_warble_task
390
467
  desc 'Create a WAR package with Warbler'
391
468
  task :warble do
392
- unless ENV['JRUBY_HOME']
393
- info ''
394
- info 'Environment variable JRUBY_HOME is not set, exiting -'
395
- info 'You should `export JRUBY_HOME="/usr/local/jruby"` and `sudo -E caterpillar %s`' % ARGV[0]
469
+ jruby_home = (ENV['JRUBY_HOME'] || @config.class::JRUBY_HOME)
470
+ unless jruby_home
471
+ info 'JRUBY_HOME is not set.'
472
+ info 'First preference is the environment variable JRUBY_HOME.'
473
+ info ' `export JRUBY_HOME="/usr/local/jruby"` and `sudo -E caterpillar %s`' % ARGV[0]
474
+ info 'Another option is to define portlet.class::JRUBY_HOME in %s.' % @config.class::FILE
396
475
  exit 1
397
476
  end
398
- jruby = File.join(ENV['JRUBY_HOME'],'bin','jruby')
477
+ jruby = File.join(jruby_home,'bin','jruby')
399
478
  unless File.exists?(jruby)
400
- info ''
401
479
  info 'JRuby executable was not found in %s, exiting' % jruby
402
480
  exit 1
403
481
  end
404
482
  begin
405
483
  require 'warbler'
406
484
  rescue
407
- info ''
408
485
  info 'Warbler module was not found, exiting. Install Warbler for JRuby.'
409
486
  exit 1
410
487
  end
411
488
  unless File.exists?(@config.warbler_conf)
412
- info ''
413
489
  info 'Warbler configuration file %s was not found, exiting' % @config.warbler_conf
414
490
  exit 1
415
491
  end
492
+ info 'Building WAR using Warbler %s on JRuby %i (%s)' % [
493
+ Warbler::VERSION, JRUBY_VERSION, jruby]
416
494
  info ''
417
- info 'Building WAR using Warbler %s on JRuby (%s)' % [
418
- Warbler::VERSION, jruby]
419
- info ''
420
- system(jruby+' -S warble war')
495
+ exit 1 unless system(jruby+' -S warble war')
496
+ info 'Warbler finished successfully'
421
497
  end
422
498
  end
423
499
 
424
500
  def define_deploy_task
425
501
  desc 'Deploy XML files and the application WAR to the portlet container'
426
- #info 'Deploying XML files and the application WAR'
427
502
 
428
- raise 'Only deployment to Liferay on Tomcat is supported' unless @config.container.kind_of? Liferay
429
- tasks = [:xml, :warble, 'deploy:xml', 'deploy:war']
503
+ tasks = ['db:update', :xml, :warble, 'deploy:xml', 'deploy:war']
430
504
  task :deploy => tasks
431
505
  end
432
506
 
@@ -435,11 +509,12 @@ module Caterpillar
435
509
  with_namespace_and_config do |name, config|
436
510
  desc 'Deploys the XML files'
437
511
  task :xml do
512
+ raise 'Only deployment to Liferay on Tomcat is supported' unless @config.container.kind_of? Liferay
438
513
  target = @config.container.WEB_INF
439
514
  info 'deploying XML files to %s' % target
440
515
 
441
516
  @xml_files.each do |file|
442
- system('cp %s %s' % [file,target])
517
+ exit 1 unless system('cp %s %s' % [file,target])
443
518
  info ' %s' % [file]
444
519
  end
445
520
  end
@@ -450,6 +525,7 @@ module Caterpillar
450
525
  with_namespace_and_config do |name, config|
451
526
  desc 'Deploys the WAR file'
452
527
  task :war do
528
+ raise 'Only deployment to Liferay on Tomcat is supported' unless @config.container.kind_of? Liferay
453
529
  file = @config.servlet+'.war'
454
530
  unless File.exists?(file)
455
531
  info 'cannot find the WAR file %s, exiting' % file
@@ -459,10 +535,10 @@ module Caterpillar
459
535
  target = File.join(@config.container.root,'webapps')
460
536
 
461
537
  info '..removing previous installs..'
462
- system('rm -rf %s' % File.join(target,@config.servlet+'*'))
538
+ exit 1 unless system('rm -rf %s' % File.join(target,@config.servlet+'*'))
463
539
 
464
540
  info 'deploying the WAR package to %s' % target
465
- system('cp %s %s' % [file,target])
541
+ exit 1 unless system('cp %s %s' % [file,target])
466
542
 
467
543
  end
468
544
  end
@@ -502,7 +578,7 @@ module Caterpillar
502
578
 
503
579
  #field = :path
504
580
  #fields = [:name, :id]
505
- STDOUT.puts "\t" + portlet[:title] +spaces+ portlet[:id].inspect + "\t" + portlet[:name].inspect
581
+ STDOUT.puts "\t" + portlet[:title] +spaces+ portlet[:name].inspect + "\t" + portlet[:vars].inspect
506
582
  end
507
583
  end
508
584
  end
@@ -511,6 +587,13 @@ module Caterpillar
511
587
  STDOUT.puts ' * ' + msg
512
588
  end
513
589
 
590
+ def portal_info(config=@config)
591
+ msg = 'Caterpillar configured for %s version %s at %s' % [
592
+ config.container.name, config.container.version, config.container.root
593
+ ]
594
+ @logger ? @logger.info(msg) : STDOUT.puts(msg)
595
+ end
596
+
514
597
 
515
598
 
516
599
  end