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

ends.giter8.giter8-cli-git_2.12.0.17.0.source-code.Runner.scala Maven / Gradle / Ivy

The newest version!
/*
 * Original implementation (C) 2010-2015 Nathan Hamblen and contributors
 * Adapted and extended in 2016 by foundweekends project
 *
 * 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 giter8

import java.io.File

import scopt.OptionParser
import org.apache.commons.io.FileUtils
import scala.util.{Failure, Success}

class Runner {
  java.util.logging.Logger.getLogger("").setLevel(java.util.logging.Level.SEVERE)

  val Param = """(?s)^--(\S+)=(.+)$""".r

  /** Parses command line argument, clones the template, and runs the processor.
    */
  def run(args: Array[String], workingDirectory: File, processor: Processor): Int = {
    def clone(repo: GitRepository, ref: Option[Ref], tempdir: File, knownHosts: Option[String]): Either[String, File] =
      new Git(new JGitInteractor(knownHosts)).clone(repo, ref, tempdir) match {
        case Success(_) => Right(tempdir)
        case Failure(e) => Left(e.getMessage)
      }

    val result: Either[String, String] = (args.partition { s => Param.pattern.matcher(s).matches } match {
      case (params, options) =>
        val tempdir = new File(FileUtils.getTempDirectory, "giter8-" + System.nanoTime)
        try {
          for {
            config   <- parser.parse(options, Config("")).map(Right(_)).getOrElse(Left(""))
            repo     <- GitRepository.fromString(config.repo)
            cloneDir <- clone(repo, config.ref, tempdir, config.knownHosts)
            templateDir     = new File(cloneDir, config.directory.getOrElse(""))
            outputDirectory = config.out.map(new File(_))
            p <- processor.process(templateDir, workingDirectory, params, config.forceOverwrite, outputDirectory)
          } yield p
        } finally {
          if (tempdir.exists) FileUtils.forceDelete(tempdir)
        }
      case _ => Left(parser.usage)
    })
    result match {
      case Left(error) =>
        if (error != "") {
          Console.err.println(s"\n$error\n")
        }
        1
      case Right(message) =>
        if (message != "") {
          Console.out.println(s"\n$message\n")
        }
        0
    }
  }

  def run(args: Array[String], processor: Processor): Int = run(args, new File(".").getAbsoluteFile, processor)

  val parser: OptionParser[Config] = new scopt.OptionParser[Config]("g8") {

    head("g8", giter8.BuildInfo.version)

    arg[String]("