From 185dc159acfb67d50944aeafc3bd15e79b4cf27a Mon Sep 17 00:00:00 2001 From: Nikita Kozlov Date: Tue, 19 Dec 2017 22:46:56 +0100 Subject: [PATCH] UI is now skipped if file returns valid configPOJO --- .../androidstudiopoet/AndroidStudioPoet.kt | 90 +++++++++++-------- 1 file changed, 51 insertions(+), 39 deletions(-) diff --git a/src/main/kotlin/com/google/androidstudiopoet/AndroidStudioPoet.kt b/src/main/kotlin/com/google/androidstudiopoet/AndroidStudioPoet.kt index ff7288ed..f540c37f 100644 --- a/src/main/kotlin/com/google/androidstudiopoet/AndroidStudioPoet.kt +++ b/src/main/kotlin/com/google/androidstudiopoet/AndroidStudioPoet.kt @@ -27,25 +27,19 @@ 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) : JFrame() { +class AndroidStudioPoet(private val modulesWriter: SourceModuleWriter, private val filename: String?, + private val configPojoToFlavourConfigsConverter: ConfigPojoToFlavourConfigsConverter) { companion object { @JvmStatic fun main(args: Array) { + AndroidStudioPoet(Injector.modulesWriter, args.firstOrNull(), + Injector.configPojoToFlavourConfigsConverter).run() - EventQueue.invokeLater { - try { - val frame = AndroidStudioPoet(Injector.modulesWriter, args.firstOrNull(), - Injector.configPojoToFlavourConfigsConverter) - frame.isVisible = true - } catch (e: Exception) { - e.printStackTrace() - } - } } @Language("JSON") const val SAMPLE_CONFIG = """ @@ -79,28 +73,36 @@ class AndroidStudioPoet(private val modulesWriter: SourceModuleWriter, filename: """ } - init { + fun run() { + + val configPOJO = fromFile(filename) + when (configPOJO) { + null -> showUI(SAMPLE_CONFIG) + else -> processInput(configPOJO) + } + } - val jsonText = fromFileNameOrDefault(filename) + 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 { - var projectBluePrint : ProjectBlueprint? = null - val timeSpent = measureTimeMillis { - println(textArea.text) - val config: ConfigPOJO = configFrom(textArea.text) ?: configFrom(SAMPLE_CONFIG)!! - projectBluePrint = ProjectBlueprint(config, configPojoToFlavourConfigsConverter) - 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)!!) } } @@ -112,11 +114,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) + modulesWriter.generate(projectBluePrint!!) + } + println("Finished in $timeSpent ms") + println("Dependency graph:") + projectBluePrint!!.printDependencies() + if (projectBluePrint!!.hasCircularDependencies()) { + println("WARNING: there are circular dependencies") + } } @@ -139,16 +156,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) } }