-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Preserve custom properties in gradle-wrapper.properties during wrapper updates #14026
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
feaba66
61c7cc8
d5886f4
c9523cf
f8816ac
18a29b2
f6338a0
1dcb8ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -71,7 +71,8 @@ def update_files(build_file) | |
| FileUtils.chmod("+x", "./gradlew") if has_local_script | ||
|
|
||
| properties_file = File.join(cwd, "gradle/wrapper/gradle-wrapper.properties") | ||
| validate_option = get_validate_distribution_url_option(properties_file) | ||
| # Save original content to preserve comments, order, and custom properties | ||
| original_content = File.read(properties_file) | ||
| env = { "JAVA_OPTS" => proxy_args.join(" ") } # set proxy for gradle execution | ||
|
|
||
| begin | ||
|
|
@@ -89,8 +90,8 @@ def update_files(build_file) | |
| SharedHelpers.run_shell_command(command, cwd: cwd, env: env) # retry via local wrapper | ||
| end | ||
|
|
||
| # Restore previous validateDistributionUrl option if it existed | ||
| override_validate_distribution_url_option(properties_file, validate_option) | ||
| # Update only the properties that should change, preserving everything else | ||
| update_distribution_properties(properties_file, original_content) | ||
|
|
||
| update_files_content(temp_dir, local_files, updated_files) | ||
| rescue SharedHelpers::HelperSubprocessFailed => e | ||
|
|
@@ -121,9 +122,9 @@ def command_args(requirements) | |
| # --no-validate-url is required to bypass HTTP proxy issues when running ./gradlew | ||
| # This prevents validation failures during the wrapper update process | ||
| # Note: This temporarily sets validateDistributionUrl=false in gradle-wrapper.properties | ||
| # The original value is restored after the wrapper task completes | ||
| # see method `get_validate_distribution_url_option` for more details | ||
| args = %W(wrapper --gradle-version #{version} --no-validate-url) # see | ||
| # The original value (along with all other custom properties) is restored after the wrapper task completes | ||
| # see method `update_distribution_properties` for more details | ||
| args = %W(wrapper --gradle-version #{version} --no-validate-url) | ||
| args += %W(--distribution-type #{distribution_type}) if distribution_type | ||
| args += %W(--gradle-distribution-sha256-sum #{checksum}) if checksum | ||
| args | ||
|
|
@@ -174,30 +175,37 @@ def populate_temp_directory(temp_dir) | |
| end | ||
| end | ||
|
|
||
| # This is a consequence of the lack of proper proxy support in Gradle Wrapper | ||
| # During the update process, Gradle Wrapper logic will try to validate the distribution URL | ||
| # by performing an HTTP request. If the environment requires a proxy, this validation will fail | ||
| # We need to add the `--no-validate-url` the commandline args to disable this validation | ||
| # However, this change is persistent in the `gradle-wrapper.properties` file | ||
| # To avoid side effects, we read the existing value before the update and restore it afterward | ||
| sig { params(properties_file: T.any(Pathname, String)).returns(T.nilable(String)) } | ||
| def get_validate_distribution_url_option(properties_file) | ||
| return nil unless File.exist?(properties_file) | ||
|
|
||
| properties_content = File.read(properties_file) | ||
| properties_content.match(/^validateDistributionUrl=(.*)$/)&.captures&.first | ||
| end | ||
|
|
||
| sig { params(properties_file: T.any(Pathname, String), value: T.nilable(String)).void } | ||
| def override_validate_distribution_url_option(properties_file, value) | ||
| # Updates only the distribution properties in the original file content | ||
| # This preserves the original file structure, comments, property order, and all other properties | ||
| # Only distributionUrl and distributionSha256Sum are updated from the newly generated file | ||
| sig { params(properties_file: T.any(Pathname, String), original_content: String).void } | ||
| def update_distribution_properties(properties_file, original_content) | ||
| return unless File.exist?(properties_file) | ||
|
|
||
| properties_content = File.read(properties_file) | ||
| updated_content = properties_content.gsub( | ||
| /^validateDistributionUrl=(.*)\n/, | ||
| value ? "validateDistributionUrl=#{value}\n" : "" | ||
| ) | ||
| File.write(properties_file, updated_content) | ||
| # Read the newly generated file to extract updated distribution properties | ||
| new_content = File.read(properties_file) | ||
| updated_values = T.let({}, T::Hash[String, String]) | ||
|
|
||
| # Extract the new values for distributionUrl and distributionSha256Sum | ||
| new_content.each_line do |line| | ||
| next if line.strip.start_with?("#") || line.strip.empty? | ||
| next unless line =~ /^(distributionUrl|distributionSha256Sum)=(.*)$/ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The whole point of reading the I think the IA has hallucinated here and messed up the whole logic by looking for @yeikel @kbukum1 if there is an actual fix here, please let me know because I don't picture it 🤷♂️ |
||
|
|
||
| key = T.must(::Regexp.last_match(1)) | ||
| value = T.must(::Regexp.last_match(2)) | ||
| updated_values[key] = value | ||
| end | ||
|
|
||
| # Duplicate to avoid mutating the parameter | ||
| result_content = original_content.dup | ||
|
kbukum1 marked this conversation as resolved.
|
||
|
|
||
| # Update only the distribution properties in the original content | ||
| updated_values.each do |key, new_value| | ||
| result_content.gsub!(/^(#{Regexp.escape(key)}=).*$/, "\\1#{new_value}") | ||
| end | ||
|
|
||
| # Write back the updated original content | ||
| File.write(properties_file, result_content) | ||
| end | ||
|
|
||
| # rubocop:disable Metrics/PerceivedComplexity | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| distributionBase=GRADLE_USER_HOME | ||
| distributionPath=wrapper/dists | ||
| distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.2-bin.zip | ||
| networkTimeout=30000 | ||
| validateDistributionUrl=true | ||
| zipStoreBase=GRADLE_USER_HOME | ||
| zipStorePath=wrapper/dists |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # Configuration for Gradle wrapper | ||
| # See https://docs.gradle.org/current/userguide/gradle_wrapper.html | ||
| distributionBase=GRADLE_USER_HOME | ||
| distributionPath=wrapper/dists | ||
| distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.2-bin.zip | ||
| # Custom network timeout for slow connections | ||
| networkTimeout=30000 | ||
| validateDistributionUrl=true | ||
| zipStoreBase=GRADLE_USER_HOME | ||
| zipStorePath=wrapper/dists |
Uh oh!
There was an error while loading. Please reload this page.