Permalink
Browse files

Add 'new' command to create blank ms files on disk

  • Loading branch information...
LadyCailin committed Sep 18, 2017
1 parent 6710083 commit f0c90150795750dd352cb2a8c7fb85d5e722a0c9
Showing with 130 additions and 98 deletions.
  1. +101 −97 src/main/java/com/laytonsmith/abstraction/StaticLayer.java
  2. +29 −1 src/main/java/com/laytonsmith/core/Main.java
@@ -11,175 +11,179 @@
import java.util.Set;
/**
* Unfortunately some methods just can't be overridden.
*
*/
public final class StaticLayer {
private StaticLayer(){}
private StaticLayer() {
}
//Do not rename this field, it is used reflectively in testing
private static Convertor convertor = null;
static{
InitConvertor();
}
private static synchronized void InitConvertor(){
Set<Class<?>> classes = ClassDiscovery.getDefaultInstance().loadClassesWithAnnotation(convert.class);
for(Class<?> c : classes){
if(!Convertor.class.isAssignableFrom(c)){
StreamUtils.GetSystemErr().println("The Convertor " + c.getSimpleName() + " doesn't implement Convertor!");
}
convert convert = c.getAnnotation(convert.class);
if(convert.type() == Implementation.GetServerType()){
//This is what we're looking for, instatiate it.
try{
if(convertor != null){
//Uh... There are more than one implementations for this server type
System.out.println("More than one Convertor for this server type was detected!");
}
convertor = (Convertor) c.newInstance();
//At this point we are all set
} catch(Exception e){
StreamUtils.GetSystemErr().println("Tried to instantiate the Convertor, but couldn't!");
}
}
}
if(convertor == null){
StreamUtils.GetSystemErr().println("Could not find a suitable convertor! You will experience serious issues with this plugin.");
}
static {
InitConvertor();
}
private static synchronized void InitConvertor() {
Set<Class<?>> classes = ClassDiscovery.getDefaultInstance().loadClassesWithAnnotation(convert.class);
for (Class<?> c : classes) {
if (!Convertor.class.isAssignableFrom(c)) {
StreamUtils.GetSystemErr().println("The Convertor " + c.getSimpleName() + " doesn't implement Convertor!");
}
convert convert = c.getAnnotation(convert.class);
if (convert.type() == Implementation.GetServerType()) {
//This is what we're looking for, instatiate it.
try {
if (convertor != null) {
//Uh... There are more than one implementations for this server type
System.out.println("More than one Convertor for this server type was detected!");
}
convertor = (Convertor) c.newInstance();
//At this point we are all set
} catch (Exception e) {
StreamUtils.GetSystemErr().println("Tried to instantiate the Convertor, but couldn't!");
}
}
}
if (convertor == null) {
StreamUtils.GetSystemErr().println("Could not find a suitable convertor! You will experience serious issues with this plugin.");
}
}
public static MCLocation GetLocation(MCWorld w, double x, double y, double z, float yaw, float pitch) {
return convertor.GetLocation(w, x, y, z, yaw, pitch);
return convertor.GetLocation(w, x, y, z, yaw, pitch);
}
public static Class<?> GetServerEventMixin() {
return convertor.GetServerEventMixin();
return convertor.GetServerEventMixin();
}
public static MCLocation GetLocation(MCWorld w, double x, double y, double z){
return GetLocation(w, x, y, z, 0, 0);
public static MCLocation GetLocation(MCWorld w, double x, double y, double z) {
return GetLocation(w, x, y, z, 0, 0);
}
public static MCItemStack GetItemStack(int type, int qty) {
return convertor.GetItemStack(type, qty);
}
public static MCItemStack GetItemStack(int type, int qty) {
return convertor.GetItemStack(type, qty);
}
public static MCItemStack GetItemStack(int type, int data, int qty){
return convertor.GetItemStack(type, data, qty);
}
public static MCItemStack GetItemStack(int type, int data, int qty) {
return convertor.GetItemStack(type, data, qty);
}
public static MCItemStack GetItemStack(String type, int qty) {
return convertor.GetItemStack(type, qty);
}
public static MCItemStack GetItemStack(String type, int qty) {
return convertor.GetItemStack(type, qty);
}
public static MCItemStack GetItemStack(String type, int data, int qty){
return convertor.GetItemStack(type, data, qty);
}
public static MCItemStack GetItemStack(String type, int data, int qty) {
return convertor.GetItemStack(type, data, qty);
}
public static MCItemStack GetItemStack(MCMaterial type, int qty) {
return convertor.GetItemStack(type, qty);
}
public static MCItemStack GetItemStack(MCMaterial type, int qty) {
return convertor.GetItemStack(type, qty);
}
public static MCItemStack GetItemStack(MCMaterial type, int data, int qty){
return convertor.GetItemStack(type, data, qty);
}
public static MCItemStack GetItemStack(MCMaterial type, int data, int qty) {
return convertor.GetItemStack(type, data, qty);
}
public static MCPotionData GetPotionData(MCPotionType type, boolean extended, boolean upgraded){
return convertor.GetPotionData(type, extended, upgraded);
}
public static MCPotionData GetPotionData(MCPotionType type, boolean extended, boolean upgraded) {
return convertor.GetPotionData(type, extended, upgraded);
}
public static MCServer GetServer(){
return convertor.GetServer();
public static MCServer GetServer() {
return convertor.GetServer();
}
public static MCEnchantment GetEnchantmentByName(String name){
return convertor.GetEnchantmentByName(name);
public static MCEnchantment GetEnchantmentByName(String name) {
return convertor.GetEnchantmentByName(name);
}
public static MCMaterial GetMaterial(String name) {
return convertor.GetMaterial(name);
}
public static MCMaterial GetMaterial(String name) {
return convertor.GetMaterial(name);
}
public static MCEnchantment[] GetEnchantmentValues(){
return convertor.GetEnchantmentValues();
public static MCEnchantment[] GetEnchantmentValues() {
return convertor.GetEnchantmentValues();
}
public static void Startup(CommandHelperPlugin chp) {
convertor.Startup(chp);
convertor.Startup(chp);
}
public static MCMetadataValue GetMetadataValue(Object value, MCPlugin plugin) {
return convertor.GetMetadataValue(value, plugin);
}
public static MCMetadataValue GetMetadataValue(Object value, MCPlugin plugin) {
return convertor.GetMetadataValue(value, plugin);
}
/**
* Returns the data value of the specified material name, or -1 if none was found.
*
* @param materialName
* @return
*/
public static int LookupItemId(String materialName){
return convertor.LookupItemId(materialName);
public static int LookupItemId(String materialName) {
return convertor.LookupItemId(materialName);
}
/**
* Returns the name of the material, given the material's ID.
*
* @param id
* @return
*/
public static String LookupMaterialName(int id){
return convertor.LookupMaterialName(id);
public static String LookupMaterialName(int id) {
return convertor.LookupMaterialName(id);
}
/**
* Adds a runnable to the main thread, if required by this platform,
* if a multithreaded user code would be dangerous.
* Adds a runnable to the main thread, if required by this platform, if a multithreaded user code would be
* dangerous.
*
* @param ms
* @param r
* @return
*/
public static int SetFutureRunnable(DaemonManager dm, long ms, Runnable r){
return convertor.SetFutureRunnable(dm, ms, r);
public static int SetFutureRunnable(DaemonManager dm, long ms, Runnable r) {
return convertor.SetFutureRunnable(dm, ms, r);
}
public static int SetFutureRepeater(DaemonManager dm, long ms, long initialDelay, Runnable r){
return convertor.SetFutureRepeater(dm, ms, initialDelay, r);
public static int SetFutureRepeater(DaemonManager dm, long ms, long initialDelay, Runnable r) {
return convertor.SetFutureRepeater(dm, ms, initialDelay, r);
}
public static void ClearAllRunnables() {
convertor.ClearAllRunnables();
convertor.ClearAllRunnables();
}
public static void ClearFutureRunnable(int id){
convertor.ClearFutureRunnable(id);
public static void ClearFutureRunnable(int id) {
convertor.ClearFutureRunnable(id);
}
/**
* Given an entity, returns the more specific entity type, by creating a new more
* specific type based on the actual type of the underlying object contained by the
* more generic type.
* Given an entity, returns the more specific entity type, by creating a new more specific type based on the actual
* type of the underlying object contained by the more generic type.
*
* @param e
* @return
*/
public static MCEntity GetCorrectEntity(MCEntity e) {
return convertor.GetCorrectEntity(e);
return convertor.GetCorrectEntity(e);
}
public static MCRecipe GetNewRecipe(MCRecipeType type, MCItemStack result) {
return convertor.GetNewRecipe(type, result);
}
public static MCRecipe GetNewRecipe(MCRecipeType type, MCItemStack result) {
return convertor.GetNewRecipe(type, result);
}
public static String GetPluginName() {
return convertor.GetPluginName();
}
public static String GetPluginName() {
return convertor.GetPluginName();
}
public static MCPlugin GetPlugin() {
return convertor.GetPlugin();
}
public static MCPlugin GetPlugin() {
return convertor.GetPlugin();
}
public static synchronized Convertor GetConvertor(){
return convertor;
}
public static synchronized Convertor GetConvertor() {
return convertor;
}
}
@@ -7,6 +7,7 @@
import com.laytonsmith.PureUtilities.CommandExecutor;
import com.laytonsmith.PureUtilities.Common.FileUtil;
import com.laytonsmith.PureUtilities.Common.Misc;
import com.laytonsmith.PureUtilities.Common.OSUtils;
import com.laytonsmith.PureUtilities.Common.RSAEncrypt;
import com.laytonsmith.PureUtilities.Common.StreamUtils;
import com.laytonsmith.PureUtilities.Common.StringUtils;
@@ -16,11 +17,13 @@
import com.laytonsmith.abstraction.StaticLayer;
import com.laytonsmith.annotations.api;
import com.laytonsmith.core.compiler.OptimizationUtilities;
import com.laytonsmith.core.constructs.CString;
import com.laytonsmith.core.constructs.Target;
import com.laytonsmith.core.exceptions.ConfigCompileException;
import com.laytonsmith.core.extensions.ExtensionManager;
import com.laytonsmith.core.functions.FunctionBase;
import com.laytonsmith.core.functions.FunctionList;
import com.laytonsmith.core.functions.Scheduling;
import com.laytonsmith.persistence.PersistenceNetwork;
import com.laytonsmith.persistence.io.ConnectionMixinFactory;
import com.laytonsmith.tools.ExampleLocalPackageInstaller;
@@ -82,6 +85,7 @@
private static final ArgumentParser pnViewerMode;
private static final ArgumentParser coreFunctionsMode;
private static final ArgumentParser uiMode;
private static final ArgumentParser newMode;
static {
MethodScriptFileLocations.setDefault(new MethodScriptFileLocations());
@@ -196,6 +200,12 @@
+ " command creates a subshell to run the launcher in, so that the original cmdline shell returns.")
.addFlag("in-shell", "Runs the launcher in the same shell process. By default, it creates a new process and causes the initial shell to return.");
suite.addMode("ui", uiMode);
newMode = ArgumentParser.GetParser()
.addDescription("Creates a blank script in the specified location with the appropriate permissions, having the correct hashbang, and ready to be executed. If"
+ " the specified file already exists, it will refuse to create it, unless --force is set.")
.addArgument("Location and name to create the script as. Multiple arguments can be provided, and they will create multiple files.", "<file>", true)
.addFlag('f', "force", "Forces the file to be overwritten, even if it already exists");
suite.addMode("new", newMode);
ARGUMENT_SUITE = suite;
}
@@ -234,7 +244,6 @@ public static void main(String[] args) throws Exception {
// However, if it is put back, then it needs to be figured out why this causes the terminal
// to lose focus on mac.
//AnnotationChecks.checkForceImplementation();
ArgumentParser mode;
ArgumentParser.ArgumentParserResults parsedArgs;
@@ -560,6 +569,25 @@ public static void main(String[] args) throws Exception {
ce.start();
System.exit(0);
}
} else if (mode == newMode) {
String li = OSUtils.GetLineEnding();
for (String file : parsedArgs.getStringListArgument()) {
File f = new File(file);
if (f.exists() && !parsedArgs.isFlagSet('f')) {
System.out.println(file + " already exists, refusing to create");
continue;
}
f.createNewFile();
f.setExecutable(true);
FileUtil.write("#!/usr/bin/env /usr/local/bin/mscript"
+ li + li
+ "/**" + li
+ " * Name: " + f.getName() + li
+ " * Author: " + StaticLayer.GetConvertor().GetUser(null) + li
+ " * Creation Date: " + new Scheduling.simple_date().exec(Target.UNKNOWN, null, new CString("yyyy-MM-dd", Target.UNKNOWN)).val() + li
+ " * Description: " + li
+ " */" + li + li, f, true);
}
} else {
throw new Error("Should not have gotten here");
}

0 comments on commit f0c9015

Please sign in to comment.