Skip to content

Commit 5f0ab54

Browse files
committed
[API] Generator: Build hash update is optional as a parameter now
1 parent be82a44 commit 5f0ab54

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

elasticsearch-api/utils/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ $ thor code:generate
2424
$ IGNORE_VERSION=true thor code:generate
2525
```
2626

27+
- You can use the environment variable `BUILD_HASH` to update the build hash for the generated code from the `tmp/rest-api-spec/build_hash` file. This file is updated every time you use the `elasticsearch:download_artifacts` Rake task is used in the root of the project to download the latest Elasticsearch specs and tests:
28+
```bash
29+
$ BUILD_HASH=true thor code:generate
30+
```
31+
2732
### Development
2833

2934
The main entry point is `generate_source.rb`, which contains a class that implements a Thor task: `generate`:

elasticsearch-api/utils/thor/generate_source.rb

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ class SourceGenerator < Thor
4747
method_option :tests, type: :boolean, default: false, desc: 'Generate test files'
4848

4949
def generate
50-
@build_hash = File.read(File.expand_path('../../../tmp/rest-api-spec/build_hash',__dir__))
50+
@build_hash = if ENV['BUILD_HASH']
51+
File.read(File.expand_path('../../../tmp/rest-api-spec/build_hash',__dir__))
52+
else
53+
original_build_hash
54+
end
55+
5156
self.class.source_root File.expand_path(__dir__)
5257
generate_source
5358
# -- Tree output
@@ -103,25 +108,7 @@ def generate_source
103108
end
104109

105110
run_rubocop
106-
add_hash if there_is_a_change
107-
end
108-
109-
# Check for lines on the git diff that don't match the build hash comment
110-
# We don't want to generate a new commit if the only change is the build hash
111-
# because that means there are no relevant changes
112-
def there_is_a_change
113-
# This regular expression matches the following string:
114-
# Auto generated from build hash build_hash
115-
# @see https://github.com/elastic/elasticsearch/tree/main/rest-api-spec
116-
regexp = /^(\+||-)#(\sAuto\sgenerated\sfrom\sbuild\shash\s[0-9a-f]+$|\s@see.+$|$)/
117-
# The git/grep commands print only +/- diff lines with no context and excludes lines with ---/+++
118-
# awk/wc count the lines that don't match the regexp to see lines that have changed
119-
# but are note the build hash comment:
120-
changes = `git diff -U0 #{FilesHelper.output_dir} | grep '^[+-]' | \
121-
grep -Ev '^(--- a/|\+\+\+ b/)' | \
122-
awk '!/^(\+||-)#(\sAuto\sgenerated\sfrom\sbuild\shash\s[0-9a-f]+$|\s@see.+$|$)/' | \
123-
wc -l`.strip
124-
changes.to_i.positive?
111+
add_hash
125112
end
126113

127114
def add_hash
@@ -143,6 +130,13 @@ def build_hash_comment
143130
].map { |b| "# #{b}" }.join("\n").strip
144131
end
145132

133+
def original_build_hash
134+
content = File.read("#{FilesHelper.output_dir}/info.rb")
135+
return unless (match = content.match(/Auto generated from build hash ([a-f0-9]+)/))
136+
137+
match[1]
138+
end
139+
146140
def __full_namespace
147141
names = @endpoint_name.split('.')
148142
# Return an array to expand 'ccr', 'ilm', 'ml' and 'slm'

0 commit comments

Comments
 (0)