Source

schemingmind_tuning / build_extension.pl

The default branch has multiple heads

#!/usr/bin/perl -w
# -*- coding: utf-8 -*-
#
# Skrypt budujący XPI

use strict;
use Archive::Zip;
use FindBin;
use File::Spec::Functions;
use File::Copy;
use Tie::File;
use XML::LibXSLT;
use XML::LibXML;

chdir $FindBin::Bin or die "chdir failed: $!\n";

#################################################################
# Ustalenie i patchowanie numerka wersji
#################################################################

my $what = shift @ARGV || '';
unless ($what =~ /^(major|minor|patch|devel|rebuild)$/) {
    print "Usage:\n$0 [major|minor|patch|devel|rebuild]\n";
    exit(1);
}

my $version;

if ($what ne 'devel') {
    my $status = `hg status smtuning`;
    if ($status) {
        die "Uncommited changes found\n";
    }
    my @newest = (0,0,0);
    open(F, "hg tags|");
    while(<F>) {
        if(/^(\d+)\.(\d+)\.(\d+)\b/) {
            if( $1 > $newest[0]
                  || ($1 == $newest[0]
                        && ($2 > $newest[1]
                              || ($2 == $newest[1] && $3 > $newest[2])))) {
                @newest = ($1, $2, $3);
            }
        }
    }
    close(F);

    if ($what eq 'major') {
        $version = join('.', $newest[0]+1, 0, 0);
    }
    elsif ($what eq 'minor') {
        $version = join('.', $newest[0], $newest[1]+1, 0);
    }
    elsif ($what eq 'patch') {
        $version = join('.', $newest[0], $newest[1], $newest[2]+1);
    }
    elsif ($what eq 'rebuild') {
        $version = join('.', $newest[0], $newest[1], $newest[2]);
    }
    else {
        die "Ups\n";
    }

    print "Creating version: $version"
      . ($what eq 'rebuild' ? " (rebuild - don't do it if already published)": "")
      . "\n";
    my $x;
    do {
        {
          local $/ = undef;
          print "Is it OK (y/n): ";
        }
        $x = <STDIN>;
        $x =~ s/[\s\r\n]+//g;
        $x = lc($x);
    } while $x !~ /^[yn]$/;
    if ($x eq 'n') {
        exit(0);
    }

    foreach my $filename ('smtuning/install.rdf',
                          'smtuning/locale/en-US/options.dtd',
                          #'smtuning/update.rdf',
                          'smtuning/content/options.xul',
                          'smtuning/content/about.xul') {
        my @array;
        tie @array, 'Tie::File', $filename
          or die "Can't read $filename: $!\n";
        foreach (@array) {
            s/(<em:version>|<version>| - version |\&version; |em:version=")\d+\.\d+(\.\d+)?/$1$version/;
        }
    }

    system("hg commit -m 'version patch $version'") and die $!;
    system("hg tag $version") and die $!;
}
else {
    $version = 'devel';
}

#################################################################
# Pliki/katalogi
#################################################################

my $EXT_DIR = "$FindBin::Bin/smtuning";
my $APP_NAME = 'smtuning';
my $JAR_FILE = catfile($FindBin::Bin, 'smtuning.jar');
my $XPI_FILE = catfile($FindBin::Bin, "smtuning-$version.xpi");

#################################################################
# Budowa pliku JAR
#################################################################

my $valid_files = sub {
    ! /~$/ 
    && ! /semantic\.cache/;
};

# Robimy smtuning.jar
my $jar = new Archive::Zip;
$jar->addTree(catdir($EXT_DIR, 'content'), 'content', $valid_files);
$jar->addTree(catdir($EXT_DIR, 'skin'), 'skin', $valid_files);
$jar->addTree(catdir($EXT_DIR, 'locale'), 'locale', $valid_files);
#foreach my $piece (@PIECES) {
#    $jar->addTree(catdir($PIECES_DIR, $piece),
#                  'content/pieces/' . $piece);
#}
$jar->writeToFileNamed($JAR_FILE);


#################################################################
# Budowa pliku XPI
#################################################################

# Uwaga: modyfikacja chrome.manifest by wskazywał JAR!!!
#   chrome.manifest install.rdf defaults

{
    my $xpi = new Archive::Zip;
    $xpi->addTree(catdir($EXT_DIR, 'defaults'), 'defaults', $valid_files);
    $xpi->addFile(catfile($EXT_DIR, 'install.rdf'), 'install.rdf');
    #$xpi->addFile(catfile($EXT_DIR, 'chrome.manifest'), 'chrome.manifest');
    open(M, "< " . catfile($EXT_DIR, 'chrome.manifest'));
    local $/ = undef;
    my $chrome_txt = <M>;
    close(M);
    $chrome_txt =~ s{^(content\s+\S*\s+)(\S*/)(.*)$}
                    {$1jar:chrome/$APP_NAME\.jar!/$2$3}mg;
    $chrome_txt =~ s{^(skin|locale)(\s+\S*\s+\S*\s+)(.*/)$}
                    {$1$2jar:chrome/$APP_NAME\.jar!/$3}mg;
    print $chrome_txt, "\n";
    $xpi->addString($chrome_txt, 'chrome.manifest');
    $xpi->addFile($JAR_FILE, 'chrome/smtuning.jar');
    
    $xpi->writeToFileNamed($XPI_FILE);
}

#################################################################
# Wyliczenie sumy kontrolnej
#################################################################

my $sum = `sha1sum -b $XPI_FILE` or die "Can't get the control sum for $XPI_FILE\n";
$sum =~ s/ .*$//;
chomp($sum);

#################################################################
# Aktualizacja update.rdf
#################################################################

my $update_rdf = "smtuning/update.rdf";

my $xml_parser = XML::LibXML->new();

my $doc = $xml_parser->parse_file($update_rdf);

my $node = $doc->findnodes("//em:updates//RDF:Description")->[0];
die "Bad update.rdf" unless $node;

my $v = $node->findnodes("//em:version")->[0];
$v->removeChildNodes();
$v->appendText($version);

$v = $node->findnodes("//em:updateLink")->[0];
my $txt = $v->textContent();
$txt =~ s/-[^-]+\.xpi/-$version.xpi/;
$v->removeChildNodes();
$v->appendText($txt);

$v = $node->findnodes("//em:updateHash")->[0];
$v->removeChildNodes();
$v->appendText("sha1:$sum");

#print $node->toString();
#print $doc->toString(0);

my $state = $doc->toFile($update_rdf, 0);


# Tak dopisalibyśmy nową notkę. Ale mccoy to zmasakruje, lepiej
# zostawmy pojedynczą.
# my $new_entry = <<END;
#         <RDF:li>
#           <RDF:Description>
#             <em:version>$version</em:version>
#             <em:targetApplication>
#               <RDF:Description>
#                 <em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
#                 <em:minVersion>1.5</em:minVersion>
#                 <em:maxVersion>3.0.0.*</em:maxVersion>
#                 <em:updateLink>http://mekk.waw.pl/download/smtuning/smtuning-$version.xpi</em:updateLink>
#                 <em:updateInfoURL>http://mekk.waw.pl/mk/eng/chess/art/smtuning/history</em:updateInfoURL>
#                 <em:updateHash>sha1:$sum</em:updateHash>
#               </RDF:Description>
#             </em:targetApplication>
#           </RDF:Description>
#         </RDF:li>
# END
#for (my $idx = 0; $idx < @array; ++ $idx) {
#    if($array[$idx] =~ /^\s*<RDF:li>/) {
#        splice @array, $idx-1, 0, split("\n", $new_entry);
#        last;
#    }
#}
#unless ($start && $end) {
#    die "Strange update.rdf\n";
#}
#splice @array, $start, $end-$start+1, split("\n", $new_entry);


#################################################################
# Podpisanie update.rdf
#################################################################

copy("smtuning/update.rdf", "update-$version.rdf");

print "Uruchom McCoy-a i każ mu podpisać (Sign) plik update-$version.rdf\n";
print "Następnie uruchom install_extension.pl\n";
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.