All Downloads are FREE. Search and download functionalities are using the official Maven repository.

1.dp.source-code.ng.scala Maven / Gradle / Ivy

The newest version!
/*
   Copyright 2010 Aaron J. Radke

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package cc.drx.ng

import cc.drx._  //get the drx work

import com.martiansoftware.nailgun.{NGServer,NGContext}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.annotation.tailrec

class Server extends NGServer //note: by using this NGServer the classpath will automatically include drx methods

/*
object Server extends NGSever{ //
 import com.martiansoftware.nailgun.Alias
 val SErve
 for(a <- List(
     //new Alias("cat", "cat", classOf[cc.drx.ng.Cat])
     new Alias("cat", "cat", Cat.getClass)
   )
 ) getAliasManager.addAlias(a)
}
*/

//TODO generalize the NgContext so these can all be wrapped up as regular apps as well

object Watch{
   def nailMain(ctx:NGContext):Unit = {
      val args = ctx.getArgs
      if(args.size > 2){
         val wd = File(ctx.getWorkingDirectory).canon
         val watchFile = wd / args(0)
         val glob = Glob(args(1).drop(1).dropRight(1))
         val cmd = args drop 2 mkString " "
         ctx.out.println(s"wd: $wd watch:$watchFile glob:$glob cmd:$cmd")
         val ws = watchFile.watch{f => 
            //ctx.out.println(s"$f modified at ${Date.now} checking glob match") //???
            if(glob matches f.name){
               val cmdInterpolated = cmd.replaceAllLiterally("$file",f.path)
               //ctx.out.println(s"$f modified at ${Date.now}") //???
               Timer(s"Shell run: $cmdInterpolated"){
                  for(lines <- Shell(cmdInterpolated, wd).lines){
                    lines foreach ctx.out.println
                  };
                  () //return a Unit
               }
            }
         }
         while(true){ Thread sleep 1000 } //don't end the app //TODO make it exit if watch service stops
      }
      else ctx.out.println("usage: Watch   ")
   }
}
object Draw{
   def nailMain(ctx:NGContext):Unit = {
      val args = ctx.getArgs
      if(args.size == 1){
         val wd = File(ctx.getWorkingDirectory).canon
         val watchFile = wd / args(0)
         p5.KsonDraw.main(Array(watchFile.path))
      }
   }
}

object Run{
   def nailMain(ctx:NGContext):Unit = {
      val args = ctx.getArgs
      val wd = File(ctx.getWorkingDirectory).canon
      val cmd = args mkString " "
      for(lines <- Shell(cmd, wd).lines) lines foreach ctx.out.println
      //TODO does the nail need to block if it keeps running for example in a future
   }
}

object Prop{
   lazy val defaultFile = (File.home/".bt.kson").canon orElse (File.home/".bt").canon
   def nailMain(ctx:NGContext):Unit = {
      val args = ctx.getArgs
      // val wd = File(ctx.getWorkingDirectory).canon
      val key = args.mkString(".")

      key match {
        case ""      => println("Error: command line arguments requires a key.")
        case "clear" => cc.drx.Prop.clear()
        case _       => println(cc.drx.Prop.of(defaultFile).getOrElse(key, "Error: key could not be found"))  //maybe make this a ~/.kson file
      }
   }
}

object XMLLint extends App{
   val xml = Input(System.in).cat
   val xmlParsed = scala.xml.XML.loadString(xml)
   val pretty = new scala.xml.PrettyPrinter(160,2)
   System.out.println(pretty format xmlParsed)
}
object Cat{
   def nailMain(ctx:NGContext):Unit = {
      val args = ctx.getArgs
      Input(ctx.in).lines foreach ctx.out.println //NOTE: this method will hang if there is no stdin
   }
}
object Info{
   def nailMain(ctx:NGContext):Unit = Timer("nail exec time"){
      val args = ctx.getArgs
      println("args:" + args.mkString(" ") )
      println("cwd:" + File(ctx.getWorkingDirectory).canon)
      println("system classpath:\n" + Classpath.system.nice)

      // println("stdin:") //NOTE: this method will hang if there is no stdin
      // IO(ctx.in).lines foreach ctx.out.println
   }
}
object Boot{
   def nailMain(ctx:NGContext):Unit = xsbt.boot.Boot.main(ctx.getArgs) //TODO if this test ever works that means boots can be forwarded so make it work nice, if it doesn't try the xsbt.boot.Launch methods
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy