diff --git a/src/main/kotlin/com/google/androidstudiopoet/AndroidStudioPoet.kt b/src/main/kotlin/com/google/androidstudiopoet/AndroidStudioPoet.kt index 862ce50d..05704648 100644 --- a/src/main/kotlin/com/google/androidstudiopoet/AndroidStudioPoet.kt +++ b/src/main/kotlin/com/google/androidstudiopoet/AndroidStudioPoet.kt @@ -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) { - - 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 = """ @@ -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 ") } - } } @@ -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 { @@ -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) } } diff --git a/src/main/kotlin/com/google/androidstudiopoet/generators/project/GradlewGenerator.kt b/src/main/kotlin/com/google/androidstudiopoet/generators/project/GradlewGenerator.kt index 9b49a354..73efd358 100644 --- a/src/main/kotlin/com/google/androidstudiopoet/generators/project/GradlewGenerator.kt +++ b/src/main/kotlin/com/google/androidstudiopoet/generators/project/GradlewGenerator.kt @@ -26,6 +26,8 @@ import java.nio.file.StandardCopyOption object GradlewGenerator { + private val ref = "master" + fun generateGradleW(root: String, projectBlueprint: ProjectBlueprint) { val gradlew = "gradlew" @@ -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 } @@ -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) }