Created
May 23, 2012 13:13
-
-
Save jasononeil/2775175 to your computer and use it in GitHub Desktop.
NekoScript proof of concept - simple scripting language using hscript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-lib hscript | |
-main NekoScript | |
-neko nekoscript.n |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function mapdrive(drive) | |
{ | |
print ("drive: " + drive); | |
} | |
print("hello"); | |
print("this is my test file"); | |
print(cwd()); | |
cd("/home/jason/Scripts"); | |
// Go through each file - no testing if it is a directory or not | |
for (file in readDir(cwd())) | |
{ | |
//print(file); | |
} | |
// Go through all descendants. This list is files only, not dirs. | |
for (file in readDirRecursively(cwd())) | |
{ | |
//print (file); | |
} | |
/* | |
error("Error message"); | |
info("Info message"); | |
var name = input("What is your username?"); | |
var pass = password("What is your password?"); | |
print("I will try log you on with " + name + " and " + pass); | |
*/ | |
mapdrive("X:"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
NekoScript | |
By Jason O'Neil, 2011. Licensed under the GPL. | |
*/ | |
import hscript.Parser; | |
import neko.Sys; | |
import neko.FileSystem; | |
import neko.io.File; | |
import haxe.Http; | |
using Lambda; | |
using StringTools; | |
class NekoScript | |
{ | |
static public function main() | |
{ | |
// Read the system arguments, and decide what to do. | |
if (Sys.args().length > 0) | |
{ | |
var arg = Sys.args()[0]; | |
var script = ""; | |
if (FileSystem.exists(arg)) | |
{ | |
// looks like they're asking for a local file. Attempt to load it. | |
script = File.getContent(arg); | |
executeScript(script); | |
} | |
else if (arg.indexOf("http://") == 0) | |
{ | |
// It's a http URL. Attempt to load it. | |
//TODO: look and check if there is a proxy to use here. You can read the ENV variables, and set HTTP.PROXY | |
var request = new Http(arg); | |
request.onError = function(message) { | |
print ("Attempted to load a HTTP address, but it failed:\n" + message); | |
} | |
request.onData = function(script) { | |
executeScript(script); | |
} | |
} | |
else | |
{ | |
// show them the help message | |
usage(); | |
} | |
} | |
else | |
{ | |
// show them the help message | |
usage(); | |
} | |
} | |
static public function usage() | |
{ | |
print("NekoScript"); | |
print ("By Jason O'Neil, 2011. License: GPL"); | |
print (""); | |
print ("Please use like this:"); | |
print (" neko nekoscript.n my/filetocall.nscript"); | |
print (" neko nekoscript.n http://filetocall.nscript"); | |
print (" neko nekoscript.n filetocall.nscript -arg1 --arg2 arg3"); | |
} | |
static public function executeScript(script_in:String) | |
{ | |
// Set up hscript | |
var parser = new hscript.Parser(); | |
var program = parser.parseString(script_in); | |
var interp = new hscript.Interp(); | |
// Share some classes | |
interp.variables.set("Sys",Sys); | |
interp.variables.set("FileSystem",FileSystem); | |
interp.variables.set("File",File); | |
interp.variables.set("Http",Http); | |
// Share some functions | |
interp.variables.set("print",print); | |
interp.variables.set("run",run); | |
interp.variables.set("runWithArgs",runWithArgs); | |
interp.variables.set("copy",File.copy); | |
interp.variables.set("mv",FileSystem.rename); | |
interp.variables.set("rm",FileSystem.deleteFile); | |
interp.variables.set("mkdir",FileSystem.createDirectory); | |
interp.variables.set("cwd",Sys.getCwd); | |
interp.variables.set("cd",Sys.setCwd); | |
interp.variables.set("readDir",FileSystem.readDirectory); | |
interp.variables.set("readDirRecursively",readDirRecursively); | |
interp.variables.set("error",error); | |
interp.variables.set("info",info); | |
interp.variables.set("input",input); | |
interp.variables.set("password",password); | |
// Share some variables | |
// Run the program. | |
interp.execute(program); | |
} | |
static function print(string) | |
{ | |
neko.Lib.print(string + "\n"); | |
} | |
static function run(?commandAndArgs:String) | |
{ | |
var args = commandAndArgs.split(" "); | |
var command = args.shift(); | |
return runWithArgs(command, args); | |
} | |
static function runWithArgs(command:String, args:Array<String>) | |
{ | |
var p = new neko.io.Process(command, args); | |
var exitCode = p.exitCode(); | |
var output = p.stdout.readAll().toString(); | |
return { output: output, exitCode: exitCode } | |
} | |
/** Use zenity to create a popup */ | |
static function error(errorMessage:String) | |
{ | |
runWithArgs("zenity", ["--error", "--text=" + errorMessage + ""]); | |
} | |
/** Use zenity to create a popup */ | |
static function info(infoMessage:String) | |
{ | |
runWithArgs("zenity", ["--info", "--text=" + infoMessage + ""]); | |
} | |
/** Use zenity to request a string input */ | |
static function input(infoMessage:String) | |
{ | |
return runWithArgs("zenity", ["--entry", "--text=" + infoMessage + ""]).output.rtrim(); | |
} | |
/** Use zenity to request a password string input */ | |
static function password(infoMessage:String) | |
{ | |
return runWithArgs("zenity", ["--entry", "--hide-text", "--text=" + infoMessage + ""]).output.rtrim(); | |
} | |
/** Return an array of all the files in this directory | |
and sub directories. The array does not include the subdirectories, | |
only the files in them. */ | |
static function readDirRecursively(path:String) | |
{ | |
var array = new Array(); | |
// Check we're loading a valid directory | |
if (FileSystem.isDirectory(path)) | |
{ | |
// GO through each file | |
for (file in FileSystem.readDirectory(path)) | |
{ | |
// if it's another directory | |
if (FileSystem.isDirectory(path + file)) | |
{ | |
// and it's not the one we're already on, or it's parent | |
if (file != path + "." && file != path + "..") | |
{ | |
// then recurse through this subdir, and add the results to our array. | |
var subArray = readDirRecursively(path + file + "/"); | |
array = array.concat(subArray); | |
} | |
} | |
else | |
{ | |
// add this file to the array. | |
array.push(file); | |
} | |
} | |
} | |
return array; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment