Skip to content

Commit

Permalink
Use rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-pigeon committed Jun 12, 2020
1 parent 4eaff08 commit eb8e099
Show file tree
Hide file tree
Showing 127 changed files with 2,435 additions and 2,223 deletions.
120 changes: 119 additions & 1 deletion .rubocop.yml
Expand Up @@ -5,30 +5,148 @@ require:

AllCops:
TargetRubyVersion: 2.6
Exclude:
- 'lib/rammus/protocol/*'
- 'generate_protocol.rb'
- 'spec/support/test_server.rb' # TODO cut down on noise

# TODO: cutting down on noise
Naming/AccessorMethodName:
Enabled: false

# TODO: cutting down on noise
Naming/PredicateName:
Enabled: false

# TODO: cut down on noise
Style/ExpandPathArguments:
Enabled: false

Style/DoubleNegation:
Enabled: false

Style/Documentation:
Enabled: false

Style/AsciiComments:
Enabled: false

Style/StringLiterals:
Enabled: false
EnforcedStyle: double_quotes

Style/Semicolon:
Enabled: false

Style/RescueModifier:
Enabled: false

Layout/IndentationConsistency:
Enabled: true
EnforcedStyle: indented_internal_methods

Layout/LineLength:
Enabled: false

Layout/MultilineMethodCallIndentation:
EnforcedStyle: indented

Layout/HashAlignment:
Exclude:
- 'lib/rammus/protocol_logger.rb'

Layout/RescueEnsureAlignment:
Exclude:
- 'lib/rammus/web_socket_client.rb'

Layout/ArgumentAlignment:
Enabled: false

Lint/Void:
Enabled: false

Lint/SuppressedException:
Enabled: false

Lint/ShadowedException:
Enabled: false

Metrics/ParameterLists:
Enabled: false

Metrics/ClassLength:
Enabled: false

Metrics/MethodLength:
Enabled: false

Metrics/ModuleLength:
Enabled: false

Metrics/BlockLength:
Enabled: false

Metrics/PercievedComplexity:
Metrics/AbcSize:
Enabled: false

Metrics/CyclomaticComplexity:
Enabled: false

Metrics/PerceivedComplexity:
Enabled: false

Style/MultilineBlockChain:
Exclude:
- 'lib/rammus/web_socket_client.rb'
- 'lib/rammus/lifecycle_watcher.rb'

Style/LambdaCall:
Enabled: false

Style/Lambda:
EnforcedStyle: literal

Style/TrailingUnderscoreVariable:
Enabled: false

Style/IfUnlessModifier:
Enabled: false

Style/WordArray:
Enabled: false

Style/CommentAnnotation:
Enabled: false

Style/ParallelAssignment:
Enabled: false

Style/SymbolArray:
Enabled: false

Style/RescueStandardError:
Enabled: false

Style/BlockComments:
Enabled: false

Naming/MethodParameterName:
Exclude:
- 'lib/rammus/mouse.rb'
- 'lib/rammus/touchscreen.rb'

Naming/MemoizedInstanceVariableName:
Enabled: false

Naming/RescuedExceptionsVariableName:
Enabled: false

Naming/VariableNumber:
EnforcedStyle: snake_case

Performance/RedundantBlockCall:
Enabled: false

#Naming/PredicateName:
# # Method define macros for dynamically generated method.
# MethodDefinitionMacros:
Expand Down
37 changes: 19 additions & 18 deletions generate_device_descriptions.rb
@@ -1,33 +1,34 @@
#!/usr/local/bin/ruby
# frozen_string_literal: true

require "json"
require "erb"
require "fileutils"

descriptions = JSON.parse(File.read("./device_descriptors.json"))

template = <<-RUBY
module Rammus
DEVICE_DESCRIPTORS = {
<% descriptions.each.with_index do |description, i| -%>
"<%= description["name"] %>" => {
user_agent: "<%= description["userAgent"] %>",
viewport: {
<% description["viewport"].each_with_index do |(key, value), i| -%>
<%= underscore(key) %>: <%= value %><% if i < description["viewport"].size - 1 then %>,<% end %>
<% end -%>
}
}<% if i < descriptions.size - 1 then %>,<% end %>
<% end -%>
}
private_constant :DEVICE_DESCRIPTORS
end
template = <<~RUBY
module Rammus
DEVICE_DESCRIPTORS = {
<% descriptions.each.with_index do |description, i| -%>
"<%= description["name"] %>" => {
user_agent: "<%= description["userAgent"] %>",
viewport: {
<% description["viewport"].each_with_index do |(key, value), i| -%>
<%= underscore(key) %>: <%= value %><% if i < description["viewport"].size - 1 then %>,<% end %>
<% end -%>
}
}<% if i < descriptions.size - 1 then %>,<% end %>
<% end -%>
}
private_constant :DEVICE_DESCRIPTORS
end
RUBY

