/
travis.pm
238 lines (187 loc) · 5.83 KB
/
travis.pm
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package App::test::travis;
use 5.10.0;
use strict;
use warnings;
use version; our $VERSION = version->declare("v0.9.7");
use encoding::warnings 'FATAL';
use Fatal qw(open close);
use File::Temp qw(tempdir);
use Config qw(%Config);
use File::Spec ();
use Getopt::Long qw(GetOptionsFromArray :config posix_default no_ignore_case bundling auto_version);
use autouse 'Pod::Usage' => qw(pod2usage);
use autouse 'Pod::Find' => qw(pod_where);
use YAML ();
my $path_sep = $Config{path_sep};
my %tab = (
c => {
script => './configure && make && make test',
},
cpp => {
script => './configure && make && make test',
},
clojure => {
script => 'lein test',
},
erlang => {
install => 'rebar get-deps',
script => 'rebar compile && rebar skip_deps=true eunit',
},
go => {
install => 'go get -d -v ./... && go build -v ./...',
script => 'make',
},
groovy => {
install => 'gradle assemble',
script => 'gradle check',
},
haskell => {
install => 'cabal install --only-dependencies --enable-tests',
script => 'cabal configure --enable-tests && cabal build && cabal test',
},
java => {
install => 'mvn install -DskipTests=true',
script => 'mvn test',
},
node_js => {
setup => sub {
my($config, $tempdir, $cb) = @_;
local $ENV{PATH} = join $path_sep, "node_modules/.bin", $ENV{PATH};
$cb->();
},
install => 'npm install',
script => 'npm test',
},
perl => {
setup => sub {
my($config, $tempdir, $cb) = @_;
local $ENV{PERL_CPANM_OPT} = "-l$tempdir --verbose";
local $ENV{PERL5OPT} = "-Mlib=$tempdir/lib/perl5";
local $ENV{PATH} = join $path_sep, "$tempdir/bin", $ENV{PATH};
$cb->();
},
install => 'cpanm --installdeps --notest .',
script => 'cpanm --test-only .',
},
php => {
script => 'phpunit',
},
python => {
install => 'pip install -r requirements.txt --use-mirrors',
},
ruby => {
install => 'bundle install',
script => 'bundle exec rake',
},
scala => {
script => 'sbt test',
},
);
sub run {
my($class, @args) = @_;
my $start = time();
my $DRY_RUN;
my $HELP;
GetOptionsFromArray(\@args,
'--dry-run' => \$DRY_RUN,
'--help' => \$HELP,
) or return help(1);
return help(0) if $HELP;
my($travis_yml) = @args;
$travis_yml //= '.travis.yml';
my $config = YAML::LoadFile($travis_yml);
my $language = lc($config->{language} // 'ruby');
my $behavior = $tab{$language} or die "no behavior defined for $language\n";
close STDIN;
open STDIN, '<', File::Spec->devnull;
if ($ENV{TRAVIS} && $ENV{CI}) {
say '# skip because TRAVIS and CI are already set';
return;
}
say "# running $travis_yml";
# Travis CI Environment Variables
# http://about.travis-ci.org/docs/user/osx-ci-environment/
local $ENV{CI} = 'true';
local $ENV{TRAVIS} = 'true';
my $tempdir = tempdir('.travis-run-XXXXXXX', CLEANUP => 1);
my $setup = $behavior->{setup} // sub {
my($config, $tempdir, $cb) = @_;
$cb->();
};
for my $mode(qw(before_install install script)) {
$config->{$mode} //= $behavior->{$mode};
}
$setup->($config, $tempdir, sub {
my $versions = $config->{$language} // []; # TODO
# http://about.travis-ci.org/docs/user/build-configuration/#Build-Lifecycle
run_commands($config, $DRY_RUN, 'before_install');
run_commands($config, $DRY_RUN, 'install');
run_commands($config, $DRY_RUN, 'before_script');
eval {
run_commands($config, $DRY_RUN, 'script');
};
if (! $@) {
run_commands($config, $DRY_RUN, 'after_success');
}
else {
run_commands($config, $DRY_RUN, 'after_failure');
}
run_commands($config, $DRY_RUN, 'after_script');
say '# finished: ', scalar localtime;
my $duration = (time() - $start);
if ($duration > 60) {
say sprintf '# duration: %d min %d sec', int($duration / 60), $duration % 60;
}
else {
say sprintf '# duration: %d sec', $duration;
}
});
return 0;
}
sub run_commands {
my($config, $dry_run, $mode) = @_;
return unless defined $config->{$mode};
say "# $mode";
my @cmds = ref($config->{$mode}) eq 'ARRAY'
? @{$config->{$mode}}
: ($config->{$mode});
for my $cmd(@cmds) {
xsystem($dry_run, $cmd);
}
}
sub xsystem {
my($dry_run, @command) = @_;
say "\$ @command";
unless($dry_run) {
system(@command) == 0 or die "failed to call `@command`";
}
}
sub help {
my($exit_status) = @_;
my $pod_file = pod_where({ -inc => 1 }, __PACKAGE__);
pod2usage(
-exitval => 'noexit',
-input => $pod_file,
);
return $exit_status;
}
1;
__END__
=encoding utf-8
=head1 NAME
App::test::travis - Runs Travis-CI scripts (.travis.yml)
=head1 USAGE
test-travis [--dry-run] [.travis.yml]
=head1 DESCRIPTION
C<test-travis(1)> is a helper script which runs scripts defined in F<.travis.yml>, emulating Travis-CI environments.
Note that the actual Travis-CI runs projects on Linux, so Linux specific commands like C<apt-get(1)> won't work.
=head1 SEE ALSO
L<http://about.travis-ci.org/docs/user/getting-started/>
L<http://about.travis-ci.org/docs/user/build-configuration/#Build-Lifecycle> for the build lifecycle
=head1 LICENSE
Copyright (C) Fuji, Goro (gfx) E<lt>gfuji@cpan.orgE<gt>.
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=head1 AUTHOR
Fuji, Goro (gfx) E<lt>gfuji@cpan.orgE<gt>
=cut