Release commit for 1.62
[dbsrgits/SQL-Translator.git] / maint / Makefile.PL.include
1 BEGIN { -e 'Distar' or system("git clone git://git.shadowcat.co.uk/p5sagit/Distar.git") }
2 use lib 'Distar/lib';
3 use Distar 0.001;
4
5 author      'Ken Youens-Clark <kclark@cpan.org>';
6 manifest_include 'script' => qr/.+/;
7 manifest_include 't/data' => qr/.+/;
8 manifest_include 'share' => qr/.+/;
9 manifest_include '' => qr/\A(?:AUTHORS)\z/;
10
11 # eval so can generate deps for cpanm --installdeps .
12 eval {
13   _recompile_grammars();
14   _recreate_rt_source();
15 };
16 print "Got errors:\n\n$@" if $@;
17
18 sub _recompile_grammars {
19   return; # disabled until RT#74593 is resolved
20
21   require File::Spec;
22
23   my $compiled_parser_dir = File::Spec->catdir(qw/
24     share PrecompiledParsers Parse RecDescent DDL SQLT
25   /);
26
27   # Currently consider only single-name parsers containing a grammar marker
28   # This is somewhat fragile, but better than loading all kinds of parsers
29   # to some of which we may not even have the deps
30   my $parser_libdir = 'lib/SQL/Translator/Parser';
31   for my $parser_fn (glob "$parser_libdir/*.pm") {
32     die "$parser_fn does not look like a readable file\n"
33       unless ( -f $parser_fn and -r $parser_fn );
34
35     my ($type) = $parser_fn =~ /^\Q$parser_libdir\E\/(.+)\.pm$/i
36       or die "$parser_fn not named in expected format\n";
37
38     my $parser_source = do { local (@ARGV, $/) = $parser_fn; <> };
39     next unless $parser_source =~ /\$GRAMMAR.+?END_OF_GRAMMAR/s;
40
41
42     my $precomp_parser_fn = File::Spec->catfile($compiled_parser_dir, "$type.pm");
43
44     next if (
45       -f $precomp_parser_fn
46         and
47       (stat($parser_fn))[9] <= (stat($precomp_parser_fn))[9]
48     );
49
50
51     print "Precompiling parser for $type\n";
52
53     require $parser_fn;
54     require Parse::RecDescent;
55
56     Parse::RecDescent->Precompile(
57       do {
58         no strict 'refs';
59         ${"SQL::Translator::Parser::${type}::GRAMMAR"}
60           || die "No \$GRAMMAR global found in SQL::Translator::Parser::$type ($parser_fn)\n"
61       },
62       "Parse::RecDescent::DDL::SQLT::$type"
63     );
64
65     rename( "$type.pm", $precomp_parser_fn )
66       or die "Unable to move $type.pm to $compiled_parser_dir: $!\n";
67   }
68
69 }
70
71 sub _recreate_rt_source {
72   my $base_xml = "t/data/roundtrip.xml";
73   my $autogen_yaml = "t/data/roundtrip_autogen.yaml";
74
75   print "Updating $autogen_yaml\n";
76
77   unlink $autogen_yaml;
78
79   eval {
80
81     use lib 'lib';
82
83     require SQL::Translator;
84     require SQL::Translator::Parser::XML;
85
86     open (my $fh, '>', $autogen_yaml) or die "$autogen_yaml: $!\n";
87
88     my $tr = SQL::Translator->new;
89     my $yaml = $tr->translate (
90       parser => 'XML',
91       file => $base_xml,
92       producer => 'YAML',
93     ) or  die sprintf ("Unable to translate %s to YAML: %s\n",
94               $base_xml,
95               $tr->error || 'error unknown'
96           );
97
98     print $fh $yaml;
99     close $fh;
100   };
101
102   if ($@) {
103     die <<EOE;
104
105 =========================================================================
106 ===============              WARNING !!!                =================
107 =========================================================================
108
109 Unable to update the roundtrip schema (attempt triggered by AUTHOR mode).
110 Continuing Makefile generation, but please fix the errors indicated below
111 (typically by installing the missing modules).
112
113 -------------------------------------------------------------------------
114 $@
115
116 EOE
117   }
118 }
119
120 # vim: ft=perl et sts=2 sw=2 tw=0:
121
122 1;