taler-android

Android apps for GNU Taler (wallet, PoS, cashier)
Log | Files | Refs | README | LICENSE

formatting.gradle (2612B)


      1 def androidStudioHome = providers.gradleProperty('androidStudioHome')
      2     .orElse(providers.environmentVariable('ANDROID_STUDIO_HOME'))
      3 
      4 ['formatKotlinLayout': false, 'checkKotlinFormat': true].each { taskName, dryRun ->
      5     tasks.register(taskName, Exec) {
      6         group = dryRun ? 'verification' : 'formatting'
      7         description = dryRun ? 'Check POS formatting with Android Studio.' : 'Format POS Kotlin sources with Android Studio.'
      8         def systemDirectory
      9 
     10         doFirst {
     11             def home = androidStudioHome.orNull
     12             def candidates = home ? [file(home)] : [
     13                 file('/Applications/Android Studio.app/Contents'),
     14                 file('/opt/android-studio'),
     15                 file("${System.getenv('ProgramFiles') ?: 'C:/Program Files'}/Android/Android Studio")
     16             ]
     17             def windows = System.getProperty('os.name').toLowerCase(Locale.ROOT).contains('windows')
     18             def relativeLauncher = windows ? 'bin/format.bat' : 'bin/format.sh'
     19             def launcher = candidates.collect { new File(it, relativeLauncher) }.find { it.isFile() }
     20             if (launcher == null) {
     21                 throw new GradleException('Android Studio formatter not found. Set -PandroidStudioHome=<installation directory>.')
     22             }
     23 
     24             def workspace = layout.buildDirectory.dir("formatting/${taskName}").get().asFile
     25             workspace.mkdirs()
     26             def properties = new Properties()
     27             ['config', 'log', 'plugins'].each { directory ->
     28                 properties.setProperty("idea.${directory}.path", new File(workspace, directory).absolutePath)
     29             }
     30             // A reused VFS cache can hide changes to .editorconfig from the formatter.
     31             systemDirectory = java.nio.file.Files.createTempDirectory(workspace.toPath(), 'system-').toFile()
     32             properties.setProperty('idea.system.path', systemDirectory.absolutePath)
     33             def propertiesFile = new File(workspace, 'idea.properties')
     34             propertiesFile.withOutputStream { properties.store(it, null) }
     35             environment 'STUDIO_PROPERTIES', propertiesFile.absolutePath
     36 
     37             def arguments = ['-r', '-m', '*.kt', '-allowDefaults']
     38             if (dryRun) arguments.add('-d')
     39             arguments.add(file('src').absolutePath)
     40             commandLine([launcher.absolutePath] + arguments)
     41         }
     42 
     43         doLast {
     44             delete systemDirectory
     45         }
     46     }
     47 }
     48 
     49 tasks.register('formatKotlin') {
     50     group = 'formatting'
     51     description = 'Format POS Kotlin sources with Android Studio and ktlint.'
     52     dependsOn 'ktlintFormat'
     53 }