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.
coffeescript-integration
Aaron Mueller 10 years ago
parent c7b2786a8a
commit 720564652f

@ -38,6 +38,11 @@
<echo>Creating default ops.txt for your user</echo>
<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 name="run" depends="server-setup, package, update-live-cb" description="Starts Bukkit with ScriptCraft">

@ -98,7 +98,12 @@ public class ScriptCraftPlugin extends JavaPlugin implements Listener
this.engine.put("__plugin",this);
this.engine.put("__script",boot.getCanonicalPath().replaceAll("\\\\","/"));
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){
e.printStackTrace();
}finally {
@ -147,7 +152,13 @@ public class ScriptCraftPlugin extends JavaPlugin implements Listener
javascriptCode = "command()";
this.engine.put("__cmdArgs",args);
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){
this.engine.put("self",sender);
try{

@ -276,10 +276,22 @@ var server = org.bukkit.Bukkit.server;
if (file.exists()) {
var parent = file.getParentFile();
var reader = new java.io.FileReader(file);
var br = new java.io.BufferedReader(reader);
__engine.put("__script",canonizedFilename);
__engine.put("__folder",(parent?_canonize(parent):"")+"/");
var code = "";
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;
reader.close();
}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
*/
var _listJsFiles = function(store,dir)
var _listSourceFiles = function(store,dir)
{
if (typeof dir == "undefined"){
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++){
var file = files[i];
if (file.isDirectory()){
_listJsFiles(store,file);
_listSourceFiles(store,file);
}else{
if (file.getCanonicalPath().endsWith(".js") &&
if ((file.getCanonicalPath().endsWith(".js") || file.getCanonicalPath().endsWith(".coffee")) &&
!(file.getName().startsWith("_")) &&
file.exists())
{
@ -348,10 +360,10 @@ var server = org.bukkit.Bukkit.server;
var _reload = function(pluginDir)
{
_loaded = [];
var jsFiles = [];
_listJsFiles(jsFiles,pluginDir);
var sourceFiles = [];
_listSourceFiles(sourceFiles,pluginDir);
jsFiles.sort(sortByModule);
sourceFiles.sort(sortByModule);
//
// 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
// 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++){
load(_canonize(jsFiles[i]),true);
load(_canonize(sourceFiles[i]),true);
}
};

@ -11,7 +11,12 @@ commands:
description: run a javascript plugin command.
usage: /<command> command-name command-parameters
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:
scriptcraft.*: