src/Pure/Admin/component_hugo.scala
author wenzelm
Wed, 12 Mar 2025 11:39:00 +0100
changeset 82265 4b875a4c83b0
parent 82030 bae948b1132b
permissions -rw-r--r--
update for release;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
78746
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
     1
/*  Title:      Pure/Admin/component_hugo.scala
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
     2
    Author:     Fabian Huch, TU Muenchen
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
     3
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
     4
Build Isabelle component for hugo site generator. See also:
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
     5
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
     6
  - https://gohugo.io
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
     7
  - https://github.com/gohugoio/hugo
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
     8
*/
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
     9
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    10
package isabelle
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    11
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    12
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    13
object Component_Hugo {
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    14
  /* platform information */
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    15
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    16
  sealed case class Download_Platform(platform_name: String, url_template: String) {
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    17
    override def toString: String = platform_name
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    18
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    19
    def is_windows: Boolean = url_template.contains("windows")
79975
90f4319c8b4f misc tuning;
wenzelm
parents: 79974
diff changeset
    20
90f4319c8b4f misc tuning;
wenzelm
parents: 79974
diff changeset
    21
    def download(base_url: String, version: String): String =
78746
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    22
      base_url + "/v" + version + "/" + url_template.replace("{V}", version)
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    23
  }
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    24
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    25
  val platforms: List[Download_Platform] =
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    26
    List(
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    27
      Download_Platform("x86_64-darwin", "hugo_extended_{V}_darwin-universal.tar.gz"),
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    28
      Download_Platform("x86_64-linux", "hugo_extended_{V}_linux-amd64.tar.gz"),
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    29
      Download_Platform("x86_64-windows", "hugo_extended_{V}_windows-amd64.zip"))
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    30
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    31
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    32
  /* build hugo */
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    33
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    34
  val default_url = "https://github.com/gohugoio/hugo/releases/download"
82030
bae948b1132b clarified platforms;
Fabian Huch <huch@in.tum.de>
parents: 79975
diff changeset
    35
  val default_version = "0.142.0"
78746
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    36
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    37
  def build_hugo(
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    38
    base_url: String = default_url,
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    39
    version: String = default_version,
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    40
    target_dir: Path = Path.current,
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    41
    progress: Progress = new Progress
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    42
  ): Unit = {
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    43
    /* component */
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    44
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    45
    val component = "hugo-" + version
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    46
    val component_dir =
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    47
      Components.Directory(target_dir + Path.basic(component)).create(progress = progress)
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    48
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    49
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    50
    /* download */
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    51
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    52
    for (platform <- platforms) {
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    53
      val platform_dir =
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    54
        Isabelle_System.make_directory(component_dir.path + Path.basic(platform.platform_name))
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    55
79975
90f4319c8b4f misc tuning;
wenzelm
parents: 79974
diff changeset
    56
      val download = platform.download(base_url, version)
90f4319c8b4f misc tuning;
wenzelm
parents: 79974
diff changeset
    57
      val name =
90f4319c8b4f misc tuning;
wenzelm
parents: 79974
diff changeset
    58
        Url.get_base_name(download) getOrElse
90f4319c8b4f misc tuning;
wenzelm
parents: 79974
diff changeset
    59
          error("Malformed download name " + quote(download))
78746
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    60
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    61
      val exe = Path.basic("hugo").exe_if(platform.is_windows)
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    62
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    63
      Isabelle_System.with_tmp_dir("download", component_dir.path.file) { download_dir =>
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    64
        Isabelle_System.with_tmp_dir("tmp", component_dir.path.file) { tmp_dir =>
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    65
          val archive_file = download_dir + Path.basic(name)
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    66
79975
90f4319c8b4f misc tuning;
wenzelm
parents: 79974
diff changeset
    67
          Isabelle_System.download_file(download, archive_file, progress = progress)
78746
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    68
          Isabelle_System.extract(archive_file, tmp_dir)
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    69
          Isabelle_System.move_file(tmp_dir + exe, platform_dir)
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    70
          Isabelle_System.move_file(tmp_dir + Path.basic("LICENSE"), component_dir.LICENSE)
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    71
          File.set_executable(platform_dir + exe)
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    72
        }
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    73
      }
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    74
    }
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    75
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    76
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    77
    /* settings */
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    78
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    79
    component_dir.write_settings("""
79974
f0150bc6fea5 just one copy of darwin-universal.tar.gz;
wenzelm
parents: 79629
diff changeset
    80
ISABELLE_HUGO="$COMPONENT/${ISABELLE_WINDOWS_PLATFORM64:-$ISABELLE_PLATFORM64}"
78746
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    81
""")
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    82
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    83
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    84
    /* README */
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    85
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    86
    File.write(component_dir.README,
79975
90f4319c8b4f misc tuning;
wenzelm
parents: 79974
diff changeset
    87
      """This Isabelle components provides hugo extended """ + version + """.
78746
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    88
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    89
See also https://gohugo.io and executables from """ + base_url + """
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    90
79975
90f4319c8b4f misc tuning;
wenzelm
parents: 79974
diff changeset
    91
        Fabian Huch
78746
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    92
        """ + Date.Format.date(Date.now()) + "\n")
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    93
  }
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    94
79629
4d81c0391da2 tuned comments;
wenzelm
parents: 78746
diff changeset
    95
4d81c0391da2 tuned comments;
wenzelm
parents: 78746
diff changeset
    96
  /* Isabelle tool wrapper */
4d81c0391da2 tuned comments;
wenzelm
parents: 78746
diff changeset
    97
78746
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    98
  val isabelle_tool =
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    99
    Isabelle_Tool("component_hugo", "build hugo component", Scala_Project.here,
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   100
      { args =>
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   101
        var target_dir = Path.current
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   102
        var base_url = default_url
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   103
        var version = default_version
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   104
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   105
        val getopts = Getopts("""
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   106
Usage: isabelle component_hugo [OPTIONS]
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   107
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   108
  Options are:
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   109
    -D DIR       target directory (default ".")
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   110
    -U URL       download URL (default: """" + default_url + """")
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   111
    -V VERSION   version (default: """" + default_version + """")
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   112
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   113
  Build extended hugo component.
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   114
""",
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   115
          "D:" -> (arg => target_dir = Path.explode(arg)),
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   116
          "U:" -> (arg => base_url = arg),
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   117
          "V:" -> (arg => version = arg))
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   118
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   119
        val more_args = getopts(args)
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   120
        if (more_args.nonEmpty) getopts.usage()
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   121
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   122
        val progress = new Console_Progress()
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   123
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   124
        build_hugo(base_url = base_url, version = version, target_dir = target_dir,
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   125
          progress = progress)
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   126
    })
a748a244a028 add component build tool for hugo from afp-devel;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   127
}