| 75105 |      1 | /*  Title:      Pure/Admin/build_pdfjs.scala
 | 
|  |      2 |     Author:     Makarius
 | 
|  |      3 | 
 | 
|  |      4 | Build Isabelle component for Mozilla PDF.js.
 | 
|  |      5 | 
 | 
|  |      6 | See also:
 | 
|  |      7 | 
 | 
|  |      8 |   - https://github.com/mozilla/pdf.js
 | 
|  |      9 |   - https://github.com/mozilla/pdf.js/releases
 | 
|  |     10 |   - https://github.com/mozilla/pdf.js/wiki/Setup-PDF.js-in-a-website
 | 
|  |     11 | */
 | 
|  |     12 | 
 | 
|  |     13 | package isabelle
 | 
|  |     14 | 
 | 
|  |     15 | 
 | 
| 75393 |     16 | object Build_PDFjs {
 | 
| 75105 |     17 |   /* build pdfjs component */
 | 
|  |     18 | 
 | 
|  |     19 |   val default_url = "https://github.com/mozilla/pdf.js/releases/download"
 | 
| 75706 |     20 |   val default_version = "2.14.305"
 | 
| 75105 |     21 | 
 | 
|  |     22 |   def build_pdfjs(
 | 
|  |     23 |     base_url: String = default_url,
 | 
|  |     24 |     version: String = default_version,
 | 
|  |     25 |     target_dir: Path = Path.current,
 | 
| 75393 |     26 |     progress: Progress = new Progress
 | 
|  |     27 |   ): Unit = {
 | 
| 75105 |     28 |     Isabelle_System.require_command("unzip", test = "-h")
 | 
|  |     29 | 
 | 
|  |     30 | 
 | 
|  |     31 |     /* component name */
 | 
|  |     32 | 
 | 
|  |     33 |     val component = "pdfjs-" + version
 | 
|  |     34 |     val component_dir = Isabelle_System.new_directory(target_dir + Path.basic(component))
 | 
|  |     35 |     progress.echo("Component " + component_dir)
 | 
|  |     36 | 
 | 
|  |     37 | 
 | 
|  |     38 |     /* download */
 | 
|  |     39 | 
 | 
|  |     40 |     val download_url = base_url + "/v" + version
 | 
| 75394 |     41 |     Isabelle_System.with_tmp_file("archive", ext = "zip") { archive_file =>
 | 
| 75706 |     42 |       Isabelle_System.download_file(download_url + "/pdfjs-" + version + "-legacy-dist.zip",
 | 
| 75105 |     43 |         archive_file, progress = progress)
 | 
|  |     44 |       Isabelle_System.bash("unzip -x " + File.bash_path(archive_file),
 | 
|  |     45 |         cwd = component_dir.file).check
 | 
| 75394 |     46 |     }
 | 
| 75105 |     47 | 
 | 
|  |     48 | 
 | 
|  |     49 |     /* settings */
 | 
|  |     50 | 
 | 
|  |     51 |     val etc_dir = Isabelle_System.make_directory(component_dir + Path.basic("etc"))
 | 
|  |     52 |     File.write(etc_dir + Path.basic("settings"),
 | 
|  |     53 |       """# -*- shell-script -*- :mode=shellscript:
 | 
|  |     54 | 
 | 
|  |     55 | ISABELLE_PDFJS_HOME="$COMPONENT"
 | 
|  |     56 | """)
 | 
|  |     57 | 
 | 
|  |     58 | 
 | 
|  |     59 |     /* README */
 | 
|  |     60 | 
 | 
|  |     61 |     File.write(component_dir + Path.basic("README"),
 | 
|  |     62 |       """This is PDF.js from
 | 
|  |     63 | """ + download_url + """
 | 
|  |     64 | 
 | 
|  |     65 | 
 | 
|  |     66 |         Makarius
 | 
|  |     67 |         """ + Date.Format.date(Date.now()) + "\n")
 | 
|  |     68 |   }
 | 
|  |     69 | 
 | 
|  |     70 | 
 | 
|  |     71 |   /* Isabelle tool wrapper */
 | 
|  |     72 | 
 | 
|  |     73 |   val isabelle_tool =
 | 
|  |     74 |     Isabelle_Tool("build_pdfjs", "build component for Mozilla PDF.js",
 | 
| 75394 |     75 |       Scala_Project.here,
 | 
|  |     76 |       { args =>
 | 
|  |     77 |         var target_dir = Path.current
 | 
|  |     78 |         var base_url = default_url
 | 
|  |     79 |         var version = default_version
 | 
| 75105 |     80 | 
 | 
| 75394 |     81 |         val getopts = Getopts("""
 | 
| 75105 |     82 | Usage: isabelle build_pdfjs [OPTIONS]
 | 
|  |     83 | 
 | 
|  |     84 |   Options are:
 | 
|  |     85 |     -D DIR       target directory (default ".")
 | 
|  |     86 |     -U URL       download URL (default: """" + default_url + """")
 | 
|  |     87 |     -V VERSION   version (default: """" + default_version + """")
 | 
|  |     88 | 
 | 
|  |     89 |   Build component for PDF.js.
 | 
|  |     90 | """,
 | 
| 75394 |     91 |           "D:" -> (arg => target_dir = Path.explode(arg)),
 | 
|  |     92 |           "U:" -> (arg => base_url = arg),
 | 
|  |     93 |           "V:" -> (arg => version = arg))
 | 
| 75105 |     94 | 
 | 
| 75394 |     95 |         val more_args = getopts(args)
 | 
|  |     96 |         if (more_args.nonEmpty) getopts.usage()
 | 
| 75105 |     97 | 
 | 
| 75394 |     98 |         val progress = new Console_Progress()
 | 
| 75105 |     99 | 
 | 
| 75394 |    100 |         build_pdfjs(base_url = base_url, version = version, target_dir = target_dir,
 | 
|  |    101 |           progress = progress)
 | 
|  |    102 |       })
 | 
| 75105 |    103 | }
 |