-
-
Save abstractj/1319576 to your computer and use it in GitHub Desktop.
Main
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright 2011 Douglas Campos | |
* Copyright 2011 dynjs contributors | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package org.dynjs.cli; | |
import org.dynjs.DynJSVersion; | |
import org.dynjs.api.Scope; | |
import org.dynjs.runtime.DynJS; | |
import org.dynjs.runtime.DynObject; | |
import org.dynjs.runtime.DynThreadContext; | |
import org.kohsuke.args4j.CmdLineException; | |
import org.kohsuke.args4j.CmdLineParser; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
public class Main { | |
private Arguments dynJsArguments; | |
private CmdLineParser parser; | |
private String[] arguments; | |
private DynJS dynJS; | |
private DynThreadContext context; | |
public Main(String[] args) { | |
dynJsArguments = new Arguments(); | |
parser = new CmdLineParser(dynJsArguments); | |
parser.setUsageWidth(80); | |
arguments = args; | |
dynJS = new DynJS(); | |
context = new DynThreadContext(); | |
} | |
public static void main(String[] args) { | |
new Main(args).run(); | |
} | |
void run() { | |
try{ | |
parser.parseArgument(arguments); | |
if (dynJsArguments.isHelp() || dynJsArguments.isEmpty()) { | |
showUsage(); | |
} else if (dynJsArguments.isConsole()) { | |
startRepl(); | |
} else if (dynJsArguments.isVersion()) { | |
showVersion(); | |
} else if (!dynJsArguments.getFilename().isEmpty()){ | |
executeFile(dynJsArguments.getFilename()); | |
} | |
} catch (CmdLineException e) { | |
System.out.println(e.getMessage()); | |
System.out.println(); | |
showUsage(); | |
} | |
} | |
private void executeFile(String filename) { | |
try { | |
context.setScope(new DynObject()); | |
dynJS.eval(context, new FileInputStream(filename)); | |
} catch (FileNotFoundException e) { | |
System.out.println("File " + filename + " not found"); | |
} | |
} | |
private void showVersion() { | |
System.out.println("Dyn.JS version " + DynJSVersion.FULL); | |
} | |
private void startRepl() { | |
DynThreadContext threadContext = new DynThreadContext(); | |
Scope scope = new DynObject(); | |
DynJS environment = new DynJS(); | |
Repl repl = new Repl(environment, threadContext, scope); | |
repl.run(); | |
} | |
private void showUsage() { | |
StringBuilder usageText = new StringBuilder("Usage: dynjs [--console | --help | --version | FILE]\n"); | |
usageText.append("Starts the dynjs console or executes FILENAME depending the parameters\n"); | |
System.out.println(usageText.toString()); | |
parser.printUsage(System.out); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright 2011 Douglas Campos | |
* Copyright 2011 dynjs contributors | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package org.dynjs.cli; | |
import org.junit.Test; | |
import java.net.URL; | |
public class MainTest { | |
@Test | |
public void callMainWithNoArguments(){ | |
new Main(new String[]{}).run(); | |
} | |
@Test | |
public void callMainWithInvalidFile(){ | |
new Main(new String[]{"meh.js"}).run(); | |
} | |
@Test | |
public void callMainWithValidFile(){ | |
URL url = this.getClass().getResource("valid.js"); | |
new Main(new String[]{url.getPath()}).run(); | |
} | |
@Test | |
public void callMainWithInvalidArguments(){ | |
new Main(new String[]{"--invalid"}).run(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment