Showing 3 changed files with 226 additions and 5 deletions
+7 -2
README.md
... ...
@@ -19,13 +19,18 @@ At first create **gitprep** user. This is not nesessary, but recommended.
19 19
 
20 20
 ## Download
21 21
 
22
-Donload zip or tar.gz archive and exapand it and change directory. 
22
+Download tar.gz archive and exapand it and change directory. 
23
+
24
+  curl -kL https://github.com/yuki-kimoto/gitprep/archive/0.01.tar.gz > gitprep-0.01.tar.gz
25
+  tar xf gitprep-0.01.tar.gz
26
+  cd gitprep-0.01
23 27
 
24 28
 ## Setup
25 29
 
26 30
 You execute the following command. Needed moudles is installed.
27 31
 
28
-    perl cpanm -L extlib --installdeps .
32
+    perl cpanm -n -l extlib Module::CoreList
33
+    perl -Iextlib/lib/perl5 cpanm -n -L extlib --installdeps .
29 34
 
30 35
 ## Operation
31 36
 
+2 -3
cpanfile
... ...
@@ -1,8 +1,7 @@
1
-requires 'DBI', '== 1.621';
1
+requires 'DBI', '== 1.625';
2 2
 requires 'DBD::SQLite', '== 1.37';
3 3
 requires 'Object::Simple', '== 3.08';
4 4
 requires 'DBIx::Custom', '== 0.28';
5
-requires 'Mojolicious::Plugin::INIConfig', '== 0.02';
6
-requires 'Mojolicious', '== 3.91';
7 5
 requires 'Config::Tiny', '== 2.14';
8 6
 requires 'Time::HiRes', '== 1.9725';
