Ir para o conteúdo

Contagem das linhas de código

Script que permite saber quantas linhas de código foram escritas e em que linguagens escolhendo o directório em que se encontram os ficheiros.

#! /usr/bin/perl -W
# Stats about your code

#  GPL
#
#  codestats - Written by Diogo Sousa aka orium
#  Copyright (C) 2008 Diogo Sousa
# 
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  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 <http://www.gnu.org/licenses/>.

use strict;

my %result_files;
my %result_lines;

sub search_dir
{
    my $mydir=shift;
    my @files='ls -1 \'$mydir\'';
    my $aux='%F';
    my $stat;
    my $filename;
    my $lines;
    my $type;

    chomp(@files);

    foreach my $file (@files)
    {
        $filename=$mydir.$file;

        # "Lista" de exclusões

        if ($filename eq $ENV{HOME}."/c/documentation"
            or $filename eq $ENV{HOME}."/c/encrypter/encrypter/include"
            or $file eq "bak"
            or $file eq "old"
            or $file =~ '.*\.bak$'
            or $file eq "cvs-clean.pl"
            or $file eq "conf.change.pl"
            or $file eq "config.pl")
        {
            next;
        }

        $stat='stat --format=$aux \'$filename\'';
        chomp($stat);

        if ($stat eq "directory")
        {
            search_dir($filename."/");
        }
        else
        {
            if ($filename =~ '.*\.(c|h|cc|cpp|pl|lisp)$') # Extensões que nos interessam
            {
                if ($1 eq "cpp")
                {
                    $type="cc";
                }
                else
                {
                    $type=$1;
                }

                $lines='wc -l \'$filename\'';
                chomp($lines);
                $lines =~ '^([0-9]*)';

                $result_files{$type}++;
                $result_lines{$type}+=$1;
            }
        }
    }

}

sub show_stats
{
    my $files_total=0;
    my $lines_total=0;

    foreach my $i (values %result_files)
    {
        $files_total+=$i;
    }

    foreach my $i (values %result_lines)
    {
        $lines_total+=$i;
    }

    print("Files:\n");

    foreach my $i (sort keys %result_files)
    {
        print("\t$i\t",$result_files{$i},"\t",$result_files{$i}/$files_total*100,"%\n");
    }

    print("\tTotal: $files_total\n");

    print("\n");

    print("Lines:\n");

    foreach my $i (sort keys %result_lines)
    {
        print("\t$i\t",$result_lines{$i},"\t",$result_lines{$i}/$lines_total*100,"%\n");
    }

    print("\tTotal: $lines_total\n");
}

my $path;

$path=$ENV{HOME}."/c/"; # Caminho onde esta o código

print("Analyzing code in $path\n");

search_dir($path);
show_stats();

Nota: O script está em Perl, mas deve ser fácil de adaptar para às vossas linguagens preferidas.