codercmp 0.9.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,410 @@
1
+ # TODO: change to use instance instead of static/class methods for all
2
+ $:.push File.expand_path(File.dirname(__FILE__) )
3
+
4
+ require 'yard'
5
+ require 'net/http'
6
+ require 'digest/sha1'
7
+ require 'json'
8
+ require 'rest-client'
9
+ require 'polyglot'
10
+ require 'treetop'
11
+ require 'colored'
12
+ require 'aws-sdk'
13
+ require 'codercompanion/codercompanion_exception'
14
+ require 'codercompanion/ruby/ruby_annotations'
15
+ require 'codercompanion/languages'
16
+ require 'codercompanion/parser_factory'
17
+ require 'codercompanion/tree_walker'
18
+ require 'codercompanion/language_parser'
19
+ require 'codercompanion/java/java'
20
+ require 'codercompanion/java/java_parser'
21
+ require 'codercompanion/java/java_tree_walker'
22
+ require 'codercompanion/ruby/ruby_parser'
23
+ require 'codercompanion/ruby/ruby_tree_walker'
24
+ require 'codercompanion/cpp/cpp_parser'
25
+ require 'codercompanion/cpp/cpp_tree_walker'
26
+ require 'codercompanion/api'
27
+ require 'codercompanion/cachehandler'
28
+ require 'codercompanion/s3_client'
29
+
30
+ begin
31
+ require 'awesome_print'
32
+ rescue LoadError => e
33
+ puts "Could not require \"awesome_print\"!"
34
+ puts "continuing without it..."
35
+ end
36
+
37
+ module CoderCompanion
38
+
39
+ BASEPATH = File.expand_path('..', File.dirname(__FILE__) )
40
+
41
+ STATUS_OK = 200
42
+ USER_NOT_FOUND = 461
43
+ PROJECT_NOT_FOUND = 510
44
+ GENERIC_ERROR = 500
45
+
46
+ CONFIG_FILE_NAME = 'codercmp.conf'
47
+
48
+ # @return [Hash]
49
+ @@options = {}
50
+
51
+ # @return [Hash]
52
+ @@config = nil
53
+
54
+ # @return [String]
55
+ @@visit_config_location_message = ""
56
+
57
+ # @return [String]
58
+ @@src_folder = ""
59
+
60
+ # @return [CoderCompanion::API]
61
+ @@api = CoderCompanion::API.new
62
+
63
+ def self.init
64
+ =begin
65
+ -----------------------------------------------------------------------------------------------
66
+
67
+ __-¯--__
68
+ __-¯¯ ¯¯--__
69
+ __-¯¯ ¯¯-___,
70
+ /¯-__ __-¯¯__ |
71
+ | ¯¯--__ __-¯¯__-¯¯ | |
72
+ | ¯--__-¯¯__-¯¯ | |
73
+ | | r¯¯ ¯¯ | |
74
+ | | | O | |
75
+ L | | _-| |
76
+ `~__ | | __-¯¯__-¯¯
77
+ `~__ | | __-¯¯__-¯¯ __-¯¯`~__
78
+ `~__ | '¯¯__-¯¯ __-¯¯ ;-
79
+ `L_-¯¯ __-¯¯ __-¯¯
80
+ __-¯¯ __-¯¯
81
+ `~_ __-¯¯
82
+ `~__-¯¯
83
+ #{special_format("R u n n i n g C o d e r C o m p a n i o n u p l o a d e r")}
84
+
85
+ -----------------------------------------------------------------------------------------------
86
+ =end
87
+ j_print <<INFO
88
+ -----------------------------------------------------------------------------------------------
89
+
90
+ #{special_format("R u n n i n g C o d e r C o m p a n i o n u p l o a d e r")}
91
+
92
+ -----------------------------------------------------------------------------------------------
93
+ INFO
94
+ @@visit_config_location_message = error_format("Please visit ") + error_url_format("http://codercompanion.com") + error_format(" for more info")
95
+
96
+ # Using config_filename otherwise default to 'codercmp.conf' in pwd
97
+ config_filename = @@options[:config_file] ? @@options[:config_file] : "#{CONFIG_FILE_NAME}"
98
+
99
+ file = nil
100
+ begin
101
+ file = File.open(config_filename)
102
+ rescue => e
103
+ j_print e.to_s
104
+ raise CoderCompanionException.new(error_format("An error occured while trying to open the configuration file"))
105
+ end
106
+
107
+ begin
108
+ @@config = JSON.parse(file.read)
109
+ rescue => e
110
+ j_print error_format e.to_s
111
+ raise CoderCompanionException.new(error_format("could not parse your config file. ") + @@visit_config_location_message)
112
+ end
113
+
114
+ verify_config_file
115
+ end
116
+
117
+ def self.base_path
118
+ BASEPATH
119
+ end
120
+
121
+ def self.j_print(string)
122
+ puts string if !silent_run
123
+ end
124
+
125
+ def self.j_print_inline(string)
126
+ print string if !silent_run
127
+ end
128
+
129
+ # @return [CoderCompanion::CacheHandler]
130
+ def self.cache_handler
131
+ @@cache_handler
132
+ end
133
+
134
+ # @return [Boolean]
135
+ def self.dry_run?
136
+ @@options[:dry_run]
137
+ end
138
+
139
+ # @return [Boolean]
140
+ def self.silent_run
141
+ @@options[:silent_run]
142
+ end
143
+
144
+ # @return [Boolean]
145
+ def self.fresh_run
146
+ @@options[:fresh_run]
147
+ end
148
+
149
+ # @return [Boolean]
150
+ def self.no_color
151
+ @@options[:no_color]
152
+ end
153
+
154
+ def self.excluded
155
+ @@options[:exclude]
156
+ end
157
+
158
+ # @return [Object]
159
+ def self.config
160
+ @@config
161
+ end
162
+
163
+ # @return [String]
164
+ def self.language
165
+ @@language
166
+ end
167
+
168
+ # @param language [String] the language
169
+ def self.language=(language)
170
+ @@language = language
171
+ end
172
+
173
+ # @return [String]
174
+ def self.annotation
175
+ @@annotation
176
+ end
177
+
178
+ # @param annotation [String] the annotation type
179
+ def self.annotation=(annotation)
180
+ @@annotation = annotation
181
+ end
182
+
183
+ # @param [String] string
184
+ def self.special_format(string)
185
+ return CoderCompanion.no_color ? string : string.white_on_black.bold
186
+ end
187
+
188
+ # @param [String] string
189
+ def self.warning_format(string)
190
+ return CoderCompanion.no_color ? string : string.black_on_yellow.bold
191
+ end
192
+
193
+ # @param [String] string
194
+ def self.error_format(string)
195
+ return CoderCompanion.no_color ? string : string.white_on_red.bold
196
+ end
197
+
198
+ # @param [String] string
199
+ def self.error_url_format(string)
200
+ return CoderCompanion.no_color ? string : string.yellow_on_red.bold.underline
201
+ end
202
+
203
+ # @param [String] string
204
+ def self.red_format(string)
205
+ return CoderCompanion.no_color ? string : string.red_on_black.bold
206
+ end
207
+
208
+ # @param [String] string
209
+ def self.notification_format(string)
210
+ return CoderCompanion.no_color ? string : string.yellow_on_black
211
+ end
212
+
213
+ # @param [String] string
214
+ def self.success_format(string)
215
+ return CoderCompanion.no_color ? string : string.green_on_black.bold
216
+ end
217
+
218
+ def self.verify_config_file
219
+ j_print notification_format("Verifying Config file...")
220
+ error = false
221
+ if @@config
222
+ if @@config["project"]
223
+ if @@config["project"]["access_key"] == nil || @@config["project"]["access_key"].match(/^\s*$/)
224
+ j_print error_format("No access_key for project specified.")
225
+ error = true
226
+ end
227
+
228
+ is_supported = true
229
+ self.language = @@config["project"]["language"]
230
+ if self.language == nil || self.language.match(/^\s*$/)
231
+ j_print error_format("No language specified in your config file.")
232
+ error = true
233
+ elsif !CoderCompanion::Languages.is_supported?(self.language)
234
+ is_supported = false
235
+ j_print error_format("#{self.language} is not a supported language")
236
+ j_print notification_format("To see a list of supported languages run: \n\tcodercmp --language\n")
237
+ error = true
238
+ end
239
+ =begin -- Validation when multiple annotation options is allowed
240
+ if !error && is_supported
241
+ self.annotation = @@config["project"]["annotation"]
242
+ languages = CoderCompanion::Languages.supported_languages
243
+ language_details = nil
244
+ languages.each do |lang|
245
+ language_details = (lang[:key] == language) ? lang : nil
246
+ if language_details
247
+ break
248
+ end
249
+ end
250
+ if language_details && language_details[:annotation_required] && (self.annotation == nil || self.annotation.match(/^\s*$/))
251
+ j_print error_format("#{self.language} is supported but must make use of ONE of the following annotations styles: #{language_details[:valid_annotation].join(", ")}")
252
+ error = true
253
+ end
254
+ end
255
+ =end
256
+ else
257
+ j_print error_format("Project not found in config file. ")
258
+ error = true
259
+ end
260
+ if @@config["user"]
261
+ if @@config["user"]["access_id"] == nil || @@config["user"]["access_id"] == ""
262
+ j_print error_format("No access_id for project specified.")
263
+ error = true
264
+ end
265
+ else
266
+ j_print error_format("User info not found")
267
+ error = true
268
+ end
269
+ if @@config["host"] == nil || @@config["host"] == ""
270
+ j_print error_format("Host not found")
271
+ error = true
272
+ end
273
+ end
274
+
275
+ if error
276
+ raise CoderCompanionException.new(@@visit_config_location_message)
277
+ end
278
+
279
+ j_print success_format("Config file Successfully Verified")
280
+ end
281
+
282
+ def self.pretty_inspect(thing)
283
+ begin
284
+ thing.awesome_inspect
285
+ rescue
286
+ thing.inspect
287
+ end
288
+ end
289
+
290
+ def self.display_supported_languages
291
+ languages = CoderCompanion::Languages.supported_languages
292
+ puts <<INFO
293
+ ---------------------------------------------------------------------------------------------
294
+
295
+ #{CoderCompanion::notification_format("CoderCompanion Supported Languages (2015-05-30)")}
296
+
297
+ ---------------------------------------------------------------------------------------------
298
+ INFO
299
+
300
+ languages.each do |lang|
301
+
302
+ puts <<INFO
303
+
304
+ Language: #{CoderCompanion::notification_format(lang[:key])}\t\t|\t Requires Annotations: #{lang[:annotation_required] ? CoderCompanion::success_format("YES") : CoderCompanion::red_format("NO") }\t\t|\tAnnotations Style(s): #{(lang[:annotation_required]) ? CoderCompanion::notification_format(lang[:valid_annotation].join(", ")) : CoderCompanion::notification_format("N/A")}
305
+
306
+ INFO
307
+ end
308
+ end
309
+
310
+ def self.run
311
+ if self.dry_run?
312
+
313
+ j_print <<INFO
314
+
315
+ #{notification_format("Performing dry run:")}
316
+ - No cache use
317
+ - No upload
318
+
319
+ INFO
320
+
321
+ end
322
+
323
+
324
+ @@cache_handler = CacheHandler.new
325
+ @@cache_handler.load_cache(fresh_run)
326
+
327
+
328
+ p = CoderCompanion::ParserFactory.get_parser(self.language)
329
+
330
+ # Running test for different languages
331
+ uploadable = p.create_project_json(@@src_folder)
332
+
333
+ if @@options[:show_output]
334
+ ap uploadable
335
+ end
336
+
337
+ if !uploadable
338
+ j_print error_format("Something went wrong while trying to create uploadable")
339
+ return
340
+ end
341
+
342
+
343
+ if !self.dry_run?
344
+ #puts pretty_inspect(uploadable)
345
+ # Maybe wrap this in a begin--rescue block
346
+ # Uncomment this
347
+ file_rel_path = './test_upload.codercompanion'
348
+ File.open(file_rel_path, 'w+') {|f| f.write(uploadable.to_json) }
349
+
350
+ #zip file here
351
+
352
+ j_print notification_format("uploading to #{@@config["host"]}...")
353
+
354
+ # Uncomment this
355
+ # Upload file to be processed
356
+ upload(file_rel_path)
357
+
358
+ File.delete(file_rel_path)
359
+ self.cache_handler.write_cached_data
360
+ j_print success_format("Uploaded your changes successfully")
361
+ end
362
+ end
363
+
364
+ def self.upload(file)
365
+ response = @@api.get_temporary_access_key
366
+ unless response
367
+ File.delete(file)
368
+ raise CoderCompanion::CoderCompanionException, "Failed to upload project"
369
+ end
370
+
371
+ response_data = JSON.parse(response)["data"]
372
+ credentials = response_data["credentials"]
373
+ region = response_data["region"]
374
+ s3_bucket = response_data["s3_bucket"]
375
+ s3_client = CoderCompanion::S3Client.new(
376
+ :session_token => credentials["session_token"],
377
+ :access_key_id => credentials["access_key_id"],
378
+ :secret_access_key => credentials["secret_access_key"],
379
+ :region => region,
380
+ :s3_bucket => s3_bucket
381
+ )
382
+
383
+ current_time = Time.now.utc.strftime("%Y-%m-%d:%H:%M:%S")
384
+ key = "#{@@config["user"]["access_id"]}/#{@@config["project"]["access_key"]}/#{current_time}/upload"
385
+
386
+ # put object using the s3_client
387
+ result = s3_client.put_object(file, key)
388
+ unless result
389
+ File.delete(file)
390
+ raise CoderCompanion::CoderCompanionException, "Failed to upload file to S3"
391
+ end
392
+
393
+ # notify the api that we have uploaded the file to s3
394
+ response = @@api.notify_s3_upload(key)
395
+ unless response
396
+ File.delete(file)
397
+ raise CoderCompanion::CoderCompanionException, "Failed to complete upload. It is possible our servers are down"
398
+ end
399
+ end
400
+
401
+ def self.start(options, location)
402
+ @@options = options
403
+
404
+ @@src_folder = location
405
+
406
+ self.init
407
+ self.run
408
+ end
409
+
410
+ end
@@ -0,0 +1,3 @@
1
+ module CoderCompanion
2
+ VERSION = '0.9.7'
3
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: codercmp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.7
5
+ platform: ruby
6
+ authors:
7
+ - Sega Okhiria
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: treetop
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: polyglot
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '0.3'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '0.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rest-client
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: colored
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: '1.2'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: '1.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: awesome_print
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '1.6'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '1.6'
83
+ - !ruby/object:Gem::Dependency
84
+ name: yard
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: 0.8.7
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: 0.8.7
97
+ description: codercmp is a gem intended for use as a command line interface to parse
98
+ and upload code architecture and object structure to CoderCompanion.
99
+ email:
100
+ - sega.okhiria@gmail.com
101
+ executables:
102
+ - codercmp
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - bin/codercmp
107
+ - lib/codercompanion.rb
108
+ - lib/codercompanion/api.rb
109
+ - lib/codercompanion/cachehandler.rb
110
+ - lib/codercompanion/codercompanion_exception.rb
111
+ - lib/codercompanion/common/common.rb
112
+ - lib/codercompanion/common/literals.rb
113
+ - lib/codercompanion/common_namespace.rb
114
+ - lib/codercompanion/cpp/cpp_parser.rb
115
+ - lib/codercompanion/cpp/cpp_tree_walker.rb
116
+ - lib/codercompanion/java/java.rb
117
+ - lib/codercompanion/java/java_grammar.treetop
118
+ - lib/codercompanion/java/java_parser.rb
119
+ - lib/codercompanion/java/java_tree_walker.rb
120
+ - lib/codercompanion/language_parser.rb
121
+ - lib/codercompanion/languages.rb
122
+ - lib/codercompanion/parser_factory.rb
123
+ - lib/codercompanion/ruby/ruby_annotations.rb
124
+ - lib/codercompanion/ruby/ruby_parser.rb
125
+ - lib/codercompanion/ruby/ruby_tree_walker.rb
126
+ - lib/codercompanion/s3_client.rb
127
+ - lib/codercompanion/tree_walker.rb
128
+ - lib/codercompanion_version.rb
129
+ homepage: ''
130
+ licenses:
131
+ - MIT
132
+ metadata: {}
133
+ post_install_message:
134
+ rdoc_options: []
135
+ require_paths:
136
+ - lib
137
+ required_ruby_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ required_rubygems_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - '>='
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ requirements:
148
+ - none
149
+ rubyforge_project: codercmp
150
+ rubygems_version: 2.4.5
151
+ signing_key:
152
+ specification_version: 4
153
+ summary: CoderCompanion language parser command line interface.
154
+ test_files: []
155
+ has_rdoc: