| 1 | package UR::Namespace::Command::Update::TabCompletionSpec; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use UR; | ||
| 7 | our $VERSION = "0.46"; # UR $VERSION; | ||
| 8 | use IO::File; | ||
| 9 | use POSIX qw(ENOENT); | ||
| 10 | |||
| 11 | UR::Object::Type->define( | ||
| 12 | class_name => __PACKAGE__, | ||
| 13 | is => 'UR::Namespace::Command::Base', | ||
| 14 | has => [ | ||
| 15 | classname => { | ||
| 16 | is => 'Text', | ||
| 17 | shell_args_position => 1, | ||
| 18 | doc => 'The base class to use as trunk of command tree, e.g. UR::Namespace::Command', | ||
| 19 | }, | ||
| 20 | output => { | ||
| 21 | is => 'Text', | ||
| 22 | is_optional => 1, | ||
| 23 | doc => 'Override output location of the opts spec file.', | ||
| 24 | }, | ||
| 25 | ] | ||
| 26 | ); | ||
| 27 | |||
| 28 | |||
| 29 | sub help_brief { | ||
| 30 | "Creates a .opts file beside class/module passed as argument, e.g. UR::Namespace::Command."; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub create { | ||
| 34 | my $class = shift; | ||
| 35 | |||
| 36 | my $bx = $class->define_boolexpr(@_); | ||
| 37 | if($bx->specifies_value_for('classname') and !$bx->specifies_value_for('namespace_name')) { | ||
| 38 | my $classname = $bx->value_for('classname'); | ||
| 39 | my($namespace) = ($classname =~ m/^(\w+)::/); | ||
| 40 | $bx = $bx->add_filter(namespace_name => $namespace) if $namespace; | ||
| 41 | } | ||
| 42 | return $class->SUPER::create($bx); | ||
| 43 | } | ||
| 44 | |||
| 45 | |||
| 46 | |||
| 47 | sub is_sub_command_delegator { 0; } | ||
| 48 | |||
| 49 | sub execute { | ||
| 50 | my $self = shift; | ||
| 51 | my $class = $self->classname; | ||
| 52 | |||
| 53 | my $req_exception = do { | ||
| 54 | local $@; | ||
| 55 | eval { | ||
| 56 | require Getopt::Complete; | ||
| 57 | require Getopt::Complete::Cache; | ||
| 58 | }; | ||
| 59 | $@; | ||
| 60 | }; | ||
| 61 | if ($req_exception) { | ||
| 62 | die "Errors using Getopt::Complete. Do you have Getopt::Complete installed? If not try 'cpanm Getopt::Complete'"; | ||
| 63 | } | ||
| 64 | |||
| 65 | my $use_exception = do { | ||
| 66 | local $@; | ||
| 67 | eval "use above '$class';"; | ||
| 68 | $@; | ||
| 69 | }; | ||
| 70 | if ($use_exception) { | ||
| 71 | $self->error_message("Unable to use above $class.\n$@"); | ||
| 72 | return; | ||
| 73 | } | ||
| 74 | |||
| 75 | (my $module_path) = Getopt::Complete::Cache->module_and_cache_paths_for_package($class, 1); | ||
| 76 | my $cache_path = $module_path . ".opts"; | ||
| 77 | |||
| 78 | eval { | ||
| 79 | my $rename_ok = rename($cache_path, "$cache_path.bak"); | ||
| 80 | if (!$rename_ok && $! != ENOENT) { | ||
| 81 | die "failed to rename file: $!: $cache_path"; | ||
| 82 | } | ||
| 83 | |||
| 84 | unless ($self->output) { | ||
| 85 | $self->output($cache_path); | ||
| 86 | } | ||
| 87 | $self->status_message("Generating " . $self->output . " file for $class."); | ||
| 88 | $self->status_message("This may take some time and may generate harmless warnings..."); | ||
| 89 | |||
| 90 | my $fh = IO::File->new($self->output, 'w') | ||
| 91 | or die "Cannot create file at " . $self->output . "\n"; | ||
| 92 | |||
| 93 | my $src = Data::Dumper::Dumper($class->resolve_option_completion_spec()); | ||
| 94 | $src =~ s/^\$VAR1/\$$class\:\:OPTS_SPEC/; | ||
| 95 | $fh->print($src); | ||
| 96 | }; | ||
| 97 | |||
| 98 | if (-s $cache_path) { | ||
| 99 | $self->status_message("\nOPTS_SPEC file created at $cache_path"); | ||
| 100 | unlink("$cache_path.bak") | ||
| 101 | or $self->error_message("failed to remove backup file: $!"); | ||
| 102 | } else { | ||
| 103 | if (-s "$cache_path.bak") { | ||
| 104 | $self->error_message("$cache_path is 0 bytes, reverting to previous"); | ||
| 105 | rename("$cache_path.bak", $cache_path) | ||
| 106 | or $self->error_message("failed to restore file: $!"); | ||
| 107 | } else { | ||
| 108 | $self->error_message("$cache_path is 0 bytes and no backup exists, removing file"); | ||
| 109 | unlink($cache_path) | ||
| 110 | or $self->error_message("failed to remove file: $!"); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | 1; |