Skip to content

Instantly share code, notes, and snippets.

@squioc
Created August 5, 2012 21:41
Show Gist options
  • Save squioc/3267234 to your computer and use it in GitHub Desktop.
Save squioc/3267234 to your computer and use it in GitHub Desktop.
a Lightened Build tool with bash script
action "say"
desc "say hello" #action description
#bash script here
echo "Hello world"
echo "Blank line doesn't stop block"
#Comment are unheeded
echo "executed out of function"
action "bye"
echo "byebye"
#!/usr/bin/env perl -w
use strict;
use Digest::SHA;
our $CACHE_DIR = "./";
our $BASH_HEADER="#!/bin/bash\n\n";
sub print_header
{
my ($desc) = @_;
print $desc $BASH_HEADER;
}
sub print_usage
{
my ($desc, %actions) = @_;
print $desc "function usage() {\n";
print $desc "\techo \"usage: $0 <command>\"\n";
print $desc "\techo \"\"\n";
print $desc "\techo \"commands:\"\n";
while( my ($action, $description) = each(%actions) ) {
print $desc "\techo \" $action - $description\"\n";
}
print $desc "}\n\n";
};
sub is_updated
{
my $sha = Digest::SHA->new(256);
$sha->addfile($_[0]);
my $digest_name = $sha->hexdigest;
if(! -f "$CACHE_DIR/$digest_name") {
return "$CACHE_DIR/$digest_name";
}
else {
return undef;
}
};
sub compile
{
my ($script, $compiled) = @_;
my $isBlock = 0;
my %actions = ();
my $current_action = "";
my $ligne;
open(my $SRC,"< $script");
open(my $DEST, "> $compiled");
print_header($DEST);
while(defined( $ligne = <$SRC>)) {
if( $ligne =~ m/^\s*action\s"([^"\s]+)".*$/ )
{
if($isBlock)
{
print $DEST "}\n\n";
$isBlock=0;
}
$current_action=$1;
$actions{$current_action} = "No description available";
print $DEST "function $1 {\n";
$isBlock=1;
}
elsif( $ligne =~ m/^\s*desc\s"([^"]+)".*$/ )
{
if($current_action ne "")
{
$actions{$current_action} = $1;
}
}
elsif ($ligne !~ m/^\s*#.*/ && $ligne !~ m/^$/)
{
if($isBlock && $ligne =~ m/^[^\s].*/)
{
print $DEST "}\n\n";
$isBlock=0;
$current_action = "";
}
print $DEST $ligne;
if($ligne !~ m/\n$/)
{
print $DEST "\n\n";
}
}
}
if($isBlock)
{
print $DEST "}\n\n";
$isBlock=0;
$current_action = -1;
}
print_usage($DEST,%actions);
print $DEST "command=\"\${1}\"\n";
print $DEST "case \"\$command\" in\n";
foreach my $action (keys(%actions)) {
print $DEST "\t$action)\n";
print $DEST "\t\tshift;\n";
print $DEST "\t\t$action \$@;\n";
print $DEST "\t\t;;\n";
}
print $DEST "\t*|help)\n";
print $DEST "\t\tusage;\n";
print $DEST "\t\t;;\n";
print $DEST "esac\n\n";
close($DEST);
close($SRC);
};
my $compiled;
if( defined( $compiled=is_updated("build.s") ) ) {
compile("build.s", $compiled);
chmod 0755, $compiled;
}
exec $compiled, @ARGV;
script execution:
$ ./compile.pl [<command>]
with <command>: help or one of defined action in build.s
--------------------------------------------------------
result of the compilation:
#!/bin/bash
function say {
echo "Hello world"
echo "Blank line doesn't stop block"
}
echo "executed out of function"
function bye {
echo "byebye"
}
function usage() {
echo "usage: ./compile.pl <command>"
echo ""
echo "commands:"
echo " bye - No description available"
echo " say - say hello"
}
command="${1}"
case "$command" in
bye)
shift;
bye $@;
;;
say)
shift;
say $@;
;;
*|help)
usage;
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment