/* * Copyright 2021 Ramiro Calle * * Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE.md file. */////// Effect for accessing the system environment, and its functions.///pubmod Sys.Env {use Sys.Envimport dev.flix.runtime.Globalimport java.lang.NullPointerExceptionimport java.lang.Runtimeimport java.lang.SecurityExceptionimport java.lang.Systemimport java.io.{File => JFile}import java.util.{Iterator => JIterator}import java.util.{Map => JMap}import java.util.{Set => JSet}import java.util.{Map$Entry => Entry}////// Effect for accessing the system environment.///pubeffEnv {////// Returns the arguments passed to the program via the command line.///defgetArgs(): List[String]////// Returns an map of the current system environment.///defgetEnv(): Map[String, String]////// Returns the value of the specified environment variable.///defgetVar(name: String): Option[String]////// Returns the system property by name.///defgetProp(name: String): Option[String]////// Returns the operating system name///defgetOsName(): Option[String]////// Returns the operating system architecture///defgetOsArch(): Option[String]////// Returns the operating system version///defgetOsVersion(): Option[String]////// Returns the file separator///defgetFileSeparator(): String////// Returns the path separator///defgetPathSeparator(): String////// Returns the system line separator///defgetLineSeparator(): String////// Returns the user's current working directory///defgetCurrentWorkingDirectory(): Option[String]////// Returns the default temp file path///defgetTemporaryDirectory(): Option[String]////// Returns the user's account name///defgetUserName(): Option[String]////// Returns the user's home directory///defgetUserHomeDirectory(): Option[String]////// Returns the number of virtual processors available to the JVM.////// The returned value is never less than one.///defgetVirtualProcessors(): Int32 }////// Handles the `Env` effect of the given function `f`.////// In other words, re-interprets the `Env` effect using the `IO` effect.///pubdefhandle(f: a -> b \ ef): a -> b \ (ef - Env) + IO = x ->run {f(x) } withhandlerEnv {defgetArgs(k) =k(Array.toList(Global.getArgs()))defgetEnv(k) = regionrc {try {let_ = rc;letiter: JIterator[Entry[String, String]] = unsafeIOasrc { System.getenv().entrySet().iterator() };k(getEnvHelper(iter, Map.empty())) } catch {case_: SecurityException => k(Map.empty()) } }defgetVar(name, k) =try {letresult = System.getenv(name);k(Object.toOption(result)) } catch {case_: SecurityException => k(None)case_: NullPointerException => k(None) }defgetProp(name, k) = k(getPropHelper(name))defgetOsName(k) = k(getPropHelper("os.name"))defgetOsArch(k) = k(getPropHelper("os.arch"))defgetOsVersion(k) = k(getPropHelper("os.version"))defgetFileSeparator(k) = k(JFile.separator)defgetPathSeparator(k) = k(JFile.pathSeparator)defgetLineSeparator(k) = k(System.lineSeparator())defgetCurrentWorkingDirectory(k) = k(getPropHelper("user.dir"))defgetTemporaryDirectory(k) = k(getPropHelper("java.io.tmpdir"))defgetUserName(k) = k(getPropHelper("user.name"))defgetUserHomeDirectory(k) = k(getPropHelper("user.home"))defgetVirtualProcessors(k) = k(Runtime.getRuntime().availableProcessors()) }////// Runs the `Env` effect of the given function `f`.////// In other words, re-interprets the `Env` effect using the `IO` effect.///@DefaultHandlerpubdefrunWithIO(f: Unit -> a \ ef): a \ (ef - Env) + IO = handle(f)()defgetPropHelper(name: String): Option[String] \ IO =try {letresult = System.getProperty(name);Object.toOption(result) } catch {case_: SecurityException => Nonecase_: NullPointerException => None }defgetEnvHelper(iter: JIterator[Entry[String, String]], m: Map[String, String]): Map[String, String] \ IO =if (iter.hasNext())lete: Entry[String, String] = iter.next();letk: String = e.getKey();letv: String = e.getValue();getEnvHelper(iter, Map.insert(k, v, m))elsem}