PageRenderTime 29ms CodeModel.GetById 20ms app.highlight 5ms RepoModel.GetById 2ms app.codeStats 0ms

/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 */
18package org.fusesource.scalate
19package tool.commands
20
21import java.{util => ju, lang => jl}
22import java.io.File
23import collection.JavaConversions._
24import org.apache.felix.gogo.commands.{Action, Option => option, Argument => argument, Command => command}
25import org.apache.felix.service.command.CommandSession
26
27
28/**
29 * The 'scalate run' sub command.
30 *
31 * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
32 */
33@command(scope = "scalate", name = "run", description = "Renders a Scalate template file")
34class Run extends Action {
35
36  @argument(required = true, name = "template", description = "Template file to render")
37  var template: File = _
38
39  @argument(index = 1, multiValued = true, name = "args", description = "Arguments to the template")
40  var args: ju.List[String] = new ju.ArrayList[String]
41
42  @option(name = "--workdir", description = "Sets the work directory where scalate generates class files to. Defaults to a temporary directory.")
43  var workdir: File = _
44
45  def execute(session: CommandSession): AnyRef = {
46    try {
47      val engine = new TemplateEngine
48      if (workdir != null) {
49        engine.workingDirectory = workdir
50      }
51
52      val attributes = Map("args" -> args.toList)
53      engine.layout(TemplateSource.fromFile(template), attributes)
54    } catch {
55      case e: Exception =>
56        "Error: Could not render: " + template + ". Due to: " + e
57    }
58  }
59}