Cmd.pm (4014B)
1 package Bluto::Cmd; 2 3 use Getopt::Long qw/ :config auto_help /; 4 use Cwd qw/ getcwd abs_path /; 5 use File::Spec; 6 use File::Path qw/make_path/; 7 use File::Copy; 8 use Digest::SHA; 9 10 use Bluto::SemVer; 11 use Bluto; 12 use YAML::Tiny; 13 14 use Bluto::Log qw/debug info error/; 15 16 17 my $force_version = undef; 18 my $force_help = 0; 19 my %env = ( 20 src_dir => File::Spec->catfile(getcwd, '.bluto'), 21 version => undef, 22 loglevel => undef, 23 engine => undef, 24 ); 25 my @opts_x; 26 27 sub help { 28 print("$0\n\nwould have shown help...\n"); 29 exit 0; 30 } 31 32 sub register_param { 33 my $env_k = shift; 34 my $env_v_default = shift; 35 my $switch_long = shift, 36 my $switch_short = shift, 37 my $switch_typ = shift; 38 $env{$env_k} = $v; 39 40 if (!defined $switch_typ) { 41 $switch_typ = "s"; 42 } 43 if (defined $switch_long) { 44 push(@opts_x, $switch_long . ':' . $switch_typ); 45 push(@opts_x, \$env{$env_k}); 46 } 47 if (defined $switch_short) { 48 push(@opts_x, $switch_short . ':' . $switch_typ); 49 push(@opts_x, \$env{$env_k}); 50 } 51 debug("added env k $env_k switches $switch_long / $switch_short"); 52 } 53 54 sub process_param { 55 GetOptions( 56 'd:s', \$env{src_dir}, 57 'v+', \$env{loglevel}, 58 'h+', \$force_help, 59 'help+', \$force_help, 60 @opts_x, 61 ); 62 63 if ($force_help > 0) { 64 help; 65 } 66 67 if (defined $env{version}) { 68 $env{version} = SemVer->new($env{version}); 69 } 70 71 if (defined $env{src_dir}) { 72 make_path($env{src_dir}); 73 } 74 75 $env{engine} = 'bluto v' . SemVer->new(Bluto::version()). " (perl $^V)"; 76 foreach my $k (keys %env ) { 77 if (defined $env{$k}) { 78 debug('environment "' . $k . '": ' . $env{$k}); 79 } 80 } 81 82 foreach my $k (keys %env) { 83 my $v = $env{$k}; 84 if (defined $v) { 85 debug("env k $k v $v"); 86 } else { 87 debug("env k $k not defined"); 88 } 89 } 90 } 91 92 sub get_param { 93 my $k = shift; 94 return $env{$k}; 95 } 96 97 sub get_version { 98 if (defined $env{version}) { 99 $env{version} = SemVer->new($env{version}); 100 } 101 return $env{version}; 102 } 103 104 sub base_config_path { 105 return File::Spec->catfile($env{src_dir}, 'bluto.yml'); 106 } 107 108 sub release_config_path { 109 if (!defined $env{version}) { 110 die("release config path does not exist, version not set"); 111 } 112 return File::Spec->catfile($env{src_dir}, $env{version} . '.yml'); 113 } 114 115 sub base_config { 116 my $fn = base_config_path(); 117 my $yi = YAML::Tiny->read($fn); 118 return $yi->[0]; 119 } 120 121 sub release_config { 122 my $fn = release_config_path(); 123 my $yi = YAML::Tiny->read($fn); 124 return $yi->[0]; 125 } 126 127 sub params { 128 if (_check_sanity()) { 129 return undef; 130 } 131 return \%env; 132 } 133 134 sub process_changelog { 135 my $copy = shift; 136 137 if (!defined $env{changelog_file}) { 138 die("changelog file missing"); 139 } 140 141 my $h = Digest::SHA->new('sha256'); 142 $h->addfile($env{changelog_file}); 143 my $z = $h->hexdigest; 144 debug('calculated sha256 ' . $z . ' for changelog ' . $env{changelog_file}); 145 if ($copy) { 146 my $fp = File::Spec->catfile($env{src_dir}, 'CHANGELOG.' . $env{version}); 147 copy($env{changelog_file}, $fp); 148 } 149 return $z; 150 } 151 152 sub _check_yml { 153 my $env = shift; 154 155 #my $fp = File::Spec->catfile($env->{src_dir}, 'bluto.ini'); 156 my $fp = File::Spec->catfile($env{src_dir}, 'bluto.yml'); 157 if ( ! -f $fp ) { 158 error('yml file not found: ' . $fp); 159 return 1; 160 } 161 debug('using yml file: ' . $fp); 162 return 0; 163 } 164 165 sub _check_readme { 166 my $env = shift; 167 my $f; 168 my $fp; 169 170 for my $fn (('README', 'README.txt', 'README.adoc', 'README.rst', 'README.md')) { 171 $fp = File::Spec->catfile($env{content_dir}, $fn); 172 if ( -f $fp ) { 173 debug('using readme file: ' . $fp); 174 $env{readme} = $fp; 175 return 0; 176 } 177 } 178 179 warn('no readme file found'); 180 return 1; 181 } 182 183 sub _check_version { 184 my $env = shift; 185 my $f; 186 my $fp; 187 188 $fp = File::Spec->catfile($env{src_dir}, 'VERSION'); 189 if ($env{version}) { 190 info('writing new explicit version ' . $env{version} . ' to file: ' . $fp); 191 open($f, '>', $fp); 192 print $f $env{version}; 193 close($f); 194 } 195 if (! -f $fp ) { 196 error('no version file'); 197 return 1; 198 } 199 debug('using version file: ' . $fp); 200 return 0; 201 } 202 203 204 sub _check_sanity { 205 my $r = 0; 206 207 $r += _check_readme($env); 208 $r += _check_yml($env); 209 $r += _check_version($env); 210 211 return $r; 212 } 213 214 215 1;