File Coverage

lib/UR/Namespace/Command/Define/Namespace.pm
Criterion Covered Total %
statement 33 39 84.6
branch 2 4 50.0
condition n/a
subroutine 5 7 71.4
pod 0 2 0.0
total 40 52 76.9


line stmt bran cond sub pod time code
1             package UR::Namespace::Command::Define::Namespace;
2              
3 2     2   46 use strict;
  2         2  
  2         64  
4 2     2   7 use warnings;
  2         2  
  2         56  
5 2     2   6 use UR;
  2         2  
  2         16  
6             our $VERSION = "0.46"; # UR $VERSION;
7 2     2   9 use IO::File;
  2         2  
  2         924  
8              
9             UR::Object::Type->define(
10             class_name => __PACKAGE__,
11             is => "Command",
12             has => [
13             nsname => {
14             shell_args_position => 1,
15             doc => 'The name of the namespace, and first "word" in all class names',
16             },
17             ],
18             doc => 'create a new namespace tree and top-level module',
19             );
20              
21 0     0 0 0 sub help_brief { "Used to define a new Namespace as part of starting a new project." }
22              
23 0     0 0 0 sub sub_command_sort_position { 1 }
24              
25             our $module_template=<
26             package %s;
27             use warnings;
28             use strict;
29             use UR;
30              
31             %s
32              
33             1;
34             EOS
35              
36             # FIXME This should be refactored at some point to act more like "update classes" in that it
37             # Creates a bunch of objects and them uses the change log to determine what is new and what
38             # files to create/write
39             sub execute {
40 1     1   16 my $self = shift;
41            
42 1         9 my $name = $self->nsname;
43            
44 1 50       45 if (-e $name . ".pm") {
45 0         0 $self->error_message("Module ${name}.pm already exists!");
46 0         0 return;
47             }
48 1         62 eval "package $name;";
49 1 50       6 if ($@) {
50 0         0 $self->error_message("Invalid package name $name: $@");
51 0         0 return;
52             }
53              
54              
55             # Step 1 - Make a new Namespace
56 1         7 my $namespace = UR::Object::Type->define(class_name => $name,
57             is => ['UR::Namespace'],
58             is_abstract => 0);
59 1         13 my $namespace_src = $namespace->resolve_module_header_source;
60              
61             # Step 2 - Make an empty Vocabulary
62 1         13 my $vocab_name = $name->get_vocabulary();
63 1         6 my $vocab = UR::Object::Type->define(
64             class_name => $vocab_name,
65             is => 'UR::Vocabulary',
66             is_abstract => 0,
67             );
68 1         10 my $vocab_src = $vocab->resolve_module_header_source();
69 1         11 my $vocab_filename = $vocab->module_base_name();
70              
71             # write the namespace module
72 1         20 $self->status_message("A $name (UR::Namespace)\n");
73 1         14 IO::File->new("$name.pm", 'w')->printf($module_template, $name, $namespace_src);
74              
75             # Write the vocbaulary module
76 1         460 mkdir($name);
77 1         8 IO::File->new($vocab_filename,'w')->printf($module_template, $vocab_name, $vocab_src);
78 1         168 $self->status_message("A $vocab_name (UR::Vocabulary)\n");
79              
80             # Step 3 - Make and write a new Meta DataSource module
81             # and also, the SQL source for a new, empty metadata DB
82 1         16 my ($meta_datasource, $meta_db_file) =
83             UR::DataSource::Meta->generate_for_namespace($name);
84 1         7 my $meta_datasource_name = $meta_datasource->id;
85 1         20 $self->status_message("A $meta_datasource_name (UR::DataSource::Meta)\n");
86 1         7 $self->status_message("A $meta_db_file (Metadata DB skeleton)");
87            
88 1         5 return 1;
89             }
90              
91             1;
92