/scalate-tool/src/main/scala/org/fusesource/scalate/tool/commands/Run.scala

http://github.com/scalate/scalate · Scala · 59 lines · 29 code · 8 blank · 22 comment · 2 complexity · 8eb44d789625d0372f3b686050fa7181 MD5 · raw file

  1. /**
  2. * Copyright (C) 2009-2011 the original author or authors.
  3. * See the notice.md file distributed with this work for additional
  4. * information regarding copyright ownership.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.fusesource.scalate
  19. package tool.commands
  20. import java.{util => ju, lang => jl}
  21. import java.io.File
  22. import collection.JavaConversions._
  23. import org.apache.felix.gogo.commands.{Action, Option => option, Argument => argument, Command => command}
  24. import org.apache.felix.service.command.CommandSession
  25. /**
  26. * The 'scalate run' sub command.
  27. *
  28. * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
  29. */
  30. @command(scope = "scalate", name = "run", description = "Renders a Scalate template file")
  31. class Run extends Action {
  32. @argument(required = true, name = "template", description = "Template file to render")
  33. var template: File = _
  34. @argument(index = 1, multiValued = true, name = "args", description = "Arguments to the template")
  35. var args: ju.List[String] = new ju.ArrayList[String]
  36. @option(name = "--workdir", description = "Sets the work directory where scalate generates class files to. Defaults to a temporary directory.")
  37. var workdir: File = _
  38. def execute(session: CommandSession): AnyRef = {
  39. try {
  40. val engine = new TemplateEngine
  41. if (workdir != null) {
  42. engine.workingDirectory = workdir
  43. }
  44. val attributes = Map("args" -> args.toList)
  45. engine.layout(TemplateSource.fromFile(template), attributes)
  46. } catch {
  47. case e: Exception =>
  48. "Error: Could not render: " + template + ". Due to: " + e
  49. }
  50. }
  51. }