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