a777e441d890c932091a82f95e0025426b394654
[dbsrgits/SQL-Translator.git] / Makefile.PL
1 use inc::Module::Install 1.00;
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 my $deps = {
9   requires => {
10     'Class::Base'              => 0,
11     'Class::Data::Inheritable' => 0.02,
12     'Class::MakeMethods'       => 0,
13     'Digest::SHA'              => 0,
14     'Carp::Clan'               => 0,
15     'IO::Dir'                  => 0,
16     'IO::Scalar'               => 2.110,
17     'Parse::RecDescent'        => 1.964001,
18     'Pod::Usage'               => 0,
19     'DBI'                      => 0,
20     'File::ShareDir'           => 1.0,
21     'File::Spec'               => 0,
22     'Scalar::Util'             => 0,
23     'XML::Writer'              => 0.500,
24     'Moo'                      => 0.009007,
25   },
26   recommends => {
27     'Template'                 => 2.20,
28     'GD'                       => 0,
29     'GraphViz'                 => 0,
30     'Graph::Directed'          => 0,
31     'Spreadsheet::ParseExcel'  => 0.41,
32     'Text::ParseWords'         => 0,
33     'Text::RecordParser'       => 0.02,
34     'XML::LibXML'              => 1.69,
35   },
36   test_requires => {
37     'YAML'                     => 0.66,
38     'File::Basename'           => 0,
39     'Test::More'               => 0.6,
40     'Test::Differences'        => 0,
41     'Test::Exception'          => 0,
42   },
43 };
44
45 perl_version '5.005';
46
47 name        'SQL-Translator';
48 author      'Ken Youens-Clark <kclark@cpan.org>';
49 abstract    'SQL DDL transformations and more';
50 license     'gpl';
51 repository  'git://git.shadowcat.co.uk/dbsrgits/SQL-Translator.git';
52 bugtracker  'http://rt.cpan.org/NoAuth/Bugs.html?Dist=SQL-Translator';
53
54 resources Ratings => 'http://cpanratings.perl.org/d/SQL-Translator';
55
56 all_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 tests_recursive ();
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 auto_provides();
80
81 auto_install();
82
83 if ($Module::Install::AUTHOR) {
84   _recompile_grammars();
85   _recreate_rt_source();
86 }
87
88 WriteAll();
89
90 sub _recompile_grammars {
91   require File::Spec;
92
93   my $compiled_parser_dir = File::Spec->catdir(qw/
94     share PrecompiledParsers Parse RecDescent DDL SQLT
95   /);
96
97   # Currently consider only single-name parsers containing a grammar marker
98   # This is somewhat fragile, but better than loading all kinds of parsers
99   # to some of which we may not even have the deps
100   my $parser_libdir = 'lib/SQL/Translator/Parser';
101   for my $parser_fn (glob "$parser_libdir/*.pm") {
102     die "$parser_fn does not look like a readable file\n"
103       unless ( -f $parser_fn and -r $parser_fn );
104
105     my ($type) = $parser_fn =~ /^\Q$parser_libdir\E\/(.+)\.pm$/i
106       or die "$parser_fn not named in expected format\n";
107
108     my $parser_source = do { local (@ARGV, $/) = $parser_fn; <> };
109     next unless $parser_source =~ /\$GRAMMAR.+?END_OF_GRAMMAR/s;
110
111
112     my $precomp_parser_fn = File::Spec->catfile($compiled_parser_dir, "$type.pm");
113
114     next if (
115       -f $precomp_parser_fn
116         and
117       (stat($parser_fn))[9] <= (stat($precomp_parser_fn))[9]
118     );
119
120
121     print "Precompiling parser for $type\n";
122
123     require $parser_fn;
124     require Parse::RecDescent;
125
126     Parse::RecDescent->Precompile(
127       do {
128         no strict 'refs';
129         ${"SQL::Translator::Parser::${type}::GRAMMAR"}
130           || die "No \$GRAMMAR global found in SQL::Translator::Parser::$type ($parser_fn)\n"
131       },
132       "Parse::RecDescent::DDL::SQLT::$type"
133     );
134
135     rename( "$type.pm", $precomp_parser_fn )
136       or die "Unable to move $type.pm to $compiled_parser_dir: $!\n";
137   }
138
139 }
140
141 sub _recreate_rt_source {
142   my $base_xml = "t/data/roundtrip.xml";
143   my $autogen_yaml = "t/data/roundtrip_autogen.yaml";
144
145   print "Updating $autogen_yaml\n";
146
147   unlink $autogen_yaml;
148
149   eval {
150
151     use lib 'lib';
152
153     require SQL::Translator;
154     require SQL::Translator::Parser::XML;
155
156     open (my $fh, '>', $autogen_yaml) or die "$autogen_yaml: $!\n";
157
158     my $tr = SQL::Translator->new;
159     my $yaml = $tr->translate (
160       parser => 'XML',
161       file => $base_xml,
162       producer => 'YAML',
163     ) or  die sprintf ("Unable to translate %s to YAML: %s\n",
164               $base_xml,
165               $tr->error || 'error unknown'
166           );
167
168     print $fh $yaml;
169     close $fh;
170   };
171
172   if ($@) {
173     warn <<EOE;
174
175 =========================================================================
176 ===============              WARNING !!!                =================
177 =========================================================================
178
179 Unable to update the roundtrip schema (attempt triggered by AUTHOR mode).
180 We will still generate a Makefile, but be aware that if you build a dist
181 this way, it *WILL* be broken.
182
183 -------------------------------------------------------------------------
184 $@
185
186 Press Enter to continue.
187 EOE
188   <>;
189   }
190 }