Skip to content

Commit

Permalink
Include a basic(ish) version of NIC, the New Instance Creator (you ca…
Browse files Browse the repository at this point in the history
…n call him Nicolas!), and a set of nic templates. NIC replaces mshook_layout.sh with a flexible(ish?) template system!

Also include a random Makefile change I forgot about until I saw it being committed. :(.


git-svn-id: http://svn.howett.net/svn/theos/trunk@302 4410221e-0ddf-4ce3-99c0-2db6c0dbc727
  • Loading branch information
dhowett committed Aug 6, 2010
1 parent d99f265 commit 6c62361
Show file tree
Hide file tree
Showing 10 changed files with 503 additions and 31 deletions.
3 changes: 3 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ http://www.gnu.org/licenses/gpl-3.0.html.
Projects created using Theos and/or Logos are not considered derivative works
(from a licensing standpoint, or, for that matter, any other standpoint) and
are, as such, not required to be licensed under the GNU GPL.

The included project templates are license-free. The use of a template does
not confer a license to your project.
31 changes: 31 additions & 0 deletions bin/denicify.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/perl

use warnings;

$nicfile = $ARGV[0] if($ARGV[0]);
$outputdir = $ARGV[1];
die if !$nicfile || !$outputdir;

buildNic($nicfile, $outputdir);
sub buildNic {
my $template = shift;
my $dir = shift;
mkdir($dir);
open(my($fh), "<", "$nicfile") or die $!;
chdir($dir) or die $!;
while(<$fh>) {
if(/^dir (.+)$/) {
mkdir($1);
} elsif(/^file (\d+) (.+)$/) {
my $lines = $1;
my $filename = $2;
open(my($nicfile), ">", "./$filename");
while($lines > 0) {
my $line = <$fh>;
print $nicfile ($line);
$lines--;
}
close($nicfile);
}
}
}
213 changes: 213 additions & 0 deletions bin/nic.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
#!/usr/bin/perl

use warnings;
use FindBin;
use Getopt::Long;
use Cwd 'abs_path';
use File::Spec;
use File::Find;
use File::Copy;
use User::pwent;
use POSIX qw(getuid);

my @_dirs = File::Spec->splitdir(abs_path($FindBin::Bin));
$_dirs[$#_dirs]="templates";
my $_templatepath = File::Spec->catdir(@_dirs);
$#_dirs--;
my $_theospath = File::Spec->catdir(@_dirs);

my @templates = getTemplates();

my %CONFIG = ();
loadConfig();

my $clean_project_name = "";
my $project_name = "";
my $package_prefix = $CONFIG{'package_prefix'};
$package_prefix = "com.yourcompany" if !$package_prefix;
my $package_name = "";
my $username = $CONFIG{'username'};
$username = "" if !$username;
my $template;

my $nicfile = "";

Getopt::Long::Configure("bundling");

GetOptions( "packagename|p=s" => \$package_name,
"name|n=s" => \$project_name,
"user|u=s" => \$username,
"nic=s" => \$nicfile,
"template|t=s" => \$template);

$project_name = $ARGV[0] if($ARGV[0]);

print "NIC 0.0.1 - New Instance Creator",$/;
print "--------------------------------",$/;

$template = $nicfile if $nicfile ne "";
if(!$template) {
$template = promptList(undef, "Choose a Template (required)", @templates);
}
$nicfile = "$_templatepath/$template.nic" if $nicfile eq "";
die "Couldn't open template at path $nicfile" if(! -f $nicfile);

promptIfMissing(\$project_name, undef, "Project Name (required)");
exitWithError("I can't live without a project name! Aieeee!") if !$project_name;
$clean_project_name = cleanProjectName($project_name);

$package_name = $package_prefix.".".packageNameIze($project_name) if $CONFIG{'skip_package_name'};
promptIfMissing(\$package_name, $package_prefix.".".packageNameIze($project_name), "Package Name");

promptIfMissing(\$username, getUserName(), "Authour/Maintainer Name");

my $directory = lc($clean_project_name);
if(-d $directory) {
my $response;
promptIfMissing(\$response, "N", "There's already something in $directory. Continue");
exit 1 if(uc($response) eq "N");
}

print "Instantiating $template in ".lc($clean_project_name)."/...",$/;
buildNic($template);
print "Done.",$/;

sub promptIfMissing {
my $vref = shift;
return if(${$vref});

my $default = shift;
my $prompt = shift;

if($default) {
print $prompt, " [$default]: ";
} else {
print $prompt, ": ";
}

$| = 1; $_ = <STDIN>;
chomp;

if($default) {
${$vref} = $_ ? $_ : $default;
} else {
${$vref} = $_;
}
}

sub promptList {
my $default = shift;
my $prompt = shift;
my @list = @_;

$default = -1 if(!defined $default);

map { print " ".($_==$default?">":" ")."[".($_+1).".] ",$list[$_],$/; } (0..$#list);
print $prompt,": ";
$| = 1;
my $idx = -1;
while(<STDIN>) {
chomp;
if($default > -1 && $_ eq "") {
$idx = $default;
last;
}
if($_ < 1 || $_ > $#list+1) {
print "Invalid value.",$/,$prompt,": ";
next;
}
$idx = $_-1;
last;
}
return $list[$idx];
}

sub exitWithError {
my $error = shift;
print STDERR "[error] ", $error, $/;
exit 1;
}

sub getTemplates {
our @templates = ();
find({wanted => \&templateWanted, no_chdir => 1}, $_templatepath);
sub templateWanted {
if(-f && /\.nic$/) {
my $template = substr($_,length($_templatepath)+1);
$template =~ s/\.nic$//;
push(@templates, $template);
}
}
return sort @templates;
}

sub packageNameIze {
my $name = shift;
$name =~ s/ //g;
$name =~ s/[^\w\+-.]//g;
return lc($name);
}

sub cleanProjectName {
my $name = shift;
$name =~ s/ //g;
$name =~ s/\W//g;
return $name;
}

sub getUserName {
my $pw = getpw(getuid());
my ($fullname) = split(/\s*,\s*/, $pw->gecos);
return $fullname ? $fullname : $pw->name;
}

sub getHomeDir {
my $pw = getpw(getuid());
return $pw->dir;
}

sub buildNic {
my $template = shift;
my $dir = lc($clean_project_name);
open(my($fh), "<", $nicfile) or die $!;
mkdir($dir);
chdir($dir) or die $!;
while(<$fh>) {
if(/^dir (.+)$/) {
mkdir($1);
} elsif(/^file (\d+) (.+)$/) {
my $lines = $1;
my $filename = $2;
$filename = substitute($filename);
open(my($nicfile), ">", "./$filename");
while($lines > 0) {
my $line = <$fh>;
print $nicfile (substitute($line));
$lines--;
}
close($nicfile);
}
}
close($fh);
symlink($_theospath, "theos");
}

sub substitute {
my $in = shift;
$in =~ s/\@\@FULLPROJECTNAME\@\@/$project_name/g;
$in =~ s/\@\@PROJECTNAME\@\@/$clean_project_name/g;
$in =~ s/\@\@PACKAGENAME\@\@/$package_name/g;
$in =~ s/\@\@USER\@\@/$username/g;
return $in;
}

sub loadConfig {
open(my $cfh, "<", getHomeDir()."/.nicrc") or return;
while(<$cfh>) {
if(/^(\w+)\s*=\s*\"(.*)\"$/) {
my $key = $1;
my $value = $2;
$CONFIG{$key} = $value;
}
}
}
46 changes: 46 additions & 0 deletions bin/nicify.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/perl

use warnings;
use File::Find;

@directories = ();
%files = ();

chdir $ARGV[0];
find({wanted => \&processDirectories, follow => 0, no_chdir => 1}, ".");
find({wanted => \&processFiles, follow => 0, no_chdir => 1}, ".");

foreach $dir (@directories) {
print "dir $dir",$/;
}

foreach $filename (keys %files) {
my @lines = split(/\n/, $files{$filename});
my $ln = scalar(@lines);
print "file $ln $filename",$/;
print $files{$filename},$/;
}

sub processDirectories {
return if(! -d $_);
return if(/\.svn/);
return if(/^.$/);
s/^\.\///;
push(@directories, $_);
}

sub processFiles {
return if(! -f $_);
return if(/\.svn/);
return if(/\.nic$/);
s/^\.\///;
$files{$_} = slurp($_);
}

sub slurp {
my $fn = shift;
open(my($fh), "<", $fn);
local $/ = undef;
my $d = <$fh>;
return $d;
}
2 changes: 1 addition & 1 deletion documentation/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
all: makefiles.html

%.html: %.docbook
xsltproc --output $@ /opt/local/share/xsl/docbook-xsl/xhtml/docbook.xsl $<
xsltproc --output $@ /usr/share/sgml/docbook/xsl-stylesheets/xhtml/docbook.xsl $<

clean:
rm *.html
89 changes: 89 additions & 0 deletions templates/iphone/application.nic
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
dir Resources
file 16 Resources/Info.plist
{
CFBundleExecutable = "@@PROJECTNAME@@";
CFBundleIconFile = icon.png;
CFBundleIdentifier = "@@PACKAGENAME@@";
CFBundleInfoDictionaryVersion = 6.0;
CFBundlePackageType = APPL;
CFBundleSignature = "????";
CFBundleSupportedPlatforms = (
iPhoneOS
);
CFBundleVersion = 1.0;
DTPlatformName = iphoneos;
DTSDKName = iphoneos3.0;
LSRequiresIPhoneOS = 1;
MinimumOSVersion = 3.0;
}
file 8 RootViewController.mm
#import "RootViewController.h"

@implementation RootViewController
- (void)loadView {
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view.backgroundColor = [UIColor redColor];
}
@end

file 8 main.m
int main(int argc, char **argv) {
NSAutoreleasePool *p = [[NSAutoreleasePool alloc] init];
int ret = UIApplicationMain(argc, argv, @"@@PROJECTNAME@@Application", @"@@PROJECTNAME@@Application");
[p drain];
return ret;
}

// vim:ft=objc

file 25 @@PROJECTNAME@@Application.mm
#import "RootViewController.h"

@interface @@PROJECTNAME@@Application: UIApplication <UIApplicationDelegate> {
UIWindow *_window;
RootViewController *_viewController;
}
@property (nonatomic, retain) UIWindow *window;
@end

@implementation @@PROJECTNAME@@Application
@synthesize window = _window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
_viewController = [[RootViewController alloc] init];
[_window addSubview:_viewController.view];
[_window makeKeyAndVisible];
}

- (void)dealloc {
[_window release];
[super dealloc];
}
@end

// vim:ft=objc

file 3 RootViewController.h
@interface RootViewController: UIViewController {
}
@end

file 6 Makefile
include theos/makefiles/common.mk

APPLICATION_NAME = @@PROJECTNAME@@
@@PROJECTNAME@@_FILES = main.m @@PROJECTNAME@@Application.mm RootViewController.mm

include $(FW_MAKEDIR)/application.mk

file 9 control
Package: @@PACKAGENAME@@
Name: @@FULLPROJECTNAME@@
Depends:
Version: 0.0.1
Architecture: iphoneos-arm
Description: An awesome application!
Maintainer: @@USER@@
Author: @@USER@@
Section: Utilities

Loading

0 comments on commit 6c62361

Please sign in to comment.