square / gradle-dependencies-sorter

A CLI app and Gradle plugin to sort the dependencies in your Gradle build scripts
Apache License 2.0
252 stars 12 forks source link

Sort Dependencies Fails on `if` blocks #103

Open jamesonwilliams opened 2 months ago

jamesonwilliams commented 2 months ago

Consider a Gradle script like this one, where there are some out-of-order single-line statements followed by a more complex control-flow block:

dependencies {
  testImplementation projects.modules.test.utils
  kapt deps.dagger.compiler
  implementation projects.modules.design.goodies

  // comment about if statement
  if (project.findProperty("configurableProperty").toString().toBoolean()) {
    api projects.modules.version.real
  } else {
    releaseApi projects.modules.noOp
    debugApi projects.modules.version.real
  }
}

When I run

java -jar scripts/bin/sort-gradle-dependencies-app-0.7-all.jar --verbose --mode=sort build.gradle 

The output will be

[main] INFO Sorter - Sorting build.gradle(.kts) scripts in the following paths: build.gradle
[main] INFO Sorter - It took 2 ms to find 1 build scripts.
[main] WARN Sorter - Parsing error: /Users/jameson.williams/MyProj/build.gradle 
Unknown dependency kind. Was <> for /Users/jameson.williams/MyProj/build.gradle
[main] INFO Sorter - Metrics:
  Successful sorts: 0
  Already sorted:   0
  Parse errors:     1

Sort duration: 26 ms.
[main] INFO Sorter - Operation took 33 ms
[main] INFO Sorter - See log file at /var/folders/08/jzg4dnq92tz9x4bfn3_qw8lh0000gp/T/11468170203746801189.tmp 

We'd have to define the sort behavior for such blocks, but perhaps collecting them at the bottom would be a viable option.