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