Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -183,17 +183,19 @@ def new_yaml_artifact(file_name, desired_contents, extra_comment_lines: [])
desired_contents,
->(hash) { ::YAML.dump(hash) },
->(string) { ::YAML.safe_load(string) },
extra_comment_lines
extra_comment_lines,
"#"
)
end

def new_raw_artifact(file_name, desired_contents)
def new_raw_artifact(file_name, desired_contents, comment_prefix: "#")
SchemaArtifact.new(
::File.join(@schema_artifacts_directory, file_name),
desired_contents,
_ = :itself.to_proc,
_ = :itself.to_proc,
[]
[],
comment_prefix
)
end

Expand All @@ -215,7 +217,7 @@ def pruned_runtime_metadata(graphql_schema_string)
end

# @private
class SchemaArtifact < Support::MemoizableData.define(:file_name, :desired_contents, :dumper, :loader, :extra_comment_lines)
class SchemaArtifact < Support::MemoizableData.define(:file_name, :desired_contents, :dumper, :loader, :extra_comment_lines, :comment_prefix)
def dump(output)
if out_of_date?
dirname = File.dirname(file_name)
Expand Down Expand Up @@ -271,7 +273,7 @@ def comment_preamble
]

lines = extra_comment_lines + [""] + lines unless extra_comment_lines.empty?
lines.map { |line| "# #{line}".strip }.join("\n")
lines.map { |line| "#{comment_prefix} #{line}".rstrip }.join("\n")
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ module ElasticGraph
?extra_comment_lines: ::Array[::String]
) -> SchemaArtifact[::Hash[::String, untyped]]

def new_raw_artifact: (::String, ::String) -> SchemaArtifact[::String]
def new_raw_artifact: (
::String,
::String,
?comment_prefix: ::String
) -> SchemaArtifact[::String]
def pruned_runtime_metadata: (::String) -> SchemaArtifacts::RuntimeMetadata::Schema
end

Expand All @@ -44,13 +48,15 @@ module ElasticGraph
attr_reader dumper: ^(T) -> ::String
attr_reader loader: ^(::String) -> T
attr_reader extra_comment_lines: ::Array[::String]
attr_reader comment_prefix: ::String

def initialize: (
::String,
T,
^(T) -> ::String,
^(::String) -> T,
::Array[::String]) -> void
::Array[::String],
::String) -> void
end

class SchemaArtifact[T] < SchemaArtifactSupertype[T]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright 2024 - 2026 Block, Inc.
#
# Use of this source code is governed by an MIT-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/MIT.
#
# frozen_string_literal: true

require "elastic_graph/schema_definition/schema_artifact_manager"
require "stringio"

module ElasticGraph
module SchemaDefinition
RSpec.describe SchemaArtifact, :in_temp_dir do
it "renders the comment preamble using the configured comment prefix so artifacts can use their format's comment syntax" do
artifact = SchemaArtifact.new(
"widgets.proto",
"message Widget {}",
:itself.to_proc,
:itself.to_proc,
[],
"//"
)

artifact.dump(::StringIO.new)

expect(::File.read("widgets.proto")).to eq(<<~EOS.strip)
// Generated by `bundle exec rake schema_artifacts:dump`.
// DO NOT EDIT BY HAND. Any edits will be lost the next time the rake task is run.
message Widget {}
EOS
end
end
end
end