Merge pull request #91 from arg-games/coffeescript-integration
Integrate the CoffeeScript Compiler for .coffee files
This commit is contained in:
commit
9a9188cf47
4 changed files with 44 additions and 11 deletions
|
@ -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">
|
||||
|
|
|
@ -99,6 +99,11 @@ public class ScriptCraftPlugin extends JavaPlugin implements Listener
|
|||
this.engine.put("__script",boot.getCanonicalPath().replaceAll("\\\\","/"));
|
||||
reader = new FileReader(boot);
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -12,6 +12,11 @@ commands:
|
|||
usage: /<command> command-name command-parameters
|
||||
permission: scriptcraft.proxy
|
||||
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.*:
|
||||
|
|
Reference in a new issue