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

Initial try at a Mac implementation #1

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 27 additions & 0 deletions core/js/src/main/scala/tf/bug/dirs/JSTarget.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package tf.bug.dirs
import scala.scalajs.js
import scala.scalajs.js.annotation.JSImport

/** Necessary facades around the Node.js `os` module for discovering dirs
*/
object os {
@js.native
@JSImport("os", "homedir")
def homedir(): String = js.native

@js.native
@JSImport("process", "platform")
def platform(): String = js.native

}
object JSTarget extends Target {
def homedir: Option[Path] = Some(Path.get(os.homedir()))
def platform: Platform = {
val osName = jsPlatform()
osName match {
case "win32" => Platform.Windows
case "darwin" => Platform.Mac
case _ => Platform.Unknown(osName)
}
}
}
24 changes: 24 additions & 0 deletions core/js/src/main/scala/tf/bug/dirs/MacPlatform.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package tf.bug.dirs

import java.nio.file.{Path, Paths}

object MacPlatform extends Mac {
def home: Option[Path] = JSTarget.homedir
def cache: Option[Path] = home.map(h => Paths.get(h + "/Library/Caches"))
def config: Option[Path] =
home.map(h => Paths.get(h + "/Library/Application Support"))
def data: Option[Path] =
home.map(h => Paths.get(h + "/Library/Application Support"))
def dataLocal: Option[Path] =
home.map(h => Paths.get(h + "/Library/Preferences"))
def preference: Option[Path] =
home.map(h => Paths.get(h + "/Library/Preferences"))
def audio: Option[Path] = home.map(h => Paths.get(h + "/Music"))
def desktop: Option[Path] = home.map(h => Paths.get(h + "/Desktop"))
def document: Option[Path] = home.map(h => Paths.get(h + "/Documents"))
def download: Option[Path] = home.map(h => Paths.get(h + "/Downloads"))
def font: Option[Path] = home.map(h => Paths.get(h + "/Library/)Fonts"))
def picture: Option[Path] = home.map(h => Paths.get(h + "/Pictures"))
def public: Option[Path] = home.map(h => Paths.get(h + "/Public"))
def video: Option[Path] = home.map(h => Paths.get(h + "/Movies"))
}
16 changes: 16 additions & 0 deletions core/jvm/src/main/scala/tf/bug/dirs/JVMTarget.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package tf.bug.dirs



object JVMTarget extends Target {
def platform: Platform = {
val osName = System.getProperty("os.name")
if (osName.contains("Windows")) {
Platform.Windows
} else {
Copy link
Owner

Choose a reason for hiding this comment

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

contains("Mac") || contains("Darwin")? Not sure if darwin will report lower-case here though...

Copy link
Author

Choose a reason for hiding this comment

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

I got the "Mac" part by runninggetProperty locally.
AFAIK, os.name never returns "darwin". However only evidence I can find is are blogposts.

Platform.Unknown(osName)
}
}

def homeDir: Option[Path] = Some(Paths.get(System.getProperty("user.home")))
}
24 changes: 24 additions & 0 deletions core/jvm/src/main/scala/tf/bug/dirs/MacPlatform.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package tf.bug.dirs

import java.nio.file.{Path, Paths}

object MacPlatform extends Mac {
def home: Option[Path] = JVMTarget.homeDir
def cache: Option[Path] = home.map(h => Paths.get(h + "/Library/)Caches"))
def config: Option[Path] =
home.map(h => Paths.get(h + "/Library/)Application Support"))
def data: Option[Path] =
home.map(h => Paths.get(h + "/Library/)Application Support"))
def dataLocal: Option[Path] =
home.map(h => Paths.get(h + "/Library/)Preferences"))
zetashift marked this conversation as resolved.
Show resolved Hide resolved
def preference: Option[Path] =
home.map(h => Paths.get(h + "/Library/)Preferences"))
def audio: Option[Path] = home.map(h => Paths.get(h + "/Music"))
def desktop: Option[Path] = home.map(h => Paths.get(h + "/Desktop"))
def document: Option[Path] = home.map(h => Paths.get(h + "/Documents"))
def download: Option[Path] = home.map(h => Paths.get(h + "/Downloads"))
def font: Option[Path] = home.map(h => Paths.get(h + "/Library/)Fonts"))
def picture: Option[Path] = home.map(h => Paths.get(h + "/Pictures"))
def public: Option[Path] = home.map(h => Paths.get(h + "/Public"))
def video: Option[Path] = home.map(h => Paths.get(h + "/Movies"))
}
29 changes: 21 additions & 8 deletions core/shared/src/main/scala/tf/bug/dirs/Dirs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,107 +7,120 @@ object Dirs {
sealed trait Platform
object Platform {
case object Windows extends Platform
case object Mac extends Platform
case class Unknown(osName: String) extends Platform
}

lazy val windows: Windows = WindowsPlatform
lazy val mac: Mac = MacPlatform

lazy val platform: Platform = {
val osName = System.getProperty("os.name")
if(osName.contains("Windows")) {
Platform.Windows
} else {
Platform.Unknown(osName)
}
}
lazy val platform: Platform = Target.platform

def audio: Option[Path] = platform match {
case Platform.Windows => this.windows.shGetKnownFolderPath(this.windows.folderIdMusic)
case Platform.Mac => this.mac.audio
case Platform.Unknown(osName) => ???
}

def cache: Option[Path] = platform match {
case Platform.Windows => this.windows.shGetKnownFolderPath(this.windows.folderIdLocalAppData)
case Platform.Mac => this.mac.cache
case Platform.Unknown(osName) => ???
}

def config: Option[Path] = platform match {
case Platform.Windows => this.windows.shGetKnownFolderPath(this.windows.folderIdRoamingAppData)
case Platform.Mac => this.mac.config
case Platform.Unknown(osName) => ???
}

def data: Option[Path] = platform match {
case Platform.Windows => this.windows.shGetKnownFolderPath(this.windows.folderIdRoamingAppData)
case Platform.Mac => this.mac.data
case Platform.Unknown(osName) => ???
}

def dataLocal: Option[Path] = platform match {
case Platform.Windows => this.windows.shGetKnownFolderPath(this.windows.folderIdLocalAppData)
case Platform.Mac => this.mac.dataLocal
case Platform.Unknown(osName) => ???
}

def desktop: Option[Path] = platform match {
case Platform.Windows => this.windows.shGetKnownFolderPath(this.windows.folderIdDesktop)
case Platform.Mac => this.mac.desktop
case Platform.Unknown(osName) => ???
}

def document: Option[Path] = platform match {
case Platform.Windows => this.windows.shGetKnownFolderPath(this.windows.folderIdDocuments)
case Platform.Mac => this.mac.document
case Platform.Unknown(osName) => ???
}

def download: Option[Path] = platform match {
case Platform.Windows => this.windows.shGetKnownFolderPath(this.windows.folderIdDownloads)
case Platform.Mac => this.mac.download
case Platform.Unknown(osName) => ???
}

def executable: Option[Path] = platform match {
case Platform.Windows => None
case Platform.Mac => None
case Platform.Unknown(osName) => ???
}

def font: Option[Path] = platform match {
case Platform.Windows => None
case Platform.Mac => this.mac.font
case Platform.Unknown(osName) => ???
}

def home: Option[Path] = platform match {
case Platform.Windows => this.windows.shGetKnownFolderPath(this.windows.folderIdProfile)
case Platform.Mac => this.mac.home
case Platform.Unknown(osName) => ???
}

def picture: Option[Path] = platform match {
case Platform.Windows => this.windows.shGetKnownFolderPath(this.windows.folderIdPictures)
case Platform.Mac => this.mac.picture
case Platform.Unknown(osName) => ???
}

def preference: Option[Path] = platform match {
case Platform.Windows => this.windows.shGetKnownFolderPath(this.windows.folderIdRoamingAppData)
case Platform.Mac => this.mac.preference
case Platform.Unknown(osName) => ???
}

def public: Option[Path] = platform match {
case Platform.Windows => this.windows.shGetKnownFolderPath(this.windows.folderIdPublic)
case Platform.Mac => this.mac.public
case Platform.Unknown(osName) => ???
}

def runtime: Option[Path] = platform match {
case Platform.Windows => None
case Platform.Mac => None
case Platform.Unknown(osName) => ???
}

def state: Option[Path] = platform match {
case Platform.Windows => None
case Platform.Mac => None
case Platform.Unknown(osName) => ???
}

def template: Option[Path] = platform match {
case Platform.Windows => this.windows.shGetKnownFolderPath(this.windows.folderIdTemplates)
case Platform.Mac => None
case Platform.Unknown(osName) => ???
}

def video: Option[Path] = platform match {
case Platform.Windows => this.windows.shGetKnownFolderPath(this.windows.folderIdVideos)
case Platform.Mac => this.mac.video
case Platform.Unknown(osName) => ???
}

Expand Down
24 changes: 24 additions & 0 deletions core/shared/src/main/scala/tf/bug/dirs/Mac.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package tf.bug.dirs

import java.nio.file.Path

trait Mac {
def home: Option[Path]
def cache: Option[Path]
def config: Option[Path]
def data: Option[Path]
def dataLocal: Option[Path]
def preference: Option[Path]
def executable: Option[Path]
def runtime: Option[Path]
def state: Option[Path]
def audio: Option[Path]
def desktop: Option[Path]
def document: Option[Path]
def download: Option[Path]
def font: Option[Path]
def picture: Option[Path]
def public: Option[Path]
def template: Option[Path]
def video: Option[Path]
}
8 changes: 8 additions & 0 deletions core/shared/src/main/scala/tf/bug/dirs/Target.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package tf.bug.dirs

import java.nio.file.Path

trait Target {
def homeDir: Option[Path]
def platform: Platform
}