package gw.vark;
import gw.internal.gosu.parser.IGosuAnnotation;
import gw.lang.Gosu;
import gw.lang.launch.ArgInfo;
import gw.lang.mode.GosuMode;
import gw.lang.mode.RequiresInit;
import gw.lang.parser.IDynamicFunctionSymbol;
import gw.lang.parser.exceptions.ParseResultsException;
import gw.lang.parser.statements.IFunctionStatement;
import gw.lang.reflect.IMethodInfo;
import gw.lang.reflect.IType;
import gw.lang.reflect.TypeSystem;
import gw.util.GosuExceptionUtil;
import gw.util.StreamUtil;
import gw.vark.typeloader.AntlibTypeLoader;
import org.apache.tools.ant.*;
import org.apache.tools.ant.util.ClasspathUtils;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.util.List;
@RequiresInit
{
public static final String DEFAULT_BUILD_FILE_NAME = "build.vark";
public static final int GOSU_MODE_PRIORITY_AARDVARK_HELP = 0;
public static final int GOSU_MODE_PRIORITY_AARDVARK_VERSION = 1;
public static final int GOSU_MODE_PRIORITY_AARDVARK_INTERACTIVE = 2;
public static final int GOSU_MODE_PRIORITY_AARDVARK_EDITOR = 3;
public static final int GOSU_MODE_PRIORITY_AARDVARK = 4;
private static BuildLogger _logger;
private static Project _antProjectInstance;
static final int EXITCODE_VARKFILE_NOT_FOUND = 4;
static final int EXITCODE_GOSU_VERIFY_FAILED = 8;
private static String RAW_VARK_FILE_PATH = "";
if (_antProjectInstance == null) {
throw new NoProjectInstanceException();
}
return _antProjectInstance;
}
public static void setProject(Project project, BuildLogger logger) {
_antProjectInstance = project;
if (logger != null) {
project.removeBuildListener(_logger);
_logger = logger;
logger.setMessageOutputLevel(Project.MSG_INFO);
logger.setOutputPrintStream(System.out);
logger.setErrorPrintStream(System.err);
project.addBuildListener(logger);
}
}
@SuppressWarnings("UnusedDeclaration")
return RAW_VARK_FILE_PATH;
}
public static void main( String... args )
throws Exception {
Gosu.main(args);
}
private AardvarkOptions _options;
this(new DefaultLogger());
}
logger.setMessageOutputLevel( Project.MSG_INFO );
logger.setOutputPrintStream(System.out);
logger.setErrorPrintStream(System.err);
_logger = logger;
}
@Override
return GOSU_MODE_PRIORITY_AARDVARK;
}
@Override
_options = new AardvarkOptions(_argInfo);
return true;
}
@Override
public int run()
throws Exception {
RAW_VARK_FILE_PATH = _argInfo.getProgramSource().getRawPath();
AardvarkProgram aardvarkProject;
Project antProject = new Project();
setProject(antProject, _logger);
if (_options.getLogger() != null) {
_logger = newLogger(_options.getLogger());
}
_logger.setMessageOutputLevel(_options.getLogLevel().getLevel());
if ("true".equals(System.getProperty("aardvark.dev"))) {
System.err.println("aardvark.dev is on");
pushAntlibTypeloader();
}
ArgInfo.IProgramSource programSource = _argInfo.getProgramSource();
InputStream in = null;
try {
in = programSource.openInputStream();
log("Buildfile: " + programSource.getRawPath());
aardvarkProject = AardvarkProgram.parseWithTimer(antProject, programSource.getFile(), in);
}
catch (FileNotFoundException e) {
if (programSource instanceof ArgInfo.DefaultLocalProgramSource) {
logErr("Default vark buildfile " + Aardvark.DEFAULT_BUILD_FILE_NAME + " doesn't exist");
}
else {
logErr("Specified vark buildfile " + programSource.getRawPath() + " doesn't exist");
}
return EXITCODE_VARKFILE_NOT_FOUND;
}
catch (ParseResultsException e) {
logErr(e.getMessage());
return EXITCODE_GOSU_VERIFY_FAILED;
}
finally {
try {
StreamUtil.close(in);
} catch (IOException e) {
}
}
int exitCode = 1;
try {
try {
if (_options.isHelp()) {
aardvarkProject.printProjectHelp();
}
else {
aardvarkProject.runBuild(_options.getTargetCalls());
}
exitCode = 0;
} catch (ExitStatusException ese) {
exitCode = ese.getStatus();
if (exitCode != 0) {
throw ese;
}
}
} catch (BuildException e) {
} catch (Throwable e) {
e.printStackTrace();
printMessage(e);
}
return exitCode;
}
AntlibTypeLoader loader = new AntlibTypeLoader(TypeSystem.getCurrentModule());
TypeSystem.pushTypeLoader(loader);
}
String message = t.getMessage();
if (message != null) {
logErr(message);
}
}
public static boolean isTargetMethod(IType gosuProgram, IMethodInfo methodInfo) {
return methodInfo.isPublic()
&& (methodInfo.hasAnnotation(TypeSystem.get(gw.vark.annotations.Target.class))
|| (methodInfo.getParameters().length == 0 && methodInfo.getOwnersType().equals( gosuProgram )));
}
if (target != null && target.getDynamicFunctionSymbol() != null && target.getDynamicFunctionSymbol().getModifierInfo() != null) {
IDynamicFunctionSymbol dfs = target.getDynamicFunctionSymbol();
return isPublic(dfs.getModifiers())
&& (findAnnotation(dfs.getModifierInfo().getAnnotations(), TypeSystem.get(gw.vark.annotations.Target.class))
|| dfs.getArgs().size() == 0);
}
return false;
}
private static boolean isPublic(
int mod) {
return !Modifier.isPrivate(mod) && !Modifier.isProtected(mod);
}
private static boolean findAnnotation(List<IGosuAnnotation> annotations, IType annotationType) {
for (IGosuAnnotation annotation : annotations) {
if (annotation.getExpression().getType().equals(annotationType)) {
return true;
}
}
return false;
}
private BuildLogger
newLogger(String loggerClassName) {
try {
return (BuildLogger) ClasspathUtils.newInstance(loggerClassName, Aardvark.class.getClassLoader(), BuildLogger.class);
}
catch (BuildException e) {
logErr("The specified logger class " + loggerClassName + " could not be used because " + e.getMessage());
throw e;
}
}
private void log(String message) {
getProject().log(message);
}
private void logErr(String message) {
getProject().log(message, Project.MSG_ERR);
}
URL versionResource = Aardvark.class.getResource("/gw/vark/version.txt");
try {
Reader reader = StreamUtil.getInputStreamReader(versionResource.openStream());
String version = StreamUtil.getContent(reader).trim();
return "Aardvark version " + version;
} catch (IOException e) {
throw GosuExceptionUtil.forceThrow(e);
}
}
}