";
my $line_counter = 1;
foreach my $line ( split("\n", $raw_file) ) {
if ( $line ne "" ) {
$line = check_for_html($line);
}
print $fh "$line_counter\t$line";
$line_counter++;
}
print $fh "
";
close $fh;
$logger->info("Generated line numbers for $filename");
return $html_file;
}
sub gen_diff_colors($$$) {
my $raw_diff = shift;
my $id = shift;
my $logger = shift;
my $html_diff;
open my $fh, '>>', \$html_diff or die "Can't open variable: $!";
#print $fh "";
print $fh "$id
";
my $line_counter = 1;
foreach my $line ( split("\n", $raw_diff) ) {
if ( $line ne "" ) {
$line = check_for_html($line);
}
if ( $line =~ m/^\+/ ) {
print $fh "$line";
} elsif ( $line =~ m/^\-/ ) {
print $fh "$line";
} elsif ( $line =~ m/^@@/ ) {
print $fh "$line";
} elsif ( $line =~ m/^diff/ ) {
print $fh "$line";
} elsif ( $line =~ m/^commit/ ) {
print $fh "$line";
} else {
print $fh "$line";
}
$line_counter++;
}
print $fh "
";
close $fh;
$logger->info("Generated colored diff for $id");
return $html_diff;
}
# Main sub for generating project page
# Might make more sense to split into more subs?
sub write_project_content($$$$$) {
my $project_dirs_ref = shift;
my $trimmed_project_dirs_ref = shift;
my $web_projects_dir = shift;
my $clone_path = shift;
my $logger = shift;
# Make these array's easier to work with in a hash
# Key is path to actual git dir, val is path to associated web dir
my %projects_map;
@projects_map{@$project_dirs_ref} = @$trimmed_project_dirs_ref;
$logger->info("Assembling data structures of git info");
# Write files part of project index
foreach my $project_path ( keys %projects_map ) {
my $spec_web_dir = $web_projects_dir . $projects_map{$project_path};
my $project_index = $spec_web_dir . "index.html";
write_file("",$project_index);
append_file("Return to index",$project_index);
my $uniq_clone_path = "Disabled";
if ( $clone_path ne "Disabled" ) {
$uniq_clone_path = $clone_path . $projects_map{$project_path};
}
append_file("Clone: git clone $uniq_clone_path
",$project_index);
# Get all project data structures/info
my ( $file_tree_ref, $file_content_ref, $commits_ref, $commit_ids_ref ) = get_file_tree($project_path,$logger);
# Handle README
if ( grep /^README.md$/, keys %$file_content_ref ) {
$logger->info("$projects_map{$project_path} contains a README");
my $readme_html = render_readme(${$file_content_ref}{'README.md'},$logger);
append_file("$readme_html",$project_index);
}
append_file("Files for $projects_map{$project_path}
",$project_index);
append_file("
",$project_index);
## Write files ##
append_file("
File | Commit |
",$project_index);
foreach my $filename ( sort keys %$file_content_ref ) {
my $browserCompat = $filename . ".html";
# Rewrite dir paths so we can save on disk without producing actual dir structure
if ( $filename =~ m/\// ) {
my $copy = $filename;
$copy =~ s/\//_/g;
$browserCompat = $copy . ".html";
}
append_file("$filename | ${$file_tree_ref}{$filename} | ",$project_index);
my $html_file = gen_line_nums(${$file_content_ref}{$filename},$filename,$logger);
write_file("$html_file",$spec_web_dir . $browserCompat);
}
append_file("
",$project_index);
append_file("
", $project_index);
append_file("Logs for $projects_map{$project_path}
",$project_index);
append_file("",$project_index);
append_file("
",$project_index);
# iterate over array to keep ordering
foreach my $commit_id ( @$commit_ids_ref ) {
my $filename = $commit_id . ".html";
append_file("$filename | ",$project_index);
my $html_diff = gen_diff_colors(${$commits_ref}{$commit_id},$commit_id,$logger);
write_file($html_diff,$spec_web_dir . $filename);
}
append_file("
",$project_index);
append_file("",$project_index);
}
$logger->info("Done writing files");
}
# Not used currently, need to do more trimming/etc
# TODO
# Work around is rm -rf the webroot manually and then just rerun gsg
sub clean_web_root($$$) {
my $web_projects_dir_path = shift;
my $git_projects_ref = shift;
my $logger = shift;
my $lsCmd = findBin("ls",$logger);
my $rmCmd = findBin("rm",$logger);
foreach my $dir ( split("\n", shellex("$lsCmd -d $web_projects_dir_path/*/",$logger)) ) {
if ( ! grep( /^$dir$/, @$git_projects_ref ) ) {
$logger->info("Found $dir in webroot but not in git root, removing...");
my $rmdir = $web_projects_dir_path . $dir;
$logger->info("Would remove $rmdir");
# Does this need to be safer? TODO
#shellex("$rmCmd $rmdir/*",$logger);
#shellex("$rmCmd -d $rmdir",$logger);
}
}
}
1;