Skip to content

Commit

Permalink
Merge pull request #1 from Polidea/fix/1_0_1
Browse files Browse the repository at this point in the history
Changes for 1.0.1 version:

- getters and setters use camelCase convention
- parameters' changes made by Cockpit.setParam(param) are immediately persisted
- removed Cockpit.hideCockpit(activity: CockpitActivity) method
  • Loading branch information
piwonski authored Apr 13, 2018
2 parents abfa98a + ba26fc5 commit ca8e860
Show file tree
Hide file tree
Showing 17 changed files with 102 additions and 104 deletions.
2 changes: 1 addition & 1 deletion CockpitPlugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ plugins {
}

group = 'com.polidea.cockpit'
version = '1.0.0'
version = '1.0.1'

sourceCompatibility = 1.7

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,22 @@ abstract class BaseCockpitGenerator {
protected val cockpitManagerPackage = "com.polidea.cockpit.manager"
protected val androidContentPackage = "android.content"
protected val cockpitActivityPackage = "com.polidea.cockpit.activity"
protected val cockpitUtilsPackage = "com.polidea.cockpit.utils"

protected val cockpit = "Cockpit"
protected val cockpitManager = "CockpitManager"
protected val cockpitParam = "CockpitParam"
protected val intent = "Intent"
protected val context = "Context"
protected val cockpitActivity = "CockpitActivity"
protected val fileUtils = "FileUtils"

protected val cockpitManagerClassName = ClassName.get(cockpitManagerPackage, cockpitManager)
protected val cockpitParamClassName = ClassName.get(cockpitManagerPackage, cockpitParam)
protected val androidIntentClassName = ClassName.get(androidContentPackage, intent)
protected val androidContextClassName = ClassName.get(androidContentPackage, context)
protected val cockpitActivityClassName = ClassName.get(cockpitActivityPackage, cockpitActivity)
protected val fileUtilsClassName = ClassName.get(cockpitUtilsPackage, fileUtils)

protected fun generate(file: File?, configurator: (TypeSpec.Builder) -> TypeSpec.Builder) {

Expand All @@ -44,7 +47,7 @@ abstract class BaseCockpitGenerator {

inline protected fun createGetterMethodSpecForParamAndConfigurator(param: Param<*>,
configurator: (MethodSpec.Builder) -> MethodSpec.Builder): MethodSpec {
return configurator(MethodSpec.methodBuilder("get${param.name}")
return configurator(MethodSpec.methodBuilder("get${param.name.capitalize()}")
.returns(mapToTypeClass(param))
.addModifiers(Modifier.PUBLIC, Modifier.STATIC))
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,26 @@ class DebugCockpitGenerator : BaseCockpitGenerator() {
.addMethod(createInitCockpitMethod(params))
.addMethods(propertyMethods)
.addMethod(generateShowCockpitMethod())
.addMethod(generateHideCockpitMethod())
.addMethod(generatePersistChangesMethod())
}
}

internal fun createGetterMethodSpecForParam(param: Param<*>): MethodSpec {
return createGetterMethodSpecForParamAndConfigurator(param) { builder ->
builder.addStatement("return (\$T) \$T.getInstance().getParamValue(\"${param.name}\")",
builder.addStatement("return (\$T) \$T.INSTANCE.getParamValue(\"${param.name}\")",
mapToTypeClass(param), cockpitManagerClassName)
}
}


internal fun createSetterMethodSpecForParam(param: Param<*>): MethodSpec {
val typeClass = mapToTypeClass(param)
return MethodSpec.methodBuilder("set${param.name}")
return MethodSpec.methodBuilder("set${param.name.capitalize()}")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.addParameter(ParameterSpec.builder(typeClass, param.name).build())
.addStatement("\$T.getInstance().setParamValue(\"${param.name}\", ${param.name})",
.addStatement("\$T.INSTANCE.setParamValue(\"${param.name}\", ${param.name})",
cockpitManagerClassName)
.addStatement("persistChanges()")
.build()
}

Expand All @@ -46,7 +47,7 @@ class DebugCockpitGenerator : BaseCockpitGenerator() {
.addModifiers(Modifier.PRIVATE, Modifier.STATIC)

params.forEach {
funSpec.addStatement("$cockpitManager.getInstance().addParam(new \$T(\"${it.name}\", ${it.value.javaClass.simpleName}.class, ${createWrappedValueForParam(it)}))", cockpitParamClassName)
funSpec.addStatement("$cockpitManager.INSTANCE.addParam(new \$T(\"${it.name}\", ${it.value.javaClass.simpleName}.class, ${createWrappedValueForParam(it)}))", cockpitParamClassName)
}

return funSpec.build()
Expand All @@ -62,12 +63,10 @@ class DebugCockpitGenerator : BaseCockpitGenerator() {
.build()
}


internal fun generateHideCockpitMethod(): MethodSpec {
return MethodSpec.methodBuilder("hideCockpit")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.addParameter(ParameterSpec.builder(cockpitActivityClassName, "cockpitActivity").build())
.addStatement("cockpitActivity.finish()")
internal fun generatePersistChangesMethod(): MethodSpec {
return MethodSpec.methodBuilder("persistChanges")
.addModifiers(Modifier.PRIVATE, Modifier.STATIC)
.addStatement("\$T.INSTANCE.saveCockpitAsYaml()", fileUtilsClassName)
.build()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class DebugCockpitGeneratorTest {
val doubleGetterMethodSpec = cockpitGenerator.createGetterMethodSpecForParam(DoubleParam("doubleParam", 3.0))

val expectedDoubleGetterMethodSpecString = """
|public static double getdoubleParam() {
| return (double) com.polidea.cockpit.manager.CockpitManager.getInstance().getParamValue("doubleParam");
|public static double getDoubleParam() {
| return (double) com.polidea.cockpit.manager.CockpitManager.INSTANCE.getParamValue("doubleParam");
|}"""
assertEquals(expectedDoubleGetterMethodSpecString.trimMargin(), doubleGetterMethodSpec.toString().trimMargin())
}
Expand All @@ -25,8 +25,9 @@ class DebugCockpitGeneratorTest {
val doubleSetterMethodSpec = cockpitGenerator.createSetterMethodSpecForParam(DoubleParam("doubleParam", 3.0))

val expectedDoubleSetterMethodSpecString = """
|public static void setdoubleParam(double doubleParam) {
| com.polidea.cockpit.manager.CockpitManager.getInstance().setParamValue("doubleParam", doubleParam);
|public static void setDoubleParam(double doubleParam) {
| com.polidea.cockpit.manager.CockpitManager.INSTANCE.setParamValue("doubleParam", doubleParam);
| persistChanges();
|}"""
assertEquals(expectedDoubleSetterMethodSpecString.trimMargin(), doubleSetterMethodSpec.toString().trimMargin())
}
Expand All @@ -36,8 +37,8 @@ class DebugCockpitGeneratorTest {
val intGetterMethodSpec = cockpitGenerator.createGetterMethodSpecForParam(IntegerParam("integerParam", 2))

val expectedIntegerGetterMethodSpecString = """
|public static int getintegerParam() {
| return (int) com.polidea.cockpit.manager.CockpitManager.getInstance().getParamValue("integerParam");
|public static int getIntegerParam() {
| return (int) com.polidea.cockpit.manager.CockpitManager.INSTANCE.getParamValue("integerParam");
|}"""
assertEquals(expectedIntegerGetterMethodSpecString.trimMargin(), intGetterMethodSpec.toString().trimMargin())
}
Expand All @@ -47,8 +48,9 @@ class DebugCockpitGeneratorTest {
val intSetterMethodSpec = cockpitGenerator.createSetterMethodSpecForParam(IntegerParam("integerParam", 2))

val expectedDoubleSetterMethodSpecString = """
|public static void setintegerParam(int integerParam) {
| com.polidea.cockpit.manager.CockpitManager.getInstance().setParamValue("integerParam", integerParam);
|public static void setIntegerParam(int integerParam) {
| com.polidea.cockpit.manager.CockpitManager.INSTANCE.setParamValue("integerParam", integerParam);
| persistChanges();
|}"""
assertEquals(expectedDoubleSetterMethodSpecString.trimMargin(), intSetterMethodSpec.toString().trimMargin())
}
Expand All @@ -58,8 +60,8 @@ class DebugCockpitGeneratorTest {
val booleanGetterMethodSpec = cockpitGenerator.createGetterMethodSpecForParam(BooleanParam("booleanParam", false))

val expectedBooleanGetterMethodSpecString = """
|public static boolean getbooleanParam() {
| return (boolean) com.polidea.cockpit.manager.CockpitManager.getInstance().getParamValue("booleanParam");
|public static boolean getBooleanParam() {
| return (boolean) com.polidea.cockpit.manager.CockpitManager.INSTANCE.getParamValue("booleanParam");
|}"""
assertEquals(expectedBooleanGetterMethodSpecString.trimMargin(), booleanGetterMethodSpec.toString().trimMargin())
}
Expand All @@ -69,8 +71,9 @@ class DebugCockpitGeneratorTest {
val booleanSetterMethodSpec = cockpitGenerator.createSetterMethodSpecForParam(BooleanParam("booleanParam", false))

val expectedBooleanSetterMethodSpecString = """
|public static void setbooleanParam(boolean booleanParam) {
| com.polidea.cockpit.manager.CockpitManager.getInstance().setParamValue("booleanParam", booleanParam);
|public static void setBooleanParam(boolean booleanParam) {
| com.polidea.cockpit.manager.CockpitManager.INSTANCE.setParamValue("booleanParam", booleanParam);
| persistChanges();
|}"""
assertEquals(expectedBooleanSetterMethodSpecString.trimMargin(), booleanSetterMethodSpec.toString().trimMargin())
}
Expand All @@ -80,8 +83,8 @@ class DebugCockpitGeneratorTest {
val stringGetterMethodSpec = cockpitGenerator.createGetterMethodSpecForParam(StringParam("stringParam", "testValue"))

val expectedStringGetterMethodSpecString = """
|public static java.lang.String getstringParam() {
| return (java.lang.String) com.polidea.cockpit.manager.CockpitManager.getInstance().getParamValue("stringParam");
|public static java.lang.String getStringParam() {
| return (java.lang.String) com.polidea.cockpit.manager.CockpitManager.INSTANCE.getParamValue("stringParam");
|}"""
assertEquals(expectedStringGetterMethodSpecString.trimMargin(), stringGetterMethodSpec.toString().trimMargin())
}
Expand All @@ -91,8 +94,9 @@ class DebugCockpitGeneratorTest {
val stringSetterMethodSpec = cockpitGenerator.createSetterMethodSpecForParam(StringParam("stringParam", "testValue"))

val expectedStringSetterMethodSpecString = """
|public static void setstringParam(java.lang.String stringParam) {
| com.polidea.cockpit.manager.CockpitManager.getInstance().setParamValue("stringParam", stringParam);
|public static void setStringParam(java.lang.String stringParam) {
| com.polidea.cockpit.manager.CockpitManager.INSTANCE.setParamValue("stringParam", stringParam);
| persistChanges();
|}"""
assertEquals(expectedStringSetterMethodSpecString.trimMargin(), stringSetterMethodSpec.toString().trimMargin())
}
Expand All @@ -106,10 +110,10 @@ class DebugCockpitGeneratorTest {

val expectedFunSpecString = """
|private static void initializeCockpit() {
| CockpitManager.getInstance().addParam(new com.polidea.cockpit.manager.CockpitParam("doubleParam", Double.class, 3.0));
| CockpitManager.getInstance().addParam(new com.polidea.cockpit.manager.CockpitParam("booleanParam", Boolean.class, false));
| CockpitManager.getInstance().addParam(new com.polidea.cockpit.manager.CockpitParam("stringParam", String.class, "testValue"));
| CockpitManager.getInstance().addParam(new com.polidea.cockpit.manager.CockpitParam("integerParam", Integer.class, 2));
| CockpitManager.INSTANCE.addParam(new com.polidea.cockpit.manager.CockpitParam("doubleParam", Double.class, 3.0));
| CockpitManager.INSTANCE.addParam(new com.polidea.cockpit.manager.CockpitParam("booleanParam", Boolean.class, false));
| CockpitManager.INSTANCE.addParam(new com.polidea.cockpit.manager.CockpitParam("stringParam", String.class, "testValue"));
| CockpitManager.INSTANCE.addParam(new com.polidea.cockpit.manager.CockpitParam("integerParam", Integer.class, 2));
|}"""

assertEquals(expectedFunSpecString.trimMargin(), funSpec.toString().trimMargin())
Expand All @@ -125,18 +129,16 @@ class DebugCockpitGeneratorTest {
| context.startActivity(intent);
|}"""

System.out.println("expected: " + expectedFunSpecString.trimMargin())
System.out.println("given : " + funSpec.toString().trimMargin())
assertEquals(expectedFunSpecString.trimMargin(), funSpec.toString().trimMargin())
}

@Test
fun generateHideCockpitMethodTest() {
val funSpec = cockpitGenerator.generateHideCockpitMethod()
fun generatePersistChangesMethodTest() {
val funSpec = cockpitGenerator.generatePersistChangesMethod()

val expectedFunSpecString = """
|public static void hideCockpit(com.polidea.cockpit.activity.CockpitActivity cockpitActivity) {
| cockpitActivity.finish();
|private static void persistChanges() {
| com.polidea.cockpit.utils.FileUtils.INSTANCE.saveCockpitAsYaml();
|}"""

assertEquals(expectedFunSpecString.trimMargin(), funSpec.toString().trimMargin())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ReleaseCockpitGeneratorTest {
val doubleGetterMethodSpec = cockpitGenerator.createGetterMethodSpecForParam(DoubleParam("doubleParam", 3.0))

val expectedDoubleGetterMethodSpecString = """
|public static double getdoubleParam() {
|public static double getDoubleParam() {
| return 3.0;
|}"""
assertEquals(expectedDoubleGetterMethodSpecString.trimMargin(), doubleGetterMethodSpec.toString().trimMargin())
Expand All @@ -24,7 +24,7 @@ class ReleaseCockpitGeneratorTest {
val intGetterMethodSpec = cockpitGenerator.createGetterMethodSpecForParam(IntegerParam("integerParam", 2))

val expectedIntegerGetterMethodSpecString = """
|public static int getintegerParam() {
|public static int getIntegerParam() {
| return 2;
|}"""
assertEquals(expectedIntegerGetterMethodSpecString.trimMargin(), intGetterMethodSpec.toString().trimMargin())
Expand All @@ -35,7 +35,7 @@ class ReleaseCockpitGeneratorTest {
val booleanGetterMethodSpec = cockpitGenerator.createGetterMethodSpecForParam(BooleanParam("booleanParam", false))

val expectedBooleanGetterMethodSpecString = """
|public static boolean getbooleanParam() {
|public static boolean getBooleanParam() {
| return false;
|}"""
assertEquals(expectedBooleanGetterMethodSpecString.trimMargin(), booleanGetterMethodSpec.toString().trimMargin())
Expand All @@ -46,7 +46,7 @@ class ReleaseCockpitGeneratorTest {
val stringGetterMethodSpec = cockpitGenerator.createGetterMethodSpecForParam(StringParam("stringParam", "testValue"))

val expectedStringGetterMethodSpecString = """
|public static java.lang.String getstringParam() {
|public static java.lang.String getStringParam() {
| return "testValue";
|}"""
assertEquals(expectedStringGetterMethodSpecString.trimMargin(), stringGetterMethodSpec.toString().trimMargin())
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ buildscript {
}
}
dependencies {
classpath "gradle.plugin.com.polidea.cockpit:CockpitPlugin:1.0.0"
classpath "gradle.plugin.com.polidea.cockpit:CockpitPlugin:1.0.1"
}
}
dependencies {
debugImplementation 'com.polidea.cockpit:cockpit:1.0.0'
debugImplementation 'com.polidea.cockpit:cockpit:1.0.1'
}
```
## License
Expand Down
2 changes: 1 addition & 1 deletion cockpit/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ dependencies {

testImplementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
testImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
testImplementation "io.mockk:mockk:1.7.9"
testImplementation "io.mockk:mockk:1.7.15"
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class CockpitActivity : AppCompatActivity() {

val TAG = CockpitActivity::class.java.simpleName

var params = CockpitManager.getInstance().params
var params = CockpitManager.params

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -31,14 +31,14 @@ class CockpitActivity : AppCompatActivity() {
}

private fun saveCockpit() {
val cockpitViews: ArrayList<ParamView<*>> = ViewUtils().getFlatChildren(cockpit_view)
val cockpitViews: ArrayList<ParamView<*>> = ViewUtils.getFlatChildren(cockpit_view)

cockpitViews.forEach { view ->
params.find {
it.name == view.paramName
}?.also {
try {
CockpitManager.getInstance().setParamValue(it.name, view.getCurrentValue())
CockpitManager.setParamValue(it.name, view.getCurrentValue())
} catch (e: CockpitFormatException) {
Toast.makeText(this, "Invalid param value for: ${view.paramName}", Toast.LENGTH_SHORT).show()
Log.w(TAG, "Invalid param value for: ${view.paramName}")
Expand All @@ -47,7 +47,7 @@ class CockpitActivity : AppCompatActivity() {
}
}

FileUtils(this).saveCockpitAsYaml()
FileUtils.saveCockpitAsYaml()

finish()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
package com.polidea.cockpit.manager

class CockpitManager private constructor() {
object CockpitManager {

var params: MutableList<CockpitParam> = ArrayList()

companion object {
@Volatile
private var INSTANCE: CockpitManager? = null

@JvmStatic
fun getInstance(): CockpitManager {
if (INSTANCE == null) {
INSTANCE = CockpitManager()
}

return INSTANCE as CockpitManager
}
}
val params: MutableList<CockpitParam> = ArrayList()

fun addParam(param: CockpitParam) {
checkIfExistsAndAddParam(param)
Expand All @@ -43,5 +29,9 @@ class CockpitManager private constructor() {
return params.find { it.name == key } != null
}

fun clear() {
params.clear()
}

private fun getParam(name: String): CockpitParam? = params.find { it.name == name }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import android.net.Uri
class CockpitInitProvider : ContentProvider() {

override fun onCreate(): Boolean {
FileUtils(context).readCockpitFromFile()
FileUtils.init(context.filesDir.path)
FileUtils.readCockpitFromFile()
return false
}

Expand Down
Loading

0 comments on commit ca8e860

Please sign in to comment.