|
| 1 | +# Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +# license agreements. See the NOTICE file distributed with |
| 3 | +# this work for additional information regarding copyright |
| 4 | +# ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +# the Apache License, Version 2.0 (the "License"); you may |
| 6 | +# not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +module Elasticsearch |
| 19 | + module API |
| 20 | + # Helper for the Elasticsearch build hash in source code docs |
| 21 | + module BuildHashHelper |
| 22 | + class << self |
| 23 | + def build_hash |
| 24 | + if ENV['BUILD_HASH'] |
| 25 | + File.read(File.expand_path('../../../tmp/rest-api-spec/build_hash',__dir__)) |
| 26 | + else |
| 27 | + original_build_hash |
| 28 | + end |
| 29 | + end |
| 30 | + |
| 31 | + def add_hash(build_hash) |
| 32 | + Dir.glob("#{FilesHelper.output_dir}/**/*.rb").each do |file| |
| 33 | + content = File.read(file) |
| 34 | + new_content = content.gsub(/(^#\sunder\sthe\sLicense.\n#)/) do |_| |
| 35 | + match = Regexp.last_match |
| 36 | + "#{match[1]}\n#{build_hash_comment(build_hash)}" |
| 37 | + end |
| 38 | + File.open(file, 'w') { |f| f.puts new_content } |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + def build_hash_comment(build_hash) |
| 43 | + [ |
| 44 | + "Auto generated from build hash #{build_hash}", |
| 45 | + '@see https://github.com/elastic/elasticsearch/tree/main/rest-api-spec', |
| 46 | + '' |
| 47 | + ].map { |b| "# #{b}" }.join("\n").strip |
| 48 | + end |
| 49 | + |
| 50 | + private |
| 51 | + |
| 52 | + def original_build_hash |
| 53 | + content = File.read("#{FilesHelper.output_dir}/info.rb") |
| 54 | + |
| 55 | + return unless (match = content.match(/Auto generated from build hash ([a-f0-9]+)/)) |
| 56 | + |
| 57 | + match[1] |
| 58 | + rescue |
| 59 | + return 'Unavailable' |
| 60 | + end |
| 61 | + end |
| 62 | + end |
| 63 | + end |
| 64 | +end |
0 commit comments