rbtils 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b00e004f6a120b2c92a986e94d15ec4bdf84cecc
4
+ data.tar.gz: 8e5362f564d74b21410eadcc0b9c883431fa9e80
5
+ SHA512:
6
+ metadata.gz: 61afcde4aec89976cf123edd60358ea1ac5e5a78bd18c0a9804d90102afb7486781dbce6b96c167f02a317c5019b7688b074105e7a4d3ffef34dcd51085a1590
7
+ data.tar.gz: 7e77c88344481538327a336b42f027614a8ef1a809903f271cace7cce2f2f83fc861bd9b723cd8594782c5719d4a6ccacc51566dc2582a3fad575bc5d8a471ed
data/lib/command.rb ADDED
@@ -0,0 +1,179 @@
1
+ module Granify
2
+ module Command
3
+ class Exec
4
+ attr_reader :response, :exitcode
5
+ attr_accessor :enable_logging
6
+
7
+ #
8
+ # class methods
9
+ #
10
+ class << self
11
+ def git_queue_status
12
+ if global("git log origin/#{git_current_branch}..HEAD --oneline")
13
+ response = @response.split("\n")
14
+ response.size > 0
15
+ end
16
+ end
17
+
18
+ def git_push
19
+ global("git push -q origin #{git_current_branch}")
20
+ end
21
+
22
+ def git_checkout
23
+ begin
24
+ curr_branch = git_current_branch
25
+ branch = $request.custom.nil? ? curr_branch : $request.custom[0]
26
+
27
+ if !git_branch_verify branch
28
+ raise "Requested branch not found in the working copy: #{branch}"
29
+ end
30
+
31
+ if branch == curr_branch
32
+ return Notify.warning("Requested branch is already checked out, skipping checkout")
33
+ else
34
+ global("git checkout -q #{branch}")
35
+ end
36
+
37
+ # not found locally, checkout from origin
38
+ if !@response
39
+ branch = "origin/#{branch}"
40
+ global("git checkout -q #{branch}")
41
+ end
42
+
43
+ if !@response
44
+ Notify.error("Unable to locate #{branch} on the remote server or local working copy")
45
+ end
46
+
47
+ branch
48
+ rescue SystemExit, Interrupt
49
+ Notify.error("Interrupt caught, exiting")
50
+ rescue RuntimeError => e
51
+ Notify.error(e.message)
52
+ end
53
+ end
54
+
55
+ def git_current_branch
56
+ global("git rev-parse --abbrev-ref HEAD")
57
+ end
58
+
59
+ def git_branch_verify(branch = nil)
60
+ if branch.nil?
61
+ branch = git_current_branch
62
+ end
63
+
64
+ global("git rev-parse --verify #{branch}")
65
+ end
66
+
67
+ def global(command, file = nil)
68
+ begin
69
+ # disable logging if user settings prohibit it
70
+ #file = nil if !@enable_logging
71
+ # default value for exit code is an error
72
+ @exitcode = 1
73
+
74
+ @response = `#{command}`.chomp
75
+ @exitcode = $?.exitstatus
76
+
77
+ # Log output to a file
78
+ # This method is better than redirecting output to a file because now
79
+ # the response is always populated instead of being empty when output
80
+ # is sent to the log file
81
+ if file
82
+ File.open(file.path, 'w+') do |f|
83
+ f.write(@response)
84
+ end
85
+ end
86
+
87
+ @response
88
+ rescue SystemExit, Interrupt
89
+ Notify.error("Interrupt caught, exiting")
90
+ end
91
+ end
92
+ end
93
+
94
+ #
95
+ # instance methods
96
+ #
97
+ def minify(file, destination)
98
+ begin
99
+ min_file = "#{destination}#{File.basename(file, File.extname(file))}.min.js"
100
+
101
+ @response = `uglifyjs #{file} -cm -o "#{min_file}"`
102
+
103
+ Notify.success("Minified #{file}")
104
+
105
+ $?.exitstatus == 0
106
+ rescue SystemExit, Interrupt
107
+ Notify.error("Interrupt caught, exiting")
108
+ end
109
+ end
110
+
111
+ def lint(file, log_file)
112
+ begin
113
+ command = `coffeelint -f "#{Granify::INSTALLED_DIR}/lib/configs/coffeelint.json" #{file}`
114
+
115
+ @response = command.include?("Ok!")
116
+
117
+ # only send errors to the log file
118
+ if !@response
119
+ File.open(log_file.path, 'a') do |f|
120
+ f.write("Logged at #{Time.now}\n============================================================\n\n")
121
+ f.write(command)
122
+ end
123
+ end
124
+
125
+ $?.exitstatus == 0
126
+ rescue SystemExit, Interrupt
127
+ Notify.error("Interrupt caught, exiting")
128
+ end
129
+ end
130
+
131
+ def open_editor(file=nil)
132
+ begin
133
+ log_file = file || Granify::DEFAULT_LOG
134
+
135
+ # System editor is not set/unavailable, use system default to open the
136
+ # file
137
+ if `echo $EDITOR` == ""
138
+ if Utils.os == :macosx
139
+ `open #{log_file.path}`
140
+ else
141
+ `xdg-open #{log_file.path}`
142
+ end
143
+ else
144
+ `$EDITOR #{log_file.path}`
145
+ end
146
+ rescue SystemExit, Interrupt
147
+ Notify.error("Interrupt caught, exiting")
148
+ end
149
+ end
150
+
151
+ def arbitrary(command, file = nil)
152
+ begin
153
+ # default value for exit code is an error
154
+ @exitcode = 1
155
+ @response = `#{command}`.chomp
156
+ @exitcode = $?.exitstatus
157
+
158
+ # Log output to a file
159
+ # This method is better than redirecting output to a file because now
160
+ # the response is always populated instead of being empty when output
161
+ # is sent to the log file
162
+ if file
163
+ File.open(file.path, 'w+') do |f|
164
+ f.write("Logged at #{Time.now}\n============================================================\n\n")
165
+ f.write(@response)
166
+ end
167
+ end
168
+
169
+ @exitcode == 0
170
+
171
+ # support chaining
172
+ self
173
+ rescue SystemExit, Interrupt
174
+ Notify.error("Interrupt caught, exiting")
175
+ end
176
+ end
177
+ end
178
+ end
179
+ end
data/lib/config.rb ADDED
@@ -0,0 +1,52 @@
1
+ module Granify
2
+ class Cfg
3
+ def bootstrap!
4
+ begin
5
+ # configure Notifaction gem
6
+ Notify.configure do |c|
7
+ c.plugins = []
8
+ end
9
+
10
+ if Utils.has_internet_connection?
11
+ # check for package updates
12
+ status = Command::Exec.global("(cd #{Granify::INSTALLED_DIR} && git fetch origin && git diff master origin/master) 2>&1")
13
+
14
+ if !status.empty?
15
+ Notify.warning("You are running an outdated version of #{Granify::PACKAGE_NAME}, please run `rbtils update` to update")
16
+ end
17
+ else
18
+ Notify.warning("Update check could not be performed because you are not connected to the internet")
19
+ end
20
+
21
+ # check for missing dependencies
22
+ missing = []
23
+
24
+ # test if we have all the required commands before sending anything to
25
+ # handlers
26
+ Granify::SHELL_COMMANDS[Utils.os].each do |command|
27
+ Command::Exec.global("type \"#{command}\" > /dev/null 2>&1")
28
+
29
+ if $?.exitstatus != 0
30
+ missing.push("- "+ command)
31
+ end
32
+ end
33
+
34
+ if !missing.empty?
35
+ Notify.error("Required system commands and/or configuration variables are not installed:\n#{missing.join("\n")}")
36
+ end
37
+
38
+ if missing.size > 0
39
+ # try to install the missing commands
40
+ #Command::Exec.global("npm install --silent -g #{missing.join(' ')}")
41
+
42
+ # commented out for now as it currently does nothing
43
+ # if $?.exitstatus > 0
44
+ # # TODO: implement other auto-install commands such as apt-get
45
+ # end
46
+ end
47
+ rescue => e
48
+ Notify.error("#{e.to_s}\n#{e.backtrace.join("\n")}")
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,116 @@
1
+ {
2
+ "arrow_spacing": {
3
+ "level": "ignore"
4
+ },
5
+ "camel_case_classes": {
6
+ "level": "error"
7
+ },
8
+ "coffeescript_error": {
9
+ "level": "error"
10
+ },
11
+ "colon_assignment_spacing": {
12
+ "level": "ignore",
13
+ "spacing": {
14
+ "left": 0,
15
+ "right": 0
16
+ }
17
+ },
18
+ "cyclomatic_complexity": {
19
+ "value": 10,
20
+ "level": "ignore"
21
+ },
22
+ "duplicate_key": {
23
+ "level": "error"
24
+ },
25
+ "empty_constructor_needs_parens": {
26
+ "level": "ignore"
27
+ },
28
+ "ensure_comprehensions": {
29
+ "level": "warn"
30
+ },
31
+ "indentation": {
32
+ "value": 2,
33
+ "level": "error"
34
+ },
35
+ "line_endings": {
36
+ "level": "ignore",
37
+ "value": "unix"
38
+ },
39
+ "max_line_length": {
40
+ "value": 200,
41
+ "level": "ignore",
42
+ "limitComments": true
43
+ },
44
+ "missing_fat_arrows": {
45
+ "level": "ignore"
46
+ },
47
+ "newlines_after_classes": {
48
+ "value": 3,
49
+ "level": "ignore"
50
+ },
51
+ "no_backticks": {
52
+ "level": "error"
53
+ },
54
+ "no_debugger": {
55
+ "level": "warn"
56
+ },
57
+ "no_empty_functions": {
58
+ "level": "ignore"
59
+ },
60
+ "no_empty_param_list": {
61
+ "level": "ignore"
62
+ },
63
+ "no_implicit_braces": {
64
+ "level": "ignore",
65
+ "strict": true
66
+ },
67
+ "no_implicit_parens": {
68
+ "strict": true,
69
+ "level": "ignore"
70
+ },
71
+ "no_interpolation_in_single_quotes": {
72
+ "level": "ignore"
73
+ },
74
+ "no_plusplus": {
75
+ "level": "ignore"
76
+ },
77
+ "no_stand_alone_at": {
78
+ "level": "ignore"
79
+ },
80
+ "no_tabs": {
81
+ "level": "error"
82
+ },
83
+ "no_throwing_strings": {
84
+ "level": "error"
85
+ },
86
+ "no_trailing_semicolons": {
87
+ "level": "error"
88
+ },
89
+ "no_trailing_whitespace": {
90
+ "level": "error",
91
+ "allowed_in_comments": false,
92
+ "allowed_in_empty_lines": true
93
+ },
94
+ "no_unnecessary_double_quotes": {
95
+ "level": "ignore"
96
+ },
97
+ "no_unnecessary_fat_arrows": {
98
+ "level": "warn"
99
+ },
100
+ "non_empty_constructor_needs_parens": {
101
+ "level": "ignore"
102
+ },
103
+ "prefer_english_operator": {
104
+ "level": "ignore",
105
+ "doubleNotLevel": "ignore"
106
+ },
107
+ "space_operators": {
108
+ "level": "ignore"
109
+ },
110
+ "spacing_after_comma": {
111
+ "level": "ignore"
112
+ },
113
+ "transform_messes_up_line_numbers": {
114
+ "level": "warn"
115
+ }
116
+ }
data/lib/constants.rb ADDED
@@ -0,0 +1,15 @@
1
+ module Granify
2
+ PACKAGE_NAME = "rbtils"
3
+ INSTALLED_DIR = File.dirname($0)
4
+ LOG_DIR = INSTALLED_DIR + "/logs"
5
+ DEFAULT_LOG = Log.new # no args means default log
6
+ HELPER_DIR = INSTALLED_DIR + "/lib/helpers/"
7
+ CONTROLLER_DIR = INSTALLED_DIR + "/lib/controllers/"
8
+ MODEL_DIR = INSTALLED_DIR + "/lib/models/"
9
+ LOG_DIGEST_LENGTH = 20
10
+ SHELL_COMMANDS = {
11
+ :macosx => %w(git uglifyjs rubocop),
12
+ :linux => %w(git uglifyjs rubocop)
13
+ }
14
+ DEBUG = false
15
+ end
data/lib/controller.rb ADDED
@@ -0,0 +1,127 @@
1
+ module Granify
2
+ module Controller
3
+ class Base
4
+ attr_accessor :model, :helper, :methods_require_internet, :default_method
5
+
6
+ @@options = Hash.new
7
+
8
+ # Perform pre-run tasks
9
+ def pre_exec
10
+ OptionParser.new do |opt|
11
+ opt.banner = "granify controller command [...-flags]"
12
+
13
+ opt.on("-v", "--verbose", "Verbose output") do |v|
14
+ # short output
15
+ @@options[:verbose] = v
16
+ end
17
+ end.parse!
18
+ end
19
+
20
+ # Handle the request
21
+ def exec(args = [])
22
+ self.send(@default_method.to_sym)
23
+ end
24
+
25
+ # Perform post-run cleanup tasks, such as deleting old logs
26
+ def post_exec(total_errors = 0, total_warnings = 0, total_files = 0)
27
+ # TODO: do post-test evaluation
28
+ # total_errors = @model.data.log.faults[:errors]
29
+ # total_warnings = @model.data.log.faults[:warnings]
30
+
31
+ # if @model.data.log.total_files_processed
32
+ # total_files = @model.data.log.total_files_processed
33
+ # end
34
+
35
+ if @@options[:verbose]
36
+ Notify.info("Results:\nCommand: granify #{$request.controller} #{$request.command} #{($request.flags && $request.flags.size > 0 ? $request.flags.to_json : '')}\n#{total_errors} errors and #{total_warnings} warnings in #{total_files} source files.\nTotal execution time: #{Helper::Time.human_readable(@last_model.start, Time.now)}")
37
+ end
38
+
39
+ if Logs.dirty?
40
+ Logs.clean
41
+ end
42
+ end
43
+
44
+ # Determines if the command can execute
45
+ def can_exec?(command = nil, name = nil)
46
+ @model = Granify::Model.const_get(command.capitalize).new rescue nil
47
+ @helper = Granify::Helper.const_get(command.capitalize).new rescue nil
48
+ @methods_require_internet = []
49
+
50
+ # get user-defined methods to use as a fallback
51
+ user_defined_methods = []
52
+
53
+ # timeout is the first system defined method after the user defined
54
+ # ones
55
+ for i in 0..public_methods.index(:to_json) -1
56
+ user_defined_methods.push(public_methods[i].to_sym)
57
+ end
58
+
59
+ @default_method = name || user_defined_methods.first || :sample
60
+
61
+ if !respond_to? default_method.to_sym, true
62
+ Notify.error("Command not found: #{name}")
63
+ end
64
+
65
+ true
66
+ end
67
+
68
+ # default method called by exec if no argument is passed
69
+ def sample
70
+ Notify.warning("Method not implemented");
71
+ end
72
+
73
+ def required_modules(*modules)
74
+ auto_load_required(modules).each do |type, hash|
75
+ # Make each auto-loaded module available as an instance variable
76
+ # i.e. [:hound]: @hound_controller, @hound_model, @hound_helper
77
+ # i.e. [:test]: @test_controller, @test_model, @test_helper
78
+ # Only files that exist and can be called are loaded
79
+ hash.each do |key, value|
80
+ instance_variable_set("@#{key}_#{type}".to_sym, value)
81
+ @last_model = instance_variable_get("@#{key}_model")
82
+ end
83
+ end
84
+ end
85
+
86
+ private
87
+ # autoload and instantiate required libraries, models and helpers
88
+ def auto_load_required(modules = [])
89
+ loaded = {:controller => {}, :helper => {}, :model => {}}
90
+
91
+ begin
92
+ modules.each do |mod|
93
+ if File.exists? "#{Granify::INSTALLED_DIR}/lib/controllers/#{mod}.rb"
94
+ require "#{Granify::INSTALLED_DIR}/lib/controllers/#{mod}.rb"
95
+
96
+ loaded[:controller][mod] = Granify::Controller.const_get(mod.capitalize).new
97
+ else
98
+ raise StandardError, "Controller not found: #{mod}"
99
+ end
100
+
101
+ if File.exists? "#{Granify::INSTALLED_DIR}/lib/helpers/#{mod}.rb"
102
+ require "#{Granify::INSTALLED_DIR}/lib/helpers/#{mod}.rb"
103
+ loaded[:helper][mod] = Granify::Helper.const_get(mod.capitalize).new
104
+
105
+ # auto-instantiate new instance of helper for the new instance of the controller
106
+ loaded[:controller][mod].helper = loaded[:helper][mod]
107
+ end
108
+
109
+ if File.exists? "#{Granify::INSTALLED_DIR}/lib/models/#{mod}.rb"
110
+ require "#{Granify::INSTALLED_DIR}/lib/models/#{mod}.rb"
111
+ loaded[:model][mod] = Granify::Model.const_get(mod.capitalize).new
112
+
113
+ # auto-instantiate new instance of model for the new instance of the controller
114
+ loaded[:controller][mod].model = loaded[:model][mod]
115
+ else
116
+ loaded[:controller][mod].model = Model::Base.new
117
+ end
118
+ end
119
+
120
+ loaded
121
+ rescue StandardError => e
122
+ Notify.error(e.message)
123
+ end
124
+ end
125
+ end
126
+ end
127
+ end