summaryrefslogtreecommitdiff
path: root/support/tlcockpit/src/main/scala/TeXLive/OsTools.scala
blob: 1e0aa0f03ce7d0867217d6e2ee2cc2aae6e1c858 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package TeXLive
// TLCockpit
// Copyright 2017-2018 Norbert Preining
// Licensed according to GPLv3+
//
// Front end for tlmgr

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import sys.process._

object OsTools {
  val OS: String = System.getProperty("os.name").map(_.toLower)
  val CygwinRootIsSet: Boolean =
    sys.env.get("CYGWIN_ROOT") match {
      case Some(_) => true
      case None => false
    }
  def isWindows: Boolean = {
    OS.startsWith("windows") && !CygwinRootIsSet
  }
  def isCygwin: Boolean = {
    OS.startsWith("windows") && CygwinRootIsSet
  }
  def isApple: Boolean = {
    OS.startsWith("mac os")
  }
  def isUnix: Boolean = {
    !(isWindows || isApple || isCygwin)
  }

  def openFileCmd(f: String): Seq[String] = {
    val absf = new java.io.File(f).getCanonicalPath
    if (isWindows) {
      Seq("cmd", "/c", "start", absf)
    } else if (isCygwin) {
      Seq("cygstart", absf)
    } else if (isApple) {
      Seq("open", absf)
    } else {
      Seq("xdg-open", absf)
    }
  }

  def openFile(f: String): Unit = {
    val cmd = openFileCmd(f)
    val bar = Future {
      try {
        cmd.!
      } catch {
        case e: Exception => println("Cannot run command: " + cmd + " -- " + e + "\n")
      }
    }
  }
}