7
+requires 'Test::Simple', '== 0.98';
+217
mojolegacy/lib/Mojolicious/Plugin/INIConfig.pm
... ...
@@ -0,0 +1,217 @@
1
+package Mojolicious::Plugin::INIConfig;
2
+use Mojo::Base 'Mojolicious::Plugin::Config';
3
+use Config::Tiny;
4
+use File::Spec::Functions 'file_name_is_absolute';
5
+use Mojo::Util qw/encode decode slurp/;
6
+
7
+our $VERSION = '0.02';
8
+
9
+sub parse {
10
+  my ($self, $content, $file, $conf, $app) = @_;
11
+
12
+  my $ct   = Config::Tiny->new;
13
+  my $conf_str = decode('UTF-8', $self->render($content, $file, $conf, $app));
14
+  my $config_ct = $ct->read_string($conf_str);
15
+  my $config = {%$config_ct};
16
+  
17
+  my $err = $ct->errstr;
18
+  die qq{Couldn't parse config "$file": $err} if !$config && $err;
19
+  die qq{Invalid config "$file".} if !$config || ref $config ne 'HASH';
20
+
21
+  return $config;
22
+}
23
+
24
+sub register {
25
+  my ($self, $app, $conf) = @_;
26
+  
27
+  # Config file
28
+  my $file = $conf->{file} || $ENV{MOJO_CONFIG};
29
+  $file ||= $app->moniker . '.' . ($conf->{ext} || 'ini');
30
+
31
+  # Mode specific config file
32
+  my $mode = $file =~ /^(.*)\.([^.]+)$/ ? join('.', $1, $app->mode, $2) : '';
33
+
34
+  my $home = $app->home;
35
+  $file = $home->rel_file($file) unless file_name_is_absolute $file;
36
+  $mode = $home->rel_file($mode) if $mode && !file_name_is_absolute $mode;
37
+  $mode = undef unless $mode && -e $mode;
38
+
39
+  # Read config file
40
+  my $config = {};
41
+
42
+  if (-e $file) { $config = $self->load($file, $conf, $app) }
43
+
44
+  # Check for default and mode specific config file
45
+  elsif (!$conf->{default} && !$mode) {
46
+    die qq{Config file "$file" missing, maybe you need to create it?\n};
47
+  }
48
+
49
+  # Merge everything
50
+  if ($mode) {
51
+    my $mode_config = $self->load($mode, $conf, $app);
52
+    for my $key (keys %$mode_config) {
53
+      $config->{$key}
54
+        = {%{$config->{$key} || {}}, %{$mode_config->{$key} || {}}};
55
+    }
56
+  }
57
+  if ($conf->{default}) {
58
+    my $default_config = $conf->{default};
59
+    for my $key (keys %$default_config) {
60
+      $config->{$key}
61
+        = {%{$default_config->{$key} || {}}, %{$config->{$key} || {}}, };
62
+    }
63
+  }
64
+  my $current = $app->defaults(config => $app->config)->config;
65
+  for my $key (keys %$config) {
66
+    %{$current->{$key}}
67
+      = (%{$current->{$key} || {}}, %{$config->{$key} || {}});
68
+  }
69
+
70
+  return $current;
71
+}
72
+
73
+sub render {
74
+  my ($self, $content, $file, $conf, $app) = @_;
75
+
76
+  # Application instance and helper
77
+  my $prepend = q[my $app = shift; no strict 'refs'; no warnings 'redefine';];
78
+  $prepend .= q[sub app; *app = sub { $app }; use Mojo::Base -strict;];
79
+
80
+  # Render and encode for INI decoding
81
+  my $mt = Mojo::Template->new($conf->{template} || {})->name($file);
82
+  my $ini = $mt->prepend($prepend . $mt->prepend)->render($content, $app);
83
+  return ref $ini ? die $ini : encode 'UTF-8', $ini;
84
+}
85
+
86
+1;
87
+
88
+=head1 NAME
89
+
90
+Mojolicious::Plugin::INIConfig - Mojolicious Plugin to create routes automatically
91
+
92
+=head1 CAUTION
93
+
94
+B<This module is alpha release. the feature will be changed without warnings.>
95
+
96
+=head1 SYNOPSIS
97
+
98
+  # myapp.ini
99
+  [section]
100
+  foo=bar
101
+  music_dir=<%= app->home->rel_dir('music') %>
102
+
103
+  # Mojolicious
104
+  my $config = $self->plugin('INIConfig');
105
+
106
+  # Mojolicious::Lite
107
+  my $config = plugin 'INIConfig';
108
+
109
+  # foo.html.ep
110
+  %= $config->{section}{foo}
111
+
112
+  # The configuration is available application wide
113
+  my $config = app->config;
114
+
115
+  # Everything can be customized with options
116
+  my $config = plugin INIConfig => {file => '/etc/myapp.conf'};
117
+
118
+=head1 DESCRIPTION
119
+
120
+L<Mojolicious::Plugin::INIConfig> is a INI configuration plugin that
121
+preprocesses its input with L<Mojo::Template>.
122
+
123
+The application object can be accessed via C<$app> or the C<app> function. You
124
+can extend the normal config file C<myapp.ini> with C<mode> specific ones
125
+like C<myapp.$mode.ini>. A default configuration filename will be generated
126
+from the value of L<Mojolicious/"moniker">.
127
+
128
+The code of this plugin is a good example for learning to build new plugins,
129
+you're welcome to fork it.
130
+
131
+=head1 OPTIONS
132
+
133
+L<Mojolicious::Plugin::INIConfig> inherits all options from
134
+L<Mojolicious::Plugin::Config> and supports the following new ones.
135
+
136
+=head2 default
137
+
138
+  # Mojolicious::Lite
139
+  plugin Config => {default => {section => {foo => 'bar'}}};
140
+
141
+Default configuration, making configuration files optional.
142
+
143
+=head2 template
144
+
145
+  # Mojolicious::Lite
146
+  plugin INIConfig => {template => {line_start => '.'}};
147
+
148
+Attribute values passed to L<Mojo::Template> object used to preprocess
149
+configuration files.
150
+
151
+=head1 METHODS
152
+
153
+L<Mojolicious::Plugin::INIConfig> inherits all methods from
154
+L<Mojolicious::Plugin::Config> and implements the following new ones.
155
+
156
+=head2 parse
157
+
158
+  $plugin->parse($content, $file, $conf, $app);
159
+
160
+Process content with C<render> and parse it with L<Config::Tiny>.
161
+
162
+  sub parse {
163
+    my ($self, $content, $file, $conf, $app) = @_;
164
+    ...
165
+    $content = $self->render($content, $file, $conf, $app);
166
+    ...
167
+    return $hash;
168
+  }
169
+
170
+=head2 register
171
+
172
+  my $config = $plugin->register(Mojolicious->new);
173
+  my $config = $plugin->register(Mojolicious->new, {file => '/etc/foo.conf'});
174
+
175
+Register plugin in L<Mojolicious> application.
176
+
177
+=head2 render
178
+
179
+  $plugin->render($content, $file, $conf, $app);
180
+
181
+Process configuration file with L<Mojo::Template>.
182
+
183
+  sub render {
184
+    my ($self, $content, $file, $conf, $app) = @_;
185
+    ...
186
+    return $content;
187
+  }
188
+
189
+=head1 BACKWARDS COMPATIBILITY POLICY
190
+
191
+If a feature is DEPRECATED, you can know it by DEPRECATED warnings.
192
+DEPRECATED feature is removed after C<five years>,
193
+but if at least one person use the feature and tell me that thing
194
+I extend one year each time he tell me it.
195
+
196
+DEPRECATION warnings can be suppressed
197
+by C<MOJOLICIOUS_PLUGIN_INICONFIG_SUPPRESS_DEPRECATION>
198
+environment variable.
199
+
200
+EXPERIMENTAL features will be changed without warnings.
201
+
202
+=head1 BUGS
203
+
204
+Please tell me bugs if you find bug.
205
+
206
+C<< <kimoto.yuki at gmail.com> >>
207
+
208
+L<http://github.com/yuki-kimoto/Mojolicious-Plugin-INIConfig>
209
+
210
+=head1 COPYRIGHT & LICENSE
211
+
212
+Copyright 2013-2013 Yuki Kimoto, all rights reserved.
213
+
214
+This program is free software; you can redistribute it and/or modify it
215
+under the same terms as Perl itself.
216
+
217
+=cut