1 /*
<lambda>null2  * 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 
17 package androidx.navigation.safe.args.generator
18 
19 import androidx.navigation.safe.args.generator.java.JavaNavWriter
20 import androidx.navigation.safe.args.generator.kotlin.KotlinNavWriter
21 import androidx.navigation.safe.args.generator.models.Destination
22 import java.io.File
23 
24 fun SafeArgsGenerator(
25     rFilePackage: String,
26     applicationId: String,
27     navigationXml: File,
28     outputDir: File,
29     useAndroidX: Boolean = true,
30     generateKotlin: Boolean
31 ) =
32     NavSafeArgsGenerator(
33         rFilePackage,
34         applicationId,
35         navigationXml,
36         outputDir,
37         if (generateKotlin) {
38             KotlinNavWriter(useAndroidX)
39         } else {
40             JavaNavWriter(useAndroidX)
41         }
42     )
43 
44 class NavSafeArgsGenerator<T : CodeFile>
45 internal constructor(
46     private val rFilePackage: String,
47     private val applicationId: String,
48     private val navigationXml: File,
49     private val outputDir: File,
50     private val writer: NavWriter<T>
51 ) {
generatenull52     fun generate(): GeneratorOutput {
53         val context = Context()
54         val rawDestination =
55             NavParser.parseNavigationFile(navigationXml, rFilePackage, applicationId, context)
56         val resolvedDestination = resolveArguments(rawDestination)
57         val codeFiles = mutableSetOf<CodeFile>()
58         fun writeCodeFiles(destination: Destination, parentDirectionsFileList: List<T>) {
59             val newParentDirectionFile =
60                 if (destination.actions.isNotEmpty() || parentDirectionsFileList.isNotEmpty()) {
61                         writer.generateDirectionsCodeFile(destination, parentDirectionsFileList)
62                     } else {
63                         null
64                     }
65                     ?.also { codeFiles.add(it) }
66             if (destination.args.isNotEmpty()) {
67                 codeFiles.add(writer.generateArgsCodeFile(destination))
68             }
69             destination.nested.forEach { nestedDestination ->
70                 writeCodeFiles(
71                     destination = nestedDestination,
72                     parentDirectionsFileList =
73                         newParentDirectionFile?.let { listOf(it) + parentDirectionsFileList }
74                             ?: parentDirectionsFileList
75                 )
76             }
77         }
78         writeCodeFiles(resolvedDestination, emptyList())
79         codeFiles.forEach { it.writeTo(outputDir) }
80         return GeneratorOutput(codeFiles.toList(), context.logger.allMessages())
81     }
82 }
83