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 androidx.navigation.safe.args.generator
18
19import androidx.navigation.safe.args.generator.models.Destination
20import com.squareup.javapoet.JavaFile
21import java.io.File
22
23fun generateSafeArgs(
24    rFilePackage: String,
25    applicationId: String,
26    navigationXml: File,
27    outputDir: File
28): GeneratorOutput {
29    val context = Context()
30    val rawDestination = NavParser.parseNavigationFile(navigationXml, rFilePackage, applicationId,
31            context)
32    val resolvedDestination = resolveArguments(rawDestination)
33    val javaFiles = mutableSetOf<JavaFile>()
34    fun writeJavaFiles(destination: Destination) {
35        if (destination.actions.isNotEmpty()) {
36            javaFiles.add(generateDirectionsJavaFile(destination))
37        }
38        if (destination.args.isNotEmpty()) {
39            javaFiles.add(generateArgsJavaFile(destination))
40        }
41        destination.nested.forEach(::writeJavaFiles)
42    }
43    writeJavaFiles(resolvedDestination)
44    javaFiles.forEach { javaFile -> javaFile.writeTo(outputDir) }
45    val files = javaFiles.map { javaFile -> "${javaFile.packageName}.${javaFile.typeSpec.name}" }
46    return GeneratorOutput(files, context.logger.allMessages())
47}
48