Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -614,6 +617,7 @@ async function updateBuildGradle(
);
}

gradleFile = setPropertyLookupReceiver(gradleFile, 'rootProject');
writeFileSync(filename, gradleFile, 'utf-8');
}

Expand Down
57 changes: 57 additions & 0 deletions src/property-lookup.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
12 changes: 12 additions & 0 deletions src/property-lookup.ts
Original file line number Diff line number Diff line change
@@ -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 =
/(?<![\w.])(?:rootProject|project)\.hasProperty\((['"])(\w+)\1\)(\s*\?\s*rootProject\.ext\.\2\b)/g;

/**
* Sets which project `hasProperty()` is called on in the plugin's variable lookups.
* Gradle 9 deprecates `project.hasProperty()` finding a property that is only declared on the root project
* (an error in Gradle 10), so plugins have to call it on `rootProject`, where the value is read from anyway.
*/
export function setPropertyLookupReceiver(gradleFile: string, receiver: 'project' | 'rootProject'): string {
return gradleFile.replace(PROPERTY_LOOKUP, `${receiver}.hasProperty($1$2$1)$3`);
}
Loading