summaryrefslogtreecommitdiff
path: root/util/elmgen.pl
blob: d95aba581d4527f3bd6b9c51ca20cf41e05a8728 (plain)
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
196
197
198
199
200
201
202
203
204
#!/usr/bin/perl

use warnings;
use strict;

use Cwd 'abs_path';
my $ROOT;
BEGIN { ($ROOT = abs_path $0) =~ s{/util/elmgen\.pl$}{}; }
use lib $ROOT.'/lib';
use TUWF;
use VN3::Validation;
use VN3::Auth;
use VN3::Types;
use VN3::VN::Edit;
use VN3::Release::Edit;
use VN3::Producer::Edit;
use VN3::Char::Edit;
use VN3::Staff::Edit;
use VN3::Docs::Edit;
use VN3::User::Settings;

my $conf = require $ROOT.'/data/config3.pl';


sub indent($) { $_[0] =~ s/\n/\n  /gr }
sub list      { indent "[ ".join("\n, ", @_)."\n]" }
sub string($) { '"'.($_[0] =~ s/([\\"])/\\$1/gr).'"' }
sub tuple     { '('.join(', ', @_).')' }

sub to_camel($) { (ucfirst $_[0]) =~ s/_([a-z])/'_'.uc $1/egr; }

sub nested_type {
    my($prefix, $obj) = @_;
    my @keys = $obj->{keys} ? grep $obj->{keys}{$_}{keys}||($obj->{keys}{$_}{values}&&$obj->{keys}{$_}{values}{keys}), sort keys %{$obj->{keys}} : ();

    nested_type($prefix . to_camel $_, $obj->{keys}{$_}{values} || $obj->{keys}{$_}) for @keys;

    printf "type alias %s = %s\n\n", $prefix, $obj->elm_type(
        keys => +{ map +($_, ($obj->{keys}{$_}{values} ? 'List ' : '') . $prefix . to_camel $_), @keys }
    );
}

sub encoder {
    my($name, $type, $obj) = @_;
    printf "%s : %s -> JE.Value\n", $name, $type;
    printf "%s = %s\n\n", $name, $obj->elm_encoder(json_encode => 'JE.');
}



my $perms = VN3::Auth::listPerms();

my $vn_lengths   = list map tuple($_, string vn_length_display $_), 0..$#VN_LENGTHS;
my $vn_relations = list map tuple(string $_, string vn_relation_display $_), vn_relations;
my $producer_relations=list map tuple(string $_, string producer_relation_display $_), keys %PRODUCER_RELATIONS;
my $staff_roles  = list map tuple(string $_, string $STAFF_ROLES{$_}), keys %STAFF_ROLES;
my $user_perms   = list map tuple($perms->{$_}, string $_), sort keys %$perms;
my $languages    = list map tuple(string $_, string $LANG{$_}), sort { $LANG{$a} cmp $LANG{$b} } keys %LANG;
my $platforms    = list map tuple(string $_, string $PLATFORMS{$_}), keys %PLATFORMS;
my $release_types= list map string($_), release_types;
my $producer_types=list map tuple(string $_, string $PRODUCER_TYPES{$_}), keys %PRODUCER_TYPES;
my $minages      = list map tuple($_, string minage_display_full $_), @MINAGE;
my $resolutions  = list map tuple(string $_, string resolution_display_full $_), keys %RESOLUTIONS;
my $voiced       = list map string($_), @VOICED;
my $animated     = list map string($_), @ANIMATED;
my $gender       = list map tuple(string $_, string gender_display $_), keys %GENDERS;
my $bloodt       = list map tuple(string $_, string blood_type_display $_), keys %BLOOD_TYPES;
my $charroles    = list map tuple(string $_, string char_role_display $_), keys %CHAR_ROLES;
my $media        = list map tuple(string($_), sprintf('{ qty = %s, single = %s, plural = %s }',
                              $MEDIA{$_}{qty} ? 'True' : 'False',
                              string($MEDIA{$_}{single}), string($MEDIA{$_}{plural}))), keys %MEDIA;
my $vnlist_status= list map tuple($_, string $VNLIST_STATUS[$_]), 0..$#VNLIST_STATUS;
my $email        = string { tuwf->compile({ email  => 1 })->analyze->html5_validation() }->{pattern};
my $weburl       = string { tuwf->compile({ weburl => 1 })->analyze->html5_validation() }->{pattern};
my $vnvote       = string { tuwf->compile({ vnvote => 1 })->analyze->html5_validation() }->{pattern};


print <<"EOF";
-- This file is automatically generated from util/elmgen.pl
-- DO NOT EDIT!
module Lib.Gen exposing (..)

import Json.Encode as JE

urlStatic : String
urlStatic = "$conf->{url_static}"

vnLengths : List (Int, String)
vnLengths =
  $vn_lengths

vnRelations : List (String, String)
vnRelations =
  $vn_relations

producerRelations : List (String, String)
producerRelations =
  $producer_relations

staffRoles : List (String, String)
staffRoles =
  $staff_roles

userPerms : List (Int, String)
userPerms =
  $user_perms

languages : List (String, String)
languages =
  $languages

platforms : List (String, String)
platforms =
  $platforms

releaseTypes : List String
releaseTypes =
  $release_types

producerTypes : List (String, String)
producerTypes =
  $producer_types

minAges : List (Int, String)
minAges =
  $minages

resolutions : List (String, String)
resolutions =
  $resolutions

voiced : List String
voiced =
  $voiced

animated : List String
animated =
  $animated

genders : List (String, String)
genders =
  $gender

bloodTypes : List (String, String)
bloodTypes =
  $bloodt

charRoles : List (String, String)
charRoles =
  $charroles

type alias Medium =
  { qty    : Bool
  , single : String
  , plural : String
  }

media : List (String, Medium)
media =
  $media

vnlistStatus : List (Int, String)
vnlistStatus =
  $vnlist_status

emailPattern : String
emailPattern = $email

weburlPattern : String
weburlPattern = $weburl

vnvotePattern : String
vnvotePattern = $vnvote

EOF


nested_type VNEdit => $VN3::VN::Edit::FORM_OUT->analyze;
nested_type VNEditSend => $VN3::VN::Edit::FORM_IN->analyze;
encoder vneditSendEncode => VNEditSend => $VN3::VN::Edit::FORM_IN->analyze;

nested_type RelEdit => $VN3::Release::Edit::FORM_OUT->analyze;
nested_type RelEditSend => $VN3::Release::Edit::FORM_IN->analyze;
encoder releditSendEncode => RelEditSend => $VN3::Release::Edit::FORM_IN->analyze;

nested_type ProdEdit => $VN3::Producer::Edit::FORM_OUT->analyze;
nested_type ProdEditSend => $VN3::Producer::Edit::FORM_IN->analyze;
encoder prodeditSendEncode => ProdEditSend => $VN3::Producer::Edit::FORM_IN->analyze;

nested_type CharEdit => $VN3::Char::Edit::FORM_OUT->analyze;
nested_type CharEditSend => $VN3::Char::Edit::FORM_IN->analyze;
encoder chareditSendEncode => CharEditSend => $VN3::Char::Edit::FORM_IN->analyze;

nested_type StaffEdit => $VN3::Staff::Edit::FORM_OUT->analyze;
nested_type StaffEditSend => $VN3::Staff::Edit::FORM_IN->analyze;
encoder staffeditSendEncode => StaffEditSend => $VN3::Staff::Edit::FORM_IN->analyze;

nested_type DocEdit => $VN3::Docs::Edit::FORM_OUT->analyze;
nested_type DocEditSend => $VN3::Docs::Edit::FORM_IN->analyze;
encoder doceditSendEncode => DocEditSend => $VN3::Docs::Edit::FORM_IN->analyze;

nested_type UserEdit => $VN3::User::Settings::FORM_OUT->analyze;
nested_type UserEditSend => $VN3::User::Settings::FORM_IN->analyze;
encoder usereditSendEncode => UserEditSend => $VN3::User::Settings::FORM_IN->analyze;