diff --git a/elasticgraph-schema_definition/lib/elastic_graph/schema_definition/schema_artifact_manager.rb b/elasticgraph-schema_definition/lib/elastic_graph/schema_definition/schema_artifact_manager.rb index 0ae2e7ff7..10c29023b 100644 --- a/elasticgraph-schema_definition/lib/elastic_graph/schema_definition/schema_artifact_manager.rb +++ b/elasticgraph-schema_definition/lib/elastic_graph/schema_definition/schema_artifact_manager.rb @@ -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 @@ -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) @@ -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 diff --git a/elasticgraph-schema_definition/sig/elastic_graph/schema_definition/schema_artifact_manager.rbs b/elasticgraph-schema_definition/sig/elastic_graph/schema_definition/schema_artifact_manager.rbs index 713693ac3..88c63edb6 100644 --- a/elasticgraph-schema_definition/sig/elastic_graph/schema_definition/schema_artifact_manager.rbs +++ b/elasticgraph-schema_definition/sig/elastic_graph/schema_definition/schema_artifact_manager.rbs @@ -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 @@ -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] diff --git a/elasticgraph-schema_definition/spec/unit/elastic_graph/schema_definition/schema_artifact_spec.rb b/elasticgraph-schema_definition/spec/unit/elastic_graph/schema_definition/schema_artifact_spec.rb new file mode 100644 index 000000000..f071d0600 --- /dev/null +++ b/elasticgraph-schema_definition/spec/unit/elastic_graph/schema_definition/schema_artifact_spec.rb @@ -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