Skip to content

Commit

Permalink
Suggest more specific write locks command in error message (#1627)
Browse files Browse the repository at this point in the history
  • Loading branch information
pkoenig10 authored Apr 8, 2024
1 parent f003818 commit 03894e4
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@
import org.gradle.language.base.plugins.LifecycleBasePlugin;

public abstract class CreateManifestTask extends DefaultTask {
public static final String WRITE_PRODUCT_DEPENDENCIES_LOCKS_TASK_NAME = "writeProductDependenciesLocks";
public static final String WRITE_SCHEMA_VERSION_LOCKS_TASK_NAME = "writeSchemaVersionLocks";

@Input
abstract SetProperty<ProductId> getInRepoProductIds();
Expand Down Expand Up @@ -134,14 +132,14 @@ final void createManifest() throws IOException {

List<ProductDependency> productDependencies = productDependencyManifest.productDependencies();
if (productDependencies.isEmpty()) {
requireAbsentLockfile(WRITE_PRODUCT_DEPENDENCIES_LOCKS_TASK_NAME, getProductDependenciesLockfile());
requireAbsentLockfile(WriteProductDependenciesLocksMarkerTask.NAME, getProductDependenciesLockfile());
} else {
ensurePdepsLockfileIsUpToDate(productDependencies);
}

List<SchemaMigration> schemaMigrations = getSchemaMigrations();
if (schemaMigrations.isEmpty()) {
requireAbsentLockfile(WRITE_SCHEMA_VERSION_LOCKS_TASK_NAME, getSchemaVersionLockfile());
requireAbsentLockfile(WriteSchemaVersionLocksMarkerTask.NAME, getSchemaVersionLockfile());
} else {
ensureSchemaLockfileIsUpToDate(schemaMigrations);
}
Expand Down Expand Up @@ -179,8 +177,10 @@ private void requireAbsentLockfile(String writeLocksTaskName, File lockfile) {
getLogger().lifecycle("Deleted {}", relativePath);
} else {
throw new ExceptionWithSuggestion(
String.format("%s must not exist, please run `%s` to delete it", relativePath, getSuggestedFix()),
getSuggestedFix());
String.format(
"%s must not exist, please run `%s` to delete it",
relativePath, getSuggestedFix(writeLocksTaskName)),
getSuggestedFix(writeLocksTaskName));
}
}

Expand All @@ -200,7 +200,7 @@ private void ensurePdepsLockfileIsUpToDate(List<ProductDependency> productDeps)
File lockfile = getProductDependenciesLockfile();
String upToDateContents = ProductDependencyLockFile.asString(
productDeps, getInRepoProductIds().get());
ensureFileIsUpToDate(WRITE_PRODUCT_DEPENDENCIES_LOCKS_TASK_NAME, lockfile, upToDateContents);
ensureFileIsUpToDate(WriteProductDependenciesLocksMarkerTask.NAME, lockfile, upToDateContents);
}

private void ensureFileIsUpToDate(String writeLocksTaskName, File lockfile, String upToDateContents)
Expand All @@ -221,20 +221,20 @@ private void ensureFileIsUpToDate(String writeLocksTaskName, File lockfile, Stri
throw new ExceptionWithSuggestion(
String.format(
"%s does not exist, please run `%s` and commit the resultant file",
relativePath, getSuggestedFix()),
getSuggestedFix());
relativePath, getSuggestedFix(writeLocksTaskName)),
getSuggestedFix(writeLocksTaskName));
} else {
String fromDisk = Files.readString(lockfile.toPath());
if (!fromDisk.equals(upToDateContents)) {
throw new ExceptionWithSuggestion(
String.format(
"%s is out of date, please run `%s` to update it%s",
relativePath,
getSuggestedFix(),
getSuggestedFix(writeLocksTaskName),
diff(lockfile, upToDateContents)
.map(s -> ":\n" + s)
.orElse("")),
getSuggestedFix());
getSuggestedFix(writeLocksTaskName));
}
}
}
Expand Down Expand Up @@ -269,7 +269,7 @@ private File getSchemaVersionLockfile() {
private void ensureSchemaLockfileIsUpToDate(List<SchemaMigration> schemaMigrations) throws IOException {
File lockfile = getSchemaVersionLockfile();
String upToDateContents = ObjectMappers.writeSchemaVersionsAsString(SchemaVersionLockFile.of(schemaMigrations));
ensureFileIsUpToDate(WRITE_SCHEMA_VERSION_LOCKS_TASK_NAME, lockfile, upToDateContents);
ensureFileIsUpToDate(WriteSchemaVersionLocksMarkerTask.NAME, lockfile, upToDateContents);
}

private void validateProjectVersion() {
Expand Down Expand Up @@ -312,8 +312,8 @@ public static TaskProvider<CreateManifestTask> createManifestTask(Project projec
task.getOutputs().upToDateWhen(new Spec<Task>() {
@Override
public boolean isSatisfiedBy(Task _task) {
return !(shouldWriteLocks(project, WRITE_PRODUCT_DEPENDENCIES_LOCKS_TASK_NAME)
|| shouldWriteLocks(project, WRITE_SCHEMA_VERSION_LOCKS_TASK_NAME));
return !(shouldWriteLocks(project, WriteProductDependenciesLocksMarkerTask.NAME)
|| shouldWriteLocks(project, WriteSchemaVersionLocksMarkerTask.NAME));
}
});

Expand All @@ -328,13 +328,13 @@ public boolean isSatisfiedBy(Task _task) {

project.getTasks()
.register(
WRITE_PRODUCT_DEPENDENCIES_LOCKS_TASK_NAME,
WriteProductDependenciesLocksMarkerTask.NAME,
WriteProductDependenciesLocksMarkerTask.class,
task -> {
task.dependsOn(createManifest);
});
project.getTasks()
.register(WRITE_SCHEMA_VERSION_LOCKS_TASK_NAME, WriteSchemaVersionLocksMarkerTask.class, task -> {
.register(WriteSchemaVersionLocksMarkerTask.NAME, WriteSchemaVersionLocksMarkerTask.class, task -> {
task.dependsOn(createManifest);
});

Expand All @@ -352,7 +352,7 @@ public boolean isSatisfiedBy(Task _task) {
return createManifest;
}

private String getSuggestedFix() {
return String.format("./gradlew %s --write-locks", getName());
private String getSuggestedFix(String writeLocksTaskName) {
return String.format("./gradlew %s", writeLocksTaskName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
import org.gradle.api.tasks.TaskAction;

public class WriteProductDependenciesLocksMarkerTask extends DefaultTask {

public static final String NAME = "writeProductDependenciesLocks";

@TaskAction
public final void checkWriteLocksShouldBeRunning() {
// Check that our task name matcher for writeProductDependenciesLocks is actually matching up the Gradle one;
// if this task is running but we didn't actually write locks, error out.
if (!CreateManifestTask.shouldWriteLocks(
getProject(), CreateManifestTask.WRITE_PRODUCT_DEPENDENCIES_LOCKS_TASK_NAME)) {
if (!CreateManifestTask.shouldWriteLocks(getProject(), NAME)) {
throw new GradleException("This `writeProductDependenciesLocks` marker task has been run, but the "
+ "product-dependencies.lock files did not actually get written out at configuration time. Either "
+ "there is another task dependency on this task, which is not supported "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
import org.gradle.api.tasks.TaskAction;

public class WriteSchemaVersionLocksMarkerTask extends DefaultTask {

public static final String NAME = "writeSchemaVersionLocks";

@TaskAction
public final void checkWriteLocksShouldBeRunning() {
// Check that our task name matcher for writeSchemaVersionLocks is actually matching up the Gradle one;
// if this task is running but we didn't actually write locks, error out.
if (!CreateManifestTask.shouldWriteLocks(
getProject(), CreateManifestTask.WRITE_SCHEMA_VERSION_LOCKS_TASK_NAME)) {
if (!CreateManifestTask.shouldWriteLocks(getProject(), NAME)) {
throw new GradleException("This `writeSchemaVersionLocks` marker task has been run, but the "
+ "schema-versions.lock files did not actually get written out at configuration time. Either "
+ "there is another task dependency on this task, which is not supported "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class CreateManifestTaskIntegrationSpec extends IntegrationSpec {

then:
buildResult.getStandardError().contains(
"product-dependencies.lock is out of date, please run `./gradlew createManifest --write-locks` to update it")
"product-dependencies.lock is out of date, please run `./gradlew writeProductDependenciesLocks` to update it")
}

def 'fails if unexpected lockfile exists'() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class CreateManifestTaskSchemaVersionsIntegrationSpec extends IntegrationSpec {

then:
buildResult.getStandardError().contains(
"schema-versions.lock is out of date, please run `./gradlew createManifest --write-locks` to update it")
"schema-versions.lock is out of date, please run `./gradlew writeSchemaVersionLocks` to update it")
}

def 'fails if unexpected lockfile exists'() {
Expand Down

0 comments on commit 03894e4

Please sign in to comment.