diff --git a/Licenses.pm b/Licenses.pm new file mode 100644 index 0000000..89d1553 --- /dev/null +++ b/Licenses.pm @@ -0,0 +1,27 @@ +package Licenses; + +use Module::Pluggable instantiate => 'new', search_path => "Licenses"; +use Data::Dumper; + +sub new { + return bless {}, shift; +} + +sub get_licenses { + my ($self, $app) = @_; + + print "Plugins\n"; + foreach my $plugin ($self->plugins()) { + my $stub = $plugin->{'header'}; + $stub =~ s/###DESCRIPTION###/$app->{'options'}->{'about'}/; + $stub =~ s/###DATE###/$app->{'options'}->{'date'}/; + $stub =~ s/###NAME###/$app->{'options'}->{'user'}/; + + $app->{'licenses'}->{$plugin->{'name'}} = { + 'pretty_name' => $plugin->{'pretty_name'}, + 'header_stub' => $stub + }; + } +} + +1; diff --git a/licenses/gpl_v3.header b/Licenses/GPLV2.pm similarity index 77% rename from licenses/gpl_v3.header rename to Licenses/GPLV2.pm index f10fa9f..353f0de 100644 --- a/licenses/gpl_v3.header +++ b/Licenses/GPLV2.pm @@ -1,3 +1,10 @@ +package Licenses::GPLV2; + +sub new { + return bless { + 'name' => 'gpl_v3', + 'pretty_name' => 'GPL v3', + 'header' => qq { ###DESCRIPTION### Copyright (C) ###DATE### ###NAME### @@ -13,3 +20,12 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . + } + }, shift; +} + +sub get_license_stub { + my ($self) = @_; +} + +1; diff --git a/licenses/mit.header b/Licenses/MIT.pm similarity index 87% rename from licenses/mit.header rename to Licenses/MIT.pm index d6aeac1..43747cd 100644 --- a/licenses/mit.header +++ b/Licenses/MIT.pm @@ -1,3 +1,10 @@ +package Licenses::MIT; + +sub new { + return bless { + 'name' => 'mit', + 'pretty_name' => 'MIT', + 'header' => qq { Copyright (c) ###DATE### ###NAME### Permission is hereby granted, free of charge, to any person obtaining a copy of @@ -17,3 +24,8 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + } + }, shift; +} + +1; diff --git a/licapp.pl b/licapp.pl deleted file mode 100755 index 322de49..0000000 --- a/licapp.pl +++ /dev/null @@ -1,129 +0,0 @@ -#!/bin/env perl - -use strict; -use warnings; -use File::Find; -use File::Basename; -use FileHandle; -use Data::Dumper; -use Getopt::Long; -use Cwd; - -# Application configuration and state storage -my $app = {}; - -# Defaults -$app->{'options'}->{'directory'} = getcwd; -$app->{'options'}->{'license'} = 'mit'; -$app->{'options'}->{'user'} = $ENV{'USER'}; -$app->{'options'}->{'date'} = (localtime(time))[5] + 1900; -$app->{'options'}->{'about'} = "This is an open-source software"; -$app->{'state'}->{'license_stub'} = "NON EXISTANT"; -$app->{'state'}->{'app_dir'} = dirname($0); - -# Command line options -my $correct_usage = GetOptions ( - 'directory=s' => \$app->{'options'}->{'directory'}, - 'license=s' => \$app->{'options'}->{'license'}, - 'about=s' => \$app->{'options'}->{'about'}, - 'user=s' => \$app->{'options'}->{'user'}, - 'date=s' => \$app->{'options'}->{'date'}, - ); - -setup_license_pretty_names($app); -setup_filetype_comment_mapping($app); -setup_license_stub($app); - -print "Applying '" - . $app->{'license_pretty_names'}->{$app->{'options'}->{'license'}} - . "' license to files in '" - . $app->{'options'}->{'directory'} - . "' directory\n\n"; - -print "Copyright: Copyright (C) " - . $app->{'options'}->{'date'} - . " " . $app->{'options'}->{'user'} - . "\n\n"; - -print "About: " . $app->{'options'}->{'about'} - . "\n\n"; - -print "Stub:\n-----\n$app->{'state'}->{'license_stub'}\n-----\n"; - -find (sub { - find_files($app, ${File::Find::name}, $_) - }, $app->{'options'}->{'directory'}); - -# -# Subroutines -# -sub find_files { - my ($app, $file, $name) = @_; - - # - # Opportunity to filter any file types befrore any processing starts - # - handle_files ($app, $file, $name); -} - -sub handle_files { - my ($app, $file, $name) = @_; - - my $rh = new FileHandle ($name); - my @contents = <$rh>; - - print "Handling... $file\n"; - - if ((defined $contents[0]) && ($contents[0] =~ /^\s*#!/)) { - print " ... has #! line\n"; - } -} - -sub setup_license_stub { - my ($app) = @_; - my $header_file = $app->{'state'}->{'app_dir'} - ."/licenses/" - . $app->{'options'}->{'license'} . ".header"; - - if (-e $header_file) { - my $rh = new FileHandle ($header_file); - $app->{'state'}->{'license_stub'} = do {local $/; <$rh>}; - - $app->{'state'}->{'license_stub'} =~ - s/###DESCRIPTION###/$app->{'options'}->{'about'}/; - $app->{'state'}->{'license_stub'} =~ - s/###DATE###/$app->{'options'}->{'date'}/; - $app->{'state'}->{'license_stub'} =~ - s/###NAME###/$app->{'options'}->{'user'}/; - } - else { - warn "[WARN] License Header '$header_file' does not exist"; - } -} - -sub setup_license_pretty_names { - my ($app) = @_; - - $app->{'license_pretty_names'} = { - 'mit' => 'MIT', - 'gpl_v1' => 'GPL v1', - 'gpl_v2' => 'GPL v2', - 'gpl_v3' => 'GPL v3', - 'apache_v2' => 'Apache v2', - 'apache_v1' => 'Apache v1' - }; -} - -sub setup_filetype_comment_mapping { - my ($app) = @_; - - $app->{'filetype_commants'} = { - '.pl' => '#', - '.rb' => '#', - '.erb' => [''], - '.html' => [''], - '.v' => '//', - '.sv' => '//', - '.vr' => '//' - }; -} diff --git a/licenses/apache_v2.header b/licenses/apache_v2.header deleted file mode 100644 index 2c7a9d0..0000000 --- a/licenses/apache_v2.header +++ /dev/null @@ -1,13 +0,0 @@ -Copyright ###DATE### ###NAME### - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. diff --git a/licensor.pl b/licensor.pl new file mode 100755 index 0000000..7d6acd3 --- /dev/null +++ b/licensor.pl @@ -0,0 +1,123 @@ +#!/bin/env perl + +use strict; +use warnings; +use FindBin; +use lib "$FindBin::Bin"; +use File::Find; +use File::Basename; +use FileHandle; +use Data::Dumper; +use Getopt::Long; +use Cwd; +use File::Glob ':globally'; +use Licenses; + +# Application configuration and state storage +my $app = {}; + +# Defaults +$app->{'options'}->{'directory'} = getcwd; +$app->{'options'}->{'license'} = 'mit'; +$app->{'options'}->{'user'} = $ENV{'USER'}; +$app->{'options'}->{'date'} = (localtime(time))[5] + 1900; +$app->{'options'}->{'about'} = "This is an open-source software"; +$app->{'state'}->{'license_stub'} = "NON EXISTANT"; +$app->{'state'}->{'app_dir'} = dirname($0); + +# Command line options +my $correct_usage = GetOptions ( + 'directory=s' => \$app->{'options'}->{'directory'}, + 'license=s' => \$app->{'options'}->{'license'}, + 'about=s' => \$app->{'options'}->{'about'}, + 'user=s' => \$app->{'options'}->{'user'}, + 'date=s' => \$app->{'options'}->{'date'}, + ); + +setup_licenses($app); +setup_filetype_comment_mapping($app); + +print "Applying '" + . $app->{'licenses'}->{$app->{'options'}->{'license'}}->{'pretty_name'} + . "' license to files in '" + . $app->{'options'}->{'directory'} + . "' directory\n\n"; + +print "Copyright: Copyright (C) " + . $app->{'options'}->{'date'} + . " " . $app->{'options'}->{'user'} + . "\n\n"; + +print "About: " . $app->{'options'}->{'about'} + . "\n\n"; + +print "Stub:\n-----\n" + . $app->{'licenses'}->{$app->{'options'}->{'license'}}->{'header_stub'} + . "\n-----\n"; + +find (sub { + find_files($app, ${File::Find::name}, $_) + }, $app->{'options'}->{'directory'}); + +# +# Subroutines +# +sub find_files { + my ($app, $file, $name) = @_; + + # + # Opportunity to filter any file types befrore any processing starts + # + handle_files ($app, $file, $name); +} + +sub handle_files { + my ($app, $file, $name) = @_; + + return unless -f $name; + + my($filename, $directories, $suffix) = fileparse($file, qr/\.[^.]*/); + + if (exists $app->{'filetype_comments'}->{$suffix}) { + my $rh = new FileHandle ($name); + my @contents = <$rh>; + + print "Handling... $filename - $directories - $suffix\n"; + + if ((defined $contents[0]) && ($contents[0] =~ /^\s*#!/)) { + print " ... has #! line\n"; + } + } + else { + print "[ALERT] Don't know how to comment in file with '$suffix' extension\n"; + } +} + +sub setup_licenses { + my ($app) = @_; + + my $licenses = new Licenses; + $licenses->get_licenses($app); + + print "[INFO] Imported " + . join (', ', keys %{$app->{'licenses'}}) + . " licenses\n"; +} + +sub setup_filetype_comment_mapping { + my ($app) = @_; + + $app->{'filetype_comments'} = { + '.pl' => '#', + '.rb' => '#', + '.erb' => [''], + '.html' => [''], + '.css' => ['/*', '*/'], + '.coffee' => '#', + '.js' => '//', + '.scss' => '//', + '.v' => '//', + '.sv' => '//', + '.vr' => '//' + }; +}