def underscore(word)
word
.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
.gsub(/([a-z\d])([A-Z])/,'\1_\2')
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
.tr("-", "_")
.downcase
end
Expand Down
86 changes: 46 additions & 40 deletions generate_protocol.rb
@@ -1,53 +1,58 @@
#!/usr/local/bin/ruby
# frozen_string_literal: true

require "json"
require "erb"
require "fileutils"

template = <<-RUBY
module Rammus
module Protocol
module <%= domain.name %>
extend self\
<% domain.commands.each do |command| -%>
<% if command.description.size > 0 -%>
<% command.description.lines.each do |line| -%>
# <%= line -%>
<% end %>
#
<% end -%>
<% if command.has_parameter_descriptions? -%>
<% command.parameters_with_descriptions.each do |parameter| -%>
# <%= parameter.yard_doc %>
<% end -%>
#
<% end -%>
def <%= underscore(command.name) %><%= command.parameters_header %>
{
method: "<%= domain.name %>.<%= command.name %>"<% if command.has_parameters? then %>,
params: { <%= command.parameters_body %> }.compact<% end %>
}
end\
<% end %>
<% domain.events.each do |event| -%>
def <%= event.method_name %>
<%= event.method %>
template = <<~RUBY
# frozen_string_literal: true
module Rammus
module Protocol
module <%= domain.name %>
extend self\
<% domain.commands.each do |command| -%>
<% if command.description.size > 0 -%>
<% command.description.lines.each do |line| -%>
# <%= line -%>
<% end %>
#
<% end -%>
<% if command.has_parameter_descriptions? -%>
<% command.parameters_with_descriptions.each do |parameter| -%>
# <%= parameter.yard_doc %>
<% end -%>
#
<% end -%>
def <%= underscore(command.name) %><%= command.parameters_header %>
{
method: "<%= domain.name %>.<%= command.name %>"<% if command.has_parameters? then %>,
params: { <%= command.parameters_body %> }.compact<% end %>
}
end\
<% end %>
<% domain.events.each do |event| -%>
def <%= event.method_name %>
<%= event.method %>
end
<% end -%>
end
<% end -%>
end
end
end
RUBY

protocol_template = <<-RUBY
module Rammus
module Protocol<% protocol.domains.each do |domain| %>
protocol_template = <<~RUBY
# frozen_string_literal: true
module Rammus
module Protocol<% protocol.domains.each do |domain| %>
autoload :<%= domain.name %>, "rammus/protocol/<%= underscore(domain.name)%>.rb"<% end %>
end
end
end
RUBY

Protocol = Struct.new(:domains) do
Expand All @@ -60,7 +65,7 @@ def self.from_json(json)
new(
json["domain"],
json["commands"].map { |command| Command.from_json command },
json.fetch("events",[]).map { |event| Event.from_json json["domain"], event }
json.fetch("events", []).map { |event| Event.from_json json["domain"], event }
)
end
end
Expand Down Expand Up @@ -117,6 +122,7 @@ def self.from_json(command, json)

def name
return self["name"] unless command == "getPossibleBreakpoints" && ["start", "end"].include?(self["name"])

"breakpoint_#{self["name"]}"
end

Expand Down Expand Up @@ -154,8 +160,8 @@ def method

def underscore(word)
word
.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
.gsub(/([a-z\d])([A-Z])/,'\1_\2')
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
.tr("-", "_")
.downcase
end
Expand Down
6 changes: 4 additions & 2 deletions lib/rammus.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'json'
require 'tmpdir'
require 'set'
Expand Down Expand Up @@ -29,8 +31,8 @@ def self.devices
DEVICE_DESCRIPTORS
end

def self.launch(headless: true, args: [])
Launcher.launch headless: headless, args: args
def self.launch(headless: true)
Launcher.launch headless: headless
end

def self.connect(ws_endpoint:)
Expand Down

0 comments on commit eb8e099

Please sign in to comment.