Die instead of warning if roundtrip regen fails
[dbsrgits/SQL-Translator.git] / Makefile.PL
1 use inc::Module::Install 1.06;
2 use strict;
3 use warnings;
4
5 # to deal wuth x.y.z versions properly
6 configure_requires 'ExtUtils::MakeMaker' => '6.54';
7
8 perl_version '5.008001';
9
10 my $deps = {
11   requires => {
12     'Digest::SHA'              => '0',
13     'Carp::Clan'               => '0',
14     'Parse::RecDescent'        => '1.967009',
15     'DBI'                      => '1.54',
16     'File::ShareDir'           => '1.0',
17     'Moo'                      => '1.000003',
18     'Package::Variant'         => '1.001001',
19     'Sub::Quote'               => '0',
20     'Try::Tiny'                => '0.04',
21     'List::MoreUtils'          => '0.09',
22     'Scalar::Util'             => '0',
23   },
24   recommends => {
25     'Template'                 => '2.20',
26     'GD'                       => '0',
27     'GraphViz'                 => '0',
28     'Graph::Directed'          => '0',
29     'Spreadsheet::ParseExcel'  => '0.41',
30     'Text::RecordParser'       => '0.02',
31     'XML::LibXML'              => '1.69',
32   },
33   test_requires => {
34     'JSON'                     => '2.0',
35     'YAML'                     => '0.66',
36     'XML::Writer'              => '0.500',
37     'Test::More'               => '0.88',
38     'Test::Differences'        => '0',
39     'Test::Exception'          => '0.31',
40   },
41 };
42
43
44 name        'SQL-Translator';
45 author      'Ken Youens-Clark <kclark@cpan.org>';
46 abstract    'SQL DDL transformations and more';
47 license     'perl';
48 repository  'git://git.shadowcat.co.uk/dbsrgits/SQL-Translator.git';
49 bugtracker  'http://rt.cpan.org/NoAuth/Bugs.html?Dist=SQL-Translator';
50
51 resources Ratings => 'http://cpanratings.perl.org/d/SQL-Translator';
52
53 Meta->{values}{x_authority} = 'cpan:JROBINSON';
54
55 all_from    'lib/SQL/Translator.pm';
56 readme_from 'lib/SQL/Translator.pm';
57
58 for my $type (qw/requires recommends test_requires/) {
59   no strict qw/refs/;
60   my $f = \&$type;
61   for my $mod (keys %{$deps->{$type} || {} }) {
62     $f->($mod, $deps->{$type}{$mod});
63   }
64 }
65
66 install_script (qw|
67   script/sqlt-diagram
68   script/sqlt-diff
69   script/sqlt-diff-old
70   script/sqlt-dumper
71   script/sqlt-graph
72   script/sqlt
73 |);
74
75 install_share();
76
77 tests_recursive ();
78
79
80 # temporary(?) until I get around to fix M::I wrt xt/
81 # needs Module::Install::AuthorTests
82 eval {
83   # this should not be necessary since the autoloader is supposed
84   # to work, but there were reports of it failing
85   require Module::Install::AuthorTests;
86   recursive_author_tests (qw/xt/);
87   1;
88 } || do {
89   if ($Module::Install::AUTHOR) {
90     my $err = $@;
91
92     # better error message in case of missing dep
93     eval { require Module::Install::AuthorTests }
94       || die "\nYou need Module::Install::AuthorTests installed to run this Makefile.PL in author mode:\n\n$@\n";
95
96     die $err;
97   }
98 };
99
100 auto_install();
101
102 if ($Module::Install::AUTHOR) {
103   _recompile_grammars();
104   _recreate_rt_source();
105 }
106
107 WriteAll();
108
109 sub _recompile_grammars {
110   return; # disabled until RT#74593 is resolved
111
112   require File::Spec;
113
114   my $compiled_parser_dir = File::Spec->catdir(qw/
115     share PrecompiledParsers Parse RecDescent DDL SQLT
116   /);
117
118   # Currently consider only single-name parsers containing a grammar marker
119   # This is somewhat fragile, but better than loading all kinds of parsers
120   # to some of which we may not even have the deps
121   my $parser_libdir = 'lib/SQL/Translator/Parser';
122   for my $parser_fn (glob "$parser_libdir/*.pm") {
123     die "$parser_fn does not look like a readable file\n"
124       unless ( -f $parser_fn and -r $parser_fn );
125
126     my ($type) = $parser_fn =~ /^\Q$parser_libdir\E\/(.+)\.pm$/i
127       or die "$parser_fn not named in expected format\n";
128
129     my $parser_source = do { local (@ARGV, $/) = $parser_fn; <> };
130     next unless $parser_source =~ /\$GRAMMAR.+?END_OF_GRAMMAR/s;
131
132
133     my $precomp_parser_fn = File::Spec->catfile($compiled_parser_dir, "$type.pm");
134
135     next if (
136       -f $precomp_parser_fn
137         and
138       (stat($parser_fn))[9] <= (stat($precomp_parser_fn))[9]
139     );
140
141
142     print "Precompiling parser for $type\n";
143
144     require $parser_fn;
145     require Parse::RecDescent;
146
147     Parse::RecDescent->Precompile(
148       do {
149         no strict 'refs';
150         ${"SQL::Translator::Parser::${type}::GRAMMAR"}
151           || die "No \$GRAMMAR global found in SQL::Translator::Parser::$type ($parser_fn)\n"
152       },
153       "Parse::RecDescent::DDL::SQLT::$type"
154     );
155
156     rename( "$type.pm", $precomp_parser_fn )
157       or die "Unable to move $type.pm to $compiled_parser_dir: $!\n";
158   }
159
160 }
161
162 sub _recreate_rt_source {
163   my $base_xml = "t/data/roundtrip.xml";
164   my $autogen_yaml = "t/data/roundtrip_autogen.yaml";
165
166   print "Updating $autogen_yaml\n";
167
168   unlink $autogen_yaml;
169
170   eval {
171
172     use lib 'lib';
173
174     require SQL::Translator;
175     require SQL::Translator::Parser::XML;
176
177     open (my $fh, '>', $autogen_yaml) or die "$autogen_yaml: $!\n";
178
179     my $tr = SQL::Translator->new;
180     my $yaml = $tr->translate (
181       parser => 'XML',
182       file => $base_xml,
183       producer => 'YAML',
184     ) or  die sprintf ("Unable to translate %s to YAML: %s\n",
185               $base_xml,
186               $tr->error || 'error unknown'
187           );
188
189     print $fh $yaml;
190     close $fh;
191   };
192
193   if ($@) {
194     die <<EOE;
195
196 =========================================================================
197 ===============              WARNING !!!                =================
198 =========================================================================
199
200 Unable to update the roundtrip schema (attempt triggered by AUTHOR mode).
201 Aborting Makefile generation, please fix the errors indicated below
202 (typically by installing the missing modules).
203
204 -------------------------------------------------------------------------
205 $@
206
207 EOE
208   }
209 }