Skip to content
This repository has been archived by the owner on Dec 18, 2023. It is now read-only.

Commit

Permalink
Merge branch 'master' into fat-jar-resources
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main/kotlin/com/google/androidstudiopoet/AndroidStudioPoet.kt
  • Loading branch information
borisf committed Dec 20, 2017
2 parents 4a4297d + b011e52 commit 18e8428
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 47 deletions.
98 changes: 54 additions & 44 deletions src/main/kotlin/com/google/androidstudiopoet/AndroidStudioPoet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,20 @@ import java.awt.EventQueue
import java.awt.Font
import java.io.File
import javax.swing.*
import javax.swing.JFrame.EXIT_ON_CLOSE
import javax.swing.border.EmptyBorder
import kotlin.system.measureTimeMillis

class AndroidStudioPoet(private val modulesWriter: SourceModuleWriter, filename: String?,
configPojoToFlavourConfigsConverter: ConfigPojoToFlavourConfigsConverter,
configPojoToBuildTypeConfigsConverter: ConfigPojoToBuildTypeConfigsConverter) : JFrame() {
class AndroidStudioPoet(private val modulesWriter: SourceModuleWriter, private val filename: String?,
private val configPojoToFlavourConfigsConverter: ConfigPojoToFlavourConfigsConverter,
private val configPojoToBuildTypeConfigsConverter: ConfigPojoToBuildTypeConfigsConverter) {

companion object {
@JvmStatic
fun main(args: Array<String>) {

EventQueue.invokeLater {
try {
val frame = AndroidStudioPoet(Injector.modulesWriter, args.firstOrNull(),
Injector.configPojoToFlavourConfigsConverter,
Injector.configPojoToBuildTypeConfigsConverter)
frame.isVisible = true
} catch (e: Exception) {
e.printStackTrace()
}
}
AndroidStudioPoet(Injector.modulesWriter, args.firstOrNull(),
Injector.configPojoToFlavourConfigsConverter,
Injector.configPojoToBuildTypeConfigsConverter).run()
}

@Language("JSON") const val SAMPLE_CONFIG = """
Expand Down Expand Up @@ -83,36 +76,42 @@ class AndroidStudioPoet(private val modulesWriter: SourceModuleWriter, filename:
"""
}

init {
fun run() {

val jsonText = fromFileNameOrDefault(filename)
val configPOJO = fromFile(filename)
when (configPOJO) {
null -> showUI(SAMPLE_CONFIG)
else -> processInput(configPOJO)
}
}

private fun showUI(jsonText: String) {
EventQueue.invokeLater {
try {
val frame = createUI(jsonText)
frame.isVisible = true
} catch (e: Exception) {
e.printStackTrace()
}
}
}

private fun createUI(jsonText: String): JFrame {
val frame = JFrame()
val textArea = createTextArea(jsonText)
val scrollPane = JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS)

val btnGenerate = JButton("Generate").apply {
addActionListener {

try {
var projectBluePrint: ProjectBlueprint? = null
val timeSpent = measureTimeMillis {
println(textArea.text)
val config: ConfigPOJO = configFrom(textArea.text) ?: configFrom(SAMPLE_CONFIG)!!
projectBluePrint = ProjectBlueprint(config, configPojoToFlavourConfigsConverter, configPojoToBuildTypeConfigsConverter)
modulesWriter.generate(projectBluePrint!!)
}
println("Finished in $timeSpent ms")
println("Dependency graph:")
projectBluePrint!!.printDependencies()
if (projectBluePrint!!.hasCircularDependencies()) {
println("WARNING: there are circular dependencies")
}
val text = textArea.text
println(text)
processInput(configFrom(text)!!)

} catch (e: Exception) {
println("ERROR: the generation failed due to JSON script errors - " +
"please fix and try again ")
"please fix and try again ")
}

}
}

Expand All @@ -124,11 +123,26 @@ class AndroidStudioPoet(private val modulesWriter: SourceModuleWriter, filename:
add(btnGenerate, BorderLayout.SOUTH)
}

defaultCloseOperation = JFrame.EXIT_ON_CLOSE
frame.defaultCloseOperation = EXIT_ON_CLOSE

setContentPane(contentPane)
frame.contentPane = contentPane

pack()
frame.pack()
return frame
}

private fun processInput(configPOJO: ConfigPOJO) {
var projectBluePrint: ProjectBlueprint? = null
val timeSpent = measureTimeMillis {
projectBluePrint = ProjectBlueprint(configPOJO, configPojoToFlavourConfigsConverter, configPojoToBuildTypeConfigsConverter)
modulesWriter.generate(projectBluePrint!!)
}
println("Finished in $timeSpent ms")
println("Dependency graph:")
projectBluePrint!!.printDependencies()
if (projectBluePrint!!.hasCircularDependencies()) {
println("WARNING: there are circular dependencies")
}
}

private fun createTitleLabel(): JLabel {
Expand All @@ -150,15 +164,11 @@ class AndroidStudioPoet(private val modulesWriter: SourceModuleWriter, filename:
}
}

private fun fromFileNameOrDefault(filename: String?): String = when {
filename == null -> SAMPLE_CONFIG
!File(filename).canRead() -> SAMPLE_CONFIG
else -> File(filename).readText().let { json ->
if (configFrom(json) == null) {
SAMPLE_CONFIG
} else {
json
}
private fun fromFile(filename: String?): ConfigPOJO? = when {
filename == null -> null
!File(filename).canRead() -> null
else -> File(filename).readText().let {
return configFrom(it)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import java.nio.file.StandardCopyOption

object GradlewGenerator {

private val ref = "master"

fun generateGradleW(root: String, projectBlueprint: ProjectBlueprint) {

val gradlew = "gradlew"
Expand All @@ -38,7 +40,7 @@ object GradlewGenerator {
) {

println("AS Poet needs network access to download gradle files from Github " +
"\nhttps://github.com/android/android-studio-poet/tree/master/resources/gradle-assets " +
"\nhttps://github.com/android/android-studio-poet/tree/master/resources/gradle-assets?ref=$ref " +
"\nplease copy/paste the gradle folder directly to the generated root folder")
return
}
Expand Down Expand Up @@ -74,10 +76,9 @@ object GradlewGenerator {
.joinPath("gradle-assets")

if (!File(assetsFolder).exists()) {
val commitId = "fbc09b6"
GithubDownloader().downloadDir(
"https://api.github.com/repos/android/" +
"android-studio-poet/contents/resources/gradle-assets?ref=$commitId",
"android-studio-poet/contents/resources/gradle-assets?ref=$ref",
assetsFolder)
}

Expand Down

0 comments on commit 18e8428

Please sign in to comment.