get-licensed/get-licensed.pl

173 lines
4.4 KiB
Perl
Raw Normal View History

#!/bin/env perl
use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin";
use File::Find;
use File::Basename;
2013-03-26 01:22:42 +00:00
use File::Copy;
use FileHandle;
2013-03-26 01:22:42 +00:00
use File::Glob ':globally';
use Data::Dumper;
use Getopt::Long;
use Cwd;
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";
2013-03-26 01:22:42 +00:00
$app->{'options'}->{'dry'} = 0;
$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'},
2013-03-26 01:22:42 +00:00
'dry' => \$app->{'options'}->{'dry'},
);
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";
2013-02-26 09:35:02 +00:00
# print Data::Dumper->Dump([$app]);
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
#
2013-02-26 09:35:02 +00:00
return if ($file =~ /\.git\b/);
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}) {
2013-03-26 01:22:42 +00:00
my $rh = FileHandle->new($name);
my @contents = <$rh>;
2013-03-26 01:22:42 +00:00
print "Handling... $filename - $directories - $suffix ----- " .
ref($app->{'filetype_comments'}->{$suffix}) . "\n";
my $stub = get_license_stub($app, $suffix);
if ((defined $contents[0]) && ($contents[0] =~ /^\s*#!/)) {
print " ... has #! line\n";
2013-03-26 01:22:42 +00:00
$contents[0] = $contents[0] . "\n" . $stub . "\n";
} else {
$contents[0] = $stub . "\n" . $contents[0] . "\n";
}
if ($app->{'options'}->{'dry'} == 0) {
copy($name, $name . ".licensor_backup") or die "Could not create backup file";
my $wh = FileHandle->new($name, O_WRONLY);
print $wh @contents;
}
}
else {
print "[ALERT] Don't know how to comment in file with '$suffix' extension\n";
}
}
2013-03-26 01:22:42 +00:00
sub get_license_stub {
my ($app, $suffix) = @_;
my $stub = $app->{'licenses'}->{$app->{'options'}->{'license'}}->{'header_stub'};
if (ref($app->{'filetype_comments'}->{$suffix}) eq '') {
$stub =~ s/^(.)/$app->{'filetype_comments'}->{$suffix} $1/msg;
$stub = $app->{'filetype_comments'}->{$suffix} . " -----\n" .
"$stub\n" .
$app->{'filetype_comments'}->{$suffix} . " -----\n";
} elsif (ref($app->{'filetype_comments'}->{$suffix}) eq 'ARRAY') {
$stub =~ s/^(.)/ $1/msg;
$stub = $app->{'filetype_comments'}->{$suffix}[0] .
" -----\n$stub\n ----- " .
$app->{'filetype_comments'}->{$suffix}[1];
} else {
# not an option
}
$stub =~ s/\s+$//msg;
return $stub;
}
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'} = {
2013-02-26 09:35:02 +00:00
'.pl' => '#',
'.pm' => '#',
'.rb' => '#',
'.erb' => ['<!--', '-->'],
'.html' => ['<!--', '-->'],
'.css' => ['/*', '*/'],
'.coffee' => '#',
'.js' => '//',
'.scss' => '//',
'.v' => '//',
'.sv' => '//',
'.vr' => '//',
'.gitignore' => '#',
'.gitkeep' => '#'
};
}