diff --git a/src/index.ts b/src/index.ts index 0566d97..98a514c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -22,6 +22,7 @@ import type { CommandRunner } from './install-scripts'; import { logger } from './log'; import { declaresOtherPackageManager } from './package-manager'; import { migratePackageSwift } from './package-swift'; +import { setPropertyLookupReceiver } from './property-lookup'; import { runCommand } from './subprocess'; const coreVersion = '9.0.0-rc.0'; @@ -520,6 +521,8 @@ async function updateBuildGradle( return; } gradleFile = gradleFile.replaceAll(' = ', ' = '); + // the matchers below expect `project.hasProperty`, also when the tool runs again on a migrated file + gradleFile = setPropertyLookupReceiver(gradleFile, 'project'); logger.info('Updating build.gradle'); gradleFile = gradleFile.replaceAll('androidxCoreKTXVersion', 'androidxCoreVersion'); @@ -614,6 +617,7 @@ async function updateBuildGradle( ); } + gradleFile = setPropertyLookupReceiver(gradleFile, 'rootProject'); writeFileSync(filename, gradleFile, 'utf-8'); } diff --git a/src/property-lookup.test.ts b/src/property-lookup.test.ts new file mode 100644 index 0000000..ce9e3f5 --- /dev/null +++ b/src/property-lookup.test.ts @@ -0,0 +1,57 @@ +import assert from 'node:assert/strict'; +import { describe, it } from 'node:test'; + +import { setPropertyLookupReceiver } from './property-lookup.ts'; + +describe('setPropertyLookupReceiver', () => { + const legacy = `ext { + junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2' +} + +buildscript { + ext.kotlin_version = project.hasProperty("kotlin_version") ? rootProject.ext.kotlin_version : '2.2.20' +} + +android { + compileSdk = project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 37 + defaultConfig { + minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 26 + } +}`; + const migrated = `ext { + junitVersion = rootProject.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2' +} + +buildscript { + ext.kotlin_version = rootProject.hasProperty("kotlin_version") ? rootProject.ext.kotlin_version : '2.2.20' +} + +android { + compileSdk = rootProject.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 37 + defaultConfig { + minSdkVersion rootProject.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 26 + } +}`; + + it('moves variable lookups to rootProject', () => { + assert.equal(setPropertyLookupReceiver(legacy, 'rootProject'), migrated); + }); + + it('is idempotent', () => { + assert.equal(setPropertyLookupReceiver(migrated, 'rootProject'), migrated); + }); + + it('moves them back to project', () => { + assert.equal(setPropertyLookupReceiver(migrated, 'project'), legacy); + }); + + it('leaves other hasProperty calls alone', () => { + const other = [ + `def flag = project.hasProperty('myFlag') ? project.myFlag : false`, + `x = project.hasProperty('a') ? rootProject.ext.b : 1`, + `if (subproject.hasProperty('x') ? rootProject.ext.x : 1) {}`, + `return hasProperty('NEXUS_USERNAME') ? NEXUS_USERNAME : ""`, + ].join('\n'); + assert.equal(setPropertyLookupReceiver(other, 'rootProject'), other); + }); +}); diff --git a/src/property-lookup.ts b/src/property-lookup.ts new file mode 100644 index 0000000..0efb12d --- /dev/null +++ b/src/property-lookup.ts @@ -0,0 +1,12 @@ +// Matches `project.hasProperty('x') ? rootProject.ext.x` (or the `rootProject.hasProperty` form) with the same `x` on both sides. +const PROPERTY_LOOKUP = + /(?