Permalink
Browse files

Initial check-in

  • Loading branch information...
0 parents commit c6d270eb680724da70d637c79f8791efc8012477 @ray1729 committed Jul 21, 2011
Showing with 161 additions and 0 deletions.
  1. +88 −0 bin/dt
  2. +73 −0 build.pl
88 bin/dt
@@ -0,0 +1,88 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings FATAL => 'all';
+
+use Getopt::Long;
+
+sub LOG {
+ my ( $level, $mesg ) = @_;
+ printf STDERR "%s [%s] %s\n", scalar localtime, $level, $mesg;
+}
+
+sub WARN { LOG 'WARN', @_ }
+
+sub INFO { LOG 'INFO', @_ }
+
+sub ERROR { LOG 'ERROR', @_ };
+
+sub LOGDIE { ERROR @_; exit 2 };
+
+{
+ my %tasks;
+
+ sub deftask ($&) {
+ my ( $task, $thunk ) = @_;
+ LOGDIE "Invalid task name '$task' (may only contain alphanumeric, '_', '-', and '.')"
+ unless $task =~ m/^[\w\-.]+$/;
+ $tasks{$task}{thunk} = $thunk;
+ }
+
+ sub tasks {
+ sort keys %tasks;
+ }
+
+ sub runtask {
+ my $task = shift;
+ LOGDIE "No such task '$task'" unless exists $tasks{$task};
+ unless ( $tasks{$task}{has_run} ) {
+ INFO "Running $task";
+ $tasks{$task}{thunk}->();
+ $tasks{$task}{has_run} = 1;
+ }
+ }
+
+ sub depends {
+ my @dependencies = map { split ',' } @_;
+ runtask($_) for @dependencies;
+ }
+}
+
+sub shell {
+ my $script = shift;
+ open( my $pipe, '|bash -e -x' );
+ print $pipe $script;
+ close $pipe;
+ my $exit_code = $? >> 8;
+ LOGDIE "Shell script exited $exit_code"
+ if $exit_code != 0;
+}
+
+{
+ my $build_file = 'build.pl';
+ our $BASEDIR = '/tmp/foo';
+
+ GetOptions(
+ 'build-file=s' => \$build_file,
+ 'base-dir=s' => \$BASEDIR,
+ );
+
+ LOGDIE "$BASEDIR is not writable"
+ unless -w $BASEDIR;
+
+ $build_file = 'build.pl' unless defined $build_file;
+
+ {
+ my $rc = do $build_file;
+ LOGDIE "Couldn't parse $build_file" if $@;
+ LOGDIE "Couldn't do $build_file: $!" unless defined $rc;
+ LOGDIE "Couldn't run $build_file" unless $rc;
+ }
+
+ if ( @ARGV ) {
+ runtask $_ for @ARGV;
+ }
+ else {
+ print "Available tasks: \n", map( " $_\n", tasks() );
+ }
+}
@@ -0,0 +1,73 @@
+my $CPANM_URL = 'http://cpanmin.us';
+
+my $GIT_VERSION = '1.7.6';
+my $GIT_URL = "http://kernel.org/pub/software/scm/git/git-${GIT_VERSION}.tar.bz2";
+
+my $ZLIB_VERSION = '1.2.5';
+my $ZLIB_URL = "http://prdownloads.sourceforge.net/libpng/zlib-${ZLIB_VERSION}.tar.bz2?download";
+
+deftask 'base-dirs' => sub {
+ shell "mkdir -p $BASEDIR/{bin,lib,man,share,src}"
+};
+
+sub append_path {
+ my ( $var_name, $value ) = @_;
+ if ( $ENV{$var_name} ) {
+ $ENV{$var_name} = $value . ':' . $ENV{$var_name};
+ }
+ else {
+ $ENV{$var_name} = $value;
+ }
+}
+
+deftask 'setenv' => sub {
+ append_path PATH => "$BASEDIR/bin";
+ append_path LD_LIBRARY_PATH => "$BASEDIR/lib";
+ append_path MANPATH => "$BASEDIR/man";
+};
+
+deftask 'env' => sub {
+ depends "setenv";
+ shell 'env';
+};
+
+deftask 'cpanm' => sub {
+ depends "base-dirs,setenv";
+ my $cpanm = "$BASEDIR/bin/cpanm";
+ shell "wget -O $cpanm $CPANM_URL" unless -e $cpanm;
+ chmod 0755, "$cpanm" unless -x $cpanm;
+};
+
+deftask 'local-lib' => sub {
+ depends "cpanm";
+ shell "cpanm -l $BASEDIR local::lib";
+};
+
+deftask 'zlib' => sub {
+ depends "base-dirs,setenv";
+ my $tarball = "zlib-${ZLIB_VERSION}.tar.bz2";
+ shell <<"EOT";
+cd $BASEDIR/src
+test -e $tarball || wget -v $ZLIB_URL -O $tarball
+test -d zlib-${ZLIB_VERSION} || tar xf $tarball
+cd zlib-${ZLIB_VERSION}
+./configure --prefix=$BASEDIR
+make
+make install
+EOT
+};
+
+deftask 'git' => sub {
+ depends "base-dirs,setenv,zlib";
+ shell <<"EOT";
+cd $BASEDIR/src
+wget -N -v $GIT_URL
+tar xf git-${GIT_VERSION}.tar.bz2
+cd git-$GIT_VERSION
+./configure --prefix=$BASEDIR --with-zlib=$BASEDIR
+make
+make install
+EOT
+};
+
+1;

0 comments on commit c6d270e

Please sign in to comment.