Skip to content

Commit

Permalink
Added global structure
Browse files Browse the repository at this point in the history
Added externs listing
  • Loading branch information
fantoine committed Jul 8, 2016
1 parent c99c34f commit 7aad465
Show file tree
Hide file tree
Showing 15 changed files with 544 additions and 0 deletions.
30 changes: 30 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# cache files for sublime text
*.tmlanguage.cache
*.tmPreferences.cache
*.stTheme.cache

# workspace files are user-specific
*.sublime-workspace

# project files should be checked into the repository, unless a significant
# proportion of contributors will probably not be using SublimeText
*.sublime-project

# sftp configuration file
sftp-config.json

# Package control specific files
Package Control.last-run
Package Control.ca-list
Package Control.ca-bundle
Package Control.system-ca-bundle
Package Control.cache/
Package Control.ca-certs/
bh_unicode_properties.cache

# Sublime-github package stores a github token in this file
# https://packagecontrol.io/packages/sublime-github
GitHub.sublime-settings

# Haxe
.haxelib
4 changes: 4 additions & 0 deletions build.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--interp
-main hxextern.Main
-cp src/
-lib hxargs
15 changes: 15 additions & 0 deletions haxelib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"main": "hxextern.Main",
"name": "hxextern",
"url": "https://github.com/ExternKit/hxextern",
"license": "MIT",
"tags": ["extern", "hxextern"],
"description": "Extern manager",
"version": "0.1.0",
"classPath": "src/",
"contributors": ["fantoine"],
"dependencies": {
"hxargs": "",
"hxssl": ""
}
}
66 changes: 66 additions & 0 deletions src/hxextern/Cli.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package hxextern;

import hxargs.Args;
import hxextern.command.*;
import hxextern.service.*;

typedef ArgsHandler = {
function getDoc() : String;

function parse(__args : Array<Dynamic>) : Void;
};

class Cli
{
private var handler : ArgsHandler;

public function new()
{
this.handler = Args.generate([
@doc('List available externs')
'list' => function(?target : String) : Void {
this.runCommand(new ListCommand(target));
},

@doc('Search for an extern')
'search' => function(name : String, ?target : String) : Void {
this.runCommand(new SearchCommand(name, target));
},

@doc('Generate extern code')
'generate' => function(?path : String) : Void {
trace('Not implemented');
},

@doc('Show this help')
'help' => function() : Void {
this.runCommand(new HelpCommand(this.handler.getDoc()));
},

_ => function(arg : String) : Void {
this.runCommand(new HelpCommand(this.handler.getDoc()));
},
]);
}

public function run(args : Array<String>) : Void
{
if (args.length == 0) {
args.push('help');
}
try {
this.handler.parse(args);
} catch (e : Dynamic) {
Console.instance.error(Std.string(e));
}
}

private function runCommand(command : ICommand) : Void
{
try {
command.run();
} catch (e : Dynamic) {
Console.instance.error(Std.string(e));
}
}
}
15 changes: 15 additions & 0 deletions src/hxextern/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package hxextern;

class Main
{
public static function main() : Void
{
// Get arguments
var args = Sys.args();
args.pop();

// Run
var cli = new Cli();
cli.run(args);
}
}
30 changes: 30 additions & 0 deletions src/hxextern/Target.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package hxextern;

@:enum
abstract Target(String) to String
{
var Cpp : Target = 'cpp';
var Cs : Target = 'cs';
var Flash : Target = 'flash';
var Java : Target = 'java';
var Js : Target = 'js';
var Lua : Target = 'lua';
var Neko : Target = 'neko';
var Php : Target = 'php';
var Python : Target = 'python';

public static function fromString(target : String) : Null<Target>
{
return switch (target.toLowerCase()) {
case 'cpp' | 'c++': Cpp;
case 'cs' | 'csharp': Cs;
case 'flash': Flash;
case 'js' | 'javascript': Js;
case 'lua': Lua;
case 'neko' | 'n': Neko;
case 'php': Php;
case 'py' | 'python': Python;
case _: throw 'Unsupported target "${target}"';
}
}
}
32 changes: 32 additions & 0 deletions src/hxextern/command/AbstractListCommand.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package hxextern.command;

import hxextern.service.Console;
import hxextern.service.Repository;

class AbstractListCommand implements ICommand
{
public function new()
{
// Nothing to do
}

public function run() : Void
{
throw 'Must be overriden';
}

private function printList(infos : Array<RepositoryInfo>) : Void
{
if (0 == infos.length) {
Console.instance.info('No results found');
return;
}

Console.instance.success('Found ${infos.length} result(s)');
for (info in infos) {
Console.instance.message(' * ', false);
Console.instance.info('${info.name} (${info.target})', false);
Console.instance.message(' : ${info.description}');
}
}
}
16 changes: 16 additions & 0 deletions src/hxextern/command/HelpCommand.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package hxextern.command;

class HelpCommand implements ICommand
{
private var doc : String;

public function new(doc : String)
{
this.doc = doc;
}

public function run() : Void
{
Sys.println(this.doc);
}
}
6 changes: 6 additions & 0 deletions src/hxextern/command/ICommand.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package hxextern.command;

interface ICommand
{
public function run() : Void;
}
23 changes: 23 additions & 0 deletions src/hxextern/command/ListCommand.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package hxextern.command;

import hxextern.service.Repository;

class ListCommand extends AbstractListCommand
{
private var target : String;

public function new(?target : String)
{
super();

this.target = target;
}

public override function run() : Void
{
var target = (null == this.target ? null : Target.fromString(this.target));

var infos = Repository.instance.list(target);
this.printList(infos);
}
}
25 changes: 25 additions & 0 deletions src/hxextern/command/SearchCommand.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package hxextern.command;

import hxextern.service.Repository;

class SearchCommand extends AbstractListCommand
{
private var name : String;
private var target : String;

public function new(name : String, ?target : String)
{
super();

this.name = name;
this.target = target;
}

public override function run() : Void
{
var target = (null == this.target ? null : Target.fromString(this.target));

var infos = Repository.instance.find(this.name, target);
this.printList(infos);
}
}
65 changes: 65 additions & 0 deletions src/hxextern/service/Console.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package hxextern.service;

import haxe.io.Output;
import hxextern.utils.Colors;

class Console
{
@:isVar
public static var instance(get, null) : Console;
private static function get_instance() : Console
{
if (null == Console.instance) {
Console.instance = new Console();
}
return Console.instance;
}

private var stdout : Output;
private var stderr : Output;

private function new()
{
this.stdout = Sys.stdout();
this.stderr = Sys.stderr();
}

public function message(message : String, newline : Bool = true) : Void
{
this.print(this.stdout, message, newline);
}

public function debug(message : String, newline : Bool = true) : Void
{
this.print(this.stdout, Colors.magenta('${message}'), newline);
}

public function info(message : String, newline : Bool = true) : Void
{
this.print(this.stdout, Colors.cyan('${message}'), newline);
}

public function success(message : String, newline : Bool = true) : Void
{
this.print(this.stdout, Colors.green('${message}'), newline);
}

public function warning(message : String, newline : Bool = true) : Void
{
this.print(this.stderr, Colors.yellow('${message}'), newline);
}

public function error(message : String, newline : Bool = true) : Void
{
this.print(this.stderr, Colors.red('${message}'), newline);
}

private function print(output : Output, message : String, newline : Bool = true) : Void
{
output.writeString(message);
if (newline) {
output.writeString('\n');
}
output.flush();
}
}
52 changes: 52 additions & 0 deletions src/hxextern/service/Process.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package hxextern.service;

import haxe.io.Bytes;
import sys.io.Process as HxProcess;

typedef ProcessOutput = {
var output : Null<String>;
var error : Null<String>;
var valid : Bool;
var code : Int;
};

class Process
{
@:isVar
public static var instance(get, null) : Process;
private static function get_instance() : Process
{
if (null == Process.instance) {
Process.instance = new Process();
}
return Process.instance;
}

private function new()
{

}

public function execute(command : String, ?args : Array<String>) : ProcessOutput
{
Console.instance.debug('Calling command "${command}' + (null != args && args.length > 0 ? ' ' + args.join(' ') : '') + '"');

var process = new HxProcess(command, args);
var output = process.stdout.readAll().toString();
var error = process.stderr.readAll().toString();
var result = ('' != error && null != error ? {
output: null,
error: error,
valid: false,
code: process.exitCode(),
} : {
output: output,
error: null,
valid: true,
code: process.exitCode(),
});
process.close();

return result;
}
}
Loading

0 comments on commit 7aad465

Please sign in to comment.