Integrate the CoffeeScript Compiler for .coffee files

Simple integration of CoffeeScript support. The code uses the JavaScript
implementation of the CoffeeScript compiler. All *.coffee files get
compiled to JavaScript just before eval(). Additionaly a new command
named /coffe is introduced to run CoffeeScript code straight in the
Minecraft console.
This commit is contained in:
Aaron Mueller 2013-08-11 20:22:36 +02:00
parent c7b2786a8a
commit 720564652f
4 changed files with 44 additions and 11 deletions

View file

@ -38,6 +38,11 @@
<echo>Creating default ops.txt for your user</echo> <echo>Creating default ops.txt for your user</echo>
<echo message="${op.name}" file="${minecraft.dir}/ops.txt" /> <echo message="${op.name}" file="${minecraft.dir}/ops.txt" />
<echo>Retrieving Coffeescript compiler</echo>
<mkdir dir="${minecraft.dir}/js-plugins/core" />
<get src="https://raw.github.com/jashkenas/coffee-script/master/extras/coffee-script.js"
dest="${minecraft.dir}/js-plugins/core/_coffeescript.js"/>
</target> </target>
<target name="run" depends="server-setup, package, update-live-cb" description="Starts Bukkit with ScriptCraft"> <target name="run" depends="server-setup, package, update-live-cb" description="Starts Bukkit with ScriptCraft">

View file

@ -99,6 +99,11 @@ public class ScriptCraftPlugin extends JavaPlugin implements Listener
this.engine.put("__script",boot.getCanonicalPath().replaceAll("\\\\","/")); this.engine.put("__script",boot.getCanonicalPath().replaceAll("\\\\","/"));
reader = new FileReader(boot); reader = new FileReader(boot);
this.engine.eval(reader); this.engine.eval(reader);
// Load the CoffeeScript compiler
File coffeescript = new File(JS_PLUGINS_DIR + "/core/_coffeescript.js");
this.engine.eval(new FileReader(coffeescript));
}catch(Exception e){ }catch(Exception e){
e.printStackTrace(); e.printStackTrace();
}finally { }finally {
@ -147,7 +152,13 @@ public class ScriptCraftPlugin extends JavaPlugin implements Listener
javascriptCode = "command()"; javascriptCode = "command()";
this.engine.put("__cmdArgs",args); this.engine.put("__cmdArgs",args);
result = true; result = true;
} else if (cmd.getName().equalsIgnoreCase("coffee")) {
for (int i = 0;i < args.length; i++)
javascriptCode += args[i] + " ";
javascriptCode = "eval(CoffeeScript.compile(\""+javascriptCode+"\", {bare: true}))";
result = true;
} }
if (result){ if (result){
this.engine.put("self",sender); this.engine.put("self",sender);
try{ try{

View file

@ -276,10 +276,22 @@ var server = org.bukkit.Bukkit.server;
if (file.exists()) { if (file.exists()) {
var parent = file.getParentFile(); var parent = file.getParentFile();
var reader = new java.io.FileReader(file); var reader = new java.io.FileReader(file);
var br = new java.io.BufferedReader(reader);
__engine.put("__script",canonizedFilename); __engine.put("__script",canonizedFilename);
__engine.put("__folder",(parent?_canonize(parent):"")+"/"); __engine.put("__folder",(parent?_canonize(parent):"")+"/");
var code = "";
try{ try{
result = __engine.eval(reader); if (file.getCanonicalPath().endsWith(".coffee")) {
var r = undefined;
while ((r = br.readLine()) !== null) code += "\"" + r + "\" +\n";
code += "\"\"";
var code = "load(__folder + \"../core/_coffeescript.js\"); var ___code = "+code+"; eval(CoffeeScript.compile(___code, {bare: true}))";
} else {
while ((r = br.readLine()) !== null) code += r + "\n";
}
result = __engine.eval(code);
_loaded[canonizedFilename] = true; _loaded[canonizedFilename] = true;
reader.close(); reader.close();
}catch (e){ }catch (e){
@ -296,7 +308,7 @@ var server = org.bukkit.Bukkit.server;
/* /*
recursively walk the given directory and return a list of all .js files recursively walk the given directory and return a list of all .js files
*/ */
var _listJsFiles = function(store,dir) var _listSourceFiles = function(store,dir)
{ {
if (typeof dir == "undefined"){ if (typeof dir == "undefined"){
dir = new java.io.File(_originalScript).getParentFile().getParentFile(); dir = new java.io.File(_originalScript).getParentFile().getParentFile();
@ -305,9 +317,9 @@ var server = org.bukkit.Bukkit.server;
for (var i = 0;i < files.length; i++){ for (var i = 0;i < files.length; i++){
var file = files[i]; var file = files[i];
if (file.isDirectory()){ if (file.isDirectory()){
_listJsFiles(store,file); _listSourceFiles(store,file);
}else{ }else{
if (file.getCanonicalPath().endsWith(".js") && if ((file.getCanonicalPath().endsWith(".js") || file.getCanonicalPath().endsWith(".coffee")) &&
!(file.getName().startsWith("_")) && !(file.getName().startsWith("_")) &&
file.exists()) file.exists())
{ {
@ -348,10 +360,10 @@ var server = org.bukkit.Bukkit.server;
var _reload = function(pluginDir) var _reload = function(pluginDir)
{ {
_loaded = []; _loaded = [];
var jsFiles = []; var sourceFiles = [];
_listJsFiles(jsFiles,pluginDir); _listSourceFiles(sourceFiles,pluginDir);
jsFiles.sort(sortByModule); sourceFiles.sort(sortByModule);
// //
// script files whose name begins with _ (underscore) // script files whose name begins with _ (underscore)
@ -363,9 +375,9 @@ var server = org.bukkit.Bukkit.server;
// then it's assumed that _myMiniGame_currency.js and _myMiniGame_events.js will be loaded // then it's assumed that _myMiniGame_currency.js and _myMiniGame_events.js will be loaded
// as dependencies by myMiniGame.js and do not need to be loaded via js reload // as dependencies by myMiniGame.js and do not need to be loaded via js reload
// //
var len = jsFiles.length; var len = sourceFiles.length;
for (var i = 0;i < len; i++){ for (var i = 0;i < len; i++){
load(_canonize(jsFiles[i]),true); load(_canonize(sourceFiles[i]),true);
} }
}; };

View file

@ -12,6 +12,11 @@ commands:
usage: /<command> command-name command-parameters usage: /<command> command-name command-parameters
permission: scriptcraft.proxy permission: scriptcraft.proxy
permission-message: You don't have <permission> permission. permission-message: You don't have <permission> permission.
coffee:
description: Evaluate coffeescript.
usage: /<command> Coffeescript code
permission: scriptcraft.evaluate
permission-message: You don't have <permission> permission.
permissions: permissions:
scriptcraft.*: scriptcraft.*: