1 /*
<lambda>null2  * Copyright 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.room.ext
18 
19 import androidx.room.RoomProcessor
20 
21 /**
22  * Map of dejetified packages names. Useful for letting Room know which packages names to use when
23  * generating code in a dejetified environment. To use this map add a resource file named
24  * 'dejetifier.config' containing one key-value pair per line separated by '=' where the key is the
25  * androidx package name to dejetify and the value is the dejetified package name.
26  *
27  * Example of a typical config:
28  * ```
29  * # Room dejetifier packages for XPoet class names.
30  * androidx.sqlite = android.arch.persistence
31  * androidx.room = android.arch.persistence.room
32  * androidx.paging = android.arch.paging
33  * androidx.lifecycle = android.arch.lifecycle
34  * androidx.collection = com.android.support
35  * ```
36  */
37 private val PACKAGE_NAME_OVERRIDES: Map<String, String> by lazy {
38     RoomProcessor::class.java.classLoader.getResourceAsStream("dejetifier.config")?.reader()?.use {
39         try {
40             it.readLines()
41                 .filterNot { it.startsWith('#') }
42                 .associate { it.split('=').let { split -> split[0].trim() to split[1].trim() } }
43         } catch (ex: Exception) {
44             throw RuntimeException("Malformed dejetifier.config file.", ex)
45         }
46     } ?: emptyMap()
47 }
48 
49 val SQLITE_PACKAGE = getOrDefault("androidx.sqlite")
50 val ROOM_PACKAGE = getOrDefault("androidx.room")
51 val PAGING_PACKAGE = getOrDefault("androidx.paging")
52 val LIFECYCLE_PACKAGE = getOrDefault("androidx.lifecycle")
53 val COLLECTION_PACKAGE = getOrDefault("androidx.collection")
54 
getOrDefaultnull55 private fun getOrDefault(key: String) = PACKAGE_NAME_OVERRIDES.getOrDefault(key, key)
56