Application/Command.pm untested

filter: Statementstmt 0.0% 0 / 147 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 220 CC 19 SCAR 47.2
CC
19
Cov
0%
field
240.0
interface
20.0
service_worker
6.0
CRAP 112.6
1 package Progressive::Web::Application::Command;
2  
3 use Progressive::Web::Application;
4 use Module::Find;
5 use Text::Wrap qw//;
6  
7 our ($pwa_link, $width, @MANIFEST_HELP, %TEMPLATE_FIELDS);
8 BEGIN {
9 $width = 72;
10 $Text::Wrap::columns = $width;
11 $pwa_link = 'https://developer.mozilla.org/en-US/docs/Web/Progressive_web_apps';
12 @MANIFEST_HELP = (
13 {
14 key => 'name',
15 field => 'Application Full Name',
16 info => 'The name member is a string that represents the name of the web application as it is usually displayed to the user (e.g., amongst a list of other applications, or as a label for an icon). name is directionality-capable, which means it can be displayed left-to-right or right-to-left based on the values of the dir and lang manifest members.',
17 required => 1,
18 },
19 {
20 key => 'short_name',
21 field => 'Application Short Name',
22 info => 'The short_name member is a string that represents the name of the web application displayed to the user if there is not enough space to display name (e.g., as a label for an icon on the phone home screen). short_name is directionality-capable, which means it can be displayed left-to-right or right-to-left based on the value of the dir and lang manifest members.',
23 },
24 {
25 key => 'description',
26 field => 'Description',
27 info => 'The description member is a string in which developers can explain what the application does. description is directionality-capable, which means it can be displayed left to right or right to left based on the values of the dir and lang manifest members.',
28 },
29 {
30 key => 'lang',
31 field => 'Application Language',
32 info => q|The lang member is a string containing a single language tag (https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/lang). It specifies the primary language for the values of the manifest's directionality-capable members, and together with dir determines their directionality.|
33 },
34 {
35 key => 'dir',
36 field => 'Application Language Direction',
37 info => 'The base direction in which to display direction-capable members of the manifest. Together with the lang member, it helps to correctly display right-to-left languages.',
38 map => {
39 1 => 'auto',
40 2 => 'ltr',
41 3 => 'rtl'
42 }
43 },
44 {
45 key => 'display',
46 field => 'display',
47 info => 'The display member is a string that determines the developers’ preferred display mode for the website. The display mode changes how much of browser UI is shown to the user and can range from "browser" (when the full browser window is shown) to "fullscreen" (when the app is full-screened). (fullscreen, standalone, minimal-ui, browser)',
48 map => {
49 1 => 'standalone',
50 2 => 'minimal-ui',
51 3 => 'fullscreen',
52 4 => 'browser',
53 }
54 },
55 {
56 key => 'iarc_rating_id',
57 field => 'International Age Rating Coalition (IARC) certification code',
58 info => 'The iarc_rating_id member is a string that represents the International Age Rating Coalition (https://www.globalratings.com) certification code of the web application. It is intended to be used to determine which ages the web application is appropriate for.'
59 },
60 {
61 key => 'orientation',
62 field => 'Orientation',
63 info => q|The orientation member defines the default orientation for all the website's top-level browsing contexts.|,
64 map => {
65 1 => 'any',
66 2 => 'natural',
67 3 => 'landscape',
68 4 => 'landscape-primary',
69 5 => 'landscape-secondary',
70 6 => 'portrait',
71 7 => 'portrait-primary',
72 8 => 'portrait-secondary'
73 }
74 },
75 {
76 key => 'scope',
77 field => 'Scope',
78 info => q|The scope member is a string that defines the navigation scope of this web application's application context. It restricts what web pages can be viewed while the manifest is applied. If the user navigates outside the scope, it reverts to a normal web page inside a browser tab or window.|
79 },
80 {
81 key => 'start_url',
82 field => 'Start Path',
83 info => q|The start_url member is a string that represents the start URL of the web application — the prefered URL that should be loaded when the user launches the web application (e.g., when the user taps on the web application's icon from a device's application menu or homescreen).|
84 },
85 {
86 key => 'theme_color',
87 field => 'Theme Colour',
88 info => q|The theme_color member is a string that defines the default theme color for the application. This sometimes affects how the OS displays the site (e.g., on Android's task switcher, the theme color surrounds the site).|
89 },
90 {
91 key => 'background_color',
92 field => 'Background Colour',
93 info => 'The background_color member defines a placeholeder background color for the application page to display before its stylesheet is loaded. This value is used by the user agent to draw the background color of a shortcut when the manifest is available before the stylesheet has loaded.'
94 },
95 {
96 key => 'icons',
97 field => 'Icons',
98 info => q|The icons member specifies an array of objects representing image files that can serve as application icons for different contexts. For example, they can be used to represent the web application amongst a list of other applications, or to integrate the web application with an OS's task switcher and/or system preferences.|,
99 map => {
100 1 => 'Directory',
101 2 => 'Generate'
102 },
103 options => {
104 Directory => [{
105 key => 'icons',
106 field => 'Path',
107 info => q|The directory path to retrieve the icons|
108 }],
109 Generate => [{
110 key => 'file',
111 field => 'Icon File',
112 info => q|The path to the icon to be used to generate|
113 },{
114 key => 'outpath',
115 field => 'Out Directory Path',
116 info => q|The directory that the icons will be written|
117 }]
118 },
119 required => 1
120 },
121 );
122 # {
123 # key => "prefer_related_applications",
124 # field => 'Prefer Related Application',
125 # info => 'The prefer_related_applications member is a boolean value that specifies that applications listed in related_applications should be preferred over the web application. If the prefer_related_applications member is set to true, the user agent might suggest installing one of the related applications instead of this web app. (t/f)'
126 # },
127 # {
128 # key => 'related_applications',
129 # field => 'Related Applications',
130 # info => 'The related_applications field is an array of objects specifying native applications that are installable by, or accessible to, the underlying platform — for example, a native Android application obtainable through the Google Play Store. Such applications are intended to be alternatives to the manifest's website that provides similar/equivalent functionality — like the native app equivalent.',
131 # },
132 # {
133 # key => 'screenshots',
134 # field => 'Screenshots',
135 # info => 'The screenshots member defines an array of screenshots intended to showcase the application. These images are intended to be used by progressive web app stores.'
136 # },
137 # {
138 # key => 'categories',
139 # field => 'Categories',
140 # info => 'The categories member is an array of strings defining the names of categories that the application supposedly belongs to. There is no standard list of possible values, but the W3C maintains a list of known categories. (https://github.com/w3c/manifest/wiki/Categories)'
141 # }
142 %TEMPLATE_FIELDS = (
143 push_button_selector => {
144 key => 'push_button_selector',
145 field => 'Push Button Selector',
146 info => 'A selector for an HTML button used to install the application.',
147 required => 1
148 },
149 scope => {
150 key => 'scope',
151 field => 'Scope',
152 info => q|The scope member is a string that defines the navigation scope of this web application's application context. It restricts what web pages can be viewed while the manifest is applied. If the user navigates outside the scope, it reverts to a normal web page inside a browser tab or window.|,
153 required => 1
154 },
155 cache_name => {
156 key => 'cache_name',
157 field => 'Cache Name',
158 info => 'Control the cache using a versioned name. Each time you deploy you should increment/modify this value so that your endusers caches are cleared on their next visit.',
159 required => 1,
160 },
161 offline_path => {
162 key => 'offline_path',
163 field => 'Offline Path',
164 info => 'The offline path should be a fallback offline "response" the content type of this "response" is up to you and your application/use case.',
165 required => 1,
166 },
167 precache_endpoint => {
168 key => 'precache_endpoint',
169 field => 'Precache Endpoint',
170 info => 'An API endpoint that returns an JSON array containing a list of resources/endpoints to add to the precache.',
171 required => 1,
172 },
173 files_to_cache => {
174 key => 'files_to_cache',
175 field => 'Files To Pre Cache',
176 info => 'We can cache the HTML, CSS, JS, and any static files that make up the application shell in the install event of the service worker.',
177 map => {
178 1 => 'List',
179 2 => 'Directory',
180 },
181 options => { # we probably want a way of doing both... but no energy left)))
182 'List' => [
183 {
184 key => 'files_to_cache',
185 list => 1,
186 info => q|Manually curate a list of endpoints/resources to pre-cache|,
187 field => 'File (type f to finalise list)'
188 },
189 ],
190 Directory => [{
191 key => 'directory',
192 field => 'Directory',
193 info => q|The Directory your resources exist under|
194 },{
195 key => 'recurse',
196 field => 'Recurse (n/y)',
197 info => q|Should we recurse the directory|
198 },{
199 key => 'blacklist_regex',
200 field => 'Blacklist Regex',
201 info => q|Files that match this regex will be skipped|
202 },{
203 key => 'whitelist_regex',
204 field => 'Whitelist Regex',
205 info => q|Files that do not match this regex will be skipped|
206 }]
207 },
208 required => 1
209 }
210 );
211 }
212  
213 sub wrap { Text::Wrap::wrap("", "", $_[0]) }
214  
215 sub say { print wrap $_[0] . "\n";}
216  
217 sub spacer { print '=' x $width . "\n"; }
218  
219 sub title {
220 my $spacer = (($width - (length($_[0]) + 2)) / 2);
221 print '=' x $spacer . ' ' . $_[0] . ' ' . '=' x $spacer . "\n";
222 }
223  
224 sub user_input { my $input; chomp($input = <STDIN>); return $input; };
225  
226 sub field {
227 my ($field, $pwa, $meth) = @_;
228 my $uf = $field->{field};
229 $uf .= ' (Required)' if $field->{required};
230 $uf .= stringify_map($field->{map}) if $field->{map};
231 print wrap "$uf: ";
232 my $value = user_input;
233 if ($value =~ m/^info$/i) {
234 say $field->{info};
235 return field(@_);
236 }
237 if ($value) {
238 if ($field->{map} && $value =~ m/^\d*$/) {
239 $value = $field->{map}->{$value};
240 if (!$value) {
241 say 'Error: Invalid map value passed';
242 field(@_);
243 }
244 }
245 if ($field->{options}) {
246 my $options = $field->{options}->{$value};
247 if (!$options) {
248 say 'Error: Invalid option';
249 return field(@_);
250 }
251 for (@{$options}) {
252 if ($_->{list}) {
253 $value = [];
254 while((my $input = field($_)) !~ m/^e$/i) {
255 push @{$value}, $input;
256 }
257 } else {
258 my $ovalue = field($_);
259 if ($_->{key} eq $field->{key}) {
260 $value = $ovalue;
261 } else {
262 $value = {} if !ref $value;
263 $value->{$_->{key}} = $ovalue;
264 }
265 }
266 }
267 }
268 if ($meth) {
269 eval { $pwa->$meth($field->{key} => $value); };
270 if ($@) {
271 $@ =~ s/at lib.*$//;
272 print 'Error: ' . $@;
273 return field(@_);
274 }
275 }
276 return $value;
277 } else {
278 if ($field->{required}) {
279 say 'Error: This field is required';
280 return field(@_);
281 }
282 }
283 }
284  
285 sub interface {
286 my $pwa = Progressive::Web::Application->new();
287 spacer;
288 title 'PWA Utility CL';
289 say q|Howdy, you're about to use the Progressive::Web::Application utility Command line application|;
290 say q|For more information on what a PWA follow this link|;
291 say $pwa_link;
292 spacer;
293  
294 title q|Settings|;
295 say q|Relative to your current terminal window where is the root folder of your application (MyApp/root)|;
296 field({
297 key => "root",
298 field => 'Root Directory',
299 required => 1
300 }, $pwa, 'set_root');
301 say q|Is your application proxied to a path part AKA (localhost/my/app/*)|;
302 field({
303 key => "path",
304 field => 'Proxy Path (leave blank if at root(/))',
305 }, $pwa, 'set_pathpart');
306 spacer;
307  
308 title q|PWA Manifest|;
309 say q|The web app manifest provides information about a web application in a JSON text file,|
310 . q| necessary for the web app to be downloaded and be presented to the user similarly to a|
311 . q| native app (e.g., be installed on the homescreen of a device, providing users with|
312 . q| quicker access and a richer experience).|;
313 spacer;
314 print q|Would you like to create a manifest? (n/y): |;
315 if (user_input =~ m/y/i) {
316 manifest($pwa);
317 say 'Manifest: ' . $pwa->manifest(1);
318 } else {
319 say q|No manifest will be created|;
320 }
321 spacer;
322  
323 title q|PWA Service Worker|;
324 say q|A service worker is a type of web worker.|
325 . q| It's essentially a JavaScript file that runs separately from the main browser thread,|
326 . q| intercepting network requests, caching or retrieving resources from the cache, and |
327 . q|delivering push messages.|;
328 spacer;
329 print q|Would you like to create a service worker (n/y): |;
330 if (user_input =~ m/y/i) {
331 service_worker($pwa);
332 say 'Templates';
333 } else {
334 say q|No service worker will be created|;
335 }
336 spacer;
337  
338 if ($pwa->has_params || $pwa->has_manifest) {
339 title 'Compiling';
340 say 'About to compile';
341 $pwa->compile();
342 say 'Compiled';
343 title 'PWA CLA Complete';
344 spacer;
345 } else {
346 title 'Nothing to compile - bailing';
347 }
348 }
349  
350 sub manifest {
351 my $pwa = shift;
352 say q|Okay, lets create the manifest.|
353 . q| Please complete the following form if you need help for a field type 'INFO'.|
354 . q| You can skip optional fields by leaving them blank and pressing ENTER.|;
355 field($_, $pwa, 'set_manifest') for @MANIFEST_HELP;
356 say q|Manifest form complete|;
357 }
358  
359 sub service_worker {
360 my $pwa = shift;
361 my @avail = map {substr($_, rindex($_, ':') + 1) }
362 grep { $_ !~ m/Base$/ } findsubmod Progressive::Web::Application::Template;
363 print wrap 'Please select a template that you would like to use'
364 . stringify_templates(@avail) . ': ';
365 my $val = user_input;
366 if (!$avail[$val]) {
367 say 'Error: Invalid template selected';
368 service_worker($pwa);
369 }
370 say q|Template has loaded.|
371 . q| Please complete the following form if you need help for a field type 'INFO'.|
372 . q| You can skip optional fields by leaving them blank and pressing ENTER.|;
373 $pwa->set_template(template => $avail[$val]);
374 my @required = $pwa->template->required_params;
375 field($TEMPLATE_FIELDS{$_}, $pwa, 'set_params') for (@required);
376 say q|Service worker form complete|;
377 }
378  
379 sub stringify_templates {
380 my @templates = @_;
381 ' (' . (join ", ", map {
382 $_ . '=' . $templates[$_]
383 } ( 0 .. $#templates )) . ')';
384 }
385  
386 sub stringify_map {
387 my $map = shift;
388 ' (' .
389 (join ', ', map { $_ . '=' . $map->{$_} } sort keys %{$map})
390 . ')';
391 }
392  
393 1;