|
| 1 | +/* |
| 2 | + * Copyright (c) 2021 ByteSkript org (Moderocky) |
| 3 | + * View the full licence information and permissions: |
| 4 | + * https://github.com/Moderocky/ByteSkript/blob/master/LICENSE |
| 5 | + */ |
| 6 | + |
| 7 | +package org.byteskript.skript.app; |
| 8 | + |
| 9 | +import mx.kenzie.foundation.language.PostCompileClass; |
| 10 | +import org.byteskript.skript.runtime.Skript; |
| 11 | + |
| 12 | +import java.io.File; |
| 13 | +import java.io.FileInputStream; |
| 14 | +import java.io.InputStream; |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +import static org.byteskript.skript.app.ByteSkriptApp.Colour.*; |
| 19 | + |
| 20 | +public class ByteSkriptApp extends SkriptApp { |
| 21 | + protected static final Skript SKRIPT = new Skript(); |
| 22 | + |
| 23 | + public static void main(String... args) throws Throwable { |
| 24 | + if (args.length < 1) { |
| 25 | + makeFiles(); |
| 26 | + System.out.println(RESET + "Welcome to " + MAGENTA_BRIGHT + "ByteSkript" + RESET + "!"); |
| 27 | + System.out.println(RESET + "Available arguments:"); |
| 28 | + System.out.println(RESET + "\trun | " + CYAN + "Run scripts in the " + CYAN_UNDERLINED + "skripts/" + CYAN + " directory."); |
| 29 | + System.out.println(RESET + "\trun <file> | " + CYAN + "Run a single script in isolation."); |
| 30 | + System.out.println(RESET + "\tcompile | " + CYAN + "Compile library class files for all scripts."); |
| 31 | + System.out.println(RESET + "\t | " + CYAN + "Syntax-providing classes can be moved to the " + CYAN_UNDERLINED + "libraries/" + CYAN + " folder."); |
| 32 | + System.out.println(RESET + "\tjar <name> | " + CYAN + "Build a Jar file in " + CYAN_UNDERLINED + "compiled/" + CYAN + " from all your scripts."); |
| 33 | + System.out.println(RESET + "\t | " + CYAN + "This will include files in " + CYAN_UNDERLINED + "resources/" + CYAN); |
| 34 | + System.out.println(RESET + "\tclean | " + CYAN + "Cleans the " + CYAN_UNDERLINED + "compiled/" + CYAN + " folder."); |
| 35 | + System.out.print(RESET); |
| 36 | + System.out.println("Visit https://docs.byteskript.org for help and tutorials."); |
| 37 | + } else if (args[0].equalsIgnoreCase("clean")) { |
| 38 | + final List<File> files = getFiles(new ArrayList<>(), OUTPUT.toPath()); |
| 39 | + int i = 50; |
| 40 | + while (!files.isEmpty() && i > 0) { |
| 41 | + files.removeIf(File::delete); |
| 42 | + i--; |
| 43 | + } |
| 44 | + if (!OUTPUT.exists()) OUTPUT.mkdirs(); |
| 45 | + } else if (args[0].equalsIgnoreCase("run") && args.length < 2) { |
| 46 | + ScriptLoader.main(); |
| 47 | + } else if (args[0].equalsIgnoreCase("run")) { |
| 48 | + final String name = args[1]; |
| 49 | + final File file = new File(name); |
| 50 | + registerLibraries(SKRIPT); |
| 51 | + try (final InputStream stream = new FileInputStream(file)) { |
| 52 | + final PostCompileClass[] classes = SKRIPT.compileComplexScript(stream, "skript." + file.getName()); |
| 53 | + for (final PostCompileClass type : classes) { |
| 54 | + SKRIPT.loadScript(type); |
| 55 | + } |
| 56 | + } |
| 57 | + new SimpleThrottleController(SKRIPT).run(); |
| 58 | + } else if (args[0].equalsIgnoreCase("jar")) { |
| 59 | + final String[] arguments; |
| 60 | + if (args.length > 1) { |
| 61 | + arguments = new String[args.length - 1]; |
| 62 | + System.arraycopy(args, 1, arguments, 0, arguments.length); |
| 63 | + } else arguments = new String[0]; |
| 64 | + ScriptJarBuilder.main(arguments); |
| 65 | + } else if (args[0].equalsIgnoreCase("compile")) { |
| 66 | + ScriptCompiler.main(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + static void makeFiles() { |
| 71 | + if (!SOURCE.exists()) SOURCE.mkdirs(); |
| 72 | + if (!OUTPUT.exists()) OUTPUT.mkdirs(); |
| 73 | + if (!LIBRARIES.exists()) LIBRARIES.mkdirs(); |
| 74 | + if (!RESOURCES.exists()) RESOURCES.mkdirs(); |
| 75 | + } |
| 76 | + |
| 77 | + enum Colour { |
| 78 | + RESET("\033[0m"), |
| 79 | + BLACK("\033[0;30m"), |
| 80 | + RED("\033[0;31m"), |
| 81 | + GREEN("\033[0;32m"), |
| 82 | + YELLOW("\033[0;33m"), |
| 83 | + BLUE("\033[0;34m"), |
| 84 | + PURPLE("\033[0;35m"), |
| 85 | + CYAN("\033[0;36m"), |
| 86 | + WHITE("\033[0;37m"), |
| 87 | + |
| 88 | + // Bold |
| 89 | + BLACK_BOLD("\033[1;30m"), // BLACK |
| 90 | + RED_BOLD("\033[1;31m"), // RED |
| 91 | + GREEN_BOLD("\033[1;32m"), // GREEN |
| 92 | + YELLOW_BOLD("\033[1;33m"), // YELLOW |
| 93 | + BLUE_BOLD("\033[1;34m"), // BLUE |
| 94 | + MAGENTA_BOLD("\033[1;35m"), // MAGENTA |
| 95 | + CYAN_BOLD("\033[1;36m"), // CYAN |
| 96 | + WHITE_BOLD("\033[1;37m"), // WHITE |
| 97 | + |
| 98 | + // Underline |
| 99 | + BLACK_UNDERLINED("\033[4;30m"), // BLACK |
| 100 | + RED_UNDERLINED("\033[4;31m"), // RED |
| 101 | + GREEN_UNDERLINED("\033[4;32m"), // GREEN |
| 102 | + YELLOW_UNDERLINED("\033[4;33m"), // YELLOW |
| 103 | + BLUE_UNDERLINED("\033[4;34m"), // BLUE |
| 104 | + MAGENTA_UNDERLINED("\033[4;35m"), // MAGENTA |
| 105 | + CYAN_UNDERLINED("\033[4;36m"), // CYAN |
| 106 | + WHITE_UNDERLINED("\033[4;37m"), // WHITE |
| 107 | + |
| 108 | + // Background |
| 109 | + BLACK_BACKGROUND("\033[40m"), // BLACK |
| 110 | + RED_BACKGROUND("\033[41m"), // RED |
| 111 | + GREEN_BACKGROUND("\033[42m"), // GREEN |
| 112 | + YELLOW_BACKGROUND("\033[43m"), // YELLOW |
| 113 | + BLUE_BACKGROUND("\033[44m"), // BLUE |
| 114 | + MAGENTA_BACKGROUND("\033[45m"), // MAGENTA |
| 115 | + CYAN_BACKGROUND("\033[46m"), // CYAN |
| 116 | + WHITE_BACKGROUND("\033[47m"), // WHITE |
| 117 | + |
| 118 | + // High Intensity |
| 119 | + BLACK_BRIGHT("\033[0;90m"), // BLACK |
| 120 | + RED_BRIGHT("\033[0;91m"), // RED |
| 121 | + GREEN_BRIGHT("\033[0;92m"), // GREEN |
| 122 | + YELLOW_BRIGHT("\033[0;93m"), // YELLOW |
| 123 | + BLUE_BRIGHT("\033[0;94m"), // BLUE |
| 124 | + MAGENTA_BRIGHT("\033[0;95m"), // MAGENTA |
| 125 | + CYAN_BRIGHT("\033[0;96m"), // CYAN |
| 126 | + WHITE_BRIGHT("\033[0;97m"), // WHITE |
| 127 | + |
| 128 | + // Bold High Intensity |
| 129 | + BLACK_BOLD_BRIGHT("\033[1;90m"), // BLACK |
| 130 | + RED_BOLD_BRIGHT("\033[1;91m"), // RED |
| 131 | + GREEN_BOLD_BRIGHT("\033[1;92m"), // GREEN |
| 132 | + YELLOW_BOLD_BRIGHT("\033[1;93m"), // YELLOW |
| 133 | + BLUE_BOLD_BRIGHT("\033[1;94m"), // BLUE |
| 134 | + MAGENTA_BOLD_BRIGHT("\033[1;95m"), // MAGENTA |
| 135 | + CYAN_BOLD_BRIGHT("\033[1;96m"), // CYAN |
| 136 | + WHITE_BOLD_BRIGHT("\033[1;97m"), // WHITE |
| 137 | + |
| 138 | + // High Intensity backgrounds |
| 139 | + BLACK_BACKGROUND_BRIGHT("\033[0;100m"), // BLACK |
| 140 | + RED_BACKGROUND_BRIGHT("\033[0;101m"), // RED |
| 141 | + GREEN_BACKGROUND_BRIGHT("\033[0;102m"), // GREEN |
| 142 | + YELLOW_BACKGROUND_BRIGHT("\033[0;103m"), // YELLOW |
| 143 | + BLUE_BACKGROUND_BRIGHT("\033[0;104m"), // BLUE |
| 144 | + MAGENTA_BACKGROUND_BRIGHT("\033[0;105m"), // MAGENTA |
| 145 | + CYAN_BACKGROUND_BRIGHT("\033[0;106m"), // CYAN |
| 146 | + WHITE_BACKGROUND_BRIGHT("\033[0;107m"); // WHITE |
| 147 | + |
| 148 | + private final String code; |
| 149 | + |
| 150 | + Colour(String code) { |
| 151 | + this.code = code; |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public String toString() { |
| 156 | + if (System.console() == null || System.getenv().get("TERM") != null) { |
| 157 | + return code; |
| 158 | + } else { |
| 159 | + return ""; |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | +} |
0 commit comments