forked from duckduckgo/p5-app-duckpan
/
New.pm
268 lines (212 loc) · 7.87 KB
/
New.pm
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package App::DuckPAN::Cmd::New;
# ABSTRACT: Take a name as input and generates a new, named Goodie or Spice instant answer skeleton
# See the template/templates.yml file in the Goodie or Spice repository for the
# list of template-sets and files generated for them
use Moo;
with qw( App::DuckPAN::Cmd );
use MooX::Options protect_argv => 0;
use Try::Tiny;
use App::DuckPAN::TemplateDefinitions;
##########################
# Command line arguments #
##########################
# A 'template' for the user is equivalent to a 'template-set' for the program
option template => (
is => 'ro',
format => 's',
default => 'default',
doc => 'template used to generate the instant answer skeleton (default: default)',
);
option list_templates => (
is => 'ro',
doc => 'list the available instant answer templates and exit',
);
##############
# Attributes #
##############
has _template_defs => (
is => 'ro',
init_arg => undef,
lazy => 1,
builder => 1,
doc => 'Template definitions for the templates for the current IA type',
);
sub _build__template_defs {
my $self = shift;
my $template_defs;
# Read the templates.yml file
try {
$template_defs = App::DuckPAN::TemplateDefinitions->new;
} catch {
my $error = $_;
if ($error =~ /no such file/i) {
# Handle the 'no such file or directory' exception
# specially to show more information since it can be a
# common error for users with an older IA repository
my $type = $self->app->get_ia_type();
$self->app->emit_and_exit(-1,
"Template definitions file not found for " . $type->{name} .
" Instant Answers. You may need to pull the latest version " .
"of this repository.");
} else {
$self->app->emit_and_exit(-1, $error);
}
};
return $template_defs;
}
has _template_set => (
is => 'ro',
init_arg => undef,
lazy => 1,
builder => 1,
doc => 'The template set chosen by the user',
);
sub _build__template_set {
my $self = shift;
my $type = $self->app->get_ia_type();
my $template_defs = $self->_template_defs;
# Get the template chosen by the user
my $template_set = $template_defs->get_template_set($self->template);
unless ($template_set) {
# We didn't find the template-set by the name. This could mean
# that there was a typo in the name or the user has an older IA
# repo and it not present in that version.
$self->app->emit_and_exit(-1,
"'" . $self->template . "' is not a valid template for a " .
$type->{name} . " Instant Answer. You may need to update " .
"your repository to get the latest templates.\n" .
$self->_available_templates_message);
}
return $template_set;
}
###########
# Methods #
###########
# Copy of @ARGV before MooX::Options processes it
my @ORIG_ARGV;
before new_with_options => sub { @ORIG_ARGV = @ARGV };
sub run {
my ($self, @args) = @_;
# Check which IA repo we're in...
my $type = $self->app->get_ia_type();
# Process the --list-templates option: List the template-set names and exit with success
$self->app->emit_and_exit(0, $self->_available_templates_message)
if $self->list_templates;
# Gracefully handle the case where '--template' is the last argument
$self->app->emit_and_exit(
1,
"Please specify the template for your Instant Answer.\n" .
$self->_available_templates_message
) if ($ORIG_ARGV[$#ORIG_ARGV] // '') eq '--template';
# Get the template-set instance based on the command line arguments.
my $template_set = $self->_template_set();
$self->app->emit_info("Creating a new " . $template_set->description . "...");
# Instant Answer name as parameter
my $entered_name = (@args) ? join(' ', @args) : $self->app->get_reply('Please enter a name for your Instant Answer: ');
# Validate the entered name
$self->app->emit_and_exit(-1, "Must supply a name for your Instant Answer.") unless $entered_name;
$self->app->emit_and_exit(-1,
"'$entered_name' is not a valid name for an Instant Answer. " .
"Please run the program again and provide a valid name."
) unless $entered_name =~ m!^[/a-zA-Z0-9\s]+$!;
$self->app->emit_and_exit(-1,
"The name for this type of Instant Answer cannot contain path separators. " .
"Please run the program again and provide a valid name."
) if !$template_set->subdir_support && $entered_name =~ m!/!;
$entered_name =~ s/\//::/g; #change "/" to "::" for easier handling
my $package_name = $self->app->phrase_to_camel($entered_name);
my ($name, $separated_name, $path, $lc_path) = ($package_name, $package_name, "", "");
$separated_name =~ s/::/ /g;
if ($package_name =~ m/::/) {
my @path_parts = split(/::/, $package_name);
if (scalar @path_parts > 1) {
$name = pop @path_parts;
$path = join("/", @path_parts);
$lc_path = join("/", map { $self->app->camel_to_underscore($_) } @path_parts);
} else {
$self->app->emit_and_exit(-1, "Malformed input. Please provide a properly formatted package name for your Instant Answer.");
}
}
my $lc_name = $self->app->camel_to_underscore($name);
my $filepath = ($path eq "") ? $name : "$path/$name";
my $lc_filepath = ($lc_path eq "") ? $lc_name : "$lc_path/$lc_name";
if (scalar $lc_path) {
$lc_path =~ s/\//_/g; #safe to modify, we already used this in $lc_filepath
$lc_name = $lc_path . "_" . $lc_name;
}
my @optional_templates = $self->_ask_optional_templates;
my %vars = (
ia_package_name => $package_name,
ia_name_separated => $separated_name,
ia_id => $lc_name,
ia_path => $filepath,
ia_path_lc => $lc_filepath,
);
# Generate the instant answer files. The return value is a hash with
# information about the created files and any error that was encountered.
my %generate_result = $template_set->generate(\%vars, \@optional_templates);
# Show the list of files that were successfully created
my @created_files = @{$generate_result{created_files}};
$self->app->emit_info("Created files:");
$self->app->emit_info(" $_") for @created_files;
$self->app->emit_info(" (none)") unless @created_files; # possible on error
if (my $error = $generate_result{error}) {
# Remove the line number information if not in verbose mode.
# This error message would be seen mostly by users writing IAs
# for whom the line numbers don't add much value.
$error =~ s/.*\K at .* line \d+\.$//
unless $self->app->verbose;
$self->app->emit_and_exit(-1, $error)
}
$self->app->emit_info("Success!");
}
# Ask the user for which optional templates they want to use and return a list
# of the chosen templates
sub _ask_optional_templates {
my $self = shift;
my $template_set = $self->_template_set;
my $combinations = $template_set->optional_template_combinations;
# no optional templates; nothing to do
return unless @$combinations;
my $show_optional_templates = $self->app->ask_yn(
'Would you like to configure optional templates?',
default => 0,
);
if ($show_optional_templates) {
# The choice strings to show to the user
my @choices;
# Mapping from a choice string to the corresponding template combination
my %choice_combinations;
for my $combination (@$combinations) {
# Label of every template in the combination
my @labels = map { $_->label } @$combination;
my $choice = join(', ', @labels);
push @choices, $choice;
$choice_combinations{$choice} = $combination;
}
my $reply = $self->app->get_reply(
'Choose configuration',
choices => \@choices,
default => $choices[0],
);
return @{$choice_combinations{$reply}};
}
return;
}
# Create a message with the list of available template-sets for this IA type
sub _available_templates_message {
my $self = shift;
my $template_defs = $self->_template_defs;
# template-sets, sorted by name
my @template_sets =
sort { $a->name cmp $b->name } $template_defs->get_template_sets;
my $message = "Available templates:";
for my $template_set (@template_sets) {
$message .= sprintf("\n %10s - %s",
$template_set->name,
$template_set->description,
);
}
return $message;
}
1;