Skip to content

Commit

Permalink
windows platform fix (#37)
Browse files Browse the repository at this point in the history
* v0.2.1

* ci for windows

* platform specefic test
  • Loading branch information
psuzn authored May 11, 2024
1 parent 06f3cfc commit 7a11dc0
Show file tree
Hide file tree
Showing 12 changed files with 332 additions and 4 deletions.
42 changes: 42 additions & 0 deletions .github/workflows/build-windows.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Build Linux

on:
workflow_call:
workflow_dispatch:

jobs:
build:
runs-on: windows-latest
env:
GRADLE_OPTS: -Dorg.gradle.jvmargs="-Xmx3g"

steps:
- uses: actions/checkout@v4

- name: JDK setup
uses: actions/setup-java@v4
with:
java-version: |
11
17
distribution: corretto

- name: Cache
uses: actions/cache@v4
with:
path: |
./build
./.gradle
~/.gradle/caches
~/.gradle/wrapper
~/.m2/repository
key: ${{ runner.os }}-gradle-${{ hashFiles('*.gradle.kts') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: Tests
run: ./gradlew allTest --parallel --no-daemon --stacktrace

- name: Build Artifacts
run: ./gradlew publishToMavenLocal

3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ jobs:
name: Build macOS
uses: ./.github/workflows/build-macos.yml

build-windows:
name: Build Windows
uses: ./.github/workflows/build-windows.yml
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ All notable changes to this project will be documented in this file.

### Changed

## [0.2.1] - 2024-04-23

### Fixes

- NSHomeDirectory is only allowed to be written on iOS Simulators, but not on physical devices #33

## [0.2.0] - 2024-04-21

This version contains general house keeping, dependencies updates and ci improvement for better maintainability.
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ repositories {
}

dependencies {
implementation("me.sujanpoudel.multiplatform.utils:multiplatform-paths:0.2.0")
implementation("me.sujanpoudel.multiplatform.utils:multiplatform-paths:0.2.1")
}
```

Expand Down Expand Up @@ -113,7 +113,7 @@ repositories {
}

dependencies {
implementation("me.sujanpoudel.multiplatform.utils:platform-identifier:0.2.0")
implementation("me.sujanpoudel.multiplatform.utils:platform-identifier:0.2.1")
}
```

Expand Down Expand Up @@ -177,7 +177,7 @@ repositories {
}

dependencies {
implementation("me.sujanpoudel.multiplatform.utils:context-provider:0.2.0")
implementation("me.sujanpoudel.multiplatform.utils:context-provider:0.2.1")
}
```

Expand Down
17 changes: 17 additions & 0 deletions multiplatform-paths/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ kotlin {
}
}

val androidUnitTest by getting {
dependencies {
implementation(libs.junit)
implementation(libs.ext.junit)
implementation(libs.espresso.core)
implementation(libs.robolectric)
}
}

val darwinMain by creating {
dependsOn(commonMain)
}
Expand Down Expand Up @@ -90,6 +99,14 @@ kotlin {
}
}

android {
testOptions {
unitTests {
isIncludeAndroidResources = true
}
}
}

@Suppress("ktlint:standard:max-line-length")
mavenPublishing {
pom {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2024 Sujan Poudel
*
* 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
*
* http://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 me.sujanpoudel.utils.paths

import android.content.Context
import androidx.test.core.app.ApplicationProvider
import kotlinx.io.files.Path
import me.sujanpoudel.utils.paths.utils.toPath
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import kotlin.test.assertEquals

@RunWith(RobolectricTestRunner::class)
class AndroidDirectoriesTest {
private val appId: String = "me.sujanpoudel.utils.paths.test"

private fun exceptedCacheDir(appId: String): Path {
val applicationContext = ApplicationProvider.getApplicationContext<Context>()
return applicationContext.cacheDir.absolutePath.toPath()
}

private fun exceptedDataDir(appId: String): Path {
val applicationContext = ApplicationProvider.getApplicationContext<Context>()
return applicationContext.dataDir.absolutePath.toPath()
}

@Test
fun testCacheDirectory() {
assertEquals(exceptedCacheDir(appId), cacheDirectory(appId))
}

@Test
fun testDataDirectory() {
assertEquals(exceptedDataDir(appId), dataDirectory(appId))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2024 Sujan Poudel
*
* 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
*
* http://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 me.sujanpoudel.utils.paths

import kotlinx.io.files.Path
import kotlin.test.Test
import kotlin.test.assertEquals

abstract class DirectoriesTest {
private val appId: String = "me.sujanpoudel.utils.paths.test"

abstract fun exceptedCacheDir(appId: String): Path

abstract fun exceptedDataDir(appId: String): Path

@Test
fun testCacheDirectory() {
assertEquals(exceptedCacheDir(appId), cacheDirectory(appId))
}

@Test
fun testDataDirectory() {
assertEquals(exceptedDataDir(appId), dataDirectory(appId))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2024 Sujan Poudel
*
* 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
*
* http://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 me.sujanpoudel.utils.paths

import kotlinx.io.files.Path
import me.sujanpoudel.utils.paths.utils.div
import me.sujanpoudel.utils.paths.utils.toPath
import me.sujanpoudel.utils.platformIdentifier.Platform
import me.sujanpoudel.utils.platformIdentifier.hostOs

class DesktopDirectoriesTest : DirectoriesTest() {
private fun getEnv(key: String) = System.getenv(key) as String

override fun exceptedCacheDir(appId: String): Path {
return when (hostOs) {
is Platform.OS.Linux -> getEnv("HOME").toPath() / ".cache" / appId
is Platform.OS.MacOs -> getEnv("HOME").toPath() / "Library/Caches" / appId
is Platform.OS.Windows -> getEnv("APPDATA").toPath() / "Caches" / appId
else -> error("not supported")
}
}

override fun exceptedDataDir(appId: String): Path {
return when (hostOs) {
is Platform.OS.Linux -> getEnv("HOME").toPath() / ".local/share" / appId
is Platform.OS.MacOs -> getEnv("HOME").toPath() / "Library/Application Support" / appId
is Platform.OS.Windows -> getEnv("APPDATA").toPath() / appId
else -> error("not supported")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2024 Sujan Poudel
*
* 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
*
* http://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 me.sujanpoudel.utils.paths

import kotlinx.io.files.Path
import me.sujanpoudel.utils.paths.utils.div
import me.sujanpoudel.utils.paths.utils.toPath
import platform.Foundation.NSApplicationSupportDirectory
import platform.Foundation.NSCachesDirectory
import platform.Foundation.NSSearchPathForDirectoriesInDomains
import platform.Foundation.NSUserDomainMask

class IosDirectoriesTest : DirectoriesTest() {
override fun exceptedCacheDir(appId: String): Path {
return NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, true)
.firstOrNull()?.toString()?.toPath() ?: error("Unable to get 'NSCachesDirectory'")
}

override fun exceptedDataDir(appId: String): Path {
return NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true)
.firstOrNull()?.toString()?.toPath()
?.let { it / appId } ?: error("Unable to get 'NSApplicationSupportDirectory'")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2024 Sujan Poudel
*
* 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
*
* http://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 me.sujanpoudel.utils.paths

import kotlinx.io.files.Path
import me.sujanpoudel.utils.paths.utils.div
import me.sujanpoudel.utils.paths.utils.toPath
import me.sujanpoudel.utils.platformIdentifier.Platform
import me.sujanpoudel.utils.platformIdentifier.platform

class NodeDirectoriesTest : DirectoriesTest() {
private fun getEnv(key: String) = eval("""process.env["$key"]""") as String

override fun exceptedCacheDir(appId: String): Path {
val platform = platform() as? Platform.JS.Node ?: error("not supported")

return when (platform.os) {
is Platform.OS.Linux -> getEnv("HOME").toPath() / ".cache" / appId
is Platform.OS.MacOs -> getEnv("HOME").toPath() / "Library/Caches" / appId
is Platform.OS.Windows -> getEnv("APPDATA").toPath() / "Caches" / appId

else -> error("not supported")
}
}

override fun exceptedDataDir(appId: String): Path {
val platform = platform() as? Platform.JS.Node ?: error("not supported")

return when (platform.os) {
is Platform.OS.Linux -> getEnv("HOME").toPath() / ".local/share" / appId
is Platform.OS.MacOs -> getEnv("HOME").toPath() / "Library/Application Support" / appId
is Platform.OS.Windows -> getEnv("APPDATA").toPath() / appId
else -> error("not supported")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2024 Sujan Poudel
*
* 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
*
* http://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 me.sujanpoudel.utils.paths

import kotlinx.io.files.Path
import me.sujanpoudel.utils.paths.utils.div
import me.sujanpoudel.utils.paths.utils.toPath
import platform.Foundation.NSApplicationSupportDirectory
import platform.Foundation.NSCachesDirectory
import platform.Foundation.NSSearchPathForDirectoriesInDomains
import platform.Foundation.NSUserDomainMask

class MacosDirectoriesTest : DirectoriesTest() {
override fun exceptedCacheDir(appId: String): Path {
return NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, true)
.firstOrNull()?.toString()?.toPath()?.let { it / appId } ?: error("Unable to get 'NSCachesDirectory'")
}

override fun exceptedDataDir(appId: String): Path {
return NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true)
.firstOrNull()?.toString()?.toPath()?.let { it / appId } ?: error("Unable to get 'NSApplicationSupportDirectory'")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ internal fun hostOs(osName: String, archName: String, version: String): Platform
return when (hostOs(osName)) {
DesktopOs.Macos -> Platform.OS.MacOs(arch, version)
DesktopOs.Linux -> Platform.OS.Linux(arch, version)
DesktopOs.Windows -> Platform.OS.Linux(arch, version)
DesktopOs.Windows -> Platform.OS.Windows(arch, version)
DesktopOs.Unknown -> Platform.OS.Unknown(arch, version)
}
}

0 comments on commit 7a11dc0

Please sign in to comment.