├── gradle.properties ├── src ├── reactMain │ └── kotlin │ │ └── com │ │ └── example │ │ └── demo │ │ ├── ui │ │ ├── app │ │ │ ├── App.css │ │ │ └── App.kt │ │ └── components │ │ │ ├── Info.kt │ │ │ ├── HeaderInput.kt │ │ │ ├── TodoList.kt │ │ │ ├── TodoBar.kt │ │ │ └── TodoItem.kt │ │ ├── utils │ │ ├── Utils.kt │ │ └── I18n.kt │ │ ├── client │ │ └── create.kt │ │ ├── index.kt │ │ └── repository │ │ └── LocalStorageTodoRepository.kt ├── springMain │ ├── resources │ │ └── application.properties │ └── kotlin │ │ └── com │ │ └── example │ │ └── demo │ │ ├── DemoApplication.kt │ │ ├── configuration │ │ ├── AppConfiguration.kt │ │ └── KotlinxSerializationStrategyConfiguration.kt │ │ ├── controller │ │ ├── IndexController.kt │ │ └── TodoController.kt │ │ └── repository │ │ └── InMemoryTodoRepository.kt ├── springTest │ └── kotlin │ │ └── com │ │ └── example │ │ └── demo │ │ └── DemoApplicationTests.kt ├── commonMain │ └── kotlin │ │ └── com │ │ └── example │ │ └── demo │ │ ├── repository │ │ └── TodoRepository.kt │ │ ├── client │ │ └── Client.kt │ │ ├── model │ │ └── Todo.kt │ │ └── service │ │ └── TodoService.kt └── commonClientMain │ └── kotlin │ └── com │ └── example │ └── demo │ └── client │ └── RSocketClient.kt ├── screens └── screen.png ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── settings.gradle.kts ├── .run └── DemoApplication.run.xml ├── .gitignore ├── README.md ├── gradlew.bat └── gradlew /gradle.properties: -------------------------------------------------------------------------------- 1 | kotlin.code.style=official 2 | kotlin.incremental.js.ir=false -------------------------------------------------------------------------------- /src/reactMain/kotlin/com/example/demo/ui/app/App.css: -------------------------------------------------------------------------------- 1 | a { 2 | cursor: pointer; 3 | } 4 | -------------------------------------------------------------------------------- /screens/screen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kotlin/full-stack-spring-collaborative-todo-list-sample/HEAD/screens/screen.png -------------------------------------------------------------------------------- /src/springMain/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.rsocket.server.mapping-path=/rsocket 2 | spring.rsocket.server.transport=websocket -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kotlin/full-stack-spring-collaborative-todo-list-sample/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /settings.gradle.kts: -------------------------------------------------------------------------------- 1 | pluginManagement { 2 | repositories { 3 | maven { url = uri("https://repo.spring.io/milestone") } 4 | maven { url = uri("https://repo.spring.io/snapshot") } 5 | gradlePluginPortal() 6 | } 7 | } 8 | rootProject.name = "collaborative-todo-list" 9 | -------------------------------------------------------------------------------- /src/springTest/kotlin/com/example/demo/DemoApplicationTests.kt: -------------------------------------------------------------------------------- 1 | package com.example.demo 2 | 3 | import org.junit.jupiter.api.Test 4 | import org.springframework.boot.test.context.SpringBootTest 5 | 6 | @SpringBootTest 7 | class DemoApplicationTests { 8 | 9 | @Test 10 | fun contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/commonMain/kotlin/com/example/demo/repository/TodoRepository.kt: -------------------------------------------------------------------------------- 1 | package com.example.demo.repository 2 | 3 | import com.example.demo.model.Todo 4 | 5 | interface TodoRepository { 6 | fun save(todo: Todo) 7 | fun remove(todo: Todo) 8 | fun update(todo: Todo) 9 | fun all(): List 10 | fun get(todoId: String): Todo? 11 | } 12 | -------------------------------------------------------------------------------- /src/springMain/kotlin/com/example/demo/DemoApplication.kt: -------------------------------------------------------------------------------- 1 | package com.example.demo 2 | 3 | import org.springframework.boot.autoconfigure.SpringBootApplication 4 | import org.springframework.boot.runApplication 5 | 6 | 7 | @SpringBootApplication 8 | class DemoApplication 9 | 10 | fun main(args: Array) { 11 | runApplication(*args) 12 | } 13 | -------------------------------------------------------------------------------- /src/commonMain/kotlin/com/example/demo/client/Client.kt: -------------------------------------------------------------------------------- 1 | package com.example.demo.client 2 | 3 | import com.example.demo.model.Todo 4 | import com.example.demo.model.TodoEvent 5 | 6 | interface Client { 7 | fun handleTodos(handler: (TodoEvent) -> Unit) 8 | fun exchange(todo: List) 9 | fun addTodo(todo: Todo) 10 | fun updateTodo(todo: Todo) 11 | fun removeTodo(todo: Todo) 12 | } -------------------------------------------------------------------------------- /src/springMain/kotlin/com/example/demo/configuration/AppConfiguration.kt: -------------------------------------------------------------------------------- 1 | package com.example.demo.configuration 2 | 3 | import com.example.demo.repository.TodoRepository 4 | import com.example.demo.service.TodoService 5 | import org.springframework.context.annotation.Bean 6 | import org.springframework.context.annotation.Configuration 7 | 8 | @Configuration 9 | class AppConfiguration { 10 | 11 | @Bean 12 | fun todoService(todoRepository: TodoRepository) = TodoService(todoRepository) 13 | } -------------------------------------------------------------------------------- /.run/DemoApplication.run.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | .gradle 3 | build/ 4 | !gradle/wrapper/gradle-wrapper.jar 5 | !**/src/main/**/build/ 6 | !**/src/test/**/build/ 7 | 8 | ### STS ### 9 | .apt_generated 10 | .classpath 11 | .factorypath 12 | .project 13 | .settings 14 | .springBeans 15 | .sts4-cache 16 | bin/ 17 | !**/src/main/**/bin/ 18 | !**/src/test/**/bin/ 19 | 20 | ### IntelliJ IDEA ### 21 | .idea 22 | *.iws 23 | *.iml 24 | *.ipr 25 | out/ 26 | !**/src/main/**/out/ 27 | !**/src/test/**/out/ 28 | 29 | ### NetBeans ### 30 | /nbproject/private/ 31 | /nbbuild/ 32 | /dist/ 33 | /nbdist/ 34 | /.nb-gradle/ 35 | 36 | ### VS Code ### 37 | .vscode/ 38 | -------------------------------------------------------------------------------- /src/springMain/kotlin/com/example/demo/controller/IndexController.kt: -------------------------------------------------------------------------------- 1 | package com.example.demo.controller 2 | 3 | import kotlinx.html.* 4 | import kotlinx.html.stream.createHTML 5 | import org.springframework.stereotype.Controller 6 | import org.springframework.web.bind.annotation.GetMapping 7 | import org.springframework.web.bind.annotation.ResponseBody 8 | 9 | @Controller 10 | class IndexController { 11 | 12 | @GetMapping 13 | @ResponseBody 14 | fun index() = createHTML().html { 15 | head { 16 | title("ToDo List") 17 | } 18 | body { 19 | div { 20 | id = "root" 21 | } 22 | script(src = "/main.js") {} 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /src/reactMain/kotlin/com/example/demo/utils/Utils.kt: -------------------------------------------------------------------------------- 1 | package com.example.demo.utils 2 | 3 | import org.w3c.dom.events.Event 4 | import kotlin.js.Date 5 | 6 | val Event.value: String 7 | get() = this.currentTarget.asDynamic().value as String 8 | 9 | fun Date.toLocalString(): String { 10 | return this.toLocaleDateString(kotlinx.browser.window.navigator.language) 11 | } 12 | 13 | enum class Keys { 14 | Enter, 15 | Escape; 16 | 17 | companion object { 18 | fun fromString(keyName: String): Keys? { 19 | return if(Keys.values().map { key -> key.toString() }.contains(keyName)) { 20 | Keys.valueOf(keyName) 21 | } else { 22 | null 23 | } 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /src/reactMain/kotlin/com/example/demo/ui/components/Info.kt: -------------------------------------------------------------------------------- 1 | package com.example.demo.ui.components 2 | 3 | import com.example.demo.utils.translate 4 | import react.FC 5 | import react.Props 6 | import react.RBuilder 7 | import react.dom.a 8 | import react.dom.footer 9 | import react.dom.p 10 | import react.functionComponent 11 | 12 | private val Info: FC = functionComponent { 13 | footer("info") { 14 | p { +"Double-click to edit a todo".translate() } 15 | p { 16 | +"Created by".translate() 17 | +" " 18 | a("https://venturus.org.br/") { +"venturus.org.br" } 19 | } 20 | p { 21 | +"Part of" 22 | +" " 23 | a("http://todomvc.com") { +"TodoMVC" } 24 | } 25 | } 26 | } 27 | 28 | fun RBuilder.info() = child(Info) {} 29 | -------------------------------------------------------------------------------- /src/commonMain/kotlin/com/example/demo/model/Todo.kt: -------------------------------------------------------------------------------- 1 | package com.example.demo.model 2 | 3 | import com.benasher44.uuid.uuid4 4 | import kotlinx.serialization.Serializable 5 | 6 | @Serializable 7 | data class Todo ( 8 | val id: String = uuid4().toString(), 9 | val title: String, 10 | var completed: Boolean = false, 11 | var removed: Boolean = false 12 | ) 13 | 14 | @Serializable 15 | enum class EventType { 16 | ADD, UPDATE, UPSERT, REMOVE 17 | } 18 | 19 | @Serializable 20 | data class TodoEvent(val type: EventType, val todo: Todo) 21 | 22 | enum class TodoFilter { 23 | ANY, COMPLETED, PENDING; 24 | 25 | fun filter(todo: Todo): Boolean { 26 | return when (this) { 27 | ANY -> true 28 | COMPLETED -> todo.completed 29 | PENDING -> !todo.completed 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /src/springMain/kotlin/com/example/demo/repository/InMemoryTodoRepository.kt: -------------------------------------------------------------------------------- 1 | package com.example.demo.repository 2 | 3 | import com.example.demo.model.Todo 4 | import org.springframework.stereotype.Repository 5 | import java.util.concurrent.ConcurrentHashMap 6 | import java.util.concurrent.ConcurrentMap 7 | 8 | @Repository 9 | class InMemoryTodoRepository : TodoRepository { 10 | 11 | private val store: ConcurrentMap = ConcurrentHashMap() 12 | 13 | override fun save(todo: Todo) { 14 | store[todo.id] = todo 15 | } 16 | 17 | override fun remove(todo: Todo) { 18 | store.remove(todo.id) 19 | } 20 | 21 | override fun update(todo: Todo) { 22 | store[todo.id] = todo 23 | } 24 | 25 | override fun all(): List = store.values.toList() 26 | 27 | override fun get(todoId: String): Todo? = store[todoId] 28 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # full-stack-spring-collaborative-todo-list-sample 2 | 3 | Fork of [https://github.com/OlegDokuka/collaborative-todo-list](https://github.com/OlegDokuka/collaborative-todo-list) 4 | 5 | A full-stack demo application written with `Kotlin/JS` and `Spring` 6 | 7 | ![screenshot](screens/screen.png) 8 | 9 | ## **Run application** 10 | 11 | - Open project in Intellij IDEA 12 | - Run `DemoApplication` configuration 13 | 14 | ## **Description** 15 | 16 | This application is an example of ToDo list application for collaborative work. 17 | 18 | It is a [Kotlin Multiplatform](https://kotlinlang.org/docs/reference/multiplatform.html) project. 19 | 20 | It uses: 21 | 22 | - `kotlin-multiplatform`, with two targets `js` and `jvm`; 23 | - Spring framework for backend; 24 | - [Kotlin/JS](https://kotlinlang.org/docs/js-overview.html) with React framework for frontend; 25 | - RSocket -------------------------------------------------------------------------------- /src/reactMain/kotlin/com/example/demo/client/create.kt: -------------------------------------------------------------------------------- 1 | package com.example.demo.client 2 | 3 | import io.ktor.client.* 4 | import io.ktor.client.plugins.websocket.* 5 | import io.rsocket.kotlin.core.RSocketConnector 6 | import io.rsocket.kotlin.core.WellKnownMimeType 7 | import io.rsocket.kotlin.ktor.client.RSocketSupport 8 | import io.rsocket.kotlin.ktor.client.rSocket 9 | import io.rsocket.kotlin.payload.PayloadMimeType 10 | import kotlinx.browser.window 11 | 12 | actual suspend fun RSocketClient.Companion.create(): Client { 13 | val client: HttpClient = HttpClient { 14 | install(WebSockets) 15 | install(RSocketSupport) { 16 | connector = RSocketConnector { 17 | connectionConfig { 18 | payloadMimeType = PayloadMimeType( 19 | data = WellKnownMimeType.ApplicationJson, 20 | metadata = WellKnownMimeType.MessageRSocketCompositeMetadata 21 | ) 22 | } 23 | } 24 | } 25 | } 26 | 27 | val rSocket = client.rSocket( 28 | host = window.location.hostname, 29 | port = window.location.port.toInt(), 30 | path = "/rsocket" 31 | ) 32 | 33 | return RSocketClient(rSocket) 34 | } -------------------------------------------------------------------------------- /src/springMain/kotlin/com/example/demo/configuration/KotlinxSerializationStrategyConfiguration.kt: -------------------------------------------------------------------------------- 1 | package com.example.demo.configuration 2 | 3 | import kotlinx.serialization.json.Json 4 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass 5 | import org.springframework.boot.rsocket.messaging.RSocketStrategiesCustomizer 6 | import org.springframework.context.annotation.Bean 7 | import org.springframework.context.annotation.Configuration 8 | import org.springframework.core.annotation.Order 9 | import org.springframework.http.codec.json.KotlinSerializationJsonDecoder 10 | import org.springframework.http.codec.json.KotlinSerializationJsonEncoder 11 | import org.springframework.messaging.rsocket.RSocketStrategies 12 | 13 | 14 | @Configuration(proxyBeanMethods = false) 15 | @ConditionalOnClass( 16 | Json::class, 17 | ) 18 | class KotlinxSerializationStrategyConfiguration { 19 | @Bean 20 | @Order(-1) 21 | fun kotlinxSerializationRSocketStrategyCustomizer(): RSocketStrategiesCustomizer { 22 | return RSocketStrategiesCustomizer { strategy: RSocketStrategies.Builder -> 23 | strategy.decoder(KotlinSerializationJsonDecoder()) 24 | strategy.encoder(KotlinSerializationJsonEncoder()) 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /src/reactMain/kotlin/com/example/demo/index.kt: -------------------------------------------------------------------------------- 1 | package com.example.demo 2 | 3 | import com.example.demo.ui.app.AppOptions 4 | import com.example.demo.ui.app.app 5 | import com.example.demo.client.RSocketClient 6 | import com.example.demo.client.create 7 | import com.example.demo.service.TodoService 8 | import kotlinext.js.require 9 | import kotlinext.js.requireAll 10 | import kotlinx.browser.document 11 | import react.dom.render 12 | import com.example.demo.repository.LocalStorageTodoRepository 13 | import react.router.dom.BrowserRouter 14 | 15 | 16 | suspend fun main(args: Array) { 17 | initStyles() 18 | AppOptions.language = "en_US" 19 | 20 | val client = RSocketClient.create() 21 | val service = TodoService(LocalStorageTodoRepository()) 22 | 23 | render(document.getElementById("root")) { 24 | BrowserRouter { 25 | app(client, service) 26 | } 27 | } 28 | 29 | } 30 | 31 | fun initStyles() { 32 | requireAll(require.context("", true, js("/\\.css$/"))) 33 | requireAll(require.context("../../../node_modules/todomvc-app-css", true, js("/\\.css$/"))) 34 | requireAll(require.context("../../../node_modules/todomvc-common", true, js("/\\.css$/"))) 35 | requireAll(require.context("../../../node_modules/todomvc-common", true, js("/\\.js$/"))) 36 | } 37 | -------------------------------------------------------------------------------- /src/reactMain/kotlin/com/example/demo/utils/I18n.kt: -------------------------------------------------------------------------------- 1 | package com.example.demo.utils 2 | 3 | import com.example.demo.ui.app.AppOptions 4 | import kotlin.math.absoluteValue 5 | 6 | object I18n { 7 | 8 | private val enUSLanguage = mapOf( 9 | "todos" to "todos", 10 | "What needs to be done?" to "What needs to be done?", 11 | "Mark all as complete" to "Mark all as complete", 12 | "Double-click to edit a todo" to "Double-click to edit a todo", 13 | "Created by" to "Created by", 14 | "Part of" to "Part of", 15 | "item left" to "item left", 16 | "PLURALS" to mapOf( 17 | "item left" to "items left" 18 | ) 19 | ) 20 | 21 | private val languageMap = mapOf("en_US" to enUSLanguage) 22 | 23 | private val currentLanguage: Map by lazy { 24 | languageMap[AppOptions.language]!! 25 | } 26 | 27 | 28 | fun translate(key: String): String { 29 | return (currentLanguage[key] as String?) ?: "***$key" 30 | } 31 | 32 | fun pluralize(key: String): String { 33 | return ((currentLanguage["PLURALS"] as Map<*, *>)[key] as String?) ?: "***$key***" 34 | } 35 | } 36 | 37 | fun String.translate(): String { 38 | return I18n.translate(this) 39 | } 40 | 41 | fun String.pluralize(count: Int): String { 42 | return if (count.absoluteValue == 1) { 43 | this 44 | } else { 45 | I18n.pluralize(this) 46 | } 47 | } -------------------------------------------------------------------------------- /src/reactMain/kotlin/com/example/demo/repository/LocalStorageTodoRepository.kt: -------------------------------------------------------------------------------- 1 | package com.example.demo.repository 2 | 3 | import com.example.demo.ui.app.AppOptions 4 | import com.example.demo.model.Todo 5 | import kotlinx.browser.localStorage 6 | import org.w3c.dom.get 7 | 8 | class LocalStorageTodoRepository : TodoRepository { 9 | val quickStore: MutableMap = mutableMapOf() 10 | 11 | override fun save(todo: Todo) { 12 | quickStore[todo.id] = todo 13 | 14 | localStorage.setItem( 15 | AppOptions.localStorageKey, 16 | JSON.stringify(quickStore.values.toTypedArray()) 17 | ) 18 | } 19 | 20 | override fun remove(todo: Todo) { 21 | quickStore.remove(todo.id) 22 | 23 | localStorage.setItem( 24 | AppOptions.localStorageKey, 25 | JSON.stringify(quickStore.values.toTypedArray()) 26 | ) 27 | } 28 | 29 | override fun update(todo: Todo) { 30 | quickStore[todo.id] = todo 31 | 32 | localStorage.setItem( 33 | AppOptions.localStorageKey, 34 | JSON.stringify(quickStore.values.toTypedArray()) 35 | ) 36 | } 37 | 38 | override fun all(): List { 39 | val storedTodosJSON = localStorage[AppOptions.localStorageKey] 40 | 41 | return if (storedTodosJSON != null) { 42 | JSON.parse>(storedTodosJSON).map { 43 | Todo(it.id, it.title, it.completed) 44 | }.toList() 45 | } else { 46 | emptyList() 47 | } 48 | } 49 | 50 | override fun get(todoId: String): Todo? = quickStore[todoId] 51 | } -------------------------------------------------------------------------------- /src/commonMain/kotlin/com/example/demo/service/TodoService.kt: -------------------------------------------------------------------------------- 1 | package com.example.demo.service 2 | 3 | import com.example.demo.model.EventType 4 | import com.example.demo.model.Todo 5 | import com.example.demo.model.TodoEvent 6 | import com.example.demo.repository.TodoRepository 7 | 8 | class TodoService(private val todoRepository: TodoRepository) { 9 | fun handleEvent(todoEvent: TodoEvent) { 10 | when (todoEvent.type) { 11 | EventType.ADD -> todoRepository.save(todoEvent.todo) 12 | EventType.UPDATE -> todoRepository.update(todoEvent.todo) 13 | EventType.REMOVE -> todoRepository.remove(todoEvent.todo) 14 | EventType.UPSERT -> when (check(todoEvent.todo)) { 15 | CheckResult.NEW -> todoRepository.save(todoEvent.todo) 16 | CheckResult.CHANGED -> todoRepository.update(todoEvent.todo) 17 | CheckResult.REMOVED -> todoRepository.remove(todoEvent.todo) 18 | else -> { 19 | } 20 | } 21 | } 22 | } 23 | 24 | fun listTodos(): List = todoRepository.all() 25 | 26 | private fun check(todo: Todo): CheckResult { 27 | val existingTodo = todoRepository.get(todo.id) 28 | 29 | if (existingTodo != null) { 30 | return if (existingTodo == todo) { 31 | CheckResult.SAME 32 | } else if (existingTodo.removed || todo.removed) { 33 | CheckResult.REMOVED 34 | } else { 35 | CheckResult.CHANGED 36 | } 37 | } 38 | 39 | return if (todo.removed) CheckResult.REMOVED else CheckResult.NEW 40 | 41 | } 42 | 43 | enum class CheckResult { 44 | SAME, CHANGED, REMOVED, NEW 45 | } 46 | } -------------------------------------------------------------------------------- /src/reactMain/kotlin/com/example/demo/ui/components/HeaderInput.kt: -------------------------------------------------------------------------------- 1 | package com.example.demo.ui.components 2 | 3 | import kotlinx.html.InputType 4 | import kotlinx.html.js.onChangeFunction 5 | import kotlinx.html.js.onKeyDownFunction 6 | import com.example.demo.model.Todo 7 | import react.* 8 | import react.dom.attrs 9 | import react.dom.h1 10 | import react.dom.header 11 | import react.dom.input 12 | import com.example.demo.utils.Keys 13 | import com.example.demo.utils.translate 14 | import com.example.demo.utils.value 15 | 16 | external interface HeaderInputProps : Props { 17 | var create: (Todo) -> Unit 18 | } 19 | 20 | private val HeaderInput : FC = functionComponent { props -> 21 | val (title, setTitle) = useState("") 22 | 23 | header(classes = "header") { 24 | h1 { 25 | +"todos".translate() 26 | } 27 | input(classes = "new-todo", type = InputType.text) { 28 | attrs { 29 | autoFocus = true 30 | placeholder = "What needs to be done?".translate() 31 | value = title 32 | 33 | onChangeFunction = { event -> 34 | val newValue = event.value 35 | setTitle(newValue) 36 | } 37 | 38 | onKeyDownFunction = { keyEvent -> 39 | val key = Keys.fromString(keyEvent.asDynamic().key as String) 40 | 41 | if (key == Keys.Enter) { 42 | if (title.isNotBlank()) { 43 | props.create(Todo(title = title.trim())) 44 | } 45 | setTitle("") 46 | } 47 | } 48 | } 49 | } 50 | } 51 | 52 | } 53 | 54 | fun RBuilder.headerInput(create: (Todo) -> Unit) = child(HeaderInput) { 55 | attrs.create = create 56 | } 57 | -------------------------------------------------------------------------------- /src/reactMain/kotlin/com/example/demo/ui/components/TodoList.kt: -------------------------------------------------------------------------------- 1 | package com.example.demo.ui.components 2 | 3 | import com.example.demo.model.Todo 4 | import com.example.demo.model.TodoFilter 5 | import kotlinx.html.js.onDoubleClickFunction 6 | import react.* 7 | import react.dom.li 8 | import react.dom.ul 9 | 10 | private val TodoList: FC = functionComponent { props -> 11 | val (editingIdx, setEditingIdx) = useState(-1) 12 | 13 | fun endEditing() { 14 | setEditingIdx(-1) 15 | } 16 | 17 | ul(classes = "todo-list") { 18 | val filter = props.filter 19 | 20 | props.todos.filter { todo -> 21 | filter.filter(todo) 22 | 23 | }.forEachIndexed { idx, todo -> 24 | val isEditing = idx == editingIdx 25 | 26 | val classes = when { 27 | todo.completed -> "completed" 28 | isEditing -> "editing" 29 | else -> "" 30 | } 31 | 32 | 33 | li(classes = classes) { 34 | attrs.onDoubleClickFunction = { 35 | setEditingIdx(idx) 36 | } 37 | 38 | todoItem( 39 | todo = todo, 40 | editing = isEditing, 41 | endEditing = ::endEditing, 42 | removeTodo = { props.removeTodo(todo) }, 43 | updateTodo = { title, completed -> 44 | props.updateTodo(todo.copy(title = title, completed = completed)) 45 | } 46 | ) 47 | } 48 | } 49 | } 50 | } 51 | 52 | external interface TodoListProps : Props { 53 | var removeTodo: (Todo) -> Unit 54 | var updateTodo: (Todo) -> Unit 55 | var todos: List 56 | var filter: TodoFilter 57 | } 58 | 59 | fun RBuilder.todoList( 60 | removeTodo: (Todo) -> Unit, 61 | updateTodo: (Todo) -> Unit, 62 | todos: List, 63 | filter: TodoFilter, 64 | ) = child(TodoList) { 65 | attrs.todos = todos 66 | attrs.removeTodo = removeTodo 67 | attrs.updateTodo = updateTodo 68 | attrs.filter = filter 69 | } -------------------------------------------------------------------------------- /src/reactMain/kotlin/com/example/demo/ui/components/TodoBar.kt: -------------------------------------------------------------------------------- 1 | package com.example.demo.ui.components 2 | 3 | import com.example.demo.model.TodoFilter 4 | import com.example.demo.utils.pluralize 5 | import kotlinx.html.LI 6 | import kotlinx.html.js.onClickFunction 7 | import react.FC 8 | import react.Props 9 | import react.RBuilder 10 | import react.dom.* 11 | import react.functionComponent 12 | import react.router.dom.Link 13 | 14 | external interface TodoBarProps : Props { 15 | var pendingCount: Int 16 | var anyCompleted: Boolean 17 | var clearCompleted: () -> Unit 18 | var currentFilter: TodoFilter 19 | } 20 | 21 | private fun RDOMBuilder
  • .filterItem(props: TodoBarProps, filter: TodoFilter, text: String) { 22 | val classes = if (props.currentFilter == filter) { 23 | "selected" 24 | } else { 25 | "" 26 | } 27 | 28 | Link { 29 | +text 30 | attrs.className = classes 31 | attrs.to = when(filter) { 32 | TodoFilter.ANY -> "/?route=any" 33 | TodoFilter.COMPLETED -> "/?route=completed" 34 | TodoFilter.PENDING -> "/?route=pending" 35 | } 36 | } 37 | } 38 | 39 | private val TodoBar: FC = functionComponent { props -> 40 | 41 | footer("footer") { 42 | span("todo-count") { 43 | strong { +props.pendingCount.toString() } 44 | +" " 45 | +"item left".pluralize(props.pendingCount) 46 | } 47 | 48 | ul(classes = "filters") { 49 | li { 50 | filterItem(props, TodoFilter.ANY, "All") 51 | } 52 | span {} 53 | 54 | li { 55 | filterItem(props, TodoFilter.PENDING, "Active") 56 | } 57 | span {} 58 | li { 59 | filterItem(props, TodoFilter.COMPLETED, "Completed") 60 | } 61 | span {} 62 | } 63 | 64 | if (props.anyCompleted) { 65 | button(classes = "clear-completed") { 66 | +"Clear completed" 67 | attrs.onClickFunction = { props.clearCompleted() } 68 | } 69 | } 70 | } 71 | } 72 | 73 | fun RBuilder.todoBar( 74 | pendingCount: Int, 75 | anyCompleted: Boolean, 76 | clearCompleted: () -> Unit, 77 | currentFilter: TodoFilter, 78 | ) = child(TodoBar) { 79 | attrs.pendingCount = pendingCount 80 | attrs.clearCompleted = clearCompleted 81 | attrs.anyCompleted = anyCompleted 82 | attrs.currentFilter = currentFilter 83 | } -------------------------------------------------------------------------------- /src/springMain/kotlin/com/example/demo/controller/TodoController.kt: -------------------------------------------------------------------------------- 1 | package com.example.demo.controller 2 | 3 | import com.benasher44.uuid.Uuid 4 | import com.benasher44.uuid.uuid4 5 | import com.example.demo.model.EventType 6 | import com.example.demo.model.Todo 7 | import com.example.demo.model.TodoEvent 8 | import com.example.demo.service.TodoService 9 | import kotlinx.coroutines.flow.* 10 | import org.springframework.messaging.handler.annotation.DestinationVariable 11 | import org.springframework.messaging.handler.annotation.MessageMapping 12 | import org.springframework.messaging.handler.annotation.Payload 13 | import org.springframework.messaging.rsocket.RSocketRequester 14 | import org.springframework.messaging.rsocket.annotation.ConnectMapping 15 | import org.springframework.stereotype.Controller 16 | import java.util.concurrent.ConcurrentHashMap 17 | import java.util.concurrent.ConcurrentMap 18 | 19 | @Controller 20 | class TodoController(val todoService: TodoService) { 21 | 22 | val clients: ConcurrentMap = ConcurrentHashMap() 23 | val streams: ConcurrentMap> = ConcurrentHashMap() 24 | 25 | @ConnectMapping("") 26 | fun handleCollaborator(rSocketRequester: RSocketRequester) { 27 | val uuid4 = uuid4() 28 | 29 | println("connected new client $uuid4") 30 | 31 | clients[uuid4] = rSocketRequester 32 | streams[rSocketRequester] = MutableSharedFlow() 33 | 34 | rSocketRequester.rsocket()!! 35 | .onClose() 36 | .subscribe { 37 | clients.remove(uuid4) 38 | streams.remove(rSocketRequester) 39 | } 40 | } 41 | 42 | @MessageMapping("todos") 43 | fun streamTodos(rSocketRequester: RSocketRequester): Flow = 44 | todoService.listTodos().asFlow() 45 | .map { TodoEvent(EventType.UPSERT, it) } 46 | .onCompletion { emitAll(streams[rSocketRequester]!!) } 47 | 48 | 49 | @MessageMapping("todos.{action}") 50 | suspend fun handleTodoAction( 51 | @Payload todo: Todo, 52 | @DestinationVariable("action") action: String, 53 | rSocketRequester: RSocketRequester 54 | ) { 55 | val evenType = when (action) { 56 | "add" -> EventType.ADD 57 | "update" -> EventType.UPDATE 58 | "remove" -> EventType.REMOVE 59 | "upsert" -> EventType.UPSERT 60 | else -> throw Error("Unsupported action type $action") 61 | } 62 | val todoEvent = TodoEvent(evenType, todo) 63 | 64 | todoService.handleEvent(todoEvent) 65 | streams 66 | .filter { it.key != rSocketRequester } 67 | .forEach { 68 | it.value.emit(todoEvent) 69 | } 70 | } 71 | } -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if "%ERRORLEVEL%"=="0" goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 84 | exit /b 1 85 | 86 | :mainEnd 87 | if "%OS%"=="Windows_NT" endlocal 88 | 89 | :omega 90 | -------------------------------------------------------------------------------- /src/commonClientMain/kotlin/com/example/demo/client/RSocketClient.kt: -------------------------------------------------------------------------------- 1 | package com.example.demo.client 2 | 3 | import com.example.demo.model.Todo 4 | import com.example.demo.model.TodoEvent 5 | import io.ktor.utils.io.core.* 6 | import io.rsocket.kotlin.ExperimentalMetadataApi 7 | import io.rsocket.kotlin.RSocket 8 | import io.rsocket.kotlin.metadata.CompositeMetadata 9 | import io.rsocket.kotlin.metadata.RoutingMetadata 10 | import io.rsocket.kotlin.metadata.metadata 11 | import io.rsocket.kotlin.payload.buildPayload 12 | import io.rsocket.kotlin.payload.data 13 | import kotlinx.coroutines.MainScope 14 | import kotlinx.coroutines.flow.launchIn 15 | import kotlinx.coroutines.flow.map 16 | import kotlinx.coroutines.flow.onEach 17 | import kotlinx.coroutines.launch 18 | import kotlinx.serialization.decodeFromString 19 | import kotlinx.serialization.encodeToString 20 | import kotlinx.serialization.json.Json 21 | 22 | @OptIn(ExperimentalMetadataApi::class) 23 | class RSocketClient(val rsocket: RSocket) : Client { 24 | 25 | val scope = MainScope() 26 | 27 | override fun handleTodos(handler: (TodoEvent) -> Unit) { 28 | rsocket 29 | .requestStream(buildPayload { 30 | data(ByteReadPacket.Empty) 31 | metadata(CompositeMetadata(RoutingMetadata("todos"))) 32 | }) 33 | .map { 34 | val string = it.data.readText() 35 | Json.decodeFromString(string) 36 | } 37 | .onEach { 38 | handler(it) 39 | } 40 | .launchIn(scope) 41 | } 42 | 43 | override fun exchange(todo: List) { 44 | scope.launch { 45 | todo.forEach { 46 | rsocket 47 | .fireAndForget(buildPayload { 48 | data(Json.encodeToString(it)) 49 | metadata(CompositeMetadata(RoutingMetadata("todos.upsert"))) 50 | }) 51 | } 52 | } 53 | } 54 | 55 | override fun addTodo(todo: Todo) { 56 | scope.launch { 57 | rsocket 58 | .fireAndForget(buildPayload { 59 | data(Json.encodeToString(todo)) 60 | metadata(CompositeMetadata(RoutingMetadata("todos.add"))) 61 | }) 62 | } 63 | } 64 | 65 | override fun updateTodo(todo: Todo) { 66 | scope.launch { 67 | rsocket 68 | .fireAndForget(buildPayload { 69 | data(Json.encodeToString(todo)) 70 | metadata(CompositeMetadata(RoutingMetadata("todos.update"))) 71 | }) 72 | } 73 | } 74 | 75 | override fun removeTodo(todo: Todo) { 76 | scope.launch { 77 | rsocket 78 | .fireAndForget(buildPayload { 79 | data(Json.encodeToString(todo)) 80 | metadata(CompositeMetadata(RoutingMetadata("todos.remove"))) 81 | }) 82 | } 83 | } 84 | 85 | companion object 86 | } 87 | 88 | expect suspend fun RSocketClient.Companion.create(): Client -------------------------------------------------------------------------------- /src/reactMain/kotlin/com/example/demo/ui/components/TodoItem.kt: -------------------------------------------------------------------------------- 1 | package com.example.demo.ui.components 2 | 3 | import com.example.demo.model.Todo 4 | import com.example.demo.utils.Keys 5 | import com.example.demo.utils.value 6 | import kotlinx.html.InputType 7 | import kotlinx.html.Tag 8 | import kotlinx.html.js.onBlurFunction 9 | import kotlinx.html.js.onChangeFunction 10 | import kotlinx.html.js.onClickFunction 11 | import kotlinx.html.js.onKeyUpFunction 12 | import org.w3c.dom.events.Event 13 | import react.* 14 | import react.dom.* 15 | 16 | private val TodoItem: FC = functionComponent { props -> 17 | val (editText, setEditText) = useState(props.todo.title) 18 | 19 | fun finishEditing(title: String) { 20 | if (title.isNotBlank()) { 21 | props.updateTodo(title, props.todo.completed) 22 | } else { 23 | props.removeTodo() 24 | } 25 | 26 | props.endEditing() 27 | } 28 | 29 | fun handleKeyUp(keyEvent: Event) { 30 | val key = Keys.fromString(keyEvent.asDynamic().key as String) 31 | when (key) { 32 | Keys.Enter -> { 33 | finishEditing(editText) 34 | } 35 | Keys.Escape -> { 36 | props.endEditing() 37 | } 38 | 39 | else -> { 40 | //TODO? 41 | } 42 | } 43 | } 44 | 45 | div(classes = "view") { 46 | 47 | input(classes = "toggle", type = InputType.checkBox) { 48 | 49 | attrs.onChangeFunction = { event -> 50 | val c = event.currentTarget.asDynamic().checked as Boolean 51 | props.updateTodo(props.todo.title, c) 52 | } 53 | 54 | ref { it?.checked = props.todo.completed } 55 | } 56 | label { 57 | +props.todo.title 58 | } 59 | button(classes = "destroy") { 60 | attrs.onClickFunction = { 61 | props.removeTodo() 62 | } 63 | } 64 | } 65 | input(classes = "edit", type = InputType.text) { 66 | attrs { 67 | value = editText 68 | onChangeFunction = { event -> 69 | val text = event.value 70 | setEditText(text) 71 | } 72 | onBlurFunction = { finishEditing(editText) } 73 | onKeyUpFunction = ::handleKeyUp 74 | 75 | } 76 | 77 | if (props.editing) { 78 | ref { it?.focus() } 79 | } 80 | } 81 | } 82 | 83 | external interface TodoItemProps : Props { 84 | var todo: Todo 85 | var editing: Boolean 86 | var removeTodo: () -> Unit 87 | var updateTodo: (String, Boolean) -> Unit 88 | var endEditing: () -> Unit 89 | } 90 | 91 | fun RBuilder.todoItem( 92 | todo: Todo, 93 | editing: Boolean, 94 | removeTodo: () -> Unit, 95 | updateTodo: (String, Boolean) -> Unit, 96 | endEditing: () -> Unit, 97 | ) = child(TodoItem) { 98 | attrs.todo = todo 99 | attrs.editing = editing 100 | attrs.removeTodo = removeTodo 101 | attrs.updateTodo = updateTodo 102 | attrs.endEditing = endEditing 103 | } 104 | 105 | fun RDOMBuilder.ref(handler: (dynamic) -> Unit) { 106 | domProps.ref(handler) 107 | } 108 | 109 | fun Props.ref(ref: (T?) -> Unit) { 110 | asDynamic().ref = ref 111 | } -------------------------------------------------------------------------------- /src/reactMain/kotlin/com/example/demo/ui/app/App.kt: -------------------------------------------------------------------------------- 1 | package com.example.demo.ui.app 2 | 3 | import com.example.demo.client.Client 4 | import com.example.demo.model.EventType 5 | import com.example.demo.model.Todo 6 | import com.example.demo.model.TodoEvent 7 | import com.example.demo.model.TodoFilter 8 | import com.example.demo.service.TodoService 9 | import com.example.demo.ui.components.headerInput 10 | import com.example.demo.ui.components.info 11 | import com.example.demo.ui.components.todoBar 12 | import com.example.demo.ui.components.todoList 13 | import com.example.demo.utils.translate 14 | import kotlinx.html.InputType 15 | import kotlinx.html.id 16 | import kotlinx.html.js.onChangeFunction 17 | import kotlinx.html.title 18 | import org.w3c.dom.HTMLInputElement 19 | import org.w3c.dom.url.URLSearchParams 20 | import react.* 21 | import react.dom.attrs 22 | import react.dom.input 23 | import react.dom.label 24 | import react.dom.section 25 | import react.router.dom.useLocation 26 | 27 | object AppOptions { 28 | var language = "no-language" 29 | var localStorageKey = "todos-koltin-react" 30 | } 31 | 32 | fun useQuery(): URLSearchParams = URLSearchParams(useLocation().search) 33 | 34 | private val App: FC = functionComponent { props -> 35 | val (todos, setTodos) = useState(emptyList()) 36 | val query = useQuery() 37 | 38 | useEffect(dependencies = emptyArray()) { 39 | props.client.handleTodos { 40 | props.service.handleEvent(it) 41 | setTodos( props.service.listTodos()) 42 | } 43 | } 44 | 45 | fun pendingTodos(): List { 46 | return todos.filter { todo -> !todo.completed } 47 | } 48 | 49 | fun countPending() = pendingTodos().size 50 | 51 | fun removeTodo(todo: Todo) { 52 | console.log("removeTodo [${todo.id}] ${todo.title}") 53 | props.client.removeTodo(todo) 54 | props.service.handleEvent(TodoEvent(EventType.REMOVE, todo)) 55 | setTodos(props.service.listTodos()) 56 | } 57 | 58 | fun createTodo(todo: Todo) { 59 | console.log("createTodo [${todo.id}] ${todo.title}") 60 | 61 | props.client.addTodo(todo) 62 | 63 | props.service.handleEvent(TodoEvent(EventType.ADD, todo)) 64 | 65 | setTodos(props.service.listTodos()) 66 | } 67 | 68 | fun updateTodo(todo: Todo) { 69 | console.log("updateTodo [${todo.id}] ${todo.title}") 70 | 71 | props.client.updateTodo(todo) 72 | 73 | props.service.handleEvent(TodoEvent(EventType.UPDATE, todo)) 74 | setTodos(props.service.listTodos()) 75 | } 76 | 77 | fun setAllStatus(newStatus: Boolean) { 78 | todos.forEach { todo -> updateTodo(todo.copy(completed = newStatus)) } 79 | } 80 | 81 | fun clearCompleted() { 82 | todos.filter { todo -> todo.completed } 83 | .forEach { todo -> removeTodo(todo.copy(removed = true)) } 84 | } 85 | 86 | fun isAllCompleted(): Boolean { 87 | return todos.fold(true) { allCompleted, todo -> 88 | allCompleted && todo.completed 89 | } 90 | } 91 | 92 | val currentFilter = when (query.get("route")) { 93 | "pending" -> TodoFilter.PENDING 94 | "completed" -> TodoFilter.COMPLETED 95 | else -> TodoFilter.ANY 96 | } 97 | 98 | section("todoapp") { 99 | headerInput(::createTodo) 100 | 101 | 102 | if (todos.isNotEmpty()) { 103 | 104 | val allChecked = isAllCompleted() 105 | 106 | section("main") { 107 | input(InputType.checkBox, classes = "toggle-all") { 108 | attrs { 109 | id = "toggle-all" 110 | checked = allChecked 111 | 112 | onChangeFunction = { event -> 113 | val isChecked = (event.currentTarget as HTMLInputElement).checked 114 | 115 | setAllStatus(isChecked) 116 | } 117 | } 118 | } 119 | label { 120 | attrs["htmlFor"] = "toggle-all" 121 | attrs.title = "Mark all as complete".translate() 122 | } 123 | 124 | todoList(::removeTodo, ::updateTodo, todos, currentFilter) 125 | } 126 | 127 | todoBar( 128 | pendingCount = countPending(), 129 | anyCompleted = todos.any { todo -> todo.completed }, 130 | clearCompleted = ::clearCompleted, 131 | currentFilter = currentFilter, 132 | ) 133 | } 134 | 135 | } 136 | info() 137 | } 138 | 139 | external interface AppProps : Props { 140 | var client: Client 141 | var service: TodoService 142 | } 143 | 144 | fun RBuilder.app(client: Client, service: TodoService) = child(App) { 145 | attrs.client = client 146 | attrs.service = service 147 | } 148 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # 4 | # Copyright 2015 the original author or authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | ## 21 | ## Gradle start up script for UN*X 22 | ## 23 | ############################################################################## 24 | 25 | # Attempt to set APP_HOME 26 | # Resolve links: $0 may be a link 27 | PRG="$0" 28 | # Need this for relative symlinks. 29 | while [ -h "$PRG" ] ; do 30 | ls=`ls -ld "$PRG"` 31 | link=`expr "$ls" : '.*-> \(.*\)$'` 32 | if expr "$link" : '/.*' > /dev/null; then 33 | PRG="$link" 34 | else 35 | PRG=`dirname "$PRG"`"/$link" 36 | fi 37 | done 38 | SAVED="`pwd`" 39 | cd "`dirname \"$PRG\"`/" >/dev/null 40 | APP_HOME="`pwd -P`" 41 | cd "$SAVED" >/dev/null 42 | 43 | APP_NAME="Gradle" 44 | APP_BASE_NAME=`basename "$0"` 45 | 46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 48 | 49 | # Use the maximum available, or set MAX_FD != -1 to use that value. 50 | MAX_FD="maximum" 51 | 52 | warn () { 53 | echo "$*" 54 | } 55 | 56 | die () { 57 | echo 58 | echo "$*" 59 | echo 60 | exit 1 61 | } 62 | 63 | # OS specific support (must be 'true' or 'false'). 64 | cygwin=false 65 | msys=false 66 | darwin=false 67 | nonstop=false 68 | case "`uname`" in 69 | CYGWIN* ) 70 | cygwin=true 71 | ;; 72 | Darwin* ) 73 | darwin=true 74 | ;; 75 | MINGW* ) 76 | msys=true 77 | ;; 78 | NONSTOP* ) 79 | nonstop=true 80 | ;; 81 | esac 82 | 83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 84 | 85 | 86 | # Determine the Java command to use to start the JVM. 87 | if [ -n "$JAVA_HOME" ] ; then 88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 89 | # IBM's JDK on AIX uses strange locations for the executables 90 | JAVACMD="$JAVA_HOME/jre/sh/java" 91 | else 92 | JAVACMD="$JAVA_HOME/bin/java" 93 | fi 94 | if [ ! -x "$JAVACMD" ] ; then 95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 96 | 97 | Please set the JAVA_HOME variable in your environment to match the 98 | location of your Java installation." 99 | fi 100 | else 101 | JAVACMD="java" 102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 103 | 104 | Please set the JAVA_HOME variable in your environment to match the 105 | location of your Java installation." 106 | fi 107 | 108 | # Increase the maximum file descriptors if we can. 109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 110 | MAX_FD_LIMIT=`ulimit -H -n` 111 | if [ $? -eq 0 ] ; then 112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 113 | MAX_FD="$MAX_FD_LIMIT" 114 | fi 115 | ulimit -n $MAX_FD 116 | if [ $? -ne 0 ] ; then 117 | warn "Could not set maximum file descriptor limit: $MAX_FD" 118 | fi 119 | else 120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 121 | fi 122 | fi 123 | 124 | # For Darwin, add options to specify how the application appears in the dock 125 | if $darwin; then 126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 127 | fi 128 | 129 | # For Cygwin or MSYS, switch paths to Windows format before running java 130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then 131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 133 | 134 | JAVACMD=`cygpath --unix "$JAVACMD"` 135 | 136 | # We build the pattern for arguments to be converted via cygpath 137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 138 | SEP="" 139 | for dir in $ROOTDIRSRAW ; do 140 | ROOTDIRS="$ROOTDIRS$SEP$dir" 141 | SEP="|" 142 | done 143 | OURCYGPATTERN="(^($ROOTDIRS))" 144 | # Add a user-defined pattern to the cygpath arguments 145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 147 | fi 148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 149 | i=0 150 | for arg in "$@" ; do 151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 153 | 154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 156 | else 157 | eval `echo args$i`="\"$arg\"" 158 | fi 159 | i=`expr $i + 1` 160 | done 161 | case $i in 162 | 0) set -- ;; 163 | 1) set -- "$args0" ;; 164 | 2) set -- "$args0" "$args1" ;; 165 | 3) set -- "$args0" "$args1" "$args2" ;; 166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;; 167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 172 | esac 173 | fi 174 | 175 | # Escape application args 176 | save () { 177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 178 | echo " " 179 | } 180 | APP_ARGS=`save "$@"` 181 | 182 | # Collect all arguments for the java command, following the shell quoting and substitution rules 183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 184 | 185 | exec "$JAVACMD" "$@" 186 | --------------------------------------------------------------------------------