Alte Konfigurationsdateien config/lx-erp.conf{.default} entfernt
[kivitendo-erp.git] / scripts / rose_auto_create_model.pl
1 #!/usr/bin/perl
2
3 use strict;
4
5 BEGIN {
6   unshift @INC, "modules/override"; # Use our own versions of various modules (e.g. YAML).
7   push    @INC, "modules/fallback"; # Only use our own versions of modules if there's no system version.
8 }
9
10 use CGI qw( -no_xhtml);
11 use Config::Std;
12 use Data::Dumper;
13 use English qw( -no_match_vars );
14 use List::MoreUtils qw(any);
15
16 use SL::Auth;
17 use SL::DBUtils;
18 use SL::DB;
19 use SL::Form;
20 use SL::Locale;
21 use SL::LXDebug;
22 use SL::DB::Helper::ALL;
23 use SL::DB::Helper::Mappings;
24
25 our $form;
26 our $cgi;
27 our $auth;
28 our %lx_office_conf;
29
30 our $script =  __FILE__;
31 $script     =~ s:.*/::;
32
33 $OUTPUT_AUTOFLUSH       = 1;
34 $Data::Dumper::Sortkeys = 1;
35
36 our $meta_path = "SL/DB/MetaSetup";
37
38 sub setup {
39   if (@ARGV < 2) {
40     print "Usage: $PROGRAM_NAME login table1[=package1] [table2[=package2] ...]\n";
41     print "   or  $PROGRAM_NAME login [--all|-a] [--sugar|-s]\n";
42     exit 1;
43   }
44
45   read_config "config/lx_office.conf" => %lx_office_conf;
46
47   my $login     = shift @ARGV;
48
49   $::lxdebug    = LXDebug->new();
50
51   # locale messages
52   $::locale       = Locale->new("de");
53   $::form         = new Form;
54   $::cgi          = new CGI('');
55   $::auth         = SL::Auth->new();
56
57   $::user         = User->new($login);
58
59   %::myconfig     = $auth->read_user($login);
60   $form->{script} = 'rose_meta_data.pl';
61   $form->{login}  = $login;
62
63   map { $form->{$_} = $::myconfig{$_} } qw(stylesheet charset);
64
65   mkdir $meta_path unless -d $meta_path;
66 }
67
68 sub process_table {
69   my @spec       =  split(/=/, shift, 2);
70   my $table      =  $spec[0];
71   my $schema     = '';
72   ($schema, $table) = split(m/\./, $table) if $table =~ m/\./;
73   my $package    =  ucfirst($spec[1] || $spec[0]);
74   $package       =~ s/_+(.)/uc($1)/ge;
75   my $meta_file  =  "${meta_path}/${package}.pm";
76   my $file       =  "SL/DB/${package}.pm";
77
78   $schema        = <<CODE if $schema;
79     __PACKAGE__->meta->schema('$schema');
80 CODE
81
82   my $definition =  eval <<CODE;
83     package SL::DB::AUTO::$package;
84     use SL::DB::Object;
85     use base qw(SL::DB::Object);
86
87     __PACKAGE__->meta->table('$table');
88 $schema
89     __PACKAGE__->meta->auto_initialize;
90
91     __PACKAGE__->meta->perl_class_definition(indent => 2); # , braces => 'bsd'
92 CODE
93
94   if ($EVAL_ERROR) {
95     print STDERR "Error in execution for table '$table': $EVAL_ERROR";
96     return;
97   }
98
99   $definition =~ s/::AUTO::/::/g;
100
101   my $file_exists = -f $meta_file;
102
103   open(OUT, ">$meta_file") || die;
104   print OUT <<CODE;
105 # This file has been auto-generated. Do not modify it; it will be overwritten
106 # by $::script automatically.
107 $definition;
108 CODE
109   close OUT;
110
111   print "File '$meta_file' " . ($file_exists ? 'updated' : 'created') . " for table '$table'\n";
112
113   if (! -f $file) {
114     open(OUT, ">$file") || die;
115     print OUT <<CODE;
116 # This file has been auto-generated only because it didn't exist.
117 # Feel free to modify it at will; it will not be overwritten automatically.
118
119 package SL::DB::${package};
120
121 use strict;
122
123 use SL::DB::MetaSetup::${package};
124
125 # Creates get_all, get_all_count, get_all_iterator, delete_all and update_all.
126 __PACKAGE__->meta->make_manager_class;
127
128 1;
129 CODE
130     close OUT;
131
132     print "File '$file' created as well.\n";
133   }
134 }
135
136 setup();
137
138 my %blacklist     = SL::DB::Helper::Mappings->get_blacklist;
139 my %package_names = SL::DB::Helper::Mappings->get_package_names;
140
141 my @tables = ();
142 if (($ARGV[0] eq '--all') || ($ARGV[0] eq '-a') || ($ARGV[0] eq '--sugar') || ($ARGV[0] eq '-s')) {
143   my ($type, $prefix) = ($ARGV[0] eq '--sugar') || ($ARGV[0] eq '-s') ? ('SUGAR', 'sugar_') : ('LXOFFICE', '');
144   my $db              = SL::DB::create(undef, $type);
145   @tables             = map  { $package_names{$type}->{$_} ? "${_}=" . $package_names{$type}->{$_} : $prefix ? "${_}=${prefix}${_}" : $_ }
146                         grep { my $table = $_; !any { $_ eq $table } @{ $blacklist{$type} } }
147                         $db->list_tables;
148
149 } else {
150   @tables = @ARGV;
151 }
152
153 foreach my $table (@tables) {
154   # add default model name unless model name is given or no defaults exists
155   $table .= '=' . $package_names{LXOFFICE}->{lc $table} if $table !~ /=/ && $package_names{LXOFFICE}->{lc $table};
156
157   process_table($table);
158 }