@Test public void testReloadCallsLastSourcedFile() throws Exception { final TestAdapter a = new TestAdapter(); final SourceCommand command = new SourceCommand(a, new SoarEventManager()); command.execute(DefaultSoarCommandContext.empty(), new String[] {"source", "-a", "test.soar"}); assertEquals(1, a.files.size()); assertEquals("test.soar", a.files.get(0).getName()); a.files.clear(); command.execute(DefaultSoarCommandContext.empty(), new String[] {"source", "-r"}); assertEquals(1, a.files.size()); assertEquals("test.soar", a.files.get(0).getName()); a.files.clear(); command.execute(DefaultSoarCommandContext.empty(), new String[] {"source", "--reload"}); assertEquals(1, a.files.size()); assertEquals("test.soar", a.files.get(0).getName()); }
@Override public String execute(SoarCommandContext commandContext, String[] args) throws SoarException { if(args.length < 2) { throw new SoarException("Expected fileName argument"); } final boolean topLevel = topLevelState == null; final boolean reload = "-r".equals(args[1]) || "--reload".equals(args[1]); if(topLevel && reload && lastTopLevelCommand != null) { return "Reloaded: " + StringTools.join(Arrays.asList(lastTopLevelCommand), " ") + "\n" + execute(commandContext, lastTopLevelCommand); } else if(!topLevel && reload) { throw new SoarException(args[1] + " option only valid at top level"); } else if(lastTopLevelCommand == null && reload) { throw new SoarException("No previous file to reload"); } final List<String> files = new ArrayList<String>(); final EnumSet<Options> opts = EnumSet.noneOf(Options.class); for(int i = 1; i < args.length; ++i) { final String arg = args[i]; if(arg.equals("-d") || arg.equals("--disable")) opts.add(Options.DISABLE); else if(arg.equals("-a") || arg.equals("--all")) opts.add(Options.ALL); else if(arg.equals("-v") || arg.endsWith("--verbose")) opts.add(Options.VERBOSE); else if(arg.startsWith("-")) { throw new SoarException("Unknown option '" + arg + "'"); } else { files.add(arg); } } if(topLevel) { topLevelState = new TopLevelState(); events.addListener(ProductionAddedEvent.class, eventListener); events.addListener(ProductionExcisedEvent.class, eventListener); } try { for(String file : files) { source(file); } if(topLevel) { lastTopLevelCommand = Arrays.copyOf(args, args.length); } return generateResult(topLevel, opts); } finally { if(topLevel) { topLevelState = null; events.removeListener(null, eventListener); } } }
SourceCommand implements SoarCommand { @Override public String execute(SoarCommandContext commandContext, String[] args) throws SoarException { if(args.length < 2) { throw new SoarException("Expected fileName argument"); } final boolean topLevel = topLevelState == null; final boolean reload = "-r".equals(args[1]) || "--reload".equals(args[1]); if(topLevel && reload && lastTopLevelCommand != null) { return "Reloaded: " + StringTools.join(Arrays.asList(lastTopLevelCommand), " ") + "\n" + execute(commandContext, lastTopLevelCommand); } else if(!topLevel && reload) { throw new SoarException(args[1] + " option only valid at top level"); } else if(lastTopLevelCommand == null && reload) { throw new SoarException("No previous file to reload"); } final List<String> files = new ArrayList<String>(); final EnumSet<Options> opts = EnumSet.noneOf(Options.class); for(int i = 1; i < args.length; ++i) { final String arg = args[i]; if(arg.equals("-d") || arg.equals("--disable")) opts.add(Options.DISABLE); else if(arg.equals("-a") || arg.equals("--all")) opts.add(Options.ALL); else if(arg.equals("-v") || arg.endsWith("--verbose")) opts.add(Options.VERBOSE); else if(arg.startsWith("-")) { throw new SoarException("Unknown option '" + arg + "'"); } else { files.add(arg); } } if(topLevel) { topLevelState = new TopLevelState(); events.addListener(ProductionAddedEvent.class, eventListener); events.addListener(ProductionExcisedEvent.class, eventListener); } try { for(String file : files) { source(file); } if(topLevel) { lastTopLevelCommand = Arrays.copyOf(args, args.length); } return generateResult(topLevel, opts); } finally { if(topLevel) { topLevelState = null; events.removeListener(null, eventListener); } } } }
SourceCommand implements SoarCommand { @Override public String execute(SoarCommandContext commandContext, String[] args) throws SoarException { if(args.length < 2) { throw new SoarException("Expected fileName argument"); } final boolean topLevel = topLevelState == null; final boolean reload = "-r".equals(args[1]) || "--reload".equals(args[1]); if(topLevel && reload && lastTopLevelCommand != null) { return "Reloaded: " + StringTools.join(Arrays.asList(lastTopLevelCommand), " ") + "\n" + execute(commandContext, lastTopLevelCommand); } else if(!topLevel && reload) { throw new SoarException(args[1] + " option only valid at top level"); } else if(lastTopLevelCommand == null && reload) { throw new SoarException("No previous file to reload"); } final List<String> files = new ArrayList<String>(); final EnumSet<Options> opts = EnumSet.noneOf(Options.class); for(int i = 1; i < args.length; ++i) { final String arg = args[i]; if(arg.equals("-d") || arg.equals("--disable")) opts.add(Options.DISABLE); else if(arg.equals("-a") || arg.equals("--all")) opts.add(Options.ALL); else if(arg.equals("-v") || arg.endsWith("--verbose")) opts.add(Options.VERBOSE); else if(arg.startsWith("-")) { throw new SoarException("Unknown option '" + arg + "'"); } else { files.add(arg); } } if(topLevel) { topLevelState = new TopLevelState(); events.addListener(ProductionAddedEvent.class, eventListener); events.addListener(ProductionExcisedEvent.class, eventListener); } try { for(String file : files) { source(file); } if(topLevel) { lastTopLevelCommand = Arrays.copyOf(args, args.length); } return generateResult(topLevel, opts); } finally { if(topLevel) { topLevelState = null; events.removeListener(null, eventListener); } } } SourceCommand(SourceCommandAdapter interp, SoarEventManager events); }
SourceCommand implements SoarCommand { @Override public String execute(SoarCommandContext commandContext, String[] args) throws SoarException { if(args.length < 2) { throw new SoarException("Expected fileName argument"); } final boolean topLevel = topLevelState == null; final boolean reload = "-r".equals(args[1]) || "--reload".equals(args[1]); if(topLevel && reload && lastTopLevelCommand != null) { return "Reloaded: " + StringTools.join(Arrays.asList(lastTopLevelCommand), " ") + "\n" + execute(commandContext, lastTopLevelCommand); } else if(!topLevel && reload) { throw new SoarException(args[1] + " option only valid at top level"); } else if(lastTopLevelCommand == null && reload) { throw new SoarException("No previous file to reload"); } final List<String> files = new ArrayList<String>(); final EnumSet<Options> opts = EnumSet.noneOf(Options.class); for(int i = 1; i < args.length; ++i) { final String arg = args[i]; if(arg.equals("-d") || arg.equals("--disable")) opts.add(Options.DISABLE); else if(arg.equals("-a") || arg.equals("--all")) opts.add(Options.ALL); else if(arg.equals("-v") || arg.endsWith("--verbose")) opts.add(Options.VERBOSE); else if(arg.startsWith("-")) { throw new SoarException("Unknown option '" + arg + "'"); } else { files.add(arg); } } if(topLevel) { topLevelState = new TopLevelState(); events.addListener(ProductionAddedEvent.class, eventListener); events.addListener(ProductionExcisedEvent.class, eventListener); } try { for(String file : files) { source(file); } if(topLevel) { lastTopLevelCommand = Arrays.copyOf(args, args.length); } return generateResult(topLevel, opts); } finally { if(topLevel) { topLevelState = null; events.removeListener(null, eventListener); } } } SourceCommand(SourceCommandAdapter interp, SoarEventManager events); @Override Object getCommand(); String getWorkingDirectory(); String getCurrentFile(); List<String> getSourcedFiles(); void pushd(String dirString); void popd(); void source(String fileString); @Override String execute(SoarCommandContext commandContext, String[] args); }
SourceCommand implements SoarCommand { @Override public String execute(SoarCommandContext commandContext, String[] args) throws SoarException { if(args.length < 2) { throw new SoarException("Expected fileName argument"); } final boolean topLevel = topLevelState == null; final boolean reload = "-r".equals(args[1]) || "--reload".equals(args[1]); if(topLevel && reload && lastTopLevelCommand != null) { return "Reloaded: " + StringTools.join(Arrays.asList(lastTopLevelCommand), " ") + "\n" + execute(commandContext, lastTopLevelCommand); } else if(!topLevel && reload) { throw new SoarException(args[1] + " option only valid at top level"); } else if(lastTopLevelCommand == null && reload) { throw new SoarException("No previous file to reload"); } final List<String> files = new ArrayList<String>(); final EnumSet<Options> opts = EnumSet.noneOf(Options.class); for(int i = 1; i < args.length; ++i) { final String arg = args[i]; if(arg.equals("-d") || arg.equals("--disable")) opts.add(Options.DISABLE); else if(arg.equals("-a") || arg.equals("--all")) opts.add(Options.ALL); else if(arg.equals("-v") || arg.endsWith("--verbose")) opts.add(Options.VERBOSE); else if(arg.startsWith("-")) { throw new SoarException("Unknown option '" + arg + "'"); } else { files.add(arg); } } if(topLevel) { topLevelState = new TopLevelState(); events.addListener(ProductionAddedEvent.class, eventListener); events.addListener(ProductionExcisedEvent.class, eventListener); } try { for(String file : files) { source(file); } if(topLevel) { lastTopLevelCommand = Arrays.copyOf(args, args.length); } return generateResult(topLevel, opts); } finally { if(topLevel) { topLevelState = null; events.removeListener(null, eventListener); } } } SourceCommand(SourceCommandAdapter interp, SoarEventManager events); @Override Object getCommand(); String getWorkingDirectory(); String getCurrentFile(); List<String> getSourcedFiles(); void pushd(String dirString); void popd(); void source(String fileString); @Override String execute(SoarCommandContext commandContext, String[] args);  }