Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Reproducible portable out folder contents #4065

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions integration/feature/reproducibility/resources/build.mill
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package build
import mill._, scalalib._, scalajslib._

trait AppScalaModule extends ScalaModule {
def scalaVersion = "3.3.3"
}

trait AppScalaJSModule extends AppScalaModule with ScalaJSModule {
def scalaJSVersion = "1.16.0"
}

object `package` extends RootModule with AppScalaModule {
def moduleDeps = Seq(shared.jvm)
def ivyDeps = Agg(ivy"com.lihaoyi::cask:0.9.1")

def resources = Task {
os.makeDir(Task.dest / "webapp")
val jsPath = client.fastLinkJS().dest.path
os.copy(jsPath / "main.js", Task.dest / "webapp/main.js")
os.copy(jsPath / "main.js.map", Task.dest / "webapp/main.js.map")
super.resources() ++ Seq(PathRef(Task.dest))
}

object test extends ScalaTests with TestModule.Utest {

def ivyDeps = Agg(
ivy"com.lihaoyi::utest::0.8.4",
ivy"com.lihaoyi::requests::0.6.9"
)
}

object shared extends Module {
trait SharedModule extends AppScalaModule with PlatformScalaModule {
def ivyDeps = Agg(
ivy"com.lihaoyi::scalatags::0.12.0",
ivy"com.lihaoyi::upickle::3.0.0"
)
}

object jvm extends SharedModule
object js extends SharedModule with AppScalaJSModule
}

object client extends AppScalaJSModule {
def moduleDeps = Seq(shared.js)
def ivyDeps = Agg(ivy"org.scala-js::scalajs-dom::2.2.0")
}
}

// A Scala-JVM backend server wired up with a Scala.js front-end, with a
// `shared` module containing code that is used in both client and server.
// Rather than the server sending HTML for the initial page load and HTML for
// page updates, it sends HTML for the initial load and JSON for page updates
// which is then rendered into HTML on the client.
//
// The JSON serialization logic and HTML generation logic in the `shared` module
// is shared between client and server, and uses libraries like uPickle and
// Scalatags which work on both ScalaJVM and Scala.js. This allows us to freely
// move code between the client and server, without worrying about what
// platform or language the code was originally implemented in.
//
// This is a minimal example of shared code compiled to ScalaJVM and Scala.js,
// running on both client and server, meant for illustrating the build
// configuration. A full exploration of client-server code sharing techniques
// is beyond the scope of this example.

/** Usage

> ./mill test
+ webapp.WebAppTests.simpleRequest ...

> ./mill runBackground

> curl http://localhost:8083
...What needs to be done...
...

> curl http://localhost:8083/static/main.js
...Scala.js...
...

> ./mill clean runBackground

*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package client
import org.scalajs.dom
import shared.{Todo, Shared}
object ClientApp {
var state = "all"
var todoApp = dom.document.getElementsByClassName("todoapp")(0)

def postFetchUpdate(url: String) = {
dom.fetch(
url,
new dom.RequestInit {
method = dom.HttpMethod.POST
}
).`then`[String](response => response.text())
.`then`[Unit] { text =>
todoApp.innerHTML = Shared
.renderBody(upickle.default.read[Seq[Todo]](text), state)
.render

initListeners()
}
}

def bindEvent(cls: String, url: String, endState: Option[String]) = {
dom.document.getElementsByClassName(cls)(0).addEventListener(
"mousedown",
(evt: dom.Event) => {
postFetchUpdate(url)
endState.foreach(state = _)
}
)
}

def bindIndexedEvent(cls: String, func: String => String) = {
for (elem <- dom.document.getElementsByClassName(cls)) {
elem.addEventListener(
"mousedown",
(evt: dom.Event) => postFetchUpdate(func(elem.getAttribute("data-todo-index")))
)
}
}

def initListeners(): Unit = {
bindIndexedEvent("destroy", index => s"/delete/$state/$index")
bindIndexedEvent("toggle", index => s"/toggle/$state/$index")
bindEvent("toggle-all", s"/toggle-all/$state", None)
bindEvent("todo-all", s"/list/all", Some("all"))
bindEvent("todo-active", s"/list/all", Some("active"))
bindEvent("todo-completed", s"/list/completed", Some("completed"))
bindEvent("clear-completed", s"/clear-completed/$state", None)

val newTodoInput =
dom.document.getElementsByClassName("new-todo")(0).asInstanceOf[dom.HTMLInputElement]
newTodoInput.addEventListener(
"keydown",
(evt: dom.KeyboardEvent) => {
if (evt.keyCode == 13) {
dom.fetch(
s"/add/$state",
new dom.RequestInit {
method = dom.HttpMethod.POST
body = newTodoInput.value
}
).`then`[String](response => response.text())
.`then`[Unit] { text =>
newTodoInput.value = ""

todoApp.innerHTML = Shared
.renderBody(upickle.default.read[Seq[Todo]](text), state)
.render

initListeners()
}
}
}
)
}

def main(args: Array[String]): Unit = initListeners()
}
Loading
Loading