package com.keenwrite;
import com.keenwrite.cmdline.Arguments;
import com.keenwrite.cmdline.ColourScheme;
import com.keenwrite.cmdline.HeadlessApp;
import picocli.CommandLine;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.function.Consumer;
import java.util.logging.LogManager;
import static com.keenwrite.Bootstrap.*;
import static com.keenwrite.security.PermissiveCertificate.installTrustManager;
import static java.lang.String.format;
public final class Launcher implements Consumer<Arguments> {
private final String[] mArgs;
public static String getVersion() {
try {
final var properties = loadProperties( "app.properties" );
return properties.getProperty( "application.version" );
} catch( final Exception ex ) {
throw new RuntimeException( ex );
}
}
public static void terminate( final int exitCode ) {
System.exit( exitCode );
}
private static void parse( final String[] args ) {
assert args != null;
final var arguments = new Arguments( new Launcher( args ) );
final var parser = new CommandLine( arguments );
parser.setColorScheme( ColourScheme.create() );
final var exitCode = parser.execute( args );
final var parseResult = parser.getParseResult();
if( parseResult.isUsageHelpRequested() ) {
terminate( exitCode );
}
else if( parseResult.isVersionHelpRequested() ) {
showAppInfo();
terminate( exitCode );
}
}
@SuppressWarnings( "SameParameterValue" )
private static Properties loadProperties( final String resource )
throws IOException {
final var properties = new Properties();
properties.load( getResourceAsStream( getResourceName( resource ) ) );
return properties;
}
private static String getResourceName( final String resource ) {
return format( "%s/%s", getPackagePath(), resource );
}
private static String getPackagePath() {
return Launcher.class.getPackageName().replace( '.', '/' );
}
private static InputStream getResourceAsStream( final String resource ) {
return Launcher.class.getClassLoader().getResourceAsStream( resource );
}
private static void log( final Throwable error ) {
var message = error.getMessage();
if( message != null && message.toLowerCase().contains( "javafx" ) ) {
message = "Run using a Java Runtime Environment that includes JavaFX.";
out( "ERROR: %s", message );
}
else {
error.printStackTrace( System.err );
}
}
private static void out( final String message, final Object... args ) {
System.out.printf( format( "%s%n", message ), args );
}
public static void main( final String[] args ) {
installTrustManager();
parse( args );
}
public Launcher( final String[] args ) {
mArgs = args;
}
@Override
public void accept( final Arguments args ) {
assert args != null;
try {
int argCount = mArgs.length;
if( args.quiet() ) {
argCount--;
}
else {
showAppInfo();
}
if( args.debug() ) {
argCount--;
}
else {
disableLogging();
}
if( argCount <= 0 ) {
MainApp.main( mArgs );
}
else {
HeadlessApp.main( args );
}
} catch( final Throwable t ) {
log( t );
}
}
private static void disableLogging() {
LogManager.getLogManager().reset();
System.err.close();
}
private static void showAppInfo() {
out( "%n%s version %s", APP_TITLE, APP_VERSION );
out( "Copyright 2016-%s White Magic Software, Ltd.", APP_YEAR );
out( "Portions copyright 2015-2020 Karl Tauber.%n" );
}
}