1/*
2 * Copyright 2018 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
17package com.android.tools.build.jetifier.core
18
19import com.android.tools.build.jetifier.core.type.PackageName
20
21/**
22 * Package map to be used to rewrite packages. The rewrite rules allow duplicities where the
23 * artifact name prefix defined in a rule determines if such rule should be used or skipped.
24 * The priority is determined only by the order (top to bottom). Having a rule with no file prefix
25 * as first means that it is always applied.
26 *
27 * We use this only for the support library rewriting to rewrite packages in manifest files.
28 */
29class PackageMap(private val rules: List<PackageRule>) {
30
31    companion object {
32        val DEFAULT_RULES = listOf(
33            PackageRule(
34                from = "android/support/exifinterface",
35                to = "androidx/exifinterface"
36            ),
37            PackageRule(
38                from = "android/support/heifwriter",
39                to = "androidx/heifwriter"
40            ),
41            PackageRule(
42                from = "android/support/graphics/drawable",
43                to = "androidx/vectordrawable"
44            ),
45            PackageRule(
46                from = "android/support/graphics/drawable/animated",
47                to = "androidx/vectordrawable"
48            ),
49            PackageRule(
50                from = "android/support/media/tv",
51                to = "androidx/tvprovider"
52            ),
53            PackageRule(
54                from = "androidx/textclassifier",
55                to = "androidx/textclassifier"
56            ),
57            PackageRule(
58                from = "androidx/recyclerview/selection",
59                to = "androidx/recyclerview/selection"),
60            PackageRule(
61                from = "android/support/v4",
62                to = "androidx/legacy/v4"
63            ),
64            PackageRule(
65                from = "android/support/print",
66                to = "androidx/print"
67            ),
68            PackageRule(
69                from = "android/support/documentfile",
70                to = "androidx/documentfile"
71            ),
72            PackageRule(
73                from = "android/support/coordinatorlayout",
74                to = "androidx/coordinatorlayout"
75            ),
76            PackageRule(
77                from = "android/support/swiperefreshlayout",
78                to = "androidx/swiperefreshlayout"
79            ),
80            PackageRule(
81                from = "android/support/slidingpanelayout",
82                to = "androidx/slidingpanelayout"
83            ),
84            PackageRule(
85                from = "android/support/asynclayoutinflater",
86                to = "androidx/asynclayoutinflater"
87            ),
88            PackageRule(
89                from = "android/support/interpolator",
90                to = "androidx/interpolator"
91            ),
92            PackageRule(
93                from = "android/support/v7/palette",
94                to = "androidx/palette"
95            ),
96            PackageRule(
97                from = "android/support/v7/cardview",
98                to = "androidx/cardview"
99            ),
100            PackageRule(
101                from = "android/support/customview",
102                to = "androidx/customview"
103            ),
104            PackageRule(
105                from = "android/support/loader",
106                to = "androidx/loader"
107            ),
108            PackageRule(
109                from = "android/support/cursoradapter",
110                to = "androidx/cursoradapter"
111            ),
112            PackageRule(
113                from = "android/support/v7/mediarouter",
114                to = "androidx/mediarouter"
115            ),
116            PackageRule(
117                from = "android/support/v7/appcompat",
118                to = "androidx/appcompat"
119            ),
120            PackageRule(
121                from = "android/support/v7/recyclerview",
122                to = "androidx/recyclerview"
123            ),
124            PackageRule(
125                from = "android/support/v7/viewpager",
126                to = "androidx/viewpager"
127            ),
128            PackageRule(
129                from = "android/support/percent",
130                to = "androidx/percentlayout"
131            ),
132            PackageRule(
133                from = "android/support/v7/gridlayout",
134                to = "androidx/gridlayout"
135            ),
136            PackageRule(
137                from = "android/support/v13",
138                to = "androidx/legacy/v13"
139            ),
140            PackageRule(
141                from = "android/support/v7/preference",
142                to = "androidx/preference"
143            ),
144            PackageRule(
145                from = "android/support/v14/preference",
146                to = "androidx/legacy/preference"
147            ),
148            PackageRule(
149                from = "android/support/v17/leanback",
150                to = "androidx/leanback"
151            ),
152            PackageRule(
153                from = "android/support/v17/preference",
154                to = "androidx/leanback/preference"
155            ),
156            PackageRule(
157                from = "android/support/compat",
158                to = "androidx/core"
159            ),
160            PackageRule(
161                from = "android/support/mediacompat",
162                to = "androidx/media"
163            ),
164            PackageRule(
165                from = "android/support/fragment",
166                to = "androidx/fragment"
167            ),
168            PackageRule(
169                from = "android/support/coreutils",
170                to = "androidx/legacy/coreutils"
171            ),
172            PackageRule(
173                from = "android/support/dynamicanimation",
174                to = "androidx/dynamicanimation"
175            ),
176            PackageRule(
177                from = "android/support/customtabs",
178                to = "androidx/browser"
179            ),
180            PackageRule(
181                from = "android/support/coreui",
182                to = "androidx/legacy/coreui"
183            ),
184            PackageRule(
185                from = "android/support/content",
186                to = "androidx/contentpager"
187            ),
188            PackageRule(
189                from = "android/support/transition",
190                to = "androidx/transition"
191            ),
192            PackageRule(
193                from = "android/support/recommendation",
194                to = "androidx/recommendation"
195            ),
196            PackageRule(
197                from = "android/support/drawerlayout",
198                to = "androidx/drawerlayout"
199            ),
200            PackageRule(
201                from = "android/support/wear",
202                to = "androidx/wear"
203            ),
204            PackageRule(
205                from = "android/support/design",
206                to = "com/google/android/material"
207            ),
208            PackageRule(
209                from = "android/support/text/emoji/appcompat",
210                to = "androidx/emoji/appcompat"
211            ),
212            PackageRule(
213                from = "android/support/text/emoji/bundled",
214                to = "androidx/emoji/bundled"
215            ),
216            PackageRule(
217                from = "android/support/text/emoji",
218                to = "androidx/emoji"
219            ),
220            PackageRule(
221                from = "androidx/text/emoji/bundled",
222                to = "androidx/text/emoji/bundled"
223            ),
224            PackageRule(
225                from = "android/support/localbroadcastmanager",
226                to = "androidx/localbroadcastmanager"
227            ),
228            PackageRule(
229                from = "androidx/text/emoji/bundled",
230                to = "androidx/text/emoji/bundled"
231            ),
232            PackageRule(
233                from = "androidx/webkit",
234                to = "androidx/webkit"
235            ),
236            PackageRule(
237                from = "androidx/slice/view",
238                to = "androidx/slice/view"
239            ),
240            PackageRule(
241                from = "androidx/slice/core",
242                to = "androidx/slice/core"
243            ),
244            PackageRule(
245                from = "androidx/slice/builders",
246                to = "androidx/slice/builders"
247            ),
248            PackageRule(
249                from = "android/arch/paging/runtime",
250                to = "androidx/paging/runtime"
251            ),
252            PackageRule(
253                from = "android/arch/core/testing",
254                to = "androidx/arch/core/testing"
255            ),
256            PackageRule(
257                from = "android/arch/core",
258                to = "androidx/arch/core"
259            ),
260            PackageRule(
261                from = "android/arch/persistence/db/framework",
262                to = "androidx/sqlite/db/framework"
263            ),
264            PackageRule(
265                from = "android/arch/persistence/db",
266                to = "androidx/sqlite/db"
267            ),
268            PackageRule(
269                from = "android/arch/persistence/room/rxjava2",
270                to = "androidx/room/rxjava2"
271            ),
272            PackageRule(
273                from = "android/arch/persistence/room/guava",
274                to = "androidx/room/guava"
275            ),
276            PackageRule(
277                from = "android/arch/persistence/room/testing",
278                to = "androidx/room/testing"
279            ),
280            PackageRule(
281                from = "android/arch/persistence/room",
282                to = "androidx/room"
283            ),
284            PackageRule(
285                from = "android/arch/lifecycle/extensions",
286                to = "androidx/lifecycle/extensions"
287            ),
288            PackageRule(
289                from = "android/arch/lifecycle/livedata/core",
290                to = "androidx/lifecycle/livedata/core"
291            ),
292            PackageRule(
293                from = "android/arch/lifecycle",
294                to = "androidx/lifecycle"
295            ),
296            PackageRule(
297                from = "android/arch/lifecycle/viewmodel",
298                to = "androidx/lifecycle/viewmodel"
299            ),
300            PackageRule(
301                from = "android/arch/lifecycle/livedata",
302                to = "androidx/lifecycle/livedata"
303            ),
304            PackageRule(
305                from = "android/arch/lifecycle/reactivestreams",
306                to = "androidx/lifecycle/reactivestreams"
307            ),
308            PackageRule(
309                from = "android/support/multidex/instrumentation",
310                to = "androidx/multidex/instrumentation"
311            ),
312            PackageRule(
313                from = "android/support/multidex",
314                to = "androidx/multidex"
315            )
316        )
317
318        val EMPTY = PackageMap(emptyList())
319    }
320
321    /**
322     * Creates reversed version of this map (from becomes to and vice versa).
323     */
324    fun reverse(): PackageMap {
325        return PackageMap(rules
326            .map { PackageRule(from = it.to, to = it.from) }
327            .toList())
328    }
329
330    /**
331     * Returns a new package name for the given [fromPackage].
332     */
333    fun getPackageFor(fromPackage: PackageName): PackageName? {
334        val rule = rules.find { it.from == fromPackage.fullName }
335        if (rule != null) {
336            return PackageName(rule.to)
337        }
338        return null
339    }
340
341    data class PackageRule(val from: String, val to: String)
342}