Update Brazilian Portuguese translation
[dasher.git] / Src / update_wix.perl
blobab31f1e8c2f1efc0d0ccb3160fccaf15cadc8547
2 # update_wix.perl updates the alphabet, training and color entries in the WIX installer
3 # files.
5 # Ron Bessems <ron.b@promixis.com>
8 use XML::LibXML;
9 use XML::LibXML::PrettyPrint;
11 sub parse_makefile {
13 my $filename = @_[0];
14 my $regexp = @_[1];
15 my @alphabets;
17 open(my $fh, '<:encoding(UTF-8)', $filename) or die "Could not open file '$filename' $!";
19 while (my $row = <$fh>) {
20 my @matches = $row =~ @_[1];
21 if ( scalar @matches > 0 ) {
22 push @alphabets, @matches[0];
26 close $fh;
28 return @alphabets;
32 #Parses Makefile.am and extras the files it lists.
33 sub parse_alphabet_makefile {
34 return parse_makefile( @_[0], qr/(alphabet\.(:?\w|.)*\.xml)/);
37 #Parses Makefile.am and extras the files it lists.
38 sub parse_training_makefile {
39 return parse_makefile( @_[0], qr/(training_(:?\w|.)*\.txt)/);
42 #Parses Makefile.am and extras the files it lists.
43 sub parse_colours_makefile {
44 return parse_makefile( @_[0], qr/(colour(:?\w|.)*\.xml)/);
48 # Finds the node with specified xpath, removes it's subnodes and then adds the files as children.
49 sub update_nodes {
50 my $xc = @_[0];
51 my $xpath = @_[1];
52 my $prefix = @_[2];
53 my $files = @_[3];
55 my @nodes = $xc->findnodes($xpath);
56 if ( scalar @nodes != 1 ) {
57 die "Could not find $xpath Node.";
60 my $node = @nodes[0];
62 $node->removeChildNodes();
63 for my $file (@$files) {
64 my $source = "$prefix$file";
65 my $fileNode = $node->addNewChild( '', 'File' );
67 $fileNode->setAttribute('Id', $file );
68 $fileNode->setAttribute('Name', $file );
69 $fileNode->setAttribute('DiskId', '1' );
70 $fileNode->setAttribute('Source', $source);
75 # Takes the source XML and replaces the alphabets, training and colours.
76 sub parse_xml {
78 my $filename = @_[0];
79 my $alphabets = @_[1];
80 my $training = @_[2];
81 my $colours = @_[3];
83 my $parser = XML::LibXML->new;
84 my $dom = $parser->parse_file($filename) or die("Could not load $filename");
86 my $xc = XML::LibXML::XPathContext->new($dom);
87 $xc->registerNs('Wix', 'http://schemas.microsoft.com/wix/2006/wi');
89 update_nodes($xc, q {//Wix:Component[@Id="Alphabets"]}, "..\\..\\Data\\alphabets\\", $alphabets);
90 update_nodes($xc, q {//Wix:Component[@Id="Training"]}, "..\\..\\Data\\training\\", $training);
91 update_nodes($xc, q {//Wix:Component[@Id="Colours"]}, "..\\..\\Data\\colours\\", $colours);
93 open my $out_fh, '>', $filename;
94 print {$out_fh} XML::LibXML::PrettyPrint->pretty_print($dom);
95 close $out_fh;
98 my @alphabets = parse_alphabet_makefile("../Data/alphabets/Makefile.am");
99 my @training = parse_training_makefile("../Data/training/Makefile.am");
100 my @colours = parse_colours_makefile("../Data/colours/Makefile.am");
102 print("Found " . scalar @alphabets . " alphabets\n");
103 print("Found " . scalar @training . " training files\n");
104 print("Found " . scalar @colours . " colours\n");
106 my @files = ( "Installer/Dasher.wxs", "InstallerTobii/InstallerTobii.wxs", "InstallerW2K/InstallerW2K.wxs");
108 for my $file (@files) {
109 parse_xml ( $file, \@alphabets, \@training, \@colours);