package com.google.javascript.jscomp;

import org.kohsuke.args4j.CmdLineException;

import com.google.common.collect.ImmutableSet;
import com.google.javascript.jscomp.CommandLineRunner;
import com.google.javascript.jscomp.CompilerOptions;
import com.google.javascript.jscomp.VariableRenamingPolicy;

/**
 * DebugStrippingCompilerRunner is an extension of the Closure Compiler that
 * strips debugging information when ADVANCED_OPTIMIZATIONS is used.
 *
 * @author bolinfest@gmail.com (Michael Bolin)
 */
public class DebugStrippingCompilerRunner extends CommandLineRunner {

    public DebugStrippingCompilerRunner(String[] args) throws CmdLineException {
        super(args);
    }

    @Override
    protected CompilerOptions createOptions() {
        CompilerOptions options = super.createOptions();

        // Use this as a heuristic to determine whether ADVANCED_OPTIMIZATIONS is
        // being used -- cannot access FLAG_compilation_level because it is private
        // in CompilerRunner.
        boolean isAdvancedOptionsEnabled =
                options.variableRenaming == VariableRenamingPolicy.ALL;

        // Only enable additional options when ADVANCED_OPTIMIZATIONS is specified.
        if (isAdvancedOptionsEnabled) {
            applyDebugStrippingOptions(options);
        }
        return options;
    }

    static void applyDebugStrippingOptions(CompilerOptions options) {
        options.stripNameSuffixes = ImmutableSet.of("logger", "logger_");
        options.stripTypePrefixes = ImmutableSet.of("goog.asserts"); // goog.debug
        options.setDefineToBooleanLiteral("goog.DEBUG", false);
        options.setIdGenerators(ImmutableSet.of("goog.events.getUniqueId"));
    }

    public static void main(String[] args) {
        try {
            (new DebugStrippingCompilerRunner(args)).run();
        } catch (CmdLineException e) {
            System.exit(-1);
        }
    }
}
@@ -32,16 +32,57 @@
 scomp;%0A%0A
+import com.google.javascript.rhino.Node;%0A
 import o
@@ -117,16 +117,16 @@
 eption;%0A
-
 %0Aimport 
@@ -334,16 +334,39 @@
 olicy;%0A%0A
+import java.util.Map;%0A%0A
 /**%0A * D
@@ -1185,123 +1185,512 @@
     
-// Only enable additional options when ADVANCED_OPTIMIZATIONS is specified.%0A        if (isAdvancedOptionsEnabled) %7B
+boolean isDebug = false;%0A        Map%3CString, Node%3E defines = options.getDefineReplacements();%0A        if (defines.containsKey(%22goog.DEBUG%22)) %7B%0A            Node b = defines.get(%22goog.DEBUG%22);%0A            isDebug = b.getBooleanProp(0);%0A        %7D%0A%0A        // Only enable additional options when ADVANCED_OPTIMIZATIONS is specified%0A        // and goog.DEBUG is true%0A        System.out.print(%22to stripping%22);%0A        if (isAdvancedOptionsEnabled && !isDebug) %7B%0A            System.out.print(%22stripping apply%22);
 %0A   
@@ -1741,24 +1741,25 @@
 ;%0A        %7D%0A
+%0A
         retu
@@ -1993,16 +1993,17 @@
 s%22); // 
+%22
 goog.deb
@@ -2004,16 +2004,17 @@
 og.debug
+%22
 %0A       
@@ -2006,32 +2006,34 @@
 .debug%22%0A        
+//
 options.setDefin

add logging messages