// Creates a new thread, runs the program in that thread, and reports any errors as needed. private void run(String clazz) { try { // Makes sure the JVM resets if it's already running. if (JVMrunning) kill(); // Some String constants for java path and OS-specific separators. String separator = System.getProperty("file.separator"); String path = System.getProperty("java.home") + separator + "bin" + separator + "java"; // Tries to run compiled code. ProcessBuilder builder = new ProcessBuilder(path, clazz); // Should be good now! Everything past this is on you. Don't mess it up. println( "Build succeeded on " + java.util.Calendar.getInstance().getTime().toString(), progErr); println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~", progErr); JVM = builder.start(); // Note that as of right now, there is no support for input. Only output. Reader errorReader = new InputStreamReader(JVM.getErrorStream()); Reader outReader = new InputStreamReader(JVM.getInputStream()); // Writer inReader = new OutputStreamWriter(JVM.getOutputStream()); redirectErr = redirectIOStream(errorReader, err); redirectOut = redirectIOStream(outReader, out); // redirectIn = redirectIOStream(null, inReader); } catch (Exception e) { // This catches any other errors we might get. println("Some error thrown", progErr); logError(e.toString()); displayLog(); return; } }
public void run() { if (!new File(m_generatorPy).exists()) { Base.showMessage( "ERROR", String.format("Could not find generator python script at %s", m_generatorPy)); } Sketch sketch = m_editor.getSketch(); if (sketch.isModified()) { try { sketch.save(); } catch (java.io.IOException ex) { // Base.showMessage("ERROR", "Could not save sketch before trying to generate." ); } } // String sketchName = sketch.getName(); // SketchCode codeObject = sketch.getCurrentCode(); // String code = codeObject.getProgram(); try { String code = m_editor.getCurrentTab().getText(); // String code = m_editor.getText(); int start = code.indexOf("BEGIN AUTOMATION"); if (start != -1) { int end = code.indexOf("END AUTOMATION", start); if (end != -1) { String automationCode = code.substring(start + "BEGIN AUTOMATION".length(), end); automationCode = automationCode.replaceAll("\\*\\s+", ""); // SketchData sketchData = new MySketchData(sketch.getMainFilePath()); // File buildFolder = BaseNoGui.getBuildFolder(sketchData); File buildFolder = getBuildFolder(sketch); // File codeFolder = sketchData.getCodeFolder(); File codeFolder = new File(sketch.getFolder(), "code"); Files.createDirectories(codeFolder.toPath()); File automationFileName = new File(buildFolder, "automation.automation"); FileWriter writer = new FileWriter(automationFileName); writer.write("name automation\n"); writer.write("import " + sketch.getMainFilePath().toString() + "\n"); writer.write(automationCode); writer.close(); try { ProcessBuilder processBuilder = null; if (m_isWindows) { processBuilder = new ProcessBuilder( m_pythonPath.getAbsolutePath(), m_generatorPy, "-s", "--arduino", automationFileName.getAbsolutePath()); } else { processBuilder = new ProcessBuilder( m_generatorPy, "-s", "--arduino", automationFileName.getAbsolutePath()); } processBuilder.directory(codeFolder); processBuilder.redirectErrorStream(true); Process process = processBuilder.start(); inheritIO(process.getInputStream(), System.out); int result = process.waitFor(); String includeLibLine = "#include \"SequantoAutomation.h\"\n"; if (!code.contains(includeLibLine)) { code = includeLibLine + code; } /* File generatedFileName = new File(codeFolder, "automation_automation.c" ); String includeLine = String.format("#include \"%s\"\n", generatedFileName.getAbsolutePath()); if ( !code.contains(includeLine) ) { int i = code.indexOf(includeLibLine); code = code.substring(0, i + includeLibLine.length()) + includeLine + code.substring ( i + includeLibLine.length(), code.length() ); } */ String includeLine = String.format("#include \"code/automation_automation.h\"\n"); if (!code.contains(includeLine)) { int i = code.indexOf(includeLibLine); code = code.substring(0, i + includeLibLine.length()) + includeLine + code.substring(i + includeLibLine.length(), code.length()); } String includeCodeLine = String.format("\n#include \"code/automation_automation.c\"\n"); if (!code.contains(includeCodeLine)) { code = code + includeCodeLine; } if (m_editor.getCurrentTab().getText() != code) { // System.out.println ( "Setting code to" ); // System.out.println ( code ); // System.out.println ( "Current text was" ); // System.out.println ( m_editor.getText() ); m_editor.getCurrentTab().setText(code); } if (result == 0) { System.out.println("Sequanto automation code generated successfully!"); } else { System.out.println("ERROR!!!"); } } catch (Exception ex) { Base.showMessage("ERROR", String.format("Could not start generator script: %s", ex)); } } else { Base.showMessage("ERROR", "Can not find END AUTOMATION."); } } else { Base.showMessage("ERROR", "Can not find BEGIN AUTOMATION."); } } catch (java.io.IOException ex) { Base.showMessage("ERROR", String.format("IO Error: %s.", ex)); } }