-
Notifications
You must be signed in to change notification settings - Fork 17
/
annotate.rb
121 lines (95 loc) · 3.15 KB
/
annotate.rb
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
# encoding: utf-8
require 'guard/compat/plugin'
require 'guard/annotate/version'
module Guard
class Annotate < Plugin
autoload :Notifier, 'guard/annotate/notifier'
def initialize(options={})
super
options[:notify] = true if options[:notify].nil?
options[:position] = 'before' if options[:position].nil?
options[:tests] = false if options[:tests].nil?
options[:sort] = false if options[:sort].nil?
options[:routes] = false if options[:routes].nil?
options[:run_at_start] = true if options[:run_at_start].nil?
options[:show_indexes] = false if options[:show_indexes].nil?
options[:simple_indexes] = false if options[:simple_indexes].nil?
options[:show_migration] = false if options[:show_migration].nil?
options[:format] = nil if options[:format].nil? or not [:bare, :markdown, :rdoc].include? options[:format].to_sym
end
def start
run_annotate if options[:run_at_start]
end
def stop
true
end
def reload
run_annotate if options[:run_at_start]
end
def run_all
true
end
def run_on_changes(paths=[])
run_annotate
end
alias :run_on_change :run_on_changes if ::Guard::AnnotateVersion::VERSION < '1.1.0'
private
def notify?
!!options[:notify]
end
def annotation_position
options[:position]
end
def annotate_routes?
options[:routes]
end
def annotate_tests_flags
options[:tests] ? '' : '--exclude tests,fixtures'
end
def annotate_sort_columns?
options[:sort]
end
def show_indexes?
options[:show_indexes]
end
def annotate_format
options[:format]
end
def annotate_format?
not options[:format].nil?
end
def simple_indexes?
options[:simple_indexes]
end
def show_migration?
options[:show_migration]
end
def run_annotate
Compat::UI.info 'Running annotate', :reset => true
started_at = Time.now
annotate_models_options, annotate_options = '', ''
annotate_models_command = "bundle exec annotate #{annotate_tests_flags} -p #{annotation_position}"
annotate_models_options += ' --sort' if annotate_sort_columns?
annotate_models_options += ' --show-indexes' if show_indexes?
annotate_models_options += ' --simple-indexes' if simple_indexes?
annotate_models_options += ' --show-migration' if show_migration?
annotate_options += " --format=#{annotate_format}" if annotate_format?
annotate_models_command += annotate_models_options + annotate_options
@result = system(annotate_models_command)
Notifier::notify(@result, Time.now - started_at) if notify?
if annotate_routes?
started_at = Time.now
position = if %w[after before].include? options[:routes].to_s
options[:routes]
else
annotation_position
end
annotate_routes_command = "bundle exec annotate -r -p #{position}"
annotate_routes_command += annotate_options
@result = system(annotate_routes_command)
Notifier::notify(@result, Time.now - started_at) if notify?
end
@result
end
end
end