Skip to content

Instantly share code, notes, and snippets.

@balopat
Created July 1, 2014 04:08
Show Gist options
  • Save balopat/5464ae31cbde314ac1c8 to your computer and use it in GitHub Desktop.
Save balopat/5464ae31cbde314ac1c8 to your computer and use it in GitHub Desktop.
sb-repl
object sb {
import java.net._
import java.util._
import scala.sys.process.Process
import scala.io._
// constantsp
private val recorderUrl = "http://recorder.samebug.io"
private val uiUrl = "http://beta.samebug.io"
private val notifierUrl = new URL("https://oss.sonatype.org/service/local/repositories/snapshots/content/com/samebug/notifier/samebug-notifier/2.0.0-SNAPSHOT/samebug-notifier-2.0.0-20140620.093402-6.jar")
private val apiKey = "29c9352c-e807-4b0e-995a-9c68f5831945"
private val os = System.getProperty("os.name").toLowerCase
val openBrowser =
if (os contains "mac") "open"
else if (os contains "win") "cmd /k start"
else if ((os contains "nix") || (os contains "nux") || (os contains "aix")) "xdg-open"
// reflection
private val classLoader = new URLClassLoader(Array(notifierUrl))
private val notifierClass = classLoader.loadClass("com.samebug.notifier.SamebugNotifier")
private val notifierConstructor = notifierClass.getConstructor(classOf[java.lang.String])
private val getConfig = notifierClass.getMethod("getConfiguration")
private val notifySamebug = notifierClass.getMethod("notify", classOf[java.lang.String], classOf[java.lang.Throwable])
private val configClass = classLoader.loadClass("com.samebug.notifier.Configuration")
private val responseClass = classLoader.loadClass("com.samebug.notifier.IResponse")
private val getSolutionId = responseClass.getMethod("getSolutionId")
private val getBugId = responseClass.getMethod("getBugId")
// init notifier
private val notifier = notifierConstructor.newInstance(apiKey)
private def openUrl(url: String) {
Process(s"${openBrowser} ${url}").run
}
private def getUrl(address: String): Seq[String] = {
val url = new URL(address)
val in = url.openStream
val src = new BufferedSource(in).getLines
val lines = src.toList
in.close
lines
}
def help(exception: Throwable) = {
val resp = notifySamebug.invoke(notifier, "scala repl help", exception)
last = Some((getBugId.invoke(resp).asInstanceOf[UUID], Option(getSolutionId.invoke(resp).asInstanceOf[UUID])))
last match {
case None => println("Samebug server did not return a solution id. Please contact us at help@samebug.io")
case Some((bId, None)) => println("The last exception is internal, only you can fix it")
case Some((bId, Some(pId))) => {
val lines = getUrl(s"${uiUrl}/rest/solutions/${pId}/top_rated")
if (lines.size < 2) {
println("Samebug does not have a solution for this problem yet.\nEnter sb.support to be the first one and we promote your nick with the solution for other users.")
} else {
println(s"@${lines.head} says")
lines.tail foreach println
println("\nIf you need more help or want to contribute, enter sb.support to open the support page of this bug")
}
}
}
}
def support = last match {
case None => println("Please try to get help first: \nsb help lastException")
case Some((bId, None)) => println("The last exception is internal, only you can fix it")
case Some((bId, Some(pId))) => openUrl(s"${uiUrl}/solutions/${pId}")
}
private var last: Option[(UUID, Option[UUID])] = None
}
println("""
###########################################################
# Welcome to Samebug Scala REPL demo!
# Use the REPL as usual and if you get an exception, type
#
# sb help lastException
#
# Good luck!
###########################################################""")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment