2019-07-11 04:06:35 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-06-15 05:10:21 -04:00
|
|
|
require_relative 'helper'
|
2019-07-11 04:06:35 -04:00
|
|
|
|
2021-06-15 05:10:21 -04:00
|
|
|
module Tooling
|
2019-07-11 04:06:35 -04:00
|
|
|
module Graphql
|
|
|
|
module Docs
|
|
|
|
# Gitlab renderer for graphql-docs.
|
|
|
|
# Uses HAML templates to parse markdown and generate .md files.
|
|
|
|
# It uses graphql-docs helpers and schema parser, more information in https://github.com/gjtorikian/graphql-docs.
|
|
|
|
#
|
|
|
|
# Arguments:
|
2021-03-29 11:09:30 -04:00
|
|
|
# schema - the GraphQL schema definition. For GitLab should be: GitlabSchema
|
2019-07-11 04:06:35 -04:00
|
|
|
# output_dir: The folder where the markdown files will be saved
|
|
|
|
# template: The path of the haml template to be parsed
|
|
|
|
class Renderer
|
2021-06-15 05:10:21 -04:00
|
|
|
include Tooling::Graphql::Docs::Helper
|
2019-07-11 04:06:35 -04:00
|
|
|
|
2021-03-29 11:09:30 -04:00
|
|
|
attr_reader :schema
|
|
|
|
|
2019-07-11 04:06:35 -04:00
|
|
|
def initialize(schema, output_dir:, template:)
|
|
|
|
@output_dir = output_dir
|
|
|
|
@template = template
|
|
|
|
@layout = Haml::Engine.new(File.read(template))
|
2021-03-29 11:09:30 -04:00
|
|
|
@parsed_schema = GraphQLDocs::Parser.new(schema.graphql_definition, {}).parse
|
|
|
|
@schema = schema
|
2021-04-22 08:09:49 -04:00
|
|
|
@seen = Set.new
|
2019-07-11 04:06:35 -04:00
|
|
|
end
|
|
|
|
|
2019-10-16 14:08:01 -04:00
|
|
|
def contents
|
|
|
|
# Render and remove an extra trailing new line
|
|
|
|
@contents ||= @layout.render(self).sub!(/\n(?=\Z)/, '')
|
2019-07-11 04:06:35 -04:00
|
|
|
end
|
|
|
|
|
2019-10-16 14:08:01 -04:00
|
|
|
def write
|
2019-07-11 04:06:35 -04:00
|
|
|
filename = File.join(@output_dir, 'index.md')
|
|
|
|
|
|
|
|
FileUtils.mkdir_p(@output_dir)
|
|
|
|
File.write(filename, contents)
|
|
|
|
end
|
2021-04-22 08:09:49 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def seen_type?(name)
|
|
|
|
@seen.include?(name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def seen_type!(name)
|
|
|
|
@seen << name
|
|
|
|
end
|
2019-07-11 04:06:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|