src/Pure/Admin/other_isabelle.scala
author wenzelm
Tue, 24 Jan 2023 14:46:51 +0100
changeset 77069 348f4d95d110
parent 77055 f56800b8b085
child 77071 7a89ef6b0276
permissions -rw-r--r--
more robust (see also 7f55a3e28c88): resolve components from current Isabelle context, using Isabelle/Scala instead of shell scripts;

/*  Title:      Pure/Admin/other_isabelle.scala
    Author:     Makarius

Manage other Isabelle distributions.
*/

package isabelle


object Other_Isabelle {
  def apply(
    isabelle_home: Path,
    isabelle_identifier: String = "",
    user_home: Path = Path.USER_HOME,
    progress: Progress = new Progress
  ): Other_Isabelle = {
    new Other_Isabelle(isabelle_home.canonical, isabelle_identifier, user_home, progress)
  }
}

final class Other_Isabelle private(
  val isabelle_home: Path,
  val isabelle_identifier: String,
  user_home: Path,
  progress: Progress
) {
  other_isabelle =>

  override def toString: String = isabelle_home.toString

  if (proper_string(System.getenv("ISABELLE_SETTINGS_PRESENT")).isDefined) {
    error("Cannot initialize with enclosing ISABELLE_SETTINGS_PRESENT")
  }


  /* static system */

  def bash(
    script: String,
    redirect: Boolean = false,
    echo: Boolean = false,
    strict: Boolean = true
  ): Process_Result = {
    progress.bash(
      "export USER_HOME=" + File.bash_path(user_home) + "\n" +
      Isabelle_System.export_isabelle_identifier(isabelle_identifier) + script,
      env = null, cwd = isabelle_home.file, redirect = redirect, echo = echo, strict = strict)
  }

  def getenv(name: String): String =
    other_isabelle.bash("bin/isabelle getenv -b " + Bash.string(name)).check.out

  val isabelle_home_user: Path = Path.explode(getenv("ISABELLE_HOME_USER"))

  val etc: Path = isabelle_home_user + Path.explode("etc")
  val etc_settings: Path = etc + Path.explode("settings")
  val etc_preferences: Path = etc + Path.explode("preferences")

  def resolve_components(echo: Boolean): Unit = {
    val missing = Path.split(getenv("ISABELLE_COMPONENTS_MISSING"))
    for (path <- missing) {
      Components.resolve(path.dir, path.file_name, progress = if (echo) progress else new Progress)
    }
  }


  /* components */

  def init_components(
    component_repository: String = Components.default_component_repository,
    components_base: Path = Components.default_components_base,
    catalogs: List[String] = Components.default_catalogs,
    components: List[String] = Nil
  ): List[String] = {
    val dir = Components.admin(isabelle_home)

    ("ISABELLE_COMPONENT_REPOSITORY=" + Bash.string(component_repository)) ::
    catalogs.map(name =>
      "init_components " + File.bash_path(components_base) + " " +
        File.bash_path(dir + Path.basic(name))) :::
    components.map(name =>
      "init_component " + File.bash_path(components_base + Path.basic(name)))
  }


  /* settings */

  def clean_settings(): Boolean =
    if (!etc_settings.is_file) true
    else if (File.read(etc_settings).startsWith("# generated by Isabelle")) {
      etc_settings.file.delete
      true
    }
    else false

  def init_settings(settings: List[String]): Unit = {
    if (!clean_settings()) {
      error("Cannot proceed with existing user settings file: " + etc_settings)
    }

    Isabelle_System.make_directory(etc_settings.dir)
    File.write(etc_settings,
      "# generated by Isabelle " + Date.now() + "\n" +
      "#-*- shell-script -*- :mode=shellscript:\n" +
      settings.mkString("\n", "\n", "\n"))
  }


  /* cleanup */

  def cleanup(): Unit = {
    clean_settings()
    etc.file.delete
    isabelle_home_user.file.delete
  }
}