Skip to content

Commit

Permalink
Add generate-redirect command
Browse files Browse the repository at this point in the history
  • Loading branch information
Ostrzyciel committed May 15, 2024
1 parent df6cf9b commit d79ca65
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/main/resources/redirect_template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Redirecting</title>
<noscript>
<meta http-equiv="refresh" content="1; url={{href}}" />
</noscript>
<script>
window.location.replace(
"{{href}}" + window.location.search + window.location.hash
);
</script>
</head>
<body>
Redirecting to <a href="{{href}}">{{href}}</a>...
</body>
</html>
1 change: 1 addition & 0 deletions src/main/scala/commands/Command.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ object Command:
val commands: Seq[Command] = Seq(
CategoryDocGenCommand,
DatasetDocGenCommand,
GenerateRedirectCommand,
HelpCommand,
MainDocGenCommand,
MergeMetadataCommand,
Expand Down
48 changes: 48 additions & 0 deletions src/main/scala/commands/GenerateRedirectCommand.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.github.riverbench.ci_worker
package commands

import java.lang.module.ModuleDescriptor.Version
import java.nio.file.{FileSystems, Files, Path}
import scala.concurrent.Future

object GenerateRedirectCommand extends Command:
override def name: String = "generate-redirect"

override def description: String = "Generates or updates a redirect for a dataset or schema PURL\n" +
"Args: <doc repo dir (gh-pages branch)> <kind of redirected entity> <name of entity> <version of entity>"

override def validateArgs(args: Array[String]): Boolean = args.length == 5

override def run(args: Array[String]): Future[Unit] = Future {
val repoDir = FileSystems.getDefault.getPath(args(1))
val kind = args(2)
val name = args(3)
val version = args(4)

if kind != "datasets" && kind != "schema" then
println("The 'kind' parameter must be either 'datasets' or 'schema'.")
throw new IllegalArgumentException()

val redirectTemplate = Files.readString(
Path.of(this.getClass.getClassLoader.getResource("redirect_template.html").toURI)
)

// Get the version of RiverBench we should point to
val rbVersion = if version == "dev" then "dev" else
repoDir.resolve("v").toFile.listFiles()
.filter(_.isDirectory)
.filter(_.getName.matches("[0-9.]+"))
.sortBy(f => Version.parse(f.getName))
.reverse
.head
.getName

val redirectDir = repoDir.resolve(kind).resolve(name).resolve(version)
redirectDir.toFile.mkdirs()
Files.writeString(
redirectDir.resolve("index.html"),
redirectTemplate.replace("{{href}}", f"/v/$rbVersion/$kind/$name/")
)

println("Redirect added.")
}
1 change: 0 additions & 1 deletion src/main/scala/commands/NavGenCommand.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package commands
import util.YamlDocBuilder
import util.YamlDocBuilder.*

import java.lang.module.ModuleDescriptor.Version
import java.nio.file.{FileSystems, Files, Path}
import scala.concurrent.Future

Expand Down

0 comments on commit d79ca65

Please sign in to comment.