RobertFischer / liquibase-dsl

A Groovy-based DSL for Liquibase (with a Perl driver)

liquibase-dsl / lbdsl
100755 68 lines (55 sloc) 2.923 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/perl
 
#
# This file is part of Liquibase-DSL.
#
# Liquibase-DSL is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Liquibase-DSL is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Liquibase-DSL. If not, see <http://www.gnu.org/licenses/>.
#
 
 
use warnings;
use strict;
use File::Spec::Functions;
 
use constant DEBUG => 0;
 
sub java_run($$) {
my $cmd = ((shift @_) or die "No class to execute provided");
my $args = ((shift @_) or "");
my $exec = ((shift @_) or undef);
my $java = $ENV{'JAVA_CMD'} or die "No java command provided";
my $home = $ENV{'LBDSL_HOME'} or die "No LBDSL_HOME provided";
my $jar = $ENV{'LBDSL_JAR'} or die "No LBDSL_JAR provided";
my $opts = ($ENV{'JAVA_OPTS'} or "");
  my $doMe = "$java $opts -Dlbdsl.home=$home -classpath $jar $cmd $args 2>&1";
print "Executing: $doMe\n" if DEBUG;
exec($doMe);
die "Failed to exec java: $!";
}
 
# Check to make sure we have a command
die "USAGE: $0 command-name [arg1 [arg2 [arg3...\n" unless @ARGV;
my $cmd = shift @ARGV;
$cmd =~ s/-(\w)/\u$1/g; # Replace dash-delimited with camel-casing
$cmd = ucfirst($cmd)."Command";
print "Command will be: $cmd\n" if DEBUG;
my $args = @ARGV ? (join " ", @ARGV) : "";
 
 
# Figure out our environment
$ENV{'LBDSL_HOME'} = catdir($ENV{'HOME'}, '.lbdsl') unless $ENV{'LBDSL_HOME'} or !$ENV{'HOME'};
$ENV{'LBDSL_HOME'} = catdir('.', '.lbdsl') unless $ENV{'LBDSL_HOME'};
(mkdir $ENV{'LBDSL_HOME'} or die "Cannot create lbdsl home at $ENV{'LBDSL_HOME'}: $!") unless -e $ENV{'LBDSL_HOME'};
 
$ENV{'LBDSL_JAR'} = catfile($ENV{'LBDSL_HOME'}, 'lbdsl.jar') unless $ENV{'LBDSL_JAR'};
die "Cannot find the lbdsl jar: please ensure lbdsl.jar is at $ENV{'LBDSL_JAR'}\n" unless -e $ENV{'LBDSL_JAR'};
$ENV{'LBDSL_JAR'} = join(":", $ENV{'LBDSL_JAR'}, $ENV{'LBDSL_CLASSPATH'}) if($ENV{'LBDSL_CLASSPATH'}); # Append our classpath to LBDSL_JAR
$ENV{'LBDSL_JAR'} = join(":", $ENV{'LBDSL_JAR'}, $ENV{'CLASSPATH'}) if($ENV{'CLASSPATH'}); # Append global classpath to LBDSL_JAR
 
$ENV{'JAVA_CMD'} = 'java' unless $ENV{'JAVA_CMD'} or !defined(`java -version 2>&1`);
$ENV{'JAVA_CMD'} = `which java` unless $ENV{'JAVA_CMD'};
die "Environment variable 'JAVA_CMD' is not set and cannot find 'java' on path: $ENV{'PATH'}\n" unless $ENV{'JAVA_CMD'};
 
$ENV{'JAVA_OPTS'} = "-Xmx1024m -Xms256m" unless $ENV{'JAVA_OPTS'};
 
# Now execute the desired command
java_run('liquibase.dsl.command.exec.RunCommand', join(" ", $cmd, $args));