All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.autonomousapps.internal.utils.utils.kt Maven / Gradle / Ivy

There is a newer version: 2.0.2
Show newest version
@file:Suppress("UnstableApiUsage")

package com.autonomousapps.internal.utils

import org.gradle.api.artifacts.result.DependencyResult
import org.gradle.api.artifacts.result.ResolvedDependencyResult
import org.gradle.api.file.RegularFile
import org.gradle.api.file.RegularFileProperty
import java.io.File
import java.util.*

/**
 * Resolves the file from the property and deletes its constants, then returns the file.
 */
internal fun RegularFileProperty.getAndDelete(): File {
  val file = get().asFile
  file.delete()
  return file
}

internal inline fun  RegularFileProperty.fromJsonSet(): Set = get().fromJsonSet()
internal inline fun  RegularFile.fromJsonSet(): Set = asFile.readText().fromJsonSet()

internal inline fun  RegularFileProperty.fromJsonList(): List {
  return get().asFile.readText().fromJsonList()
}

internal inline fun  RegularFileProperty.fromJson(): T = get().fromJson()
internal inline fun  RegularFile.fromJson(): T = asFile.readText().fromJson()

internal inline fun  RegularFileProperty.fromNullableJsonSet(): Set? {
  return orNull?.asFile?.readText()?.fromJsonSet()
}

internal fun RegularFileProperty.readLines(): List {
  return get().asFile.readLines()
}

// Print dependency tree (like running the `dependencies` task).
@Suppress("unused")
internal fun printDependencyTree(dependencies: Set, level: Int = 0) {
  dependencies.filterIsInstance().forEach { result ->
    val resolvedComponentResult = result.selected
    println("${"  ".repeat(level)}- ${resolvedComponentResult.id}")
    printDependencyTree(resolvedComponentResult.dependencies, level + 1)
  }
}

// copied from StringsJVM.kt
fun String.capitalizeSafely(locale: Locale = Locale.ROOT): String {
  if (isNotEmpty()) {
    val firstChar = this[0]
    if (firstChar.isLowerCase()) {
      return buildString {
        val titleChar = firstChar.toTitleCase()
        if (titleChar != firstChar.toUpperCase()) {
          append(titleChar)
        } else {
          append([email protected](0, 1).toUpperCase(locale))
        }
        append([email protected](1))
      }
    }
  }
  return this
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy