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