Class: RuboCop::Cop::SketchupRequirements::FileStructure

Inherits:
SketchUp::Cop
  • Object
show all
Includes:
RangeHelp, SketchUp::ExtensionProject, SketchUp::NoCommentDisable
Defined in:
lib/rubocop/sketchup/cop/requirements/file_structure.rb

Overview

Check that the extension conform to expected file structure with a single root .rb file and a support folder with matching name.

Make sure to match upper and lower case characters between the root .rb file and the support folder.

Examples:

SketchUp/Plugins
+ ex_hello_world.rb
+ ex_hello_world
  + main.rb
  + ...

Constant Summary collapse

IGNORED_DIRECTORIES =
%w[
  __MACOSX
].freeze

Constants included from SketchUp::Config

SketchUp::Config::DEFAULT_CONFIGURATION

Constants inherited from SketchUp::Cop

SketchUp::Cop::SKETCHUP_DEPARTMENT_SEVERITY

Instance Method Summary collapse

Methods included from SketchUp::ExtensionProject

#config_path, #extension_directory, #extension_root_file, #extension_root_files, #path_relative_to_source, #relative_source_path, #root_file?, #source_path

Methods inherited from SketchUp::Cop

inherited, #relevant_file?

Instance Method Details

#investigate(processed_source) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rubocop/sketchup/cop/requirements/file_structure.rb', line 28

def investigate(processed_source)
  return if already_run?

  # Using range similar to RuboCop::Cop::Naming::Filename (file_name.rb)
  range = source_range(processed_source.buffer, 1, 0)

  # Find all root Ruby files in the source directory.
  pattern = "#{source_path}/*.rb"
  root_ruby_files = Dir.glob(pattern)

  # Ensure there is only one root Ruby file.
  if root_ruby_files.size != 1
    msg = 'Extensions must have exactly one root Ruby (.rb) file. '\
          'Found: %d'
    add_offense(nil,
                location: range,
                message: format(msg, root_ruby_files.size))
    return
  end

  # Find the root file and collect the sub-directories.
  root_file = root_ruby_files.first
  extension_basename = File.basename(root_file, '.*')
  sub_folders = source_path.children.select(&:directory?)
  sub_folders.reject! { |folder|
    IGNORED_DIRECTORIES.include?(folder.basename.to_s)
  }

  # Ensure there is only one sub-directory.
  if sub_folders.size != 1
    msg = 'Extensions must have exactly one support directory. Found %d'
    add_offense(nil,
                location: range,
                message: format(msg, sub_folders.size))
    return
  end

  # Ensure support directory's name match the root Ruby file.
  support_directory = sub_folders.first
  unless support_directory.basename.to_s == extension_basename
    msg = 'Extensions must have a support directory matching the name '\
          'of the root Ruby file. Expected %s, found %s'
    msg = format(msg, extension_basename, support_directory.basename)
    add_offense(nil,
                location: range,
                message: msg)
  end
end