Chapter 10. Best practices

Table of Contents

Build scripts are programs
Use EntityFS file systems
Set a temporary directory

This chapter contains some best practices, based on experiences from real-world projects where Schmant is used.

Treat build scripts just like any other program you write:

Use EntityFS file systems to store build files in. Then the build script and the tasks it uses can use EntityFS utility methods for working with files and directories.

If building from a local workspace, use a read only file system for the source files and a read/write file system for storing build artifacts and temporary build files.

Example 10.1. Creating file systems for the build script

Groovy

import java.io.File import org.entityfs.util.Directories import org.entityfs.util.filter.entity.EntityNameFilter import org.schmant.support.entityfs.SchmantFileSystems import org.schmant.support.io.TempFileUtil // Create a read only file system with its root directory in // /home/me/myproject/src srcd = SchmantFileSystems.getEntityForDirectory( new File("/home/me/myproject/src"), true) // Get a view of the root directory that hides all .svn directories. When this // view is used for getting child directories, all .svn directories will be // hidden in them too, making all .svn directories in the file system invisible // for all EntityFS-aware tasks. // The ~ is used to negate the filter. srcRoot = srcd.newView(~(new EntityNameFilter(".svn"))) // Create a read/write file system in a temporary directory to use for putting // built files in. Keep this directory when the script is done (set keep flag to // true). targetRoot = TempFileUtil.createTempDirectory( "myproject", null, true) // Set a temporary directory on the target file system. Tasks that need to // create temporary files use this directory for them. targetRoot.fileSystem.setTemporaryFilesDirectory( Directories.newDirectory(targetRoot, "tmp"))

JavaScript

// Create a read only file system with its root directory in // /home/me/myproject/src srcd = SchmantFileSystems.getEntityForDirectory( new File("/home/me/myproject/src"), true); // Get a view of the root directory that hides all .svn directories. When this // view is used for getting child directories, all .svn directories will be // hidden in them too, making all .svn directories in the file system invisible // for all EntityFS-aware tasks. srcRoot = srcd.newView(new EntityNameFilter(".svn").not()); // Create a read/write file system in a temporary directory to use for putting // built files in. Keep this directory when the script is done (set keep flag to // true). targetRoot = TempFileUtil.createTempDirectory( "myproject", null, true); // Set a temporary directory on the target file system. Tasks that need to // create temporary files use this directory for them. targetRoot.getFileSystem().setTemporaryFilesDirectory( Directories.newDirectory(targetRoot, "tmp"));

JRuby

# Create a read only file system with its root directory in # /home/me/myproject/src srcd = Schmant::SchmantFileSystems.getEntityForDirectory( Java::JavaIo::File.new("/home/me/myproject/src"), true) # Get a view of the root directory that hides all .svn directories. When this # view is used for getting child directories, all .svn directories will be # hidden in them too, making all .svn directories in the file system invisible # for all EntityFS-aware tasks. srcRoot = srcd.newView(Schmant::EntityNameFilter.new(".svn").not) # Create a read/write file system in a temporary directory to use for putting # built files in. Keep this directory when the script is done (set keep flag to # true). targetRoot = Schmant::TempFileUtil.createTempDirectory( "myproject", nil, true) # Set a temporary directory on the target file system. Tasks that need to # create temporary files use this directory for them. targetRoot.fileSystem.setTemporaryFilesDirectory( Schmant::Directories.newDirectory(targetRoot, "tmp"))

Jython

# Create a read only file system with its root directory in # /home/me/myproject/src srcd = SchmantFileSystems.getEntityForDirectory( \ File("/home/me/myproject/src"), True) # Get a view of the root directory that hides all .svn directories. When this # view is used for getting child directories, all .svn directories will be # hidden in them too, making all .svn directories in the file system invisible # for all EntityFS-aware tasks. srcRoot = srcd.newView(EntityNameFilter(".svn").not()) # Create a read/write file system in a temporary directory to use for putting # built files in. Keep this directory when the script is done (set keep flag to # true). targetRoot = TempFileUtil.createTempDirectory( \ "myproject", None, True) # Set a temporary directory on the target file system. Tasks that need to # create temporary files use this directory for them. targetRoot.getFileSystem().setTemporaryFilesDirectory( \ Directories.newDirectory(targetRoot, "tmp"))

Set a temporary directory on the target file system. Otherwise temporary files often end up in java.io.tmpdir. On Linux, using the memory-backed /dev/shm directory can give a nice speed boost.