Alexander Nazarov (nfokz) wrote in changelog,
Alexander Nazarov
nfokz
changelog

[livejournal] r22288: LJSUP-12621: Create script to generate w...

Committer: anazarov
LJSUP-12621: Create script to generate widget skeleton
A   trunk/bin/js/authors.conf
A   trunk/bin/js/factory.pl
A   trunk/bin/js/widget.tmpl
Added: trunk/bin/js/authors.conf
===================================================================
--- trunk/bin/js/authors.conf	                        (rev 0)
+++ trunk/bin/js/authors.conf	2012-06-20 11:20:19 UTC (rev 22288)
@@ -0,0 +1,6 @@
+# Author aliases for Widget Factory
+{
+    dpetrov  => 'dpetrov@sup.com (Dmitry Petrov)',
+    anazarov => 'anazarov@sup.com (Alexander Nazarov)',
+}
+# vim: filetype=perl

Added: trunk/bin/js/factory.pl
===================================================================
--- trunk/bin/js/factory.pl	                        (rev 0)
+++ trunk/bin/js/factory.pl	2012-06-20 11:20:19 UTC (rev 22288)
@@ -0,0 +1,87 @@
+#!/usr/bin/perl -w
+use strict;
+use HTML::Template;
+use File::Basename;
+use Getopt::Long;
+use vars qw{ $home $authors $author $extends $path $public $private $name $preview };
+
+chdir dirname $0 or die 'Could not change working directory';
+
+$home    = join('/', $ENV{'LJHOME'}, qw{ htdocs js jquery });
+$authors = do 'authors.conf';
+
+ref $authors and ref $authors eq 'HASH' or die 'Invalid authors.conf format';
+
+@ARGV and GetOptions(
+    'name=s'    => \$name,
+    'author=s'  => \$author, 
+    'public=s'  => \$public, 
+    'private=s' => \$private, 
+    'extends=s' => \$extends, 
+    'path=s'    => \$path, 
+    'preview'   => \$preview, 
+) or die <<"";
+LJ Widget Factory\n
+Usage $0:
+\t--name\t\tWidget name
+\t--author\tWidget author username, taken from authors.conf
+\t--extends\tParent widget, defaults to jquery.lj.basicWidget.js 
+\t--path\t\tRelative path from $home, defaults to ./
+\t--public\tComma-separated list of public methods
+\t--private\tComma-separated list of private methods
+\t--preview\tWrite result to STDOUT
+
+# Check path
+-d join('/', $home, $path) or die "Target directory $home/$path not exists" if $path;
+
+# Check name
+$name or die "Widget name required";
+
+my $file = join('/', $home, $path? $path : (), "jquery.lj.$name.js");
+
+# Check file
+-f $file and die "File $file already exists";
+
+# Check author
+if ( $author ) {
+    exists $authors->{$author} or die "Author '$author' was not found in authors.conf";
+
+    $author = $authors->{$author};
+}
+
+# Check parent widget
+if ( $extends ) {
+    -f join('/', $home, $extends) or die "Widget '$extends' was not found in $home";
+    $extends = basename $extends;
+    $extends =~ s{^jquery\.lj\.|\.js$} {}g;
+}
+
+# Transform method lists
+{
+    no strict 'refs';
+    $$_ = [map {{ method => $_ }} split m{(?<!\\),}, $$_] foreach grep $$_, qw{ public private };
+}
+
+# Generate template
+my $template = new HTML::Template
+    loop_context_vars => 1,
+    filename          => 'widget.tmpl';
+
+$template->param(
+    name        => $name,
+    author      => $author,
+    extends     => $extends,
+    has_public  => $public?  1 : 0,
+    has_private => $private? 1 : 0,
+    public      => $public  || [],
+    private     => $private || [],
+);
+
+# Write output
+if ( $preview ) {
+    print $template->output();
+} else {
+    open WIDGET, '+>', $file or die "Could not create file $file";
+    print WIDGET $template->output();
+    close WIDGET;
+}


Property changes on: trunk/bin/js/factory.pl
___________________________________________________________________
Added: svn:executable
   + *

Added: trunk/bin/js/widget.tmpl
===================================================================
--- trunk/bin/js/widget.tmpl	                        (rev 0)
+++ trunk/bin/js/widget.tmpl	2012-06-20 11:20:19 UTC (rev 22288)
@@ -0,0 +1,44 @@
+/**
+ * @name $.lj.<TMPL_VAR name>
+ * @requires $.ui.core, $.ui.widget, $.lj.basicWidget<TMPL_IF extends>, $.lj.<TMPL_VAR extends></TMPL_IF>
+ * @class TODO: Write description here
+<TMPL_IF author> * @author <TMPL_VAR author>
+</TMPL_IF> */
+
+(function($, window) {
+	'use strict';
+
+	/** @lends $.lj.<TMPL_VAR name>.prototype */
+	$.widget('lj.<TMPL_VAR name>', <TMPL_IF extends>$.lj.<TMPL_VAR extends><TMPL_ELSE>$.lj.basicWidget</TMPL_IF>, {
+		options: {
+		},
+
+		classNames: {
+		},
+
+		selectors: {
+		},
+
+		templates: {
+		},
+
+		_create: {
+			$.lj.basicWidget.prototype._create.apply(this);
+
+			this._bindControls();
+		},
+
+		_bindControls: {
+		}<TMPL_IF has_private>,<TMPL_ELSE><TMPL_IF has_public>,</TMPL_IF></TMPL_IF><TMPL_LOOP private>
+
+		_<TMPL_VAR method>: {
+		}<TMPL_UNLESS __last__>,</TMPL_UNLESS></TMPL_LOOP><TMPL_LOOP public><TMPL_IF __first__>,
+		</TMPL_IF>
+		/**
+		 * TODO: Write description here
+		 */
+		<TMPL_VAR method>: {
+		}<TMPL_UNLESS __last__>,</TMPL_UNLESS>
+		</TMPL_LOOP>
+	});
+})(jQuery, window);

Tags: anazarov, conf, livejournal, nfokz, pl, tmpl
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments