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.



39
40
41
42
43
44
45
46
47
# File 'lib/danger/plugin_support/plugin_parser.rb', line 39

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.



37
38
39
# File 'lib/danger/plugin_support/plugin_parser.rb', line 37

def registry
  @registry
end

Instance Method Details

#classes_in_fileObject



63
64
65
# File 'lib/danger/plugin_support/plugin_parser.rb', line 63

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

#parseObject



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/danger/plugin_support/plugin_parser.rb', line 49

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



67
68
69
# File 'lib/danger/plugin_support/plugin_parser.rb', line 67

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

#to_dict(classes) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/danger/plugin_support/plugin_parser.rb', line 76

def to_dict(classes)
  d_meth = lambda do |meth|
    return nil if meth.nil?
    {
      name: meth.name,
      body_md: meth.docstring,
      params: meth.parameters,
      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, so that we can access it's instance_name
    require klass.file
    real_klass = Danger.const_get klass.name
    attribute_meths = klass.attributes[:instance].values.map(&:values).flatten

    methods = klass.meths - klass.inherited_meths - attribute_meths
    usable_methods = methods.select { |m| m.visibility == :public }.reject { |m| m.name == :initialize || m.name == :insance_name }

    {
      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: usable_methods.map { |m| d_meth.call(m) },
      tags: klass.tags.select { |t| t.tag_name == "tags" }.map(&:text).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



71
72
73
74
# File 'lib/danger/plugin_support/plugin_parser.rb', line 71

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