Permalink
Browse files

Open on upload

Add functionality to open the CloudBees app in the default web browser
upon successfully uploading it. This is enabled by default and can be
changed by setting the cloudbees-open-on-upload Key to false. There is
also a cloudbees-open Task to open the app at any time.
  • Loading branch information...
1 parent 22f8dd7 commit 22778d91f58124164f1cd0a11940ddba9e85e781 @pr1001 pr1001 committed Mar 10, 2012
Showing with 28 additions and 3 deletions.
  1. +28 −3 src/main/scala/cloudbees.scala
@@ -18,6 +18,7 @@ object Plugin extends Plugin {
// settings
val host = SettingKey[String]("cloudbees-host", "Host URL of the CloudBees API")
val useDeltaWar = SettingKey[Boolean]("cloudbees-use-delta-war", "Deploy only a delta-WAR to CloudBees (default: true)")
+ val openOnUpload = SettingKey[Boolean]("cloudbees-open-on-upload", "Open the application in your default web browser after upload (default: true)")
val username = SettingKey[Option[String]]("cloudbees-username", "Your CloudBees username")
val apiKey = SettingKey[Option[String]]("cloudbees-api-key", "Your CloudBees API key")
val apiSecret = SettingKey[Option[String]]("cloudbees-api-secrect", "Your CloudBees API secret")
@@ -26,6 +27,7 @@ object Plugin extends Plugin {
// tasks
val applications = TaskKey[Unit]("cloudbees-applications")
val deploy = TaskKey[Unit]("cloudbees-deploy")
+ val open = TaskKey[Unit]("cloudbees-open", "Open the application in your default web browser")
}
import com.github.siasia.WarPlugin._
@@ -34,13 +36,15 @@ object Plugin extends Plugin {
val cloudBeesSettings: Seq[Setting[_]] = Seq(
host := "api.cloudbees.com",
useDeltaWar := true,
+ openOnUpload := true,
username := None,
apiKey := None,
apiSecret := None,
applicationId := None,
client <<= (host, apiKey, apiSecret)(Client),
applications <<= applicationsTask,
- deploy <<= deployTask
+ deploy <<= deployTask,
+ open <<= openTask
) ++ warSettings
import scala.collection.JavaConverters._
@@ -50,15 +54,24 @@ object Plugin extends Plugin {
client().applicationList.getApplications.asScala.foreach(
a => s.log.info("+ %s - %s".format(a.getTitle, a.getUrls.head)))
}
- def deployTask = ((packageWar in Compile), client, username, applicationId, useDeltaWar, streams) map {
- (war, client, user, app, delta, s) =>
+ def deployTask = ((packageWar in Compile), client, username, applicationId, useDeltaWar, openOnUpload, streams) map {
+ (war, client, user, app, delta, open, s) =>
if (war.exists) {
val to = targetAppId(require(user, username), require(app, applicationId))
s.log.info("Deploying application '%s' to Run@Cloud".format(to))
val result = client().applicationDeployWar(to, null, null, war.asFile.getAbsolutePath, null, true, new HashWriteProgress)
s.log.info("Application avalible at %s".format(result.getUrl))
+ if (open) {
+ openApp(result.getUrl)
+ }
} else sys.error("There was a problem locating the WAR file for this project")
}
+ // can't figure out a way to do this without mapping
+ def openTask = (username, applicationId) map { (user, app) => for {
+ u <- user
+ a <- app
+ } openApp("http://" + a + "." + u + ".cloudbees.net")
+ }
/***** internal *****/
private def targetAppId(username: String, appId: String) = appId.split("/").toList match {
@@ -69,4 +82,16 @@ object Plugin extends Plugin {
value.getOrElse {
sys.error("%s setting is required".format(setting.key.label))
}
+ private def openApp(url: String) {
+ // VERY roughly taken from the Ruby Launchy library
+ val command = System.getProperty("os.name") match {
+ case os if os.toLowerCase contains "os x" => "open"
+ case os if os.toLowerCase contains "windows" => "start /b"
+ case os if os.toLowerCase contains "cygwin" => "cmd /C start /b"
+ case _ => "xdg-open"
+ }
+ val str = command + " " + url + ""
+ Process.stringToProcess(str).run
+ // don't destroy the Process after running since that seems to prevent it from running at all
+ }
}

0 comments on commit 22778d9

Please sign in to comment.