<commit_before>package com.athaydes.jgrab.runner;

import com.athaydes.jgrab.Dependency;
import com.athaydes.jgrab.code.JavaCode;
import com.athaydes.jgrab.code.StdinJavaCode;
import com.athaydes.jgrab.code.StringJavaCode;
import com.athaydes.jgrab.daemon.JGrabDaemon;
import com.athaydes.jgrab.ivy.IvyGrabber;
import com.athaydes.osgiaas.api.env.ClassLoaderContext;
import com.athaydes.osgiaas.javac.internal.DefaultClassLoaderContext;
import com.athaydes.osgiaas.javac.internal.compiler.OsgiaasJavaCompilerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;

/**
 * Runs a Java file, using the JGrab annotations to find its dependencies.
 */
public class JGrabRunner {

    private static final Logger logger = LoggerFactory.getLogger( JGrabRunner.class );

    public static final String SNIPPET_OPTION = "-e";

    private static JGrabOptions parseOptions( String[] args ) {
        if ( args.length == 0 ) {
            return new JGrabOptions( JGrabRunnable.JAVA_FILE_STD_IN, "" );
        }
        if ( args.length == 1 ) {
            if ( args[ 0 ].equals( "--daemon" ) || args[ 0 ].equals( "-d" ) ) {
                return new JGrabOptions( JGrabRunnable.START_DAEMON, "" );
            }
            if ( args[ 0 ].equals( "--help" ) || args[ 0 ].equals( "-h" ) ) {
                return help();
            }

            return new JGrabOptions( JGrabRunnable.JAVA_FILE_NAME, args[ 0 ] );
        }
        if ( args.length == 2 ) {
            if ( args[ 0 ].equals( SNIPPET_OPTION ) ) {
                return new JGrabOptions( JGrabRunnable.JAVA_SOURCE_CODE, args[ 1 ] );
            }
        }

        return error( "Too many options" );
    }

    private static JGrabOptions error( String reason ) {
        throw new JGrabError( reason + "\n\nUsage: jgrab (-e <java_source>) | java_file" );
    }

    private static JGrabOptions help() {
        System.out.println( "=================== JGrab ===================\n" +
                " - https://github.com/renatoathaydes/jgrab -\n" +
                "=============================================\n" +
                "Jgrab can execute Java code from stdin (if not given any argument),\n" +
                "a Java file, or a Java snippet.\n\n" +
                "Usage:\n" +
                "  jgrab [<option> | java_file | -e java_snippet]\n" +
                "Options:\n" +
                "  --daemon -d\n" +
                "    Starts up the JGrab daemon (used by the jgrab-client).\n" +
                "  --help -h\n" +
                "    Shows this usage help." );

        return new JGrabOptions( JGrabRunnable.NONE, "" );
    }

    private static void run( String currentDir, JGrabOptions options ) throws Exception {
        switch ( options.jGrabRunnable ) {
            case JAVA_FILE_NAME:
                Path rawPath = Paths.get( options.arg );
                Path canonicalPath = rawPath.isAbsolute() ? rawPath : Paths.get( currentDir ).resolve( rawPath );
                run( new FileJavaCode( canonicalPath ) );
                break;
            case JAVA_FILE_STD_IN:<commit_msg>Added support for -v --version option.<commit_after>package com.athaydes.jgrab.runner;

import com.athaydes.jgrab.Dependency;
import com.athaydes.jgrab.code.JavaCode;
import com.athaydes.jgrab.code.StdinJavaCode;
import com.athaydes.jgrab.code.StringJavaCode;
import com.athaydes.jgrab.daemon.JGrabDaemon;
import com.athaydes.jgrab.ivy.IvyGrabber;
import com.athaydes.osgiaas.api.env.ClassLoaderContext;
import com.athaydes.osgiaas.javac.internal.DefaultClassLoaderContext;
import com.athaydes.osgiaas.javac.internal.compiler.OsgiaasJavaCompilerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.jar.JarFile;

/**
 * Runs a Java file, using the JGrab annotations to find its dependencies.
 */
public class JGrabRunner {

    private static final Logger logger = LoggerFactory.getLogger( JGrabRunner.class );

    public static final String SNIPPET_OPTION = "-e";

    private static JGrabOptions parseOptions( String[] args ) {
        if ( args.length == 0 ) {
            return new JGrabOptions( JGrabRunnable.JAVA_FILE_STD_IN, "" );
        }
        if ( args.length == 1 ) {
            if ( args[ 0 ].equals( "--daemon" ) || args[ 0 ].equals( "-d" ) ) {
                return new JGrabOptions( JGrabRunnable.START_DAEMON, "" );
            }
            if ( args[ 0 ].equals( "--help" ) || args[ 0 ].equals( "-h" ) ) {
                return help();
            }
            if ( args[ 0 ].equals( "--version" ) || args[ 0 ].equals( "-v" ) ) {
                return version();
            }

            return new JGrabOptions( JGrabRunnable.JAVA_FILE_NAME, args[ 0 ] );
        }
        if ( args.length == 2 ) {
            if ( args[ 0 ].equals( SNIPPET_OPTION ) ) {
                return new JGrabOptions( JGrabRunnable.JAVA_SOURCE_CODE, args[ 1 ] );
            }
        }

        return error( "Too many options" );
    }

    private static JGrabOptions error( String reason ) {
        throw new JGrabError( reason + "\n\nUsage: jgrab (-e <java_source>) | java_file" );
    }

    private static JGrabOptions version() {
        URL jarUrl = JGrabRunner.class.getProtectionDomain().getCodeSource().getLocation();
        String version = "UNKNOWN";

        try ( JarFile jar = new JarFile( new File( jarUrl.getFile() ) ) ) {
            String jarVersion = jar.getManifest().getMainAttributes().getValue( "Implementation-Version" );
            if ( jarVersion == null ) {
                logger.warn( "JGrab jar file's manifest does not contain Implementation-Version, " +
                        "unable to find out JGrab version." );
            } else {
                version = jarVersion;
            }
        } catch ( IOException e ) {
            logger.error( "Cannot access JGrab jar file: " + e );
        }

        System.out.println( "JGrab Version: " + version );
        System.out.println( "Java Version: " + System.getProperty( "java.version" ) );

        return new JGrabOptions( JGrabRunnable.NONE, "" );
    }

    private static JGrabOptions help() {
        System.out.println( "=================== JGrab ===================\n" +
                " - https://github.com/renatoathaydes/jgrab -\n" +
                "=============================================\n" +
                "Jgrab can execute Java code from stdin (if not given any argument),\n" +
                "a Java file, or a Java snippet.\n\n" +
                "Usage:\n" +
                "  jgrab [<option> | java_file | -e java_snippet]\n" +
                "Options:\n" +
                "  --daemon -d\n" +
                "    Starts up the JGrab daemon (used by the jgrab-client).\n" +
                "  --help -h\n" +
                "    Shows this usage help." );

        return new JGrabOptions( JGrabRunnable.NONE, "" );
    }

    private static void run( String currentDir, JGrabOptions options ) throws Exception {
        switch ( options.jGrabRunnable ) {
            case JAVA_FILE_NAME:
                Path rawPath = Paths.get( options.arg );
                Path canonicalPath = rawPath.isAbsolute() ? rawPath : Paths.get( currentDir ).resolve( rawPath );
                run( new FileJavaCode( canonicalPath ) );
                break;
            case JAVA_FILE_STD_IN:
{'fraction_non_alphanumeric': 0.1366906474820144, 'fraction_numerical': 0.0032815852581093022, 'mean_word_length': 2.2185215272136474, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1373926', 'n_tokens_mistral': 2541, 'n_tokens_neox': 2410, 'n_words': 570}