]> git.lyx.org Git - features.git/blob - lib/scripts/prefTest.pl.in
keytests: Allow definition of shortcuts for the test.
[features.git] / lib / scripts / prefTest.pl.in
1 #! /usr/bin/env perl
2 # -*- mode: perl; -*-
3
4 use strict;
5
6 # Syntax: ReplaceValuesprefTest.pl [<var1>=<Subst1> [<var2>=<Subst> ...]] [[ctest parameters]]
7
8 my $bindir = "@CMAKE_BINARY_DIR@";
9
10 my $userdir = "$bindir/Testing/.lyx";
11
12 my %allowedKeys = (
13   "use_converter_needauth_forbidden" => ["true", "false"],
14   "use_converter_needauth" => ["true", "false"],
15     );
16
17 # Bindings used by some tests
18 # used file is "$userdir/bind/user.bind"
19 my %bindings = (
20   # Empty for now, example below
21   # "A-h" => ["dialog-hide", "findreplaceadv"],
22   );
23
24 chdir($bindir);
25
26 # Parse Arguments for strings to substitute
27
28 my %Subst = ();
29
30 my $ctestparams = 0;
31 my @ctestpars = ();
32 for my $arg (@ARGV) {
33   if ($ctestparams) {
34     push(@ctestpars, $arg);
35   }
36   else {
37     if ($arg =~ /^([^=]+)=(.*)$/) {
38       my $key = $1;
39       my $value = $2;
40       my $valid = 0;
41       if (defined($allowedKeys{$key})) {
42         for my $val (@{$allowedKeys{$key}}) {
43           if ($val eq $value) {
44             $valid = 1;
45             last;
46           }
47         }
48       }
49       if ($valid) {
50         $Subst{$key} = [$value, 0];
51       }
52       else {
53         die("invalid key or value specified in \"$arg\"");
54       }
55     }
56     else {
57       $ctestparams = 1;
58       push(@ctestpars, $arg);
59     }
60   }
61 }
62
63 if (%Subst) { # Try to do something only if a substitute is requested
64   if (open(FO, '>', "$userdir/preferences.tmp")) {
65     if (open(FI, "$userdir/preferences")) {
66       while (my $l = <FI>) {
67         for my $k (keys %Subst) {
68           if ($l =~ /^\\$k\b/) {
69             $l = "\\$k $Subst{$k}->[0]\n";
70             $Subst{$k}->[1] = 1;
71           }
72         }
73         print FO $l;
74       }
75     }
76     for my $k (keys %Subst) {
77       if ($Subst{$k}->[1] == 0) {
78         print FO "\\$k $Subst{$k}->[0]\n";
79       }
80     }
81     rename("$userdir/preferences.tmp", "$userdir/preferences");
82   }
83 }
84
85 if (open(FO, '>', "$userdir/userbind.tmp")) {
86   my %used = ();
87   if (open(FI, "$userdir/bind/user.bind")) {
88     while (my $l = <FI>) {
89       my $found = 0;
90       for my $k (keys %bindings) {
91         if ($l =~ /^\s*\\bind\s+\"$k\"/) {
92           $found = 1;
93           if (! defined($used{$k})) {
94             $used{$k} = 1;
95             $l = "\\bind \"$k\" \"" . join(' ', @{$bindings{$k}}) . "\"\n";
96             print FO $l;
97             last;
98           }
99         }
100         print FO $l if (! $found);
101       }
102     }
103     close(FI);
104   }
105   else {
106     print FO "## This file is automatically generated by lyx\n";
107     print FO "## All modifications will be lost\n";
108     print FO "\n\n";
109     print FO "Format 4\n\n";
110   }
111   for my $k (keys %bindings) {
112     if (! defined($used{$k})) {
113       $used{$k} = 1;
114       my $l = "\\bind \"$k\" \"" . join(' ', @{$bindings{$k}}) . "\"\n";
115       print FO $l;
116     }
117   }
118   close(FO);
119   rename("$userdir/userbind.tmp", "$userdir/bind/user.bind");
120 }
121
122 my $res = 0;
123 if (@ctestpars) {
124   $res = system("ctest", @ctestpars);
125 }
126
127 exit($res);