Initial commit

This commit is contained in:
Peter Niederwieser
2016-01-19 14:51:19 +01:00
committed by Dan Chao
commit ecad035dca
2972 changed files with 211653 additions and 0 deletions

View File

@@ -0,0 +1,73 @@
/**
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pkl.commons
import java.io.*
import java.nio.charset.Charset
import java.nio.file.*
import java.nio.file.attribute.FileAttribute
import java.util.stream.Stream
import kotlin.io.path.createDirectories
import kotlin.io.path.deleteIfExists
import kotlin.io.path.exists
import kotlin.io.path.isSymbolicLink
// not stored to avoid build-time initialization by native-image
val currentWorkingDir: Path
get() = System.getProperty("user.dir").toPath()
// unlike `Path.resolve`, this works across file systems if `other` is absolute
fun Path.resolveSafely(other: Path): Path = if (other.isAbsolute) other else resolve(other)
@Throws(IOException::class)
fun Path.walk(maxDepth: Int = Int.MAX_VALUE, vararg options: FileVisitOption): Stream<Path> =
Files.walk(this, maxDepth, *options)
@Throws(IOException::class)
fun Path.createTempFile(
prefix: String? = null,
suffix: String? = null,
vararg attributes: FileAttribute<*>
): Path = Files.createTempFile(this, prefix, suffix, *attributes)
@Throws(IOException::class)
fun Path.createParentDirectories(vararg attributes: FileAttribute<*>): Path = apply {
// Files.createDirectories will throw a FileAlreadyExistsException
// if the file exists and is not a directory and symlinks are never
// directories
if (parent?.isSymbolicLink() != true) {
parent?.createDirectories(*attributes)
}
}
/** [Files.writeString] seems more efficient than [kotlin.io.path.writeText]. */
@Throws(IOException::class)
fun Path.writeString(
text: String,
charset: Charset = Charsets.UTF_8,
vararg options: OpenOption
): Path = Files.writeString(this, text, charset, *options)
/** [Files.readString] seems more efficient than [kotlin.io.path.readText]. */
@Throws(IOException::class)
fun Path.readString(charset: Charset = Charsets.UTF_8): String = Files.readString(this, charset)
@Throws(IOException::class)
fun Path.deleteRecursively() {
if (exists()) {
walk().use { paths -> paths.sorted(Comparator.reverseOrder()).forEach { it.deleteIfExists() } }
}
}

View File

@@ -0,0 +1,29 @@
/**
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pkl.commons
import java.net.URI
import java.nio.file.Path
fun String.toPath(): Path = Path.of(this)
/** Copy of org.pkl.core.util.IoUtils.toUri */
fun String.toUri(): URI =
if (contains(":")) {
URI(this)
} else {
URI(null, null, this, null)
}

View File

@@ -0,0 +1,23 @@
/**
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pkl.commons
import java.io.PrintWriter
import java.io.StringWriter
/** Same as [Throwable.printStackTrace] except that it prints to a [String]. */
fun Throwable.printStackTraceToString(): String =
StringWriter().also { printStackTrace(PrintWriter(it)) }.toString()

View File

@@ -0,0 +1,21 @@
/**
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pkl.commons
import java.net.URI
import java.nio.file.Path
fun URI.toPath(): Path = Path.of(this)