Skip to content

Commit

Permalink
#44 implemented raw file resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex009 committed May 4, 2020
1 parent cf5aeb0 commit 3da5c95
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,41 @@ abstract class FilesGenerator(
file = file
)
}.sortedBy { it.key }
val typeSpec = createTypeSpec(fileSpecs.map { it.key })
val typeSpec = createTypeSpec(fileSpecs)
generateResources(resourcesGenerationDir, fileSpecs)
return typeSpec
}

private fun createTypeSpec(keys: List<String>): TypeSpec {
private fun createTypeSpec(keys: List<FileSpec>): TypeSpec {
val classBuilder = TypeSpec.objectBuilder("files")
@Suppress("SpreadOperator")
classBuilder.addModifiers(*getClassModifiers())

keys.forEach { classBuilder.addProperty(generateFileProperty(fileName = it)) }
keys.forEach {
classBuilder.addProperty(
generateFileProperty(
fileName = it.key,
fileExtension = it.file.extension
)
)
}
return classBuilder.build()
}

override fun getImports(): List<ClassName> = emptyList()

private fun generateFileProperty(
fileName: String
fileName: String,
fileExtension: String
): PropertySpec {
@Suppress("SpreadOperator")
return PropertySpec.builder(fileName, resourceClass)
return PropertySpec.builder(processKey(fileName), resourceClass)
.addModifiers(*getPropertyModifiers())
.apply {
getPropertyInitializer(fileName)?.let { initializer(it) }
getPropertyInitializer(
fileName = fileName,
fileExtension = fileExtension
)?.let { initializer(it) }
}
.build()
}
Expand All @@ -62,11 +73,15 @@ abstract class FilesGenerator(
) {
}

protected fun processKey(key: String): String {
return key.replace("-", "_")
}

abstract fun getClassModifiers(): Array<KModifier>

abstract fun getPropertyModifiers(): Array<KModifier>

abstract fun getPropertyInitializer(fileName: String): CodeBlock?
abstract fun getPropertyInitializer(fileName: String, fileExtension: String): CodeBlock?

data class FileSpec(
val key: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class AndroidFilesGenerator(

override fun getPropertyModifiers(): Array<KModifier> = arrayOf(KModifier.ACTUAL)

override fun getPropertyInitializer(fileName: String): CodeBlock? {
override fun getPropertyInitializer(fileName: String, fileExtension: String): CodeBlock? {
return CodeBlock.of("FileResource(rawResId = R.raw.%L)", keyToResourceId(fileName))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package dev.icerock.gradle.generator.common
import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.KModifier
import dev.icerock.gradle.generator.FilesGenerator
import dev.icerock.gradle.generator.FontsGenerator
import org.gradle.api.file.FileTree

class CommonFilesGenerator(
Expand All @@ -19,5 +18,5 @@ class CommonFilesGenerator(

override fun getPropertyModifiers(): Array<KModifier> = emptyArray()

override fun getPropertyInitializer(fileName: String): CodeBlock? = null
override fun getPropertyInitializer(fileName: String, fileExtension: String): CodeBlock? = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ class IosFilesGenerator(

override fun getPropertyModifiers(): Array<KModifier> = arrayOf(KModifier.ACTUAL)

override fun getPropertyInitializer(fileName: String): CodeBlock? {
override fun getPropertyInitializer(fileName: String, fileExtension: String): CodeBlock? {
return CodeBlock.of(
"FileResource(fileName = %S, bundle = ${IosMRGenerator.BUNDLE_PROPERTY_NAME})",
fileName
"FileResource(fileName = %S, extension = %S,bundle = ${IosMRGenerator.BUNDLE_PROPERTY_NAME})",
fileName,
fileExtension
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@ import platform.Foundation.stringWithContentsOfFile

actual class FileResource(
val fileName: String,
val extension: String,
val bundle: NSBundle = NSBundle.mainBundle
) {
val path: String get() = bundle.pathForResource(name = fileName, ofType = null, inDirectory = "files")!!
val url: NSURL get() = bundle.URLForResource(name = fileName, withExtension = null, subdirectory = "files")!!
val path: String get() = bundle.pathForResource(name = fileName, ofType = extension, inDirectory = "files")!!
val url: NSURL get() = bundle.URLForResource(name = fileName, withExtension = extension, subdirectory = "files")!!

fun readText(): String {
val filePath = path
val (result: String?, error: NSError?) = memScoped {
val p = alloc<ObjCObjectVar<NSError?>>()
val result: String? = runCatching {
NSString.stringWithContentsOfFile(
path = path,
path = filePath,
encoding = NSUTF8StringEncoding,
error = p.ptr
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package com.icerockdev

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
Expand Down Expand Up @@ -33,6 +34,12 @@ class MainActivity : AppCompatActivity() {

stringDescTextView.text = Testing.getStringDesc().toString(context = this)
stringDescTextView.typeface = Testing.getFont2().getTypeface(context = this)

listOf(
Testing.getTextFile().readText(context = this),
Testing.getJsonFile().readText(context = this),
Testing.getNestedJsonFile().readText(context = this)
).forEach { Log.d(MainActivity::class.java.simpleName, it) }
}

/**
Expand All @@ -41,5 +48,4 @@ class MainActivity : AppCompatActivity() {
override fun attachBaseContext(newBase: Context) {
super.attachBaseContext(LocaleHandler.updateLocale(newBase))
}

}
7 changes: 7 additions & 0 deletions sample/ios-app/src/TestViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ class TestViewController: UIViewController {

stringDescTextView.text = testing.getStringDesc().localized()
stringDescTextView.font = testing.getFont2().uiFont(withSize: 14.0)

[
testing.getTextFile(),
testing.getJsonFile(),
testing.getNestedJsonFile()
].map { $0.readText() }
.forEach { print($0) }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@

package com.icerockdev.library.nested

import dev.icerock.moko.resources.FileResource
import dev.icerock.moko.resources.desc.StringDesc
import dev.icerock.moko.resources.desc.desc

fun nestedTest(): StringDesc {
return MR.strings.nested_test.desc()
}

fun nestedFile(): FileResource {
return MR.files.nested_test
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": [
"GML",
"XML"
]
},
"GlossSee": "markup"
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

package com.icerockdev.library

import com.icerockdev.library.nested.nestedFile
import com.icerockdev.library.nested.nestedTest
import dev.icerock.moko.resources.FileResource
import dev.icerock.moko.resources.ImageResource
import dev.icerock.moko.resources.desc.StringDesc
import dev.icerock.moko.resources.desc.desc
import dev.icerock.moko.resources.desc.plus
import com.icerockdev.library.nested.nestedTest

object Testing {
fun getStrings(): List<StringDesc> {
Expand Down Expand Up @@ -84,4 +86,16 @@ object Testing {
StringDesc.localeType = if (lang != null) StringDesc.LocaleType.Custom(lang)
else StringDesc.LocaleType.System
}

fun getTextFile(): FileResource {
return MR.files.test
}

fun getJsonFile(): FileResource {
return MR.files.some
}

fun getNestedJsonFile(): FileResource {
return nestedFile()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"key": "value",
"number": 9
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
it's just test text file

0 comments on commit 3da5c95

Please sign in to comment.