-
Notifications
You must be signed in to change notification settings - Fork 3
/
gen-config.pl
executable file
·122 lines (91 loc) · 2.76 KB
/
gen-config.pl
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
#!/usr/bin/env perl
use strict;
use FindBin;
use lib "$FindBin::Bin/";
use lib "$FindBin::Bin/vendor/";
use LoadGolpe;
use Template;
my $golpe = LoadGolpe::load();
my $config = $golpe->{config} || [];
my $nested = { name => '', items => [], };
foreach my $c (@$config) {
$c->{nameCpp} = $c->{name};
$c->{path} = [ split(/__/, $c->{name}) ];
if (!defined $c->{type}) {
if ($c->{default} =~ /^(true|false)$/i) {
$c->{type} = 'bool';
} elsif ($c->{default} =~ /^\d+$/) {
$c->{type} = 'uint64';
} else {
$c->{type} = 'string';
}
}
if ($c->{type} eq 'uint64') {
$c->{typeCpp} = 'uint64_t';
$c->{defaultCpp} = $c->{default} . 'ULL';
} elsif ($c->{type} eq 'string') {
$c->{typeCpp} = 'std::string';
$c->{defaultCpp} = $c->{default};
$c->{defaultCpp} =~ s/"/\\"/g;
$c->{defaultCpp} = '"' . $c->{defaultCpp} . '"';
} elsif ($c->{type} eq 'bool') {
$c->{typeCpp} = 'bool';
$c->{defaultCpp} = lc($c->{default});
} else {
die "unknown type: $c->{type}";
}
my $n = $nested->{items};
my $items = [ split(/__/, $c->{name}) ];
my $lastItem = pop @$items;
for my $k (@$items) {
my ($item) = grep { $k eq $_->{name} } @$n;
if (!$item) {
$item = { name => $k, items => [], };
push @$n, $item;
}
$n = $item->{items};
}
push @$n, {
key => $lastItem,
%$c,
};
}
{
open(my $fh, '>', "build/$golpe->{appName}.conf") || die "couldn't write to default config";
my $o = '';
genDefaultConfig(\$o, $nested, 0);
print $fh "##\n";
print $fh "## Default $golpe->{appName} config\n";
print $fh "##\n\n";
print $fh $o;
}
my $ctx = {
config => $config,
};
my $tt = Template->new({
ABSOLUTE => 1,
INCLUDE_PATH => ".",
}) || die "$Template::ERROR\n";
$tt->process("golpe/config.h.tt", $ctx, "build/config.h") || die $tt->error(), "\n";
$tt->process("golpe/config.cpp.tt", $ctx, "build/config.cpp") || die $tt->error(), "\n";
sub genDefaultConfig {
my ($o, $n, $indent) = @_;
my $prefix = " " x $indent;
my $first = 1;
for my $i (@{ $n->{items} }) {
$$o .= "\n" if !$first;
$first = 0;
if ($i->{items}) {
$$o .= "$prefix$i->{name} {\n";
genDefaultConfig($o, $i, $indent + 1);
$$o .= "$prefix}\n";
} else {
my $extra;
$extra = " (restart required)" if $i->{noReload};
$$o .= "$prefix# $i->{desc}$extra\n" if $i->{desc};
my $default = $i->{default};
$default = qq{"$default"} if $i->{type} eq 'string';
$$o .= "$prefix$i->{key} = $default\n";
}
}
}