-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathyaml2plist.pl
More file actions
executable file
·195 lines (164 loc) · 4.25 KB
/
Copy pathyaml2plist.pl
File metadata and controls
executable file
·195 lines (164 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env perl
=head1 NAME
yaml2plist.pl - Convert a YAML file to an XML plist file
=head1 SYNOPSIS
perl yaml2plist.pl [<outfile> [<infile>]]
=head1 OPTIONS
=over
=item <outfile>
The name of the plist file to output. The default is F<ModernPerl.tmLanguage>.
The file will be overwritten if it exists.
=item <infile>
The name of the YAML file to convert to plist. The default is
F<ModernPerl.tmLanguage.yaml.ep>. If the file ends in F<.ep>, it will first be
interpreted as L<Extended
Perl|http://mojolicio.us/perldoc/Mojolicious/Guides/Rendering#Embedded_Perl> and
outputted to F<< .<outfile>.yaml >>. Then that new file will be used as
<infile>.
=back
=cut
package ModernPerl::yaml2plist;
use 5.01;
use strict;
use warnings;
use autodie;
use FindBin;
use File::Spec::Functions;
use File::Basename;
use Data::Dumper;
use lib catfile $FindBin::Bin, "inc";
use YAML::XS qw/Load/;
use Mojo::Template;
use subs qw/convert printout badprint/;
my $outfile = shift // "ModernPerl.tmLanguage";
my $infile = shift // "ModernPerl.tmLanguage.yaml.ep";
my $infilename = basename $infile;
if (@ARGV) {
say "Usage: $^X $0 [[OUTFILE] INFILE]";
exit 1;
}
my $yaml;
if ($infile =~ /.ep$/i) {
my $mt = Mojo::Template->new(auto_escape => 0);
$yaml = $mt->render_file($infile);
if (ref $yaml eq "Mojo::Exception") {
print $yaml->message;
exit 1;
}
my ($name, $dir) = fileparse $outfile;
my $yamloutfile = catfile $dir, ".$name.yaml";
printout $yamloutfile, <<YAML, $yaml;
%YAML 1.1
#
# This file was generated automatically by $0 from $infilename.
# DO NOT edit this file directly. Instead use $0 to generate it.
#
YAML
}
else {
local $/;
open my $in, "<", $infile;
$yaml = <$in>;
close $in;
}
# search for some common mistakes
while ($yaml =~
m{
\G (?s:.*?) ^
(
(\h+) \w+\h*:\h+ ([|>]) (-?) .*\n
\2\h+ (\(\? [a-z]*x[a-z-]* \))? .*\n
(?: \2\h+ .*\n )*
)
}gmx
) {
my ($lines, $taker, $minus, $x) = ($1, $3, $4, $5);
my $multiline = $lines =~ /(?:.*\n){3}/;
my $bad;
if ($multiline and !$x and $taker eq "|") {
$bad = "Can't multiline non x";
}
if ($multiline and $taker eq ">" and $x) {
$bad = "Can't > with multiline x because you can't use comments";
}
if ($multiline and $x and $minus) {
$bad = "Multiline x looks better without -";
}
if (!$minus and !$multiline) {
$bad = "Probably should be using -";
}
if ($bad) {
say $bad;
badprint $lines;
}
}
my ($tree) = Load $yaml;
my $xml = convert $tree, 1;
printout $outfile, <<XML, $xml, "</plist>\n";
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<!--
This file was generated automatically by $0 from $infilename.
DO NOT edit this file directly. Instead, use $0 to generate it.
-->
<plist version="1.0">
XML
sub convert {
my $item = shift;
my $level = shift;
my $indent = "\t" x $level;
my $a = $indent;
if (!ref $item) {
my $tag = shift;
if (!defined $tag) {
badprint "no tag: " . Data::Dumper->Dump([$item], ["value"]);
return "";
}
$item //= '';
$item =~ s/(?:^|\G)\ {4}/\t/gm; # convert indentation to tabs
$item =~ s/(?<=\n)/$indent/g;
my $orig_item = $item;
$item =~ s/&/&/g;
$item =~ s/</</g;
$item =~ s/>/>/g;
# use CDATA unless it costs us more characters than not using it
if (length $item >= 12 + length $orig_item) {
$item = "<![CDATA[$orig_item]]>" if $orig_item !~ /]]>/;
}
$a .= "<$tag>$item</$tag>";
}
elsif (ref $item eq "HASH") {
$a .= "<dict>\n";
for my $key (sort keys %$item)
{
$a .= convert $key, $level + 1, "key";
$a .= convert $item->{$key}, $level + 1, $key eq "applyEndPatternLast" ? "integer" : "string";
}
$a .= "$indent</dict>";
}
elsif (ref $item eq "ARRAY") {
$a .= "<array>\n";
for my $value (@$item) {
$a .= convert $value, $level + 1, "string";
}
$a .= "$indent</array>";
}
else {
badprint "unexpected: " . Data::Dumper->Dump([$item], ["item"]);
return "";
}
return $a . "\n";
}
sub printout {
my $outfile = shift;
chmod 0666, $outfile if -f $outfile; # read-write
open my $out, ">", $outfile;
binmode $out;
print $out @_;
close $out;
chmod 0444, $outfile; # read only
}
sub badprint {
state $once = eval "END { exit 1 }"; # exit 1 on END if we ever reach this code
print @_;
}