forked from vanntastic/config_files
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathirbrc
executable file
·1435 lines (1265 loc) · 53.9 KB
/
irbrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#load gems and wirble
require 'rubygems'
require 'irb/completion'
require 'logger'
# this is kind of stupid ... you have to gem the wirble gem and then send a require
gem 'wirble'
require 'wirble'
gem 'map_by_method'
#load wirble
Wirble.init
Wirble.colorize
IRB.conf[:AUTO_INDENT]=true
#list of editors for use with editing and opening files, you can re-arrange as to set precedence of what to use
EDITORS = %w{mate jedit} unless Object.const_defined?(:EDITORS)
#inline console logging
@script_console_running = ENV.include?('RAILS_ENV') && IRB.conf[:LOAD_MODULES] && IRB.conf[:LOAD_MODULES].include?('console_with_helpers')
@rails_running = ENV.include?('RAILS_ENV') && !(IRB.conf[:LOAD_MODULES] && IRB.conf[:LOAD_MODULES].include?('console_with_helpers'))
@irb_standalone_running = !@script_console_running && !@rails_running
if @script_console_running && !Object.constants.include?("RAILS_DEFAULT_LOGGER")
Object.const_set(:RAILS_DEFAULT_LOGGER, Logger.new(STDOUT))
# require 'active_record'
# require 'active_record/fixtures'
end
# reloads the irb console can be useful for debugging .irbrc
def reload_irb
load File.expand_path("~/.irbrc")
# will reload rails env if you are running ./script/console
reload! if @script_console_running
puts "Console Reloaded!"
end
# opens irbrc in textmate
def edit_irb
`mate ~/.irbrc` if system("mate")
end
# alias for quit
def q; quit; end
#shortcut for y model.column_names
# defaults to piping out to less
# if you need the standard output do :
# - show :model, :standard
def show(obj=nil,return_type=:less)
if @irb_standalone_running
puts "Sorry, this must be used inside script/console"
else
# yes, you can say show :people instead of show Person, no need for the shift key :)
usage = "USAGE: outputs column names \n alias for: 'y Model.column_names' \n syntax: show Model"
obj = eval obj.to_s.singularize.camelize if obj.is_a? Symbol
obj.nil? ? puts(usage) : less(obj.schema(return_type))
end
end
#shortcut for listing helpers in yaml format
def list_helpers
@irb_standalone_running ? puts("Sorry, This is only valid in script/console") : y(helper.methods.sort)
end
# determines what editors are available and will open a file in that editor
def open_file_for(file)
`mate #{file}` if system("mate")
`gedit #{file}` if system("gedit")
end
# hook for the system's less command allows you to pass in arrays too!
# EX :
# less "A bunch of content"
# less %w(1 2 3)
# less [1,2,3]
def less(content)
content = content.join("\n") if content.is_a?(Array)
system('echo "' << content << '"|less')
end
# script/console hooks
if @script_console_running
# hook into script/find
# needs git@github.com:vanntastic/project_search.git
def find(term=nil)
msg = "USAGE : find term (hooks into script/find)"
term.nil? ? puts(msg) : system("./script/find #{term}")
end
# compiles all the css files into one huge string
# this is useful when running with less
# USAGE : all_css.open # => opens the string in less allowing you to search
def all_css
css_files = Dir.glob File.join(RAILS_ROOT, "public/stylesheets/","*.css")
css_content = ""
css_files.each {|file| css_content << IO.read(file)}
css_content.instance_eval do
# opens with less command
def open
less self
# system('echo "' << self << '"|less')
end
end
css_content
end
# == ArHelper Stuff
# check to see if the ar_helper plugin is installed
# doesn't work with the new version of ArHelper
# TODO : might need to adjust this for Rails >= 2.3
# def ar_helper_exists?; File.exists?("vendor/plugins/ar_helper"); end
#
# def require_and_load_ar_helper
# require File.dirname(__FILE__) + '/../vendor/plugins/ar_helper/lib/ar_helper'
# # require "vendor/plugins/ar_helper/lib/ar_helper"
# extend ArHelper::Sugar if ArHelper.constants.include?("Sugar")
# include ArHelper::Sugar if ArHelper.constants.include?("Sugar")
# puts "ArHelper::Sugar methods not loaded ... install ArHelper plugin
# externally..." unless ArHelper.constants.include?("Sugar")
# end
#
# if defined? RAILS_GEM_VERSION
# require_and_load_ar_helper if ar_helper_exists?
# end
# == ArHelper Stuff
#show schema version
def db_version(just_version=false)
just_version ? ActiveRecord::Migrator.current_version.to_s : puts("Current version: " + ActiveRecord::Migrator.current_version.to_s)
end
# standard vi hook with some rails specific helpers
# USAGE:
# - vi :application, :controller # => opens app/controllers/application.rb
# - vi :user, :model # => opens app/models/user.rb
# - vi :default, :css # => opens public/stylesheets/default
# - vi :application, :helper # => opens app/application_helper
# - vi :application, :js # => opens public/javasripts/application.js
# - vi "admin/index.rhtml", :view # => opens app/views/admin/index.rhtml
# - vi "db/schema.rb" # => opens db/schema.rb
# - vi :users, :unit # => opens test/unit/user_test.rb
# - vi :users, :functional # => opens test/functional/users_controller_test.rb
# - vi :users, :fixture # => opens test/fixtures/users.yml
# you can also pass in the type argument with a dynamic file name
# EXAMPLE:
# - vi :application_controller # => vi :application, :controller
# - vi :users_fixture # => vi :users, :fixture
# TODO : give this the option of specifying a dir name...
# - vi :create_rjs # => it will look for the first create rjs file
# - vi :index_view # => it will look for the first index view file
def vi(file=nil, type=:any)
usage = "USAGE: vi [name_of_file], [:type]"
return usage if file.nil?
types_to_look_for = %w(controller helper model js rjs css view unit
functional fixture config layout mailer)
# determine whether or not we can dissect the file parameter
if types_to_look_for.include?(file.to_s.split("_").last)
file_ary = file.to_s.split("_")
type = file_ary.delete(file_ary.last).to_sym
file = file_ary.join("_")
end
file_path = vi_file_path_for(type, file)
file.nil? ? puts(usage) : system("vi #{file_path}")
end
# dependent on the vi method
def vi_file_path_for(type,file)
case type.to_sym
when :controller
file = "#{file.to_s}_controller"
file_path = File.join(RAILS_ROOT, "app/controllers", "#{file.to_s}.rb")
when :helper
# let's not exclude the test_helper now
if file.to_s == "test"
file_path = File.join(RAILS_ROOT, "test/test_helper.rb")
else
file_path = File.join(RAILS_ROOT, "app/helpers", "#{file.to_s}_helper.rb")
end
when :model
file = file.to_s.singularize
file_path = File.join(RAILS_ROOT, "app/models", "#{file.to_s}.rb")
when :js
file_path = File.join(RAILS_ROOT, "public/javascripts", "#{file.to_s}.js")
when :rjs
file_ary = Dir.glob(File.join("app/views","**","#{file}.rjs"))
file_path = File.join(RAILS_ROOT, file_ary.first)
when :css
file_path = File.join(RAILS_ROOT, "public/stylesheets", "#{file.to_s}.css")
when :view
file_ary = Dir.glob(File.join("app/views","**","#{file}.rhtml"))
file_path = File.join(RAILS_ROOT, "app/views/#{file_ary.first}")
when :unit
file_path = File.join(RAILS_ROOT, "test/unit/#{file.to_s}_test.rb")
when :functional
file_path = File.join(RAILS_ROOT, "test/functional/#{file.to_s}_controller_test.rb")
when :fixture
file_path = File.join(RAILS_ROOT, "test/fixtures/#{file.to_s}.yml")
when :config
file_path = File.join(RAILS_ROOT, "config/#{file.to_s}.rb")
when :layout
file_path = File.join(RAILS_ROOT, "app/views/layouts/#{file.to_s}.rhtml")
file_path = File.join(RAILS_ROOT,
"app/views/layouts/#{file.to_s}.html.erb") if !File.exists?(file_path)
when :mailer
file_path = File.join(RAILS_ROOT, "app/models/#{file.to_s}_mailer.rb")
else
file_path = File.join(RAILS_ROOT, file)
end
return file_path
end
# vi_css will open up a css file with vi command which will
# allow you to easily browse the stylesheet
# EXAMPLE : vi_css :default # => opens the stylesheet for default.css
def vi_css(css_file=nil)
# css_file.nil? ? p("USAGE : css_for :css_filename") : system("less #{RAILS_ROOT}/public/stylesheets/#{css_file.to_s}.css")
css_file.nil? ? p("USAGE : vi_css :css_filename") : system("vi #{RAILS_ROOT}/public/stylesheets/#{css_file.to_s}.css")
end
# vi_js will open up a js file with the vi command which will
# allow you to easily browse the javascript file
# EXAMPLE : vi_js :default # => opens the stylesheet for default.css
def vi_js(js_file=nil)
# js_file.nil? ? p("USAGE : js_for :js_filename") : system("less #{RAILS_ROOT}/public/javascripts/#{js_file.to_s}.js")
js_file.nil? ? p("USAGE : vi_js :js_filename") : system("vi #{RAILS_ROOT}/public/javascripts/#{js_file.to_s}.js")
end
# Run a simple sql query when you need it
# http://ozmm.org/posts/railsrc.html
def sql(query)
ActiveRecord::Base.connection.select_all(query)
end
# hooks into the Object#fixture and helper methods
# USAGE :
# say we want to operate on a User model/domain:
# - user # => shows the schema
# - users # => shows the fixture keys
# - users :keys # => gets the fixture keys
# - user_keys # => gets the fixture keys
# - users :vann # => gets the Fixture key as an AR Object
# - user_fixtures # => prints the fixtures for users
# - user_f # => prints the fixtures for users
# - user_fixtures :vann # => prints the fixtures key for vann
# - user_model # => pulls up the user model in vi
# - user_m # => alias for user_model
# - users_controller # => pulls up the users controller in vi
# - users_c # => alias for users_controller
# inspired by : http://ozmm.org/posts/railsrc.html
def method_missing(method, *args, &block)
# check to see if you are passing a call to the fixture method
method_array = method.to_s.split("_")
available_model_calls = %w(model m)
available_controller_calls = %w(controller c)
available_fixture_calls = %w(fixtures fixture f)
# all standard extensions that you can use to hook vi calls into
available_vi_calls = %w(js css helper config
func functional unit layout mailer)
available_spec_calls = %(spec)
if available_fixture_calls.include?(method_array.last)
# sanitizes the array of the 'fixture' || 'f' strings this allows you to pass things
# like user_log_fixture or history_log_fixture or super_cool_log_table_fixture
method_array.delete_if {|m| (m =~ /fixture/).is_a? Fixnum}
method_array.delete_if {|m| (m =~ /f/).is_a? Fixnum}
model = eval(method_array.join("_").singularize.camelize)
model.send(:fixture, *args)
elsif method_array.include?("keys")
# opens the fixture keys for said model
method_array.delete_if {|m| (m =~ /keys/).is_a? Fixnum}
model = eval(method_array.join("_").singularize.camelize)
model.send(:keys, *args)
elsif available_vi_calls.include?(method_array.last)
vi(method.to_sym)
elsif available_model_calls.include?(method_array.last)
# pulls up said model in vi
method_array.delete_if {|m| available_model_calls.include?(m) }
model_name = method_array.join("_")
vi_call = "#{model_name}_model".to_sym
vi(vi_call)
elsif available_controller_calls.include?(method_array.last)
# pulls up said controller in vi
method_array.delete_if {|m| available_controller_calls.include?(m) }
controller_name = method_array.join("_")
vi_call = "#{controller_name}_controller".to_sym
vi(vi_call)
elsif available_spec_calls.include?(method_array.last)
method_array.pop # => removes 'spec'
spec_type = method_array.last
method_array.pop # => removes the 'spec type'
spec_name = method_array.join("_")
spec(spec_type.to_sym, spec_name.to_sym, *args)
else
# convention over configuration at it's finest
# user # => shows the user model and relationships
# users # => shows the keys in the users fixtures
# users(:vann) # => finds vann for you and returns User for quick testing similar to what you get in Test::Unit
model_to_eval = method.to_s
singular_model = method.to_s.singularize
pluralar_model = method.to_s.pluralize
model = eval(singular_model.camelize)
if model_to_eval == pluralar_model
args << :keys if args.blank?
end
# you can pass user :keys # => User.keys (fixture keys) or
# you can pass user :vann # => User.f(:vann) # => pulls the fixture into a model
args.blank? ? show(model) : (args.include?(:keys) ? model.send(:keys) : model.send(:f,*args))
end
# TODO : have this delegate to the real error perhaps have it eval model? lowercase... learn how to pass in the argument...
rescue NameError => trace
raise NameError, "The method/variable that you typed doesn't exist\n" + trace
end
# system hook into cheat piped through less
def cheat(arg="")
system("cheat #{arg.to_s} | less")
end
# test/spec hooks
def spec(type=nil,file=nil,test_name=nil)
msg = "spec is a hook that runs your test/spec tests\n"
msg << "==\n"
msg << "spec [:unit,:func||:functional,:all,:plugin], [:file], [:name_of_spec]"
spec_types = [:unit,:func,:functional,:all,:plugin, :controller]
if ( !spec_types.include?(type) || (type.nil? && file.nil?))
puts msg
else
status = "Running specs for #{file.to_s} [#{type.to_s}]"
status << ": on '#{test_name.to_s}'" unless test_name.nil?
status << "... please wait..."
puts status
type = :func if (type == :functional || type == :controller)
case type
when :unit
test_cmd = "specrb -as test/unit/#{file.to_s}_test.rb"
when :func
test_cmd = "specrb -as test/functional/#{file.to_s}_controller_test.rb"
when :plugin
path = File.join RAILS_ROOT,
"vendor/plugins/#{file.to_s}/test/#{file.to_s}_test.rb"
test_cmd = "specrb -as #{path}"
when :all
system("rake spec")
end
# runs said spec commands and appends a name if given
unless type == :all
test_cmd << " -n '#{test_name}'" unless test_name.nil?
test_cmd ='kicker --no-growl -e "' << test_cmd << '" .' if system("which kicker")
system(test_cmd)
end
end
end
# allows you to quickly view your specs...
def specs_for(type=nil,file=nil)
msg = "specs_for is a hook that views your test/spec specs\n"
msg << "==\n"
msg << "specs_for [:unit,:func||:functional], :file"
types_to_look_for = [:unit,:func,:functional]
if ( !types_to_look_for.include?(type) || (type.nil? && file.nil?))
puts msg
else
case type
when :unit
title = `cat test/unit/#{file.to_s}_test.rb | grep context`.split('"')[1]
specs = [title,"==="]
spec_string = `cat test/unit/#{file.to_s}_test.rb | grep 'specify'`.split("\n")
spec_string.each do |spec|
specs << spec.split('"')[1]
end
y specs
when (:func || :functional)
title = `cat test/functional/#{file.to_s}_controller_test.rb | grep context`.split('"')[1]
specs = [title,"==="]
spec_string = `cat test/functional/#{file.to_s}_controller_test.rb | grep 'specify'`.split("\n")
spec_string.each do |spec|
specs << spec.split('"')[1]
end
y specs
end
end
end
#hook for tail -f into the log dir...
def trail(log=nil)
if (log.nil? || ![:test,:production,:development].include?(log))
puts "trail [:test|:production|:development]"
else
system("tail -f log/#{log.to_s}.log")
end
end
# *** HELPERS ***
#displays a list of javascripts
def javascripts
list = File.expand_path("public/javascripts")
list_ary = Dir.entries(list).select{|x| x[0..0] != "."}
list_ary.map!{|f| f.split(".")[0]}
list_ary << ["===", "#{list_ary.length} Javascripts"]
y list_ary.flatten!
end
#displays a list of all the stylesheets
def stylesheets
sheets = File.expand_path("public/stylesheets")
sheets_ary = Dir.entries(sheets).select{|x| x[0..0] != "."}
sheets_ary.map!{|f| f.split(".")[0]}
footer = "#{sheets_ary.length} Stylesheets"
sheets_ary << ["="*footer.length,footer ]
y sheets_ary.flatten!
end
alias css stylesheets
#displays a list of files for any directory
def ls(dir=nil)
return "USAGE : display [directory]" if dir.nil?
files = File.expand_path dir.to_s
files_ary = Dir.entries(files).select{|x| x[0..0] != "."}
footer = "#{files_ary.length} Files"
files_ary << ["="*footer.length,footer]
y files_ary.flatten!
end
#displays the migrations in the app
def migrations
files = File.expand_path "db/migrate"
files_ary = Dir.entries(files).select{|x| x[0..0] != "."}
migrations = files_ary.map do |i|
migration = i.humanize.split(".").first
if migration.split(" ").first.length == 14
migration_ary = migration.split(" ")
date, version = migration_ary.first.to_date, migration_ary.first
migration_ary.delete migration_ary.first
migrate_str = "[#{version}] : #{migration_ary.join(' ')} : #{date}"
version == db_version(true) ? migrate_str << " [X]" : migrate_str
else
migration
end
end
footer = "#{files_ary.length} Migrations"
footer_border = "="*migrations.last.length
migrations << [footer_border,footer]
y migrations.flatten!
end
# display a list of all the plugins
# pass in :name => plugin_name to view the README for that plugin
def plugins(name=nil,opts=nil)
if name.nil?
list = File.expand_path("vendor/plugins")
list_ary = Dir.entries(list).select{|x| x[0..0] != "."}
footer = "="*list_ary.last.length
list_ary << [footer, "#{list_ary.length} Plugins"]
y list_ary.flatten!
else
# these are special cmds that you can pass instead of the name of the plugin
cmds = %w(update help st)
if cmds.include?(name.to_s.downcase)
case name.to_s.downcase
when "update"
invalid_types = %w(. .. .DS_Store .svn)
Dir.foreach("vendor/plugins") do |d|
unless invalid_types.include?(d)
# make sure we account for git and svn repos
update_path = File.join("vendor/plugins",d)
uses_git = File.exists?(File.join(update_path,".git"))
uses_svn = File.exists?(File.join(update_path, ".svn"))
not_versioned = !uses_git && !uses_svn
update_path = File.join("vendor/plugins",d)
puts "UPDATING : #{d}"
# make sure we account for git and svn repos
system("cd #{update_path};git pull") if uses_git
svn.up(update_path) if uses_svn
puts "#{d} not versioned" if not_versioned
end
end
when "st"
invalid_types = %w(. .. .DS_Store)
Dir.foreach("vendor/plugins") do |d|
unless invalid_types.include?(d)
# make sure we account for git and svn repos
update_path = File.join("vendor/plugins",d)
uses_git = File.exists?(File.join(update_path,".git"))
uses_svn = File.exists?(File.join(update_path, ".svn"))
not_versioned = !uses_git && !uses_svn
repo_type = "[svn]" if uses_svn
if uses_git
git_config = File.join(update_path,".git","config")
git_config = `cat #{git_config}`
git_config = git_config.split("\n\t").select{ |c| c =~ /url/ }
repos = git_config.join(", ")
repo_type = "[git] \nREMOTES : #{repos}"
end
repo_type = "[not versioned]" if not_versioned
puts "STATUS OF : #{d} #{repo_type}"
system("cd #{update_path};git st") if uses_git
svn.st(update_path) if uses_svn
puts "#{d} not versioned" if not_versioned
footer = "*" * 60
puts "#{footer}\n\n"
end
end
when "help"
help = "plugins [name_of_plugin|cmd]\n"
help << "==\n"
help << "OPTIONS:\n"
help << "- help : display this help file\n"
help << "- update : update all plugins using svn:externals or git\n"
help << "- st : gets the repo status of plugins\n"
puts help
end
else
if opts.nil?
# support for different types of readmes
readme_types = %w(README README.rdoc README.markdown README.textile README.txt)
readme_ary = readme_types.map { |r| File.join("vendor/plugins/#{name}/", r) }
cmd = nil
readme_ary.each do |path|
cmd = system("vi #{path}") if File.exists?(path)
end
cmd.nil? ? puts("README for #{name.to_s} doesn't exist...") : cmd
else
# TODO write in support to include options to do the following:
# 1. ci - git/svn checkin
# 2. diff - git/svn diff
# 3. spec/test - options to test plugins
end
end
end
rescue Errno::ENOENT
puts "Plugin : '#{name}' doesn't exist...try one below"
plugins
end
#displays a list of all the models
#call with_attributes if you want to see all associated attributes
def models(with_attributes=nil)
list = File.expand_path("app/models")
list_ary = Dir.entries(list).select{|x| x[0..0] != "."}
list_ary.map!{|f| f.split(".")[0].camelize}
list_ary.map!{|l| [eval(l).schema] unless eval(l).nil?} unless with_attributes.nil?
footer = "="*list_ary.last.length
list_ary << [footer,"#{list_ary.length} Models"]
y list_ary.flatten!
end
# displays all the mailer files
def mailers
list = Dir.glob File.join("app/models","*_mailer.rb")
if list.length == 0
return puts "There are no Mailers"
else
list.map!{ |mailer| mailer.split("/").last }
list.map!{ |mailer| mailer.split(".").first }
list.map!{ |mailer| mailer.camelize }
list << [("="*list.last.length),"#{list.length} Mailers"]
y list.flatten!
end
end
#displays a list of controllers
# pass in the controller option to open a controller file in vi
def controllers(controller=nil)
return vi("#{controller}_controller".to_sym) unless controller.nil?
list = File.expand_path("app/controllers")
list_ary = Dir.glob File.join(list, "*.rb")
list_ary.map!{|f| f.split("/").last.split(".").first.camelize}
summary = "#{list_ary.length} Controllers"
list_ary << ["="*summary.length, summary]
y list_ary.flatten!
end
# converts all rhtml files in app/view to html.erb
def erb_convert
files = `find app/views -name '*.rhtml'`
file_ary = files.split("\n")
file_ary.each do |file|
new_file = file.gsub("rhtml", "html.erb")
FileUtils.mv file, new_file
end
puts "#{file_ary.length} files converted."
puts "Don't forget to update your scm with the new files!"
end
# reads the methods for the file
# you can pass a type of file to have it read a specific file
# EX :
# methods_for :users_controller # => app/controllers/users_controller.rb
# methods_for :user_model # => app/models/user.rb
# methods_for :users_helper # => app/helpers/users_helper.rb
# methods_for :users_functional # => test/functional/users_controller_test.rb
# methods_for :user_unit # => test/unit/user_test.rb
# methods_for :routes_config # => config/routes.rb
# methods_for 'my/special/file.rb' # => my/special/file.rb
def methods_for(file=nil, return_ary=false)
unless file.nil?
types_to_look_for = ["controller",
"helper",
"model",
"unit",
"functional",
"config"]
name, type = file.to_s.split("_")
path = types_to_look_for.include?(type) ? vi_file_path_for(type, name) : file
parsed_file = IO.read(path)
methods_ary = parsed_file.grep(/def/)
methods_ary.map! {|action| action = action.split(" ")[1]}
methods_ary.compact!
methods_ary << ["===", "#{methods_ary.length} Methods for #{file}"]
return_ary ? methods_ary : y(methods_ary.flatten!)
else
usage = "USAGE : methods_for [file(_controller|helper|model|unit|functional|config)]"
puts usage
end
end
#display views for a controller
#pass return_ary to return the actions as an array
def views_for(controller=nil, return_ary=false)
unless controller.nil?
list = File.expand_path("app/views/#{controller}")
# we don't need the . entry or anything that might be considered a partial
list_ary = Dir.entries(list).select{|x| ![".","_"].include? x[0..0] }
list_ary.map! { |l| l.split(".")[0] }
list_ary << ["===", "#{list_ary.length} Views for #{controller}"]
return_ary ? list_ary : y(list_ary.flatten!)
else
puts "USAGE : views_for [name_of_controller]"
end
end
# generates a new view/rjs file for a controller
def new_view_for(controller=nil,view=nil,type="html.erb")
return "USAGE : new_view_for [controller] [name_of_view] [**file_type]" if controller.nil? || view.nil?
view_path = "app/views/#{controller}/#{view}.#{type}"
File.open(view_path,"w+")
EDITORS.each do |editor|
`#{editor} #{view_path}` if system("#{editor}")
break if system("#{editor}")
end
return "View File : #{view}.#{type} has been created!"
end
#generates a new view folder
def new_view_folder(name=nil)
return "USAGE : new_view_folder [name]" if name.nil?
dir_path = File.expand_path("app/views/#{name}")
existing_dir = File.exists? dir_path
Dir.mkdir("app/views/#{name}") unless existing_dir
msg = existing_dir ? "Folder : #{name} already exists" : "Folder : #{name} has been created successfully!"
return msg
end
# generates a new partial and opens in in textmate
def new_partial_for(controller=nil,name=nil)
return "USAGE : new_partial_for [controller] [name]" if (controller.nil? || name.nil?)
controller, name = controller.to_s, name.to_s
dir_path = File.expand_path("app/views/#{controller}")
Dir.mkdir("app/views/#{controller}") unless File.exists? dir_path
partial = File.join(dir_path,"_#{name}.html.erb")
File.open(partial,"w+")
system("mate #{partial}")
return "Partial : #{partial} has been created!"
end
# generates a new layout for the app
def new_layout_for(name=nil,type="html.erb")
return "USAGE : new_layout_for [name_of_layout]" if name.nil?
layout_path = "app/views/layouts/#{name}.#{type}"
File.open(layout_path,"w+")
# TM only function sorry win guys! ... maybe you can use e
`mate #{layout_path}` if system("mate")
`gedit #{layout_path}` if system("gedit")
return "Layout File : #{name}.#{type} has been created!"
end
# generates a new js file for the app
def new_js_for(name=nil)
return "USAGE : new_js_for [name_of_js]" if name.nil?
js_path = "public/javascripts/#{name}.js"
File.open(js_path,"w+")
# TM only function sorry win guys! ... maybe you can use e
# system("mate #{js_path}")
open_file_for js_path
return "Javascript File : #{name}.js has been created!"
end
def new_rjs_for(controller=nil, name=nil, type="rjs")
no_args = controller.nil? && name.nil?
return "USAGE : new_rjs_for [controller] [rjs_filename]" if no_args
return "You forgot the :name of the rjs file" if name.nil?
return "You forgot the name of the controller" if controller.nil?
rjs_path = "app/views/#{controller.to_s}/#{name.to_s}.#{type.to_s}"
File.open(rjs_path,"w+")
open_file_for rjs_path
return "RJS File : #{name.to_s}.#{type.to_s} has been created!"
end
# generates a new css file for the app
def new_css_for(name=nil)
return "USAGE : new_css_for [name_of_css]" if name.nil?
css_path = "public/stylesheets/#{name}.css"
File.open(css_path,"w+")
# TM only function sorry win guys! ... maybe you can use e
system("mate #{css_path}")
return "CSS File : #{name}.css has been created!"
end
# generates a spec yaml file
def new_spec_for(type,name)
usage = "USAGE : new_spec_for [:model|controller], [name_of_spec]"
return usage if type.nil? || ["model","controller"].include?(type.to_s)
if type.to_s == "model"
spec_path = "test/unit"
elsif type.to_s == "controller"
spec_path = "test/functional"
end
spec_file_path = "#{spec_path}/#{name}_spec.yml"
File.open(spec_file_path, "w+")
system("mate #{spec_file_path}")
return "Spec File : #{spec_file_path} has been created!"
end
#displays the number of layouts for the app
def layouts
files = Dir.glob("app/views/layouts/*.rhtml")
files << Dir.glob("app/views/layouts/*.html.erb")
files.flatten!.map! { |f| f.split("/").last.split(".").first }
files << ["==","#{files.length} Layouts"]
y files.flatten
end
#display partials for an action
def partials_for(action=nil)
unless action.nil?
list = File.expand_path("app/views/#{action}")
# we don't need the . entry or anything that might be considered a partial
list_ary = Dir.entries(list).select{|x| ["_"].include? x.first }
list_ary.map! { |l| l.split(".")[0].sub("_","")}
footer = "#{list_ary.length} Partials for #{action}"
list_ary << ["="*footer.length,footer ]
y list_ary.flatten!
else
puts "USAGE : partials_for [name_of_action]"
end
end
# *** CONSOLE HOOKS ***
# console hook into the ./script/generate command
# EX :
# generate :model, "User"
# generate :migration, "add avatar to user"
# generate :controller, "users index new add edit show destroy"
def generate(type=nil, params=nil)
if type.nil?
return "USAGE : generate [:controller|:model|:migration|:plugin] [params]"
else
types = %w(controller model migration plugin mailer)
# camelize the params, if we are generating a migration, this way we can
# word migrations in plain english
# EX : generate :migration, "add some special sauce to my model"
params.gsub!(/[^a-z0-9]+/i, '_') if type.to_sym == :migration
return types.include?(type.to_s) ? system("./script/generate #{type.to_s} #{params}") : puts("#{type} is not a valid generator")
end
end
# console hook for sake tasks
# sake doesn't work right now 2007-09-21
# def sake(task=nil)
# if task.nil?
# return "USAGE : sake [task name]"
# else
# return system("sake #{task}")
# end
# end
# console hook for ./script/destroy commands
def destroy(type=nil, params=nil)
if type.nil?
return "USAGE : destroy [controller|model|migration] [params]"
else
types = %w(controller model migration)
params.gsub!(/[^a-z0-9]+/i, '_') if type == :migration
return types.include?(type.to_s) ? system("./script/destroy #{type.to_s} #{params}") : puts("#{type} is not a valid generator")
end
end
# console hook for ./script/plugin commands
def plugin(cmd=nil,params=nil)
if cmd.nil?
return "USAGE : plugin [install|list|discover|sources|source|remove|new|test]"
else
types = %w(install list discover sources source update remove new test)
case cmd.to_sym
when :install
if params.split(".").last == "git"
if File.exists?(".git")
# make sure that add this as a submodule
repo = params.split("/").last.split(".").first
puts "Installing plugin : #{params} as a git submodule..."
return system("git submodule add #{params} vendor/plugins/#{repo}")
else
# I prefer to clone git repositories...
puts "Cloning plugin : #{params}"
return system("cd vendor/plugins;git clone #{params}")
end
else
puts "Traditional install for plugin : #{params}"
return system("./script/plugin #{cmd.to_s} #{params}")
end
when :new
return generate(:plugin, params)
when :test
return puts("please supply a plugin to test") if params.nil?
# you can pass the name of the plugin as the param or you can pass the full path
# it assumes the the path of the plugin test is:
# vendor/plugins/[plugin_name]/test/[plugin_name]_test.rb
if params.to_s.split("/").length == 1
return system "ruby vendor/plugins/#{params.to_s}/test/#{params.to_s}_test.rb"
elsif params.split("/").length > 1
return system "ruby #{params}"
end
else
if types.include?(cmd.to_s)
return system("./script/plugin #{cmd.to_s} #{params}")
else
return puts("#{cmd} is not a valid plugin command")
end
end
end
end
# git hook
def git(*args)
if args.length == 1
# EX : git :st
# git "push origin master"
cmd = "git #{args.first.to_s}"
elsif args.length > 1
# EX : git :st, :origin, :master
# git :push, :origin
cmd = args.first.to_s
args.delete_at(0)
arg = args.join(" ")
cmd = "git #{cmd} #{arg}"
elsif args.blank?
cmd = "git [*cmd] or git.[st|ci|push|config|merge|co|b|new_branch]"
end
cmd.instance_eval do
# git.st
def st
system("git st")
end
# git.co
def co(branch)
system("git co #{branch}")
end
# creates a new branch and switches it
def new_branch(name=nil)
return "git.new_branch [name of new branch]" if name.nil?
co "-b #{name}"
end
# alias to get current branch as a string
def b
b = `git branch`.split("\n").delete_if { |i| i.first != "*" }
b.first.gsub("* ","")
end
# alias for :
# git co master
# git pull . [branch]
# git co [branch]
# branch defaults to the current branch, so you can simply say:
# git.merge # => merges the current branch with master
def merge_master(branch=nil)
branch ||= b
puts "Merging #{branch} with master..."
co "master"
system("git pull . #{branch}")
co branch
end
# git.ci 'msg'
def ci(msg=nil)
cmd = msg.nil? ? "git add .;git commit" : "git add .;git commit -m '#{msg}'"
system cmd
end
# git.push 'args'
# it pushes the master branch || current branch by default
def push(args=nil)
current_branch = b == "master" ? "" : "origin #{b}"
puts("Pushing branch : #{b}...") if b != "master"
cmd = args.nil? ? system("git push #{current_branch}") : system("git push #{args}")
end
# git.url
def url
system 'cat .git/config | grep url'
end
# git.config
def config
system 'vi .git/config'
end
# git.ignore
def ignore
system 'vi .gitignore'
end
# git.modules
def modules
system 'less .gitmodules'
end
def show(rev=nil)
rev.nil? ? system("git show") : system("git show #{rev}")
end
# git.log
# will use tig if it's available...
def log(path=nil)
return system("tig") if system("tig")
path.nil? ? system('git log') : system("git log #{path}")
end
# removes everything that has been removed from the filesystem
def rm
system "git st | grep deleted | sed -e 's/deleted: *//' | sed 's/# *//' | xargs git rm"
end
def diff
system("git diff")
end
end
args.blank? ? cmd : system(cmd)
end
# svn hook requires colordiff (http://colordiff.sourceforge.net/)
# svn hook
def svn(help=nil)
@@repos = `svn info | grep URL`.split("URL:")[1].strip
command = "USAGE : svn[.cmd] EXAMPLES : \n"
command << " svn.st [:plugin|dir]\n"
command << " - gets the status of the svn repos\n"
command << " options can be:\n"
command << " :plugin, :name_of_plugin # gets the status on a plugin\n"
command << " dir (a string value), EX: svn.st 'app/models/user.rb'\n"
command << " svn.ci [dir]\n"
command << " - checks in the changes, pass dir to \n"
command << " check in a specific directory \n"
command << " svn.add\n"