1 package org.jetbrains.dokka.gradle
2
3 import com.intellij.rt.execution.junit.FileComparisonFailure
4 import java.io.File
5 import java.io.IOException
6 import java.nio.file.*
7 import java.nio.file.attribute.BasicFileAttributes
8
9
writeStructurenull10 fun File.writeStructure(builder: StringBuilder, relativeTo: File = this, spaces: Int = 0) {
11 builder.append(" ".repeat(spaces))
12 val out = if (this != relativeTo) this.relativeTo(relativeTo) else this
13
14 builder.append(out)
15 if (this.isDirectory) {
16 builder.appendln("/")
17 this.listFiles().sortedBy { it.name }.forEach { it.writeStructure(builder, this, spaces + 4) }
18 } else {
19 builder.appendln()
20 }
21 }
22
assertEqualsIgnoringSeparatorsnull23 fun assertEqualsIgnoringSeparators(expectedFile: File, output: String) {
24 if (!expectedFile.exists()) expectedFile.createNewFile()
25 val expectedText = expectedFile.readText().replace("\r\n", "\n")
26 val actualText = output.replace("\r\n", "\n")
27
28 if (expectedText != actualText)
29 throw FileComparisonFailure("", expectedText, actualText, expectedFile.canonicalPath)
30 }
31
32 class CopyFileVisitor(private var sourcePath: Path?, private val targetPath: Path) : SimpleFileVisitor<Path>() {
33
34 @Throws(IOException::class)
preVisitDirectorynull35 override fun preVisitDirectory(dir: Path,
36 attrs: BasicFileAttributes): FileVisitResult {
37 if (sourcePath == null) {
38 sourcePath = dir
39 } else {
40 Files.createDirectories(targetPath.resolve(sourcePath?.relativize(dir)))
41 }
42 return FileVisitResult.CONTINUE
43 }
44
45 @Throws(IOException::class)
visitFilenull46 override fun visitFile(file: Path,
47 attrs: BasicFileAttributes): FileVisitResult {
48 Files.copy(file, targetPath.resolve(sourcePath?.relativize(file)), StandardCopyOption.REPLACE_EXISTING)
49 return FileVisitResult.CONTINUE
50 }
51 }
52
Pathnull53 fun Path.copy(to: Path) {
54 Files.walkFileTree(this, CopyFileVisitor(this, to))
55 }
56
57