« blib/lib/UR/Namespace/Command/Define/Datasource.pm
↑ Summary
blib/lib/UR/Namespace/Command/Update/RenameClass.pm »
| 1 | package UR::Namespace::Command::Define::Namespace; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use UR; | ||
| 6 | our $VERSION = "0.46"; # UR $VERSION; | ||
| 7 | use IO::File; | ||
| 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 | sub help_brief { "Used to define a new Namespace as part of starting a new project." } | ||
| 22 | |||
| 23 | sub sub_command_sort_position { 1 } | ||
| 24 | |||
| 25 | our $module_template=<<EOS; | ||
| 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 | my $self = shift; | ||
| 41 | |||
| 42 | my $name = $self->nsname; | ||
| 43 | |||
| 44 | if (-e $name . ".pm") { | ||
| 45 | $self->error_message("Module ${name}.pm already exists!"); | ||
| 46 | return; | ||
| 47 | } | ||
| 48 | eval "package $name;"; | ||
| 49 | if ($@) { | ||
| 50 | $self->error_message("Invalid package name $name: $@"); | ||
| 51 | return; | ||
| 52 | } | ||
| 53 | |||
| 54 | |||
| 55 | # Step 1 - Make a new Namespace | ||
| 56 | my $namespace = UR::Object::Type->define(class_name => $name, | ||
| 57 | is => ['UR::Namespace'], | ||
| 58 | is_abstract => 0); | ||
| 59 | my $namespace_src = $namespace->resolve_module_header_source; | ||
| 60 | |||
| 61 | # Step 2 - Make an empty Vocabulary | ||
| 62 | my $vocab_name = $name->get_vocabulary(); | ||
| 63 | my $vocab = UR::Object::Type->define( | ||
| 64 | class_name => $vocab_name, | ||
| 65 | is => 'UR::Vocabulary', | ||
| 66 | is_abstract => 0, | ||
| 67 | ); | ||
| 68 | my $vocab_src = $vocab->resolve_module_header_source(); | ||
| 69 | my $vocab_filename = $vocab->module_base_name(); | ||
| 70 | |||
| 71 | # write the namespace module | ||
| 72 | $self->status_message("A $name (UR::Namespace)\n"); | ||
| 73 | IO::File->new("$name.pm", 'w')->printf($module_template, $name, $namespace_src); | ||
| 74 | |||
| 75 | # Write the vocbaulary module | ||
| 76 | mkdir($name); | ||
| 77 | IO::File->new($vocab_filename,'w')->printf($module_template, $vocab_name, $vocab_src); | ||
| 78 | $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 | my ($meta_datasource, $meta_db_file) = | ||
| 83 | UR::DataSource::Meta->generate_for_namespace($name); | ||
| 84 | my $meta_datasource_name = $meta_datasource->id; | ||
| 85 | $self->status_message("A $meta_datasource_name (UR::DataSource::Meta)\n"); | ||
| 86 | $self->status_message("A $meta_db_file (Metadata DB skeleton)"); | ||
| 87 | |||
| 88 | return 1; | ||
| 89 | } | ||
| 90 | |||
| 91 | 1; | ||
| 92 |