1 /*
2  * Copyright 2020 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.compiler.processing
18 
19 import androidx.room.compiler.codegen.CodeLanguage
20 import androidx.room.compiler.codegen.XFileSpec
21 import androidx.room.compiler.codegen.XTypeSpec
22 import com.squareup.javapoet.JavaFile
23 import com.squareup.kotlinpoet.FileSpec
24 import java.io.OutputStream
25 import java.nio.file.Path
26 
27 /** Code generation interface for XProcessing. */
28 interface XFiler {
29 
writenull30     fun write(javaFile: JavaFile, mode: Mode = Mode.Isolating)
31 
32     fun write(fileSpec: FileSpec, mode: Mode = Mode.Isolating)
33 
34     /**
35      * Writes a source file that will be part of the output artifact (e.g. jar).
36      *
37      * Only source files should be written via this function, if the extension is not `.java` or
38      * `.kt` this function will throw an exception.
39      *
40      * @return the output stream to write the resource file.
41      */
42     fun writeSource(
43         packageName: String,
44         fileNameWithoutExtension: String,
45         extension: String,
46         originatingElements: List<XElement>,
47         mode: Mode = Mode.Isolating
48     ): OutputStream
49 
50     /**
51      * Writes a resource file that will be part of the output artifact (e.g. jar).
52      *
53      * Only non-source files should be written via this function, if the file path corresponds to a
54      * source file, `.java` or `.kt` this function will throw an exception.
55      *
56      * @return the output stream to write the resource file.
57      */
58     fun writeResource(
59         filePath: Path,
60         originatingElements: List<XElement>,
61         mode: Mode = Mode.Isolating
62     ): OutputStream
63 
64     /**
65      * Specifies whether a file represents aggregating or isolating inputs for incremental build
66      * purposes. This does not apply in Javac processing because aggregating vs isolating is set on
67      * the processor level. For more on KSP's definitions of isolating vs aggregating see the
68      * documentation at https://github.com/google/ksp/blob/master/docs/incremental.md
69      */
70     enum class Mode {
71         Aggregating,
72         Isolating
73     }
74 }
75 
writeTonull76 fun JavaFile.writeTo(generator: XFiler, mode: XFiler.Mode = XFiler.Mode.Isolating) {
77     generator.write(this, mode)
78 }
79 
writeTonull80 fun FileSpec.writeTo(generator: XFiler, mode: XFiler.Mode = XFiler.Mode.Isolating) {
81     generator.write(this, mode)
82 }
83 
writeTonull84 fun XTypeSpec.writeTo(
85     language: CodeLanguage,
86     packageName: String,
87     generator: XFiler,
88     mode: XFiler.Mode = XFiler.Mode.Isolating
89 ) = XFileSpec.of(packageName, this).writeTo(language, generator, mode)
90