Skip to content

Commit

Permalink
Return the generated path from FileSpec.writeTo(). (#1514)
Browse files Browse the repository at this point in the history
  • Loading branch information
fejesjoco committed Nov 9, 2023
1 parent 0172eb0 commit 1286d7b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 24 deletions.
6 changes: 4 additions & 2 deletions kotlinpoet/api/kotlinpoet.api
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,11 @@ public final class com/squareup/kotlinpoet/FileSpec : com/squareup/kotlinpoet/An
public static synthetic fun toBuilder$default (Lcom/squareup/kotlinpoet/FileSpec;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Lcom/squareup/kotlinpoet/FileSpec$Builder;
public final fun toJavaFileObject ()Ljavax/tools/JavaFileObject;
public fun toString ()Ljava/lang/String;
public final fun writeTo (Ljava/io/File;)V
public final fun writeTo (Ljava/io/File;)Ljava/io/File;
public final synthetic fun writeTo (Ljava/io/File;)V
public final fun writeTo (Ljava/lang/Appendable;)V
public final fun writeTo (Ljava/nio/file/Path;)V
public final fun writeTo (Ljava/nio/file/Path;)Ljava/nio/file/Path;
public final synthetic fun writeTo (Ljava/nio/file/Path;)V
public final fun writeTo (Ljavax/annotation/processing/Filer;)V
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import javax.tools.JavaFileObject
import javax.tools.JavaFileObject.Kind
import javax.tools.SimpleJavaFileObject
import javax.tools.StandardLocation
import kotlin.DeprecationLevel.HIDDEN
import kotlin.reflect.KClass

/**
Expand Down Expand Up @@ -71,9 +72,18 @@ public class FileSpec private constructor(
codeWriter.close()
}

/** Writes this to `directory` as UTF-8 using the standard directory structure. */
@Deprecated("", level = HIDDEN)
@JvmName("writeTo") // For binary compatibility.
public fun oldWriteTo(directory: Path) {
writeTo(directory)
}

/**
* Writes this to [directory] as UTF-8 using the standard directory structure
* and returns the newly output path.
*/
@Throws(IOException::class)
public fun writeTo(directory: Path) {
public fun writeTo(directory: Path): Path {
require(Files.notExists(directory) || Files.isDirectory(directory)) {
"path $directory exists but is not a directory."
}
Expand All @@ -88,11 +98,21 @@ public class FileSpec private constructor(

val outputPath = outputDirectory.resolve("$name.$extension")
OutputStreamWriter(Files.newOutputStream(outputPath), UTF_8).use { writer -> writeTo(writer) }
return outputPath
}

@Deprecated("", level = HIDDEN)
@JvmName("writeTo") // For binary compatibility.
public fun oldWriteTo(directory: File) {
writeTo(directory)
}

/** Writes this to `directory` as UTF-8 using the standard directory structure. */
/**
* Writes this to [directory] as UTF-8 using the standard directory structure
* and returns the newly output file.
*/
@Throws(IOException::class)
public fun writeTo(directory: File): Unit = writeTo(directory.toPath())
public fun writeTo(directory: File): File = writeTo(directory.toPath()).toFile()

/** Writes this to `filer`. */
@Throws(IOException::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,54 +69,54 @@ class FileWritingTest {

@Test fun pathDefaultPackage() {
val type = TypeSpec.classBuilder("Test").build()
FileSpec.get("", type).writeTo(fsRoot)
val testPath = FileSpec.get("", type).writeTo(fsRoot)

val testPath = fsRoot.resolve("Test.kt")
assertThat(testPath).isEqualTo(fsRoot.resolve("Test.kt"))
assertThat(Files.exists(testPath)).isTrue()
}

@Test fun pathDefaultPackageWithSubdirectory() {
val type = TypeSpec.classBuilder("Test").build()
FileSpec.get("", type).writeTo(fsRoot.resolve("sub"))
val testPath = FileSpec.get("", type).writeTo(fsRoot.resolve("sub"))

val testPath = fsRoot.resolve("sub/Test.kt")
assertThat(testPath).isEqualTo(fsRoot.resolve("sub/Test.kt"))
assertThat(Files.exists(testPath)).isTrue()
}

@Test fun fileDefaultPackage() {
val type = TypeSpec.classBuilder("Test").build()
FileSpec.get("", type).writeTo(tmp.root)
val testFile = FileSpec.get("", type).writeTo(tmp.root)

val testFile = File(tmp.root, "Test.kt")
assertThat(testFile).isEqualTo(File(tmp.root, "Test.kt"))
assertThat(testFile.exists()).isTrue()
}

@Test fun pathNestedClasses() {
val type = TypeSpec.classBuilder("Test").build()
FileSpec.get("foo", type).writeTo(fsRoot)
FileSpec.get("foo.bar", type).writeTo(fsRoot)
FileSpec.get("foo.bar.baz", type).writeTo(fsRoot)
val fooPath = FileSpec.get("foo", type).writeTo(fsRoot)
val barPath = FileSpec.get("foo.bar", type).writeTo(fsRoot)
val bazPath = FileSpec.get("foo.bar.baz", type).writeTo(fsRoot)

val fooPath = fsRoot.resolve(fs.getPath("foo", "Test.kt"))
val barPath = fsRoot.resolve(fs.getPath("foo", "bar", "Test.kt"))
val bazPath = fsRoot.resolve(fs.getPath("foo", "bar", "baz", "Test.kt"))
assertThat(fooPath).isEqualTo(fsRoot.resolve(fs.getPath("foo", "Test.kt")))
assertThat(barPath).isEqualTo(fsRoot.resolve(fs.getPath("foo", "bar", "Test.kt")))
assertThat(bazPath).isEqualTo(fsRoot.resolve(fs.getPath("foo", "bar", "baz", "Test.kt")))
assertThat(Files.exists(fooPath)).isTrue()
assertThat(Files.exists(barPath)).isTrue()
assertThat(Files.exists(bazPath)).isTrue()
}

@Test fun fileNestedClasses() {
val type = TypeSpec.classBuilder("Test").build()
FileSpec.get("foo", type).writeTo(tmp.root)
FileSpec.get("foo.bar", type).writeTo(tmp.root)
FileSpec.get("foo.bar.baz", type).writeTo(tmp.root)
val fooFile = FileSpec.get("foo", type).writeTo(tmp.root)
val barFile = FileSpec.get("foo.bar", type).writeTo(tmp.root)
val bazFile = FileSpec.get("foo.bar.baz", type).writeTo(tmp.root)

val fooDir = File(tmp.root, "foo")
val fooFile = File(fooDir, "Test.kt")
assertThat(fooFile).isEqualTo(File(fooDir, "Test.kt"))
val barDir = File(fooDir, "bar")
val barFile = File(barDir, "Test.kt")
assertThat(barFile).isEqualTo(File(barDir, "Test.kt"))
val bazDir = File(barDir, "baz")
val bazFile = File(bazDir, "Test.kt")
assertThat(bazFile).isEqualTo(File(bazDir, "Test.kt"))
assertThat(fooFile.exists()).isTrue()
assertThat(barFile.exists()).isTrue()
assertThat(bazFile.exists()).isTrue()
Expand Down

0 comments on commit 1286d7b

Please sign in to comment.