Skip to content

Commit

Permalink
Return the generated path from FileSpec.writeTo().
Browse files Browse the repository at this point in the history
  • Loading branch information
fejesjoco committed Apr 9, 2023
1 parent cf9eea1 commit f94fa23
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
15 changes: 11 additions & 4 deletions kotlinpoet/src/main/java/com/squareup/kotlinpoet/FileSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,12 @@ public class FileSpec private constructor(
codeWriter.close()
}

/** 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 path
* of the output file.
*/
@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 +91,15 @@ public class FileSpec private constructor(

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

/** 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 path
* of the 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
36 changes: 18 additions & 18 deletions kotlinpoet/src/test/java/com/squareup/kotlinpoet/FileWritingTest.kt
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 f94fa23

Please sign in to comment.