Skip to content

Instantly share code, notes, and snippets.

@ruby0x1
Last active August 29, 2015 14:16
Show Gist options
  • Save ruby0x1/488bb826e5e702adf237 to your computer and use it in GitHub Desktop.
Save ruby0x1/488bb826e5e702adf237 to your computer and use it in GitHub Desktop.
#use this class as our entry point
-main HaxelibCounter
#generate a neko file from the code
-neko run.n
#use the arguable lib as a dependency
-lib arguable
#specify the define, which we can comment out to disable
-D haxelibcounter_verbose
#execute our new file with neko to test it immediately
-cmd neko run.n
#use this class as our entry point
-main HaxelibCounter
#is this a debug build? leave this line enabled
#-debug
#generate a neko file from the code
-neko run.n
#use the arguable lib as a dependency
-lib arguable
#specify the define, which we can comment out to disable
#-D haxelibcounter_verbose
#execute our new file with neko to test it immediately
-cmd neko run.n --count --show-names
import arguable.ArgParser;
using StringTools;
class HaxelibCounter {
//The main entry point
static function main() {
var args = ArgParser.parse( Sys.args() );
print('haxelibcounter');
debug('> found args $args');
if(args.length == 0) {
return usage();
}
if(args.has('count')) {
return do_count(args);
}
} //main
static function usage() {
print('usage:');
print(' --count | displays the number of haxelib libraries installed');
print(' --show-names | must be specified with --count, lists installed haxelibs one per line');
}
static function do_count(args) {
//call `haxelib list` and read the output:
var process = new sys.io.Process('haxelib',['list']);
var result = process.stdout.readAll().toString();
debug('> haxelib list returned a ${result.length} length string');
if(result.length > 0) {
//split the list up by each line
var items = result.split('\n');
//remove any blank items from the list, in case haxelib adds empty lines
items = items.filter(function(item) return item.length > 0);
//we know how many there are now
print('${items.length} haxelibs installed!');
//now we can handle the additional flag
if(args.has('show-names')) {
var item_names = items.map(function(item) return item.split(': ')[0]);
for(name in item_names) print(' $name');
}
} else {
print('An unknown error occured. `haxelib list` was not able to be called, it returned status ${process.exitCode()}');
}
} //do_count
//Internal
static function print(value) {
#if debug trace(value);
#else Sys.println(value);
#end
}
static function debug(value) {
#if haxelibcounter_verbose
trace(value);
#end
}
} //HaxelibCount
{
"name": "haxelibcounter",
"url" : "https://github.com/underscorediscovery/haxe-entry-point-example/",
"license": "MIT",
"tags": ["cross"],
"description": "Haxe entry point - An article on getting started with Haxe",
"version": "1.0.0",
"releasenote": "Initial version",
"contributors": ["underscorediscovery"]
}
> haxelib run haxelibcounter
HaxelibCounter.hx:69: haxelibcounter
HaxelibCounter.hx:76: > found args { any => true, length => 0, valid => [], invalid => [{ name => /Users/sven/dev/haxe-entry-point-example/, value => }] }
HaxelibCounter.hx:69: usage:
HaxelibCounter.hx:69: --count | displays the number of haxelib libraries installed
HaxelibCounter.hx:69: --show-names | must be specified with --count, lists installed haxelibs one per line
HaxelibCounter.hx:68: > found args { any => true, length => 1, valid => [{ name => count, value => }], invalid => [] }
HaxelibCounter.hx:68: > haxelib list returned a 1409 length string
HaxelibCounter.hx:48: 42 haxelibs installed!
haxelib run haxelibcounter --count --show-names
haxelibcounter
43 haxelibs installed!
actuate
arguable
box2d
...
> haxelib install arguable
Downloading arguable-1,0,2.zip...
Download complete : 12071 bytes in 0.2s (47.7KB/s)
Install .gitignore
Install README.md
Created arguable/
Install arguable/ArgParser.hx
Install arguable/Stack.hx
Install haxelib.json
Created test/
Install test/Test.hx
Install test/TestArguable.n
Install test/build.hxml
Current version is now 1.0.2
Done
static function main() {
var args = ArgParser.parse( Sys.args() );
debug('> found args $args');
if(args.length == 0) {
return usage();
}
if(args.has('count')) {
return do_count(args);
}
} //main
static function usage() {
trace('haxelibcounter usage:');
trace(' --count | displays number of haxelib libraries installed');
trace(' --show-names | must be specified with --count, lists installed haxelibs one per line');
}
static function do_count(args) {
//call `haxelib list` and read the output:
var process = new sys.io.Process('haxelib',['list']);
var result = process.stdout.readAll().toString();
debug('> haxelib list returned a ${result.length} length string');
if(result.length > 0) {
//split the list up by each line
var items = result.split('\n');
//remove any blank items from the list, in case haxelib adds empty lines
items = items.filter(function(item) return item.length > 0);
//we know how many there are now
trace('${items.length} haxelibs installed!');
//:todo: handle --show-names
} else {
trace('An unknown error occured. `haxelib list` was not able to be called, it returned status ${process.exitCode()}');
}
} //do_count
//we know how many there are now
trace('${items.length} haxelibs installed!');
//now we can handle the additional flag
if(args.has('show-names')) {
var item_names = items.map(function(item) return item.split(': ')[0]);
for(name in item_names) print(' $name');
}
static function debug(value) {
#if haxelibcounter_verbose
trace(value);
#end
}
static function print(value) {
#if debug trace(value);
#else Sys.println(value);
#end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment