Add ability to include other scripts and set required min/max versions, add "engine" variable to allow direct access from inside scripts
This commit is contained in:
parent
3e67cd318d
commit
3b7cbf607c
@ -36,6 +36,8 @@ import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import javax.swing.JFileChooser;
|
||||
import javax.swing.JOptionPane;
|
||||
import javax.swing.SwingUtilities;
|
||||
@ -602,16 +604,96 @@ public class CodeEditor extends javax.swing.JInternalFrame {
|
||||
}
|
||||
|
||||
private void execCode(String lang) {
|
||||
if (!checkRequiredVersion(codeBox.getText(), lang)) {
|
||||
return;
|
||||
}
|
||||
CodeRunner cr = new CodeRunner(lang);
|
||||
String script = loadExternalScripts(codeBox.getText(), lang);
|
||||
Debug.println(lang);
|
||||
Debug.println(codeBox.getText());
|
||||
Object result = cr.evalString(codeBox.getText());
|
||||
Debug.println(script);
|
||||
Object result = cr.evalString(script);
|
||||
try {
|
||||
outputBox.append(result.toString() + "\n");
|
||||
} catch (NullPointerException ex) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private boolean checkRequiredVersion(String script, String lang) {
|
||||
String prefix = "//";
|
||||
if (lang.startsWith("p")) {
|
||||
prefix = "##";
|
||||
}
|
||||
String line = script.trim().split("\\n", 2)[0];
|
||||
if (line.startsWith(prefix + "needs ")) {
|
||||
String versions = line.substring(8).trim();
|
||||
Debug.println(versions);
|
||||
String min = versions;
|
||||
String max = "999999999";
|
||||
if (versions.contains("-")) {
|
||||
min = versions.split("-")[0];
|
||||
max = versions.split("-")[1];
|
||||
}
|
||||
try {
|
||||
int minNum = Integer.parseInt(min);
|
||||
int maxNum = Integer.parseInt(max);
|
||||
if (!(minNum <= MainGUI.APP_CODE
|
||||
&& maxNum >= MainGUI.APP_CODE)) {
|
||||
JOptionPane.showInternalMessageDialog(this, "This script "
|
||||
+ "cannot be run on this version of SyMAT.");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} catch (Exception ex) {
|
||||
outputBox.append("Error: Bad version selection syntax: "
|
||||
+ ex.getMessage() + "\n");
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load external script files, relative to the current file.
|
||||
*
|
||||
* @param script The file to parse for includes.
|
||||
* @param lang The script language.
|
||||
* @return The script modified as needed.
|
||||
*/
|
||||
private String loadExternalScripts(String script, String lang) {
|
||||
String[] lines = script.split("\n");
|
||||
String temp;
|
||||
String result = "";
|
||||
for (String line : lines) {
|
||||
if (lang.startsWith("j")) {
|
||||
if (line.startsWith("//include ") && !line.trim().endsWith("//include")) {
|
||||
temp = line.split(" ", 2)[1];
|
||||
try {
|
||||
line = FileUtils.readFile(filedata.getParent()
|
||||
+ "./" + temp);
|
||||
} catch (IOException ex) {
|
||||
outputBox.append("Error: Cannot read "
|
||||
+ "referenced script file: " + ex.getMessage()
|
||||
+ "\n");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (line.startsWith("##include ") && !line.trim().endsWith("##include")) {
|
||||
temp = line.split(" ", 2)[1];
|
||||
try {
|
||||
line = FileUtils.readFile(filedata.getParent()
|
||||
+ "./" + temp);
|
||||
} catch (IOException ex) {
|
||||
outputBox.append("Error: Cannot read "
|
||||
+ "referenced script file: " + ex.getMessage()
|
||||
+ "\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
result += line + "\n";
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private void exportMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exportMenuActionPerformed
|
||||
String lang = "js";
|
||||
if (pythonOption.isSelected()) {
|
||||
|
@ -51,8 +51,6 @@ public class CodeRunner {
|
||||
// What codez are we speaking?
|
||||
private String scriptLang = "";
|
||||
|
||||
private boolean isShell = false;
|
||||
|
||||
public CodeRunner() {
|
||||
this("javascript");
|
||||
}
|
||||
@ -68,6 +66,8 @@ public class CodeRunner {
|
||||
se.eval("importClass(net.apocalypselabs.symat.Functions);"
|
||||
+ "SyMAT_Functions = new net.apocalypselabs.symat.Functions();"
|
||||
+ getFunctions("js"));
|
||||
// Allow engine access from scripts.
|
||||
se.put("engine", se);
|
||||
} catch (Exception ex) {
|
||||
initError(ex);
|
||||
}
|
||||
@ -79,6 +79,8 @@ public class CodeRunner {
|
||||
+ "from net.apocalypselabs.symat import Functions\n"
|
||||
+ "_=Functions()\n\n"
|
||||
+ getFunctions("py"));
|
||||
// Allow engine access from scripts.
|
||||
se.put("engine", se);
|
||||
} catch (Exception ex) {
|
||||
initError(ex);
|
||||
}
|
||||
@ -88,13 +90,13 @@ public class CodeRunner {
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public CodeRunner(String lang, boolean shell) {
|
||||
this(lang);
|
||||
isShell = shell;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inits the Python engine.
|
||||
* Inits the Python engine on application start.
|
||||
* @param fakeInit Set it to true.
|
||||
*/
|
||||
public CodeRunner(boolean fakeInit) {
|
||||
|
@ -37,7 +37,7 @@ package net.apocalypselabs.symat;
|
||||
public class Debug {
|
||||
|
||||
// If output should be on or off
|
||||
public static final boolean DEBUG = false;
|
||||
public static final boolean DEBUG = true;
|
||||
|
||||
public static void println(Object data) {
|
||||
if (DEBUG) {
|
||||
|
@ -58,9 +58,9 @@ import javax.swing.UIManager;
|
||||
public class MainGUI extends javax.swing.JFrame {
|
||||
|
||||
// TODO: Add more code comments and stuff in case anybody else reads this
|
||||
public static final String APP_NAME = "SyMAT 1.0";
|
||||
public static final double APP_CODE = 12;
|
||||
public static final String VERSION_NAME = "1.0";
|
||||
public static final String APP_NAME = "SyMAT 1.1";
|
||||
public static final double APP_CODE = 13;
|
||||
public static final String VERSION_NAME = "1.1";
|
||||
public static final String API_URL = "https://apis.symatapp.com/";
|
||||
public static String argfile = "";
|
||||
public static boolean skipPython = false; // Skip python init on start?
|
||||
|
@ -1,3 +0,0 @@
|
||||
function write(data) {
|
||||
console.log(data);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user