console_util 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/console_util/version.rb +1 -1
- data/lib/console_util.rb +24 -24
- metadata +4 -4
data/lib/console_util/version.rb
CHANGED
data/lib/console_util.rb
CHANGED
@@ -3,7 +3,7 @@ require 'console_util/color'
|
|
3
3
|
module ConsoleUtil
|
4
4
|
class << self
|
5
5
|
attr_reader :suppressed_output
|
6
|
-
|
6
|
+
|
7
7
|
# Monkey-patch the ActiveRecord connection to output the SQL query before
|
8
8
|
# executing it.
|
9
9
|
def output_sql_to_console(options = {})
|
@@ -19,10 +19,10 @@ module ConsoleUtil
|
|
19
19
|
super
|
20
20
|
end
|
21
21
|
end
|
22
|
-
|
22
|
+
|
23
23
|
true
|
24
24
|
end
|
25
|
-
|
25
|
+
|
26
26
|
# Import a MySQL dump file.
|
27
27
|
# [This may work with other databases, but has only been tested with MySQL.]
|
28
28
|
def import_mysql_dump(filename)
|
@@ -36,10 +36,10 @@ module ConsoleUtil
|
|
36
36
|
def load_all_models
|
37
37
|
Rails.root.join('app/models').each_entry do |model_file|
|
38
38
|
model_match = model_file.to_s.match(/^(.*)\.rb$/)
|
39
|
-
model_match[1].
|
39
|
+
model_match[1].camelize.constantize if model_match
|
40
40
|
end
|
41
41
|
end
|
42
|
-
|
42
|
+
|
43
43
|
# Allows you to filter output to the console using grep
|
44
44
|
# Ex:
|
45
45
|
# def foo
|
@@ -47,7 +47,7 @@ module ConsoleUtil
|
|
47
47
|
# puts "The value of x is y"
|
48
48
|
# puts "The value of foo is bar"
|
49
49
|
# end
|
50
|
-
#
|
50
|
+
#
|
51
51
|
# grep_stdout(/value/) { foo }
|
52
52
|
# # => The value of x is y
|
53
53
|
# # => The value of foo is bar
|
@@ -57,22 +57,22 @@ module ConsoleUtil
|
|
57
57
|
# the first is read only (which represents a fake $stdin) and the second is
|
58
58
|
# write only (which represents a fake $stdout).
|
59
59
|
placeholder_in, placeholder_out = IO.pipe
|
60
|
-
|
60
|
+
|
61
61
|
# This child process handles the grep'ing. Its done in a child process so that
|
62
62
|
# it can operate in parallel with the main process.
|
63
63
|
pid = fork {
|
64
64
|
# sync $stdout so we can report any matches asap
|
65
65
|
$stdout.sync
|
66
|
-
|
66
|
+
|
67
67
|
# replace $stdout with placeholder_out
|
68
68
|
$stdin.reopen(placeholder_in)
|
69
|
-
|
69
|
+
|
70
70
|
# we have to close both placeholder_out and placeholder_in because all instances
|
71
71
|
# of an IO stream must be closed in order for it to ever reach EOF. There's two
|
72
72
|
# in this method; one in the child process and one in the main process.
|
73
73
|
placeholder_in.close
|
74
74
|
placeholder_out.close
|
75
|
-
|
75
|
+
|
76
76
|
# loop continuously until we reach EOF (which happens when all
|
77
77
|
# instances of placeholder_out have closed)
|
78
78
|
read_buffer = ''
|
@@ -80,52 +80,52 @@ module ConsoleUtil
|
|
80
80
|
begin
|
81
81
|
match = nil
|
82
82
|
next_read = $stdin.readpartial(4096)
|
83
|
-
|
83
|
+
|
84
84
|
read_buffer << next_read
|
85
85
|
if line_match = read_buffer.match(/^(.*\n)(.*)$/m)
|
86
86
|
match = line_match[1].grep(expression) # grep complete lines
|
87
|
-
read_buffer = line_match[2]
|
87
|
+
read_buffer = line_match[2] # save remaining partial line for the next iteration
|
88
88
|
end
|
89
89
|
rescue EOFError
|
90
|
-
match = read_buffer.grep(expression)
|
90
|
+
match = read_buffer.grep(expression) # grep any remaining partial line at EOF
|
91
91
|
break
|
92
92
|
end
|
93
|
-
|
93
|
+
|
94
94
|
if match && !match.empty?
|
95
95
|
print match
|
96
96
|
end
|
97
97
|
end
|
98
98
|
}
|
99
|
-
|
99
|
+
|
100
100
|
# Save the original stdout out to a variable so we can use it again after this
|
101
101
|
# method is done
|
102
102
|
original_stdout = $stdout
|
103
|
-
|
103
|
+
|
104
104
|
# Redirect stdout to our pipe
|
105
105
|
$stdout = placeholder_out
|
106
|
-
|
106
|
+
|
107
107
|
# sync $stdout so that we can start operating on it as soon as possible
|
108
108
|
$stdout.sync
|
109
|
-
|
109
|
+
|
110
110
|
# allow the block to execute and save its return value
|
111
111
|
return_value = yield
|
112
|
-
|
112
|
+
|
113
113
|
# Set stdout back to the original so output will flow again
|
114
114
|
$stdout = original_stdout
|
115
|
-
|
115
|
+
|
116
116
|
# close the main instances of placeholder_in and placeholder_out
|
117
117
|
placeholder_in.close
|
118
118
|
placeholder_out.close
|
119
|
-
|
119
|
+
|
120
120
|
# Wait for the child processes to finish
|
121
121
|
Process.wait pid
|
122
|
-
|
122
|
+
|
123
123
|
# Because the connection to the database has a tendency to go away when calling this, reconnect here
|
124
124
|
# if we're using ActiveRecord
|
125
125
|
if defined?(ActiveRecord)
|
126
126
|
suppress_stdout { ActiveRecord::Base.verify_active_connections! }
|
127
127
|
end
|
128
|
-
|
128
|
+
|
129
129
|
# return the value of the block
|
130
130
|
return_value
|
131
131
|
end
|
@@ -135,7 +135,7 @@ module ConsoleUtil
|
|
135
135
|
# def foo
|
136
136
|
# puts "lots of stuff directed to $stdout that I don't want to see."
|
137
137
|
# end
|
138
|
-
#
|
138
|
+
#
|
139
139
|
# suppress_stdout do |stdout|
|
140
140
|
# stdout.puts "About to call #foo"
|
141
141
|
# foo
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: console_util
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 17
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 3
|
9
|
-
-
|
10
|
-
version: 0.3.
|
9
|
+
- 1
|
10
|
+
version: 0.3.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Clyde Law
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-07-
|
18
|
+
date: 2012-07-24 00:00:00 -07:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|