Skip to content

Commit

Permalink
Merge pull request #15 from icerockdev/develop
Browse files Browse the repository at this point in the history
release 0.6.0
  • Loading branch information
Alex009 authored Dec 16, 2019
2 parents d918d2f + a1d843b commit abbecea
Show file tree
Hide file tree
Showing 28 changed files with 354 additions and 110 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/compilation-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: KMP library compilation check

on:
pull_request:
branches:
- master

jobs:
build:
runs-on: macOS-latest

steps:
- uses: actions/checkout@v1
- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Build and publish local
run: ./gradlew -PlibraryPublish :gradle-plugin:publishPluginPublicationToMavenLocal :resources:publishToMavenLocal
18 changes: 18 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: KMP library publish

on:
release:
types: [published]

jobs:
build:
runs-on: macOS-latest

steps:
- uses: actions/checkout@v1
- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Build and publish to Bintray
run: ./gradlew -PlibraryPublish :gradle-plugin:publishPluginPublicationToBintrayRepository :resources:publishAllPublicationsToBintrayRepository -DBINTRAY_USER=${{ secrets.BINTRAY_USER }} -DBINTRAY_KEY=${{ secrets.BINTRAY_KEY }}
20 changes: 0 additions & 20 deletions MultiPlatformLibraryResources.podspec

This file was deleted.

29 changes: 25 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
![moko-resources](img/logo.png)
[![GitHub license](https://img.shields.io/badge/license-Apache%20License%202.0-blue.svg?style=flat)](http://www.apache.org/licenses/LICENSE-2.0) [![Download](https://api.bintray.com/packages/icerockdev/moko/moko-resources/images/download.svg) ](https://bintray.com/icerockdev/moko/moko-resources/_latestVersion) ![kotlin-version](https://img.shields.io/badge/kotlin-1.3.60-orange)
[![GitHub license](https://img.shields.io/badge/license-Apache%20License%202.0-blue.svg?style=flat)](http://www.apache.org/licenses/LICENSE-2.0) [![Download](https://api.bintray.com/packages/icerockdev/moko/moko-resources/images/download.svg) ](https://bintray.com/icerockdev/moko/moko-resources/_latestVersion) ![kotlin-version](https://img.shields.io/badge/kotlin-1.3.61-orange)

# Mobile Kotlin resources
This is a Kotlin MultiPlatform library that provides access to the resources on iOS & Android with the support of the default system localization.
Expand All @@ -16,7 +16,7 @@ This is a Kotlin MultiPlatform library that provides access to the resources on
- [License](#license)

## Features
- **Strings, Plurals, Drawables** to access the corresponding resources from common code;
- **Strings, Plurals, Images** to access the corresponding resources from common code;
- **StringDesc** for lifecycle-aware access to resources and unified localization on both platforms.

## Requirements
Expand All @@ -32,6 +32,8 @@ This is a Kotlin MultiPlatform library that provides access to the resources on
- 0.4.0
- kotlin 1.3.60
- 0.5.0
- kotlin 1.3.61
- 0.6.0

## Installation
root build.gradle
Expand All @@ -42,7 +44,7 @@ buildscript {
}
dependencies {
classpath "dev.icerock.moko:resources-generator:0.5.0"
classpath "dev.icerock.moko:resources-generator:0.6.0"
}
}
Expand All @@ -59,7 +61,7 @@ project build.gradle
apply plugin: "dev.icerock.mobile.multiplatform-resources"
dependencies {
commonMainApi("dev.icerock.moko:resources:0.5.0")
commonMainApi("dev.icerock.moko:resources:0.6.0")
}
multiplatformResources {
Expand Down Expand Up @@ -218,6 +220,25 @@ let string1 = getUserName(user: user).localized() // we got name from User model
let string2 = getUserName(user: null).localized() // we got name_placeholder from resources
```

### Example 6 - pass image
Image resources directory is `commonMain/resources/MR/images` with support of nested directories.
Image name should be end with one of:
- `@0.75x` - android ldpi;
- `@1x` - android mdpi, ios 1x;
- `@1.5x` - android hdpi;
- `@2x` - android xhdpi, ios 2x;
- `@3x` - android xxhdpi, ios 3x;
- `@4x` - android xxxhdpi.
Supported `png` and `jpg` resources for now.

If we add to `commonMain/resources/MR/images` files:
- `[email protected]`
- `[email protected]`

We got autogenerated `MR.images.home_black_18` `ImageResource` in code, that we can use:
- Android: `imageView.setImageResource(image.drawableResId)`
- iOS: `imageView.image = image.toUIImage()`

## Samples
Please see more examples in the [sample directory](sample).

Expand Down
4 changes: 2 additions & 2 deletions buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ object Versions {
const val minSdk = 16
}

const val kotlin = "1.3.60"
const val kotlin = "1.3.61"

private const val mokoResources = "0.5.0"
private const val mokoResources = "0.6.0"

object Plugins {
const val android = "3.5.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import dev.icerock.gradle.generator.AndroidMRGenerator
import dev.icerock.gradle.generator.CommonMRGenerator
import dev.icerock.gradle.generator.IosMRGenerator
import dev.icerock.gradle.generator.MRGenerator
import dev.icerock.gradle.generator.image.AndroidImagesGenerator
import dev.icerock.gradle.generator.image.CommonImagesGenerator
import dev.icerock.gradle.generator.image.IosImagesGenerator
import dev.icerock.gradle.generator.plurals.AndroidPluralsGenerator
import dev.icerock.gradle.generator.plurals.CommonPluralsGenerator
import dev.icerock.gradle.generator.plurals.IosPluralsGenerator
Expand Down Expand Up @@ -60,6 +63,9 @@ class MultiplatformResourcesPlugin : Plugin<Project> {
val plurals = commonResources.matching {
include("MR/**/plurals.xml")
}
val images = commonResources.matching {
include("MR/images/**/*.png", "MR/images/**/*.jpg")
}

val androidExtension = target.extensions.getByType(LibraryExtension::class)
val mainAndroidSet = androidExtension.sourceSets.getByName("main")
Expand All @@ -71,6 +77,7 @@ class MultiplatformResourcesPlugin : Plugin<Project> {
project = target,
stringsFileTree = strings,
pluralsFileTree = plurals,
imagesFileTree = images,
sourceSets = sourceSets.filter { it.name.endsWith("Main") },
extension = mrExtension,
multiplatformExtension = multiplatformExtension,
Expand All @@ -94,13 +101,13 @@ class MultiplatformResourcesPlugin : Plugin<Project> {
project: Project,
stringsFileTree: FileTree,
pluralsFileTree: FileTree,
imagesFileTree: FileTree,
sourceSets: List<KotlinSourceSet>,
extension: MultiplatformResourcesPluginExtension,
multiplatformExtension: KotlinMultiplatformExtension,
androidPackage: String
) {
val generatedDir = File(project.buildDir, "generated/moko")
generatedDir.deleteRecursively()

sourceSets.forEach { sourceSet ->
val generator = createGenerator(
Expand All @@ -109,6 +116,7 @@ class MultiplatformResourcesPlugin : Plugin<Project> {
sourceSet = sourceSet,
stringsFileTree = stringsFileTree,
pluralsFileTree = pluralsFileTree,
imagesFileTree = imagesFileTree,
mrClassPackage = extension.multiplatformResourcesPackage!!,
androidRClassPackage = androidPackage
) ?: return@forEach
Expand All @@ -123,6 +131,7 @@ class MultiplatformResourcesPlugin : Plugin<Project> {
sourceSet: KotlinSourceSet,
stringsFileTree: FileTree,
pluralsFileTree: FileTree,
imagesFileTree: FileTree,
mrClassPackage: String,
androidRClassPackage: String
): MRGenerator? {
Expand All @@ -132,6 +141,7 @@ class MultiplatformResourcesPlugin : Plugin<Project> {
sourceSet = sourceSet,
stringsFileTree = stringsFileTree,
pluralsFileTree = pluralsFileTree,
imagesFileTree = imagesFileTree,
mrClassPackage = mrClassPackage
)
}
Expand All @@ -148,6 +158,7 @@ class MultiplatformResourcesPlugin : Plugin<Project> {
sourceSet = sourceSet,
stringsFileTree = stringsFileTree,
pluralsFileTree = pluralsFileTree,
imagesFileTree = imagesFileTree,
mrClassPackage = mrClassPackage,
androidRClassPackage = androidRClassPackage
)
Expand All @@ -160,6 +171,7 @@ class MultiplatformResourcesPlugin : Plugin<Project> {
sourceSet = sourceSet,
stringsFileTree = stringsFileTree,
pluralsFileTree = pluralsFileTree,
imagesFileTree = imagesFileTree,
mrClassPackage = mrClassPackage
)
} else {
Expand All @@ -178,8 +190,9 @@ class MultiplatformResourcesPlugin : Plugin<Project> {
generatedDir: File,
sourceSet: KotlinSourceSet,
stringsFileTree: FileTree,
mrClassPackage: String,
pluralsFileTree: FileTree
imagesFileTree: FileTree,
pluralsFileTree: FileTree,
mrClassPackage: String
): MRGenerator {
return CommonMRGenerator(
generatedDir = generatedDir,
Expand All @@ -193,6 +206,10 @@ class MultiplatformResourcesPlugin : Plugin<Project> {
CommonPluralsGenerator(
sourceSet = sourceSet,
pluralsFileTree = pluralsFileTree
),
CommonImagesGenerator(
sourceSet = sourceSet,
inputFileTree = imagesFileTree
)
)
)
Expand All @@ -202,9 +219,10 @@ class MultiplatformResourcesPlugin : Plugin<Project> {
generatedDir: File,
sourceSet: KotlinSourceSet,
stringsFileTree: FileTree,
pluralsFileTree: FileTree,
imagesFileTree: FileTree,
mrClassPackage: String,
androidRClassPackage: String,
pluralsFileTree: FileTree
androidRClassPackage: String
): MRGenerator {
return AndroidMRGenerator(
generatedDir = generatedDir,
Expand All @@ -220,6 +238,11 @@ class MultiplatformResourcesPlugin : Plugin<Project> {
sourceSet = sourceSet,
pluralsFileTree = pluralsFileTree,
androidRClassPackage = androidRClassPackage
),
AndroidImagesGenerator(
sourceSet = sourceSet,
inputFileTree = imagesFileTree,
androidRClassPackage = androidRClassPackage
)
)
)
Expand All @@ -229,8 +252,9 @@ class MultiplatformResourcesPlugin : Plugin<Project> {
generatedDir: File,
sourceSet: KotlinSourceSet,
stringsFileTree: FileTree,
mrClassPackage: String,
pluralsFileTree: FileTree
pluralsFileTree: FileTree,
imagesFileTree: FileTree,
mrClassPackage: String
): MRGenerator {
return IosMRGenerator(
generatedDir = generatedDir,
Expand All @@ -244,6 +268,10 @@ class MultiplatformResourcesPlugin : Plugin<Project> {
IosPluralsGenerator(
sourceSet = sourceSet,
pluralsFileTree = pluralsFileTree
),
IosImagesGenerator(
sourceSet = sourceSet,
inputFileTree = imagesFileTree
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ abstract class MRGenerator(
}

private fun generate() {
sourcesGenerationDir.deleteRecursively()
resourcesGenerationDir.deleteRecursively()

val mrClassSpec = TypeSpec.objectBuilder(mrClassName)
.addModifiers(*getMRClassModifiers())

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2019 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.gradle.generator.image

import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.KModifier
import org.gradle.api.file.FileTree
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import java.io.File

class AndroidImagesGenerator(
sourceSet: KotlinSourceSet,
inputFileTree: FileTree,
private val androidRClassPackage: String
) : ImagesGenerator(
sourceSet = sourceSet,
inputFileTree = inputFileTree
) {
override fun getClassModifiers(): Array<KModifier> = arrayOf(KModifier.ACTUAL)

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

override fun getPropertyInitializer(key: String): CodeBlock? {
return CodeBlock.of("ImageResource(R.drawable.%L)", key)
}

override fun getImports(): List<ClassName> = listOf(
ClassName(androidRClassPackage, "R")
)

override fun generateResources(
resourcesGenerationDir: File,
keyFileMap: Map<String, List<File>>
) {
keyFileMap.flatMap { (key, files) ->
files.map { key to it }
}.forEach { (key, file) ->
val scale = file.nameWithoutExtension.substringAfter("@").substringBefore("x")
val drawableDirName = "drawable-" + when (scale) {
"0.75" -> "ldpi"
"1" -> "mdpi"
"1.5" -> "hdpi"
"2" -> "xhdpi"
"3" -> "xxhdpi"
"4" -> "xxxhdpi"
else -> {
println("ignore $file - unknown scale ($scale)")
return@forEach
}
}

val drawableDir = File(resourcesGenerationDir, drawableDirName)
file.copyTo(File(drawableDir, "$key.${file.extension}"))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2019 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.gradle.generator.image

import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.KModifier
import org.gradle.api.file.FileTree
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet

class CommonImagesGenerator(
sourceSet: KotlinSourceSet,
inputFileTree: FileTree
) : ImagesGenerator(
sourceSet = sourceSet,
inputFileTree = inputFileTree
) {
override fun getClassModifiers(): Array<KModifier> = emptyArray()

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

override fun getPropertyInitializer(key: String): CodeBlock? = null
}
Loading

0 comments on commit abbecea

Please sign in to comment.