From 0b0d3c9587132a39b97feeedc89086935b26d3ce Mon Sep 17 00:00:00 2001 From: Ian Kronquist Date: Fri, 4 Sep 2015 14:25:06 -0700 Subject: [PATCH] (PDOC-23) Add JSON Registry back end --- .../strings/yard/json_registry_store.rb | 52 +++++++++++++++++++ .../puppetlabs/strings/yard/monkey_patches.rb | 17 ++++++ 2 files changed, 69 insertions(+) create mode 100644 lib/puppet_x/puppetlabs/strings/yard/json_registry_store.rb diff --git a/lib/puppet_x/puppetlabs/strings/yard/json_registry_store.rb b/lib/puppet_x/puppetlabs/strings/yard/json_registry_store.rb new file mode 100644 index 0000000..63ee59b --- /dev/null +++ b/lib/puppet_x/puppetlabs/strings/yard/json_registry_store.rb @@ -0,0 +1,52 @@ +module YARD + + class JsonRegistryStore < RegistryStore + def save(merge=true, file=nil) + super + + # FIXME: do we need this? + if file && file != @file + @file = file + @serializer = Serializers::JsonSerializer.new(@file) + end + @serializer = Serializers::JsonSerializer.new(@file) + + sdb = Registry.single_object_db + original_extension = @serializer.extension + @serializer.extension = 'json' + @serializer.basepath = 'yardoc_json' + interesting_entries = proc { |key, val| + [:puppetnamespace, :hostclass,].include? val.type or + (val.type == :method and (val['puppet_4x_function'] or + val['puppet_3x_function'])) + } + rename_methods = proc { |key, value| + [value.type == :method ? value.name.to_sym : key, + value] + } + if sdb == true || sdb == nil + @serializer.serialize(Hash[@store.select(&interesting_entries).map(&rename_methods)].to_json) + else + values(false).each do |object| + @serializer.serialize(Hash[object.select(&interesting_entries).map(&rename_methods)].to_json) + end + end + @serializer.extension = original_extension + true + end + end + + # Override the serializer because it puts the data at a whacky path and, more + # importantly, mashals the data with a bunch of non-printable characters. + module Serializers + class JsonSerializer < YardocSerializer + def serialize(data) + path = File.join(basepath, "registry_dump.#{extension}") + require 'pry'; binding.pry + log.debug "Serializing json to #{path}" + File.open!(path, "wb") {|f| f.write data } + end + end + end + +end diff --git a/lib/puppet_x/puppetlabs/strings/yard/monkey_patches.rb b/lib/puppet_x/puppetlabs/strings/yard/monkey_patches.rb index a1da734..f509fb6 100644 --- a/lib/puppet_x/puppetlabs/strings/yard/monkey_patches.rb +++ b/lib/puppet_x/puppetlabs/strings/yard/monkey_patches.rb @@ -1,4 +1,5 @@ require 'yard' +require File.join(File.dirname(__FILE__), './json_registry_store') # TODO: As far as I can tell, monkeypatching is the officially recommended way # to extend these tools to cover custom usecases. Follow up on the YARD mailing @@ -49,3 +50,19 @@ class YARD::Logger f.close() end end + + +# 15:04:42 radens | lsegal: where would you tell yard to use your custom RegistryStore? +# 15:09:54 @lsegal | https://github.com/lsegal/yard/blob/master/lib/yard/registry.rb#L428-L435 +# 15:09:54 @lsegal | you would set that attr on Registry +# 15:09:54 @lsegal | it might be worth expanding that API to swap out the store class used +# 15:10:49 @lsegal | specifically +# | https://github.com/lsegal/yard/blob/master/lib/yard/registry.rb#L190 and +# | replace RegistryStore there with a storage_class attr +module YARD::Registry + class << self + def clear + self.thread_local_store = YARD::JsonRegistryStore.new + end + end +end