fox 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # @module module Mixin
4
+ # @brief Mixin module contains various functions to be used in other components
5
+ module Mixin
6
+
7
+ # @module ConfigChoice Module
8
+ # @brief Module wrapper around ConfigChoice tasks
9
+ module ConfigChoice
10
+
11
+ def config_choice
12
+ defaults = YAML.load_file(File.expand_path( File.dirname( __FILE__ ) + '/../../../template/default_values.yml'))
13
+ defaults['jason'].each_key { |key| choice_option(defaults['jason'], key) }
14
+ defaults
15
+ end
16
+
17
+ private def choice_option(defaults, option)
18
+ print ("%s (%s): " % [option, defaults[option]])
19
+ value = STDIN.gets.chomp
20
+ defaults[option] = value unless value.empty?
21
+ end
22
+ end # of module ConfigChoice
23
+
24
+ end # of module Mixin
25
+
26
+
27
+ # vim:ts=2:tw=100:wm=100:syntax=ruby
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env ruby
2
+
3
+
4
+ # @module module Mixin
5
+ # @brief Mixin module contains various functions to be used in other components
6
+ module Mixin
7
+
8
+ # @module Configuration Module
9
+ # @brief Module wrapper around tasks which demands config file
10
+ module Configuration
11
+
12
+ # @fn def initialize *args {{{
13
+ # @brief Default constructor
14
+ #
15
+ # @param [Array] args Argument array
16
+ def initialize *args
17
+ super
18
+ unless File.exist?("~/.fox/config.yaml")
19
+ abort("Could not find configuration file in ~/fox/config.yaml. Please run 'fox config:generate' to generate it.")
20
+ end
21
+ end # }}}
22
+
23
+ end # of module Configuration
24
+
25
+ end # of module Mixin
26
+
27
+
28
+ # vim:ts=2:tw=100:wm=100:syntax=ruby
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env ruby
2
+
3
+
4
+ # System includes
5
+ require 'thor'
6
+ require 'andand'
7
+
8
+
9
+ # @module module Mixin
10
+ # @brief Mixin module contains various functions to be used in other components
11
+ module Mixin
12
+
13
+ # @module Default Module
14
+ # @brief Module wrapper around default tasks
15
+ module Default
16
+
17
+ # @fn def initialize *args {{{
18
+ # @brief Default constructor
19
+ #
20
+ # @param [Array] args Argument array
21
+ def initialize *args
22
+ super
23
+ end # }}}
24
+
25
+ end # of module Default
26
+
27
+ end # of module Mixin
28
+
29
+
30
+ # vim:ts=2:tw=100:wm=100:syntax=ruby
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Custom includes
4
+ require File.expand_path( File.dirname( __FILE__ ) + '/config_choice' )
5
+
6
+ # @module module Mixin
7
+ # @brief Mixin module contains various functions to be used in other components
8
+ module Mixin
9
+
10
+ # @module DefaultConfig Module
11
+ # @brief Module wrapper around default tasks
12
+ module DefaultConfig
13
+
14
+ # Include various partials
15
+ include ::Mixin::ConfigChoice
16
+
17
+ def defaults
18
+ config_path = File.expand_path( '~/.fox/config.yml' )
19
+ if File.exists?(config_path)
20
+ YAML.load_file(config_path)
21
+ else
22
+ FileUtils.mkdir_p( File.expand_path( '~/.fox' ) )
23
+ config_choice
24
+ end
25
+ end
26
+ end # of module DefaultConfig
27
+
28
+ end # of module Mixin
29
+
30
+
31
+ # vim:ts=2:tw=100:wm=100:syntax=ruby
@@ -0,0 +1,57 @@
1
+ #!/usr/bin/env ruby
2
+
3
+
4
+ # System includes
5
+ require 'andand'
6
+
7
+
8
+ # @module module Mixin
9
+ # @brief Mixin module contains various functions to be used in other components
10
+ module Mixin
11
+
12
+ # @module Guess Module
13
+ # @brief Module wrapper around guess tasks to extract version numbers etc
14
+ module Guess
15
+
16
+ # @fn def initialize *args {{{
17
+ # @brief Default constructor
18
+ #
19
+ # @param [Array] args Argument array
20
+ def initialize *args
21
+ super
22
+ end # }}}
23
+
24
+ # @fn def guess_version string {{{
25
+ # @brief Guess version from full version string
26
+ #
27
+ # @example e.g. "ruby 2.1.1p76 (2014-02-24 revision 45161) [i686-linux]"
28
+ # -> 2.1.1
29
+ #
30
+ # "rake, version 10.1.1"
31
+ # -> 10.1.1
32
+ def guess_version string
33
+
34
+ result = ""
35
+
36
+ begin
37
+
38
+ # Sanity
39
+ raise ArgumentError, "Version has to be of type string" unless( string.is_a?( String ) )
40
+ raise ArgumentError, "Version can't be empty" if( string.empty? )
41
+
42
+ result = string.match( /\d+\.\d+\.\d+/ ).to_s # matches first only
43
+
44
+ rescue Exception => e
45
+ say "(EE) " + e.message, :red
46
+ result = ""
47
+ end
48
+
49
+ return result
50
+ end # }}}
51
+
52
+ end # of Module Guess
53
+
54
+ end # of module Mixin
55
+
56
+
57
+ # vim:ts=2:tw=100:wm=100:syntax=ruby
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env ruby
2
+
3
+
4
+ # System includes
5
+ require 'thor'
6
+ require 'andand'
7
+
8
+ # Custom includes
9
+ # require File.expand_path( File.dirname( __FILE__ ) + '/../../../library/logger' )
10
+
11
+
12
+ # @module module Mixin
13
+ # @brief Mixin module contains various functions to be used in other components
14
+ module Mixin
15
+
16
+ # @module Logger Module
17
+ # @brief Module wrapper around logger tasks
18
+ module Logger
19
+
20
+ # @fn def initialize *args {{{
21
+ # @brief Default constructor
22
+ #
23
+ # @param [Array] args Argument array
24
+ def initialize *args
25
+ super
26
+
27
+ @logger = ::Fox::Logger.instance
28
+
29
+ @logger.color = options[ :colorize ]
30
+ @logger.silent = options[ :silent ]
31
+
32
+ end # }}}
33
+
34
+ Thor::class_option :colorize, :type => :boolean, :required => false, :default => true, :desc => 'Colorize the output for easier reading'
35
+ Thor::class_option :logger, :type => :boolean, :required => false, :default => true, :desc => 'Use default project logger'
36
+ Thor::class_option :silent, :type => :boolean, :required => false, :default => false, :desc => 'Turn off all logging'
37
+
38
+ end # of module Logger
39
+
40
+ end # of module Mixin
41
+
42
+
43
+ # vim:ts=2:tw=100:wm=100:syntax=ruby
@@ -0,0 +1,225 @@
1
+ #!/usr/bin/env ruby
2
+
3
+
4
+ # System includes
5
+ require 'andand'
6
+ require 'ptools'
7
+ require 'tempfile'
8
+
9
+ # Custom includes
10
+ require File.expand_path( File.dirname( __FILE__ ) + '/guess' )
11
+
12
+
13
+ # @module module Mixin
14
+ # @brief Mixin module contains various functions to be used in other components
15
+ module Mixin
16
+
17
+ # @module Shell Module
18
+ # @brief Module wrapper around shell commands
19
+ module Shell
20
+
21
+ include ::Mixin::Guess
22
+
23
+ # @fn def initialize *args {{{
24
+ # @brief Default constructor
25
+ #
26
+ # @param [Array] args Argument array
27
+ def initialize *args
28
+ super
29
+ end # }}}
30
+
31
+ ## Actions
32
+
33
+ # @fn def run command {{{
34
+ # @brief Run the given command gracefully (without throwing exceptions)
35
+ #
36
+ # @param [String] command The command to run
37
+ # @param [RegExp] regexp Optional regular expression used for matching output
38
+ #
39
+ # @return [String] Returns empty string if command not found or other problem,
40
+ # otherwise result of command.
41
+ def run command, regexp = nil
42
+
43
+ result = ''
44
+
45
+ begin
46
+ if( regexp.nil? )
47
+ result = execute( command ).strip
48
+ else
49
+ raise ArgumentError, 'Regular Expression input needs to be of type Regexp' unless( regexp.is_a?( Regexp ) )
50
+
51
+ result = execute( command ).andand.send( :match, regexp ).andand.send( :to_s ).strip
52
+ end
53
+
54
+ rescue Exception => e
55
+ say '(EE) ' + e.message, :red
56
+ result = ''
57
+ end
58
+
59
+ return result
60
+ end # }}}
61
+
62
+ # @fn def execute command {{{
63
+ # @brief Execute single shell command
64
+ #
65
+ # @param [String] command Shell command string with arguments
66
+ #
67
+ # @return [String] Returns result of command
68
+ #
69
+ # @throws [ArgumentError] Throws exception if command not found
70
+ def execute command
71
+
72
+ exists = which( command )
73
+ raise ArgumentError, "Command not found" unless( exists )
74
+ result = `#{command}`
75
+
76
+ return result
77
+ end # }}}
78
+
79
+ # @fn def which command {{{
80
+ # @brief Checks if command is available
81
+ #
82
+ # @param [String] command Shell command string with or without arguments.
83
+ # If arguments are given, they are split on first
84
+ # whitespace and discarded
85
+ #
86
+ # @return [Boolean] Returns boolean true if command is available, false if not
87
+ #
88
+ #
89
+ # @info Crude alternative: `#{command} 2>/dev/null`.strip.empty?
90
+ def which command
91
+ result = false
92
+
93
+ begin
94
+ partial = command.to_s
95
+ partial = partial.split(' ').first.to_s if( partial =~ %r{ }i )
96
+ path = File.which( partial )
97
+
98
+ result = true unless( path.nil? )
99
+ rescue Exception => e
100
+ say "(EE) " + e.message, :red
101
+ result = false
102
+ end
103
+
104
+ return result
105
+ end # }}}
106
+
107
+ # @fn def version command {{{
108
+ # @brief Prints the version of given command, if command exists
109
+ #
110
+ # @param [String] command Command string to probe version for, e.g. ruby
111
+ #
112
+ # @return [String] Returns version string or empty if command not found / error
113
+ def version command
114
+
115
+ result = ""
116
+
117
+ begin
118
+
119
+ # Sanity
120
+ raise ArgumentError, "Command not found" unless( which( command ) )
121
+
122
+ # Get usage help screen for command
123
+ help = usage( command )
124
+
125
+ # Get version flag from help screen
126
+ flag = version_flag( help )
127
+
128
+ return result if( flag.empty? ) # some stupid commands don't follow standard rules, e.g. bundle
129
+
130
+ # Get actual version string
131
+ banner = run( command + " " + flag )
132
+
133
+ # Guess way to extract and extract semver
134
+ result = guess_version( banner.to_s )
135
+
136
+ rescue Exception => e
137
+ say "(EE) " + e.message, :red
138
+ result = ""
139
+ end
140
+
141
+ return result
142
+ end # }}}
143
+
144
+
145
+ alias_method :exists?, :which
146
+
147
+ private
148
+
149
+ # @fn def usage command {{{
150
+ # @brief Extracts help/usage screen from command
151
+ #
152
+ # @param [String] command Command string
153
+ #
154
+ # @return [String Result screen output string
155
+ def usage command
156
+
157
+ result = ""
158
+
159
+ begin
160
+
161
+ # Some commands misbehave when using wrong arguments
162
+ # e.g. java
163
+ help = []
164
+
165
+ # Collect all possible outputs
166
+ %w(--help -h -help).each do |argument|
167
+ tempfile = Tempfile.new( "fox-" )
168
+ `#{command} #{argument} > #{tempfile.path.to_s} 2>&1`
169
+ help << File.read( tempfile.path )
170
+ end
171
+
172
+ # Prune to relevent one
173
+ help.each do |h|
174
+
175
+ # Sanity
176
+ next if( h.strip.empty? )
177
+ next if( h.split( "\n" ).length < 4 )
178
+ next unless( h.match( /unknown/i ).to_s.empty? ) # does it contain unknown? (argument)
179
+ next unless( h.match( /unrecognized/i ).to_s.empty? ) # does it contain unrecognized? (argument)
180
+ next unless( h.match( /error/i ).to_s.empty? ) # does it contain error
181
+ next unless( h.match( /does not exist/i ).to_s.empty? ) # does not exist error
182
+ next unless( result.empty? )
183
+
184
+ result = h
185
+
186
+ end # of help.each
187
+
188
+
189
+ rescue Exception => e
190
+ say "(EE) " + e.message, :red
191
+ result = ""
192
+ end
193
+
194
+ return result
195
+ end # }}}
196
+
197
+ # @fn def version_flag usage {{{
198
+ # @brief Extracts version flag from usage help screen of command
199
+ #
200
+ # @param [String] usage Usage help screen output (string), e.g. ruby --help
201
+ #
202
+ # @return [String] Returns the string which according to help screen will print version
203
+ # e.g. -version for java
204
+ def version_flag usage
205
+
206
+ result = ""
207
+
208
+ begin
209
+ result = usage.match( /--version/i ).to_s
210
+ result = usage.match( /-version/i ).to_s if( result.empty? )
211
+ rescue Exception => e
212
+ say "(EE) " + e.message, :red
213
+ result = ""
214
+ end
215
+
216
+ return result
217
+ end # }}}
218
+
219
+ end # of Module Shell
220
+
221
+ end # of module Mixin
222
+
223
+
224
+
225
+ # vim:ts=2:tw=100:wm=100:syntax=ruby