@@ -18,24 +18,31 @@ import java.io.Writer
18
18
import java.nio.channels.FileChannel
19
19
import java.nio.file.StandardOpenOption
20
20
import java.util.*
21
- import kotlin.contracts.ExperimentalContracts
22
- import kotlin.contracts.InvocationKind
23
- import kotlin.contracts.contract
24
21
25
- @ExperimentalContracts
26
- fun writeToFile (path : String , mkdirs : Boolean = true, isRelativePath : Boolean = true, block : (Writer ) -> Unit ) {
27
- contract {
28
- callsInPlace(block, InvocationKind .EXACTLY_ONCE )
29
- }
22
+ fun isWindowsHost (): Boolean {
23
+ return System .getProperty(" os.name" ).startsWith(" Windows" )
24
+ }
30
25
31
- val isWindows = System .getProperty(" os.name" ).startsWith(" Windows" )
26
+ fun writeToFile (path : String , block : (Writer ) -> Unit ) {
27
+ val isWindows = isWindowsHost()
28
+ // TODO proper detection, but it isn't needed for now,
29
+ // because all paths passed to this method are relative.
30
+ val isRelativePath = ! path.startsWith(" /" )
31
+ val isRootFolder = ! path.contains(" /" )
32
32
val file = if (isRelativePath && isWindows) {
33
33
File (" ${System .getProperty(" user.dir" )}${File .separator}$path " )
34
34
} else {
35
35
File (path)
36
36
}
37
+ writeToFile(file, mkdirs = ! isRootFolder, block)
38
+ }
37
39
38
- if (! file.parentFile.exists()) {
40
+ fun writeToFile (file : File , mkdirs : Boolean = true, block : (Writer ) -> Unit ) {
41
+ if (file.parentFile == null ) {
42
+ if (mkdirs) {
43
+ error(" Invalid file path: ${file.absolutePath} " )
44
+ }
45
+ } else if (! file.parentFile.exists()) {
39
46
if (mkdirs) {
40
47
if (! file.parentFile.mkdirs())
41
48
error(" Could not create folder: ${file.parentFile.absolutePath} " )
@@ -62,7 +69,7 @@ fun writeToFile(path: String, mkdirs: Boolean = true, isRelativePath: Boolean =
62
69
63
70
if (file.exists()) {
64
71
if (! areFileContentsIdentical(file, outFile)) {
65
- if (isWindows ) {
72
+ if (isWindowsHost() ) {
66
73
Thread .sleep(300 )
67
74
System .gc()
68
75
}
0 commit comments