Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create FileSystem.SYSTEM property in shared source set #1455

Merged
merged 3 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions okio/api/okio.api
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,10 @@ public abstract interface class okio/Source : java/io/Closeable {
public abstract fun timeout ()Lokio/Timeout;
}

public final class okio/SystemFileSystem {
public static final synthetic fun getSYSTEM (Lokio/FileSystem$Companion;)Lokio/FileSystem;
}

public final class okio/Throttler {
public fun <init> ()V
public final fun bytesPerSecond (J)V
Expand Down
8 changes: 8 additions & 0 deletions okio/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ plugins {
*
* The `hashFunctions` source set builds on all platforms. It ships as a main source set on non-JVM
* platforms and as a test source set on the JVM platform.
*
* The `systemFileSystem` source set is used on jvm and native targets, and provides the FileSystem.SYSTEM property.
*/
kotlin {
configureOrCreateOkioPlatforms()
Expand Down Expand Up @@ -87,6 +89,10 @@ kotlin {
dependsOn(commonMain)
}

val systemFileSystemMain by creating {
dependsOn(commonMain)
}

val nonJvmTest by creating {
dependsOn(commonTest)
}
Expand All @@ -104,6 +110,7 @@ kotlin {

val jvmMain by getting {
dependsOn(zlibMain)
dependsOn(systemFileSystemMain)
}
val jvmTest by getting {
kotlin.srcDir("src/jvmTest/hashFunctions")
Expand Down Expand Up @@ -131,6 +138,7 @@ kotlin {
createSourceSet("nativeMain", parent = nonJvmMain)
.also { nativeMain ->
nativeMain.dependsOn(zlibMain)
nativeMain.dependsOn(systemFileSystemMain)
createSourceSet("mingwMain", parent = nativeMain, children = mingwTargets).also { mingwMain ->
mingwMain.dependsOn(nonAppleMain)
}
Expand Down
13 changes: 13 additions & 0 deletions okio/src/jvmMain/kotlin/okio/FileSystem.System.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@file:JvmName("SystemFileSystem")

package okio

/*
* JVM and native platforms do offer a [SYSTEM] [FileSystem], however we cannot refine an 'expect' companion object.
* Therefore an extension property is provided, which on respective platforms (here JVM) will be shadowed by the
* original implementation.
*/
@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
actual inline val FileSystem.Companion.SYSTEM: FileSystem
@JvmSynthetic
get() = SYSTEM
9 changes: 9 additions & 0 deletions okio/src/nativeMain/kotlin/okio/FileSystem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,12 @@ actual abstract class FileSystem {
actual val SYSTEM_TEMPORARY_DIRECTORY: Path = PLATFORM_TEMPORARY_DIRECTORY
}
}

/*
* JVM and native platforms do offer a [SYSTEM] [FileSystem], however we cannot refine an 'expect' companion object.
* Therefore an extension property is provided, which on respective platforms (here JVM) will be shadowed by the
* original implementation.
*/
@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
actual inline val FileSystem.Companion.SYSTEM: FileSystem
get() = SYSTEM
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package okio

/*
* The current process's host file system. Use this instance directly, or dependency inject a
* [FileSystem] to make code testable.
*/
expect val FileSystem.Companion.SYSTEM: FileSystem
11 changes: 11 additions & 0 deletions samples/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,23 @@ kotlin {
jvm {
withJava()
}

linuxX64()
linuxArm64()
macosArm64()
macosX64()

sourceSets {
commonMain {
dependencies {
implementation(projects.okio)
}
}
commonTest {
dependencies {
implementation(libs.kotlin.test)
}
}
val jvmTest by getting {
dependencies {
implementation(libs.test.junit)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package okio.samples

import okio.FileSystem
import okio.Path.Companion.toPath
import okio.SYSTEM

class MultiplatformFileSystem {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe skip this new sample, and instead delete SYSTEM_FILE_SYSTEM from TestingJvm.kt and TestingNative.kt ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, added this change.

fun run() {
val currentWorkingDirectory = FileSystem.SYSTEM.canonicalize("".toPath())
val children = FileSystem.SYSTEM.listOrNull(currentWorkingDirectory)
println("The current working directory is: $currentWorkingDirectory, containing: ${children?.joinToString()}")
}
}

fun main() {
MultiplatformFileSystem().run()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package okio.samples

import kotlin.test.Test

class MultiplatformFileSystemTest {
@Test
fun testSystemFileSystem() {
MultiplatformFileSystem().run()
}
}
Loading