alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Play Framework/Scala example source code file (preCommitHook)

This example Play Framework source code file (preCommitHook) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Play Framework (and Scala) source code examples by using tags.

All credit for the original source code belongs to Play Framework; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Play Framework tags/keywords

file, no, play, process, processlogger, running, scalariform, some, string, system, the

The preCommitHook Play Framework example source code

#!/bin/sh
exec scala -savecompiled "$0" $@
!#
//
// Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
//

// This script will check that the commit is correctly formatted.  It only checks files that are to be committed.

// To use it as a pre commit hook, run ln -s ../../framework/scripts/preCommitHook .git/hooks/pre-commit

import scala.sys.process._
import java.io.File

def color(color: String, msg: String) = color + msg + Console.RESET
def log(level: String, msg: String) = println("[" + level + "] " + msg)
def info(msg: String) = log("info", msg)
def error(msg: String) = log(color(Console.RED, "error"), msg)
def success(msg: String) = log(color(Console.GREEN, "success"), msg)

info("Running Play pre commit hook...")

// Get a list of all Scala source files in the main project that are staged to be committed

def status = "git status -s".!!.split("\r?\n").toSeq

val stagedFiles = status
  .filter(_.headOption.map(_ != ' ').getOrElse(false))
  .map(_.drop(3).takeWhile(_ != ' '))
  .filter(_.startsWith("framework/src"))
  .filter(_.endsWith(".scala"))

if (stagedFiles.isEmpty) {
  info("No staged scala files in main project to check")
} else {
  // Run scalariform and see if anything changes
  val rt = Process(Seq("./build", "scalariformFormat"), Some(new File("framework"))) ! ProcessLogger { s =>
    // Ignore final success line
    if (!s.contains("success")) {
      println(s)
    }
  }
  if (rt > 0) {
    System.exit(rt)
  }

  // Get a list of all the unstaged changes
  val unstagedChangedFiles = status
    .filter(_.charAt(1) != ' ')
    .map(_.drop(3).takeWhile(_ != ' ')).toSet

  val changedStagedFiles = stagedFiles.filter(unstagedChangedFiles)

  if (!changedStagedFiles.isEmpty) {
    error("Scalariform check failed")
    error("The following staged files were modified after running scalariform:")
    changedStagedFiles.foreach(f => println("  " + f))
    System.exit(1)
  }
}

success("Pre-commit hook passed")
System.exit(0)

Other Play Framework source code examples

Here is a short list of links related to this Play Framework preCommitHook source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.