Skip to content

Commit

Permalink
Conform code to Rubocop.
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelTallet committed Nov 24, 2019
1 parent 03c8478 commit c4ec683
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 52 deletions.
19 changes: 17 additions & 2 deletions source/.rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
# See: https://github.com/bbatsov/rubocop If you want to know what is RuboCop.

AllCops:
TargetRubyVersion: 2.2
TargetRubyVersion: 2.3
Exclude:
- 'pbr/shapes.rb'
- 'pbr/lights.rb'

Layout:
Enabled: false

Style/AsciiComments:
Style:
Enabled: false

Naming/RescuedExceptionsVariableName:
PreferredName: exception

Metrics/MethodLength:
Max: 15

Metrics/AbcSize:
Max: 20

Metrics/ClassLength:
Max: 150
2 changes: 1 addition & 1 deletion source/pbr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def self.required_plugin_exist?
Centaur.const_get(:GltfExporter)
true

rescue NameError => _error
rescue NameError => _exception
false

end
Expand Down
6 changes: 3 additions & 3 deletions source/pbr/app_observer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Physically-Based Rendering extension for SketchUp 2017 or newer.
# Copyright: © 2018 Samuel Tallet-Sabathé <samuel.tallet@gmail.com>
# Copyright: © 2019 Samuel Tallet <samuel.tallet arobase gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -29,7 +29,7 @@ module PBR
# Observes SketchUp events and reacts.
class AppObserver < Sketchup::AppObserver

# rubocop: disable MethodName
# rubocop: disable Naming/MethodName

# When SketchUp user creates a new, empty model.
def onNewModel(_model)
Expand Down Expand Up @@ -60,7 +60,7 @@ def onQuit

end

# rubocop: enable MethodName
# rubocop: enable Naming/MethodName

end

Expand Down
10 changes: 8 additions & 2 deletions source/pbr/chromium.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,15 @@ def self.make_exec
def self.preferences

if Sketchup.platform == :platform_osx
File.join('~', 'Library', 'Application Support', 'Chromium', 'Default', 'Preferences')
File.join(
'~', 'Library', 'Application Support', 'Chromium',
'Default', 'Preferences'
)
else
File.join(ENV['LOCALAPPDATA'], 'Chromium', 'User Data', 'Default', 'Preferences')
File.join(
ENV['LOCALAPPDATA'], 'Chromium',
'User Data', 'Default', 'Preferences'
)
end

end
Expand Down
20 changes: 15 additions & 5 deletions source/pbr/gltf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def initialize

complete

rescue StandardError => _error
rescue StandardError => _exception

@valid = false

Expand Down Expand Up @@ -95,6 +95,19 @@ def json

end

# Applies various fixes.
#
# @return [void]
private def apply_various_fixes

Textures.fix_all_without_filename_or_not_supported
Lights.fix_without_color
NilMaterialFix.new(TRANSLATE['Propagate Materials to Whole Model'])

nil

end

# Generates almost all of glTF asset thanks to exporter made by Centaur.
# @see https://extensions.sketchup.com/content/gltf-exporter
#
Expand All @@ -105,10 +118,7 @@ def json

File.delete(gltfile) if File.exist?(gltfile)

# Apply various fixes.
Textures.fix_without_filename_or_not_supported
Lights.fix_without_color
NilMaterialFix.new(TRANSLATE['Propagate Materials to Whole Model'])
apply_various_fixes

Sketchup.active_model.layers.add(Lights::LAYER_NAME)

Expand Down
4 changes: 2 additions & 2 deletions source/pbr/model_observer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module PBR
# Observes SketchUp model events and reacts.
class ModelObserver < Sketchup::ModelObserver

# rubocop: disable MethodName
# rubocop: disable Naming/MethodName

# Before a SketchUp model is saved to disk.
def onPreSaveModel(_model)
Expand All @@ -45,7 +45,7 @@ def onPostSaveModel(_model)

end

# rubocop: enable MethodName
# rubocop: enable Naming/MethodName

end

Expand Down
6 changes: 3 additions & 3 deletions source/pbr/shapes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ def add_extruded_points(pts, center, dir, angle, numsegments)
# @param [String] radius Radius. Example: '1m'.
# @param [Integer] n90 Segments per 90 degrees. Default: 10.
# @param [Integer] smooth Smooth parameter. Default: 12.
# @param [Integer|String] layer_index_or_name Layer index or name. Default: 0.
# @param [Integer|String] layer_idx_or_name Layer index or name. Default: 0.
#
# @return [Sketchup::Group] Group that contains sphere.
def self.create_sphere(radius, n90 = 10, smooth = 12, layer_index_or_name = 0)
def self.create_sphere(radius, n90 = 10, smooth = 12, layer_idx_or_name = 0)

group = Sketchup.active_model.entities.add_group

Expand Down Expand Up @@ -167,7 +167,7 @@ def self.create_sphere(radius, n90 = 10, smooth = 12, layer_index_or_name = 0)
group.entities.add_faces_from_mesh(mesh, smooth)

# Assign group to layer.
group.layer = Sketchup.active_model.layers[layer_index_or_name]
group.layer = Sketchup.active_model.layers[layer_idx_or_name]

group

Expand Down
39 changes: 25 additions & 14 deletions source/pbr/textures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module Textures
# Fixes textures without filename or not supported by glTF (.bmp, .tif...).
#
# @return [void]
def self.fix_without_filename_or_not_supported
def self.fix_all_without_filename_or_not_supported

# XXX Works only on SketchUp >= 2018.
return if Sketchup.version.to_i < 18
Expand All @@ -45,29 +45,40 @@ def self.fix_without_filename_or_not_supported
if mat.texture.filename.empty?\
or mat.texture.filename !~ /\.(jpg|png)$/

texture_path = File.join(
Sketchup.temp_dir, 'sketchup-tex-' + mat.object_id.to_s + '.png'
)
fix_one_without_filename_or_not_supported(mat)

texture_width = mat.texture.width
end

texture_height = mat.texture.height
end

mat.texture.image_rep.save_file(texture_path)
}

mat.texture = texture_path
nil

mat.texture.size = [texture_width, texture_height]
end

File.delete(texture_path)
# Fixes texture without filename or not supported by glTF (.bmp, .tif...).
#
# @param [Sketchup::Material] mat SketchUp material texture to fix.
#
# @return [void]
private def fix_one_without_filename_or_not_supported(mat)

end
texture_path = File.join(
Sketchup.temp_dir, 'sketchup-tex-' + mat.object_id.to_s + '.png'
)

end
texture_width = mat.texture.width

}
texture_height = mat.texture.height

nil
mat.texture.image_rep.save_file(texture_path)

mat.texture = texture_path

mat.texture.size = [texture_width, texture_height]

File.delete(texture_path)

end

Expand Down
56 changes: 38 additions & 18 deletions source/pbr/updates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,41 +26,61 @@
# PBR plugin namespace.
module PBR

# Plugin updates.
# PBR plugin updates.
module Updates

# URL of plugin homepage at SketchUcation site.
# URL of PBR plugin homepage at SketchUcation website.
SKETCHUCATION_URL = 'https://sketchucation.com/plugin/2101-pbr'.freeze

# Check for plugin updates.
# Returns PBR plugin last version number.
#
# @return [String] Last version number.
def self.last_version

sketchucation_html = URI.open(
SKETCHUCATION_URL, :read_timeout => 5
).read

last_version_arr = sketchucation_html.match(/v(\d+\.\d+.\d+)/).to_a

last_version_arr[1]

end

# Alerts user if a new PBR plugin version is available.
#
# @param [String] last_version_str Last version number.
#
# @return [void]
def self.check
def self.alert_user(last_version_str)

begin
if VERSION.gsub('.', '').to_i < last_version_str.gsub('.', '').to_i

sketchucation_html = open(SKETCHUCATION_URL, { read_timeout: 5 }).read
UI.messagebox(
TRANSLATE['A newer version of the PBR plugin is available:']\
+ ' ' + last_version_str
)

last_version_array = sketchucation_html.match(/v(\d+\.\d+.\d+)/).to_a
UI.openURL(SKETCHUCATION_URL)

last_version_int = last_version_array[1].gsub('.', '').to_i
end

if VERSION.gsub('.', '').to_i < last_version_int
nil

UI.messagebox(
TRANSLATE['A newer version of the PBR plugin is available:']\
+ ' ' + last_version_array[1]
)
end

puts last_version_array[1]
# Checks for plugin updates.
#
# @return [void]
def self.check

UI.openURL(SKETCHUCATION_URL)
begin

end
alert_user(last_version)

rescue StandardError => error
rescue StandardError => exception
puts 'Impossible to check if a newer PBR plugin version exists'\
+ ' because: ' + error.to_s
+ ' because: ' + exception.to_s
end

end
Expand Down
4 changes: 2 additions & 2 deletions source/pbr/viewport.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def self.open

)

rescue StandardError => _error
rescue StandardError => _exception
UI.messagebox("Unable to find: \"#{Chromium.executable}\". Get latest.")

end
Expand All @@ -108,7 +108,7 @@ def self.close
Chromium.simulate_normal_exit
true

rescue StandardError => _error
rescue StandardError => _exception
false

end
Expand Down

0 comments on commit c4ec683

Please sign in to comment.