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.

Constant Summary collapse

IGNORED_DIRECTORIES =
%w[
  __MACOSX
].freeze

Constants included from SketchUp::Config

SketchUp::Config::DEFAULT_CONFIGURATION

Instance Method Summary collapse

Methods included from SketchUp::ExtensionProject

#config_path, #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



18
19
20
21
22
23
24
25
26
27
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
# File 'lib/rubocop/sketchup/cop/requirements/file_structure.rb', line 18

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),
        severity: :error)
    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 { |c| c.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),
        severity: :error)
    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,
        severity: :error)
  end
end