~ubuntu-branches/ubuntu/vivid/foodcritic/vivid-proposed

« back to all changes in this revision

Viewing changes to chef_dsl_metadata/Rakefile

  • Committer: Package Import Robot
  • Author(s): Stefano Rivera
  • Date: 2013-06-11 00:34:40 UTC
  • Revision ID: package-import@ubuntu.com-20130611003440-czusooxs9rrpe6sb
Tags: upstream-2.1.0
ImportĀ upstreamĀ versionĀ 2.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require 'appraisal'
 
2
require 'fileutils'
 
3
require 'yajl'
 
4
 
 
5
task :generate_chef_metadata do
 
6
  require_chef
 
7
  chef_dsl_metadata = {:dsl_methods => chef_dsl_methods,
 
8
                       :node_methods => chef_node_methods,
 
9
                       :actions => chef_resource_actions,
 
10
                       :attributes => chef_resource_attributes}
 
11
  json = Yajl::Encoder.encode(chef_dsl_metadata, :pretty => true)
 
12
  File.open("chef_#{Chef::VERSION}.json", 'w'){|f| f.write(json)}
 
13
end
 
14
 
 
15
def require_chef
 
16
  require 'chef'
 
17
  require 'chef/mixin/convert_to_class_name'
 
18
  include Chef::Mixin::ConvertToClassName
 
19
end
 
20
 
 
21
def chef_dsl_methods
 
22
  (Chef::Node.public_instance_methods +
 
23
   chef_dsl_module.included_modules.map do |mixin|
 
24
     mixin.public_instance_methods
 
25
   end).flatten.sort.uniq
 
26
end
 
27
 
 
28
def chef_node_methods
 
29
  Chef::Node.public_instance_methods.flatten.sort.uniq
 
30
end
 
31
 
 
32
def chef_resource_actions
 
33
  chef_resources do |resource_klazz,resource|
 
34
    instance = resource.new('dsl')
 
35
    if instance.respond_to?(:allowed_actions)
 
36
      [convert_to_snake_case(resource_klazz.to_s),
 
37
        instance.allowed_actions.sort]
 
38
    end
 
39
  end
 
40
end
 
41
 
 
42
def chef_resource_attributes
 
43
  chef_resources do |resource_klazz,resource|
 
44
    [convert_to_snake_case(resource_klazz.to_s),
 
45
       resource.public_instance_methods(true).sort]
 
46
  end
 
47
end
 
48
 
 
49
private
 
50
 
 
51
def chef_dsl_module
 
52
  if Chef.const_defined?('DSL')
 
53
    Chef::DSL::Recipe
 
54
  else
 
55
    Chef::Mixin::RecipeDefinitionDSLCore
 
56
  end
 
57
end
 
58
 
 
59
def chef_resources
 
60
  resources = Chef::Resource.constants.sort.map do |resource_klazz|
 
61
    resource = Chef::Resource.const_get(resource_klazz)
 
62
    if resource.respond_to?(:public_instance_methods) and
 
63
       resource.ancestors.include?(Chef::Resource) and
 
64
       resource.name != 'Chef::Resource::LWRPBase'
 
65
      yield resource_klazz, resource
 
66
    end
 
67
  end
 
68
  Hash[resources]
 
69
end