Class: Danger::PluginParser

Inherits:
Object
  • Object
show all
Defined in:
lib/danger/plugin_support/plugin_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(paths) ⇒ PluginParser

Returns a new instance of PluginParser.



7
8
9
10
11
12
13
14
15
# File 'lib/danger/plugin_support/plugin_parser.rb', line 7

def initialize(paths)
  raise "Path cannot be empty" if paths.empty?

  if paths.is_a? String
    @paths = [File.expand_path(paths)]
  else
    @paths = paths
  end
end

Instance Attribute Details

#registryObject

Returns the value of attribute registry.



5
6
7
# File 'lib/danger/plugin_support/plugin_parser.rb', line 5

def registry
  @registry
end

Instance Method Details

#classes_in_fileObject



31
32
33
# File 'lib/danger/plugin_support/plugin_parser.rb', line 31

def classes_in_file
  registry.all(:class).select { |klass| @paths.include? klass.file }
end

#parseObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/danger/plugin_support/plugin_parser.rb', line 17

def parse
  require 'yard'
  # could this go in a singleton-y place instead?
  # like class initialize?
  YARD::Tags::Library.define_tag('tags', :tags)
  YARD::Tags::Library.define_tag('availablity', :availablity)
  files = ["lib/danger/plugin_support/plugin.rb"] + @paths

  # This turns on YARD debugging
  # $DEBUG = true
  
  self.registry = YARD::Registry.load(files, true)
end

#plugins_from_classes(classes) ⇒ Object



35
36
37
# File 'lib/danger/plugin_support/plugin_parser.rb', line 35

def plugins_from_classes(classes)
  classes.select { |klass| klass.inheritance_tree.map(&:name).include? :Plugin }
end

#to_dict(classes) ⇒ Object



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
76
77
78
79
80
81
82
83
84
85
# File 'lib/danger/plugin_support/plugin_parser.rb', line 44

def to_dict(classes)
  d_meth = lambda do |meth|
    return nil if meth.nil?
    {
      name: meth.name,
      body_md: meth.docstring,
      tags: meth.tags.map do |t|
        {
           name: t.tag_name,
           types: t.types
        }
      end
    }
  end

  d_attr = lambda do |attribute|
    {
      read: d_meth.call(attribute[:read]),
      write: d_meth.call(attribute[:write])
    }
  end

  classes.map do |klass|
    # Adds the class being parsed into the ruby runtime
    require klass.file
    real_klass = Danger.const_get klass.name
    attribute_meths = klass.attributes[:instance].values.map(&:values).flatten
    {
      name: klass.name.to_s,
      body_md: klass.docstring,
      instance_name: real_klass.instance_name,
      example_code: klass.tags.select { |t| t.tag_name == "example" }.map { |tag| {:title => tag.name, :text => tag.text} }.compact,
      attributes: klass.attributes[:instance].map do |pair|
        { pair.first => d_attr.call(pair.last) }
      end,
      methods: (klass.meths - klass.inherited_meths - attribute_meths ).select { |m| m.visibility == :public }.map { |m| d_meth.call(m) },
      tags: klass.tags.select { |t| t.tag_name == "tags" }.map(&:name).compact,
      see: klass.tags.select { |t| t.tag_name == "see" }.map(&:name).map(&:split).flatten.compact,
      file: klass.file.gsub(File.expand_path("."), "")
    }
  end
end

#to_jsonObject



39
40
41
42
# File 'lib/danger/plugin_support/plugin_parser.rb', line 39

def to_json
  plugins = plugins_from_classes(classes_in_file)
  to_dict(plugins).to_json
end