<?xml version="1.0" encoding="UTF-8"?>
<project name="DMDirc-reports" default="default" basedir=".">
    <description>Checkstyle, findbugs, CPD and PMD reports</description>

    <target name="doallreports" depends="test-both,-allreports,javadoc"
            description="Run all reports." />
    <target name="domostreports" depends="test-both,-mostreports,javadoc"
            description="Run all reports except Findbugs." />

    <target name="checkstyle" depends="checkstyle-all, -move-index" description="Run checkstyle." /> 

    <target name="-allreports" depends="findbugs, cpd, pmd, checkstyle" /> 
    <target name="-mostreports" depends="cpd, pmd, checkstyle" /> 

    <target name="-init-checkstyle" depends="-init-dependencies">
        <taskdef resource="checkstyletask.properties" classpathref="analyse-classpath"/>
    </target>

    <target name="-init-pmd" depends="-init-dependencies">
        <taskdef name="pmd" classpathref="analyse-classpath" classname="net.sourceforge.pmd.ant.PMDTask" />
    </target>

    <target name="-init-cpd" depends="-init-dependencies">
        <taskdef name="cpd" classpathref="analyse-classpath" classname="net.sourceforge.pmd.cpd.CPDTask" />
    </target>

    <target name="-init-findbugs" depends="-init-dependencies">
        <taskdef name="findbugs" classpathref="analyse-classpath" classname="edu.umd.cs.findbugs.anttask.FindBugsTask" />
    </target>
    
    <target name="checkstyle-all" depends="-init-checkstyle">
        <checkstyle config="checkstyle/dmdirc_checks.xml" failureProperty="checkstyle.failure" failOnViolation="false">
            <fileset dir="src/" includes="**/*.java" excludes="net/miginfocom/**"/>
            <fileset dir="modules/plugins/src/" includes="**/*.java"/>
            <fileset dir="modules/parser/src/" includes="**/*.java"/>
            <fileset dir="modules/util/src/" includes="**/*.java"/>
            <formatter type="xml" tofile="reports/checkstyle.xml"/>
        </checkstyle>
        <xslt in="reports/checkstyle.xml" out="reports/checkstyle.html" style="checkstyle/checkstyle.xsl"/>
    </target>
    
    <target name="-move-index">
        <!-- <copy file="checkstyle/index.html" tofile="reports/index.html"/> -->
        <copy file="checkstyle/sorttable.js" tofile="reports/sorttable.js"/>
    </target>
    
    <target name="pmd" description="Run PMD." depends="-init-pmd">
        <pmd shortFilenames="true">
            <ruleset>checkstyle/pmd_checks.xml</ruleset>
            <formatter type="xml" toFile="reports/report-pmd.xml" linkPrefix="http://pmd.sourceforge.net/xref/"/>
            <fileset dir="src/" includes="com/dmdirc/**/*.java" />
            <fileset dir="modules/plugins/src/" includes="com/dmdirc/**/*.java"/>
            <fileset dir="modules/util/src/" includes="com/dmdirc/**/*.java"/>
            <fileset dir="modules/parser/src/" includes="com/dmdirc/**/*.java"/>
        </pmd>
        <xslt in="reports/report-pmd.xml" style="checkstyle/pmd.xslt" out="reports/report-pmd.html" />
    </target>

    <target name="pmd-minimal" description="Run PMD with minimal ruleset." depends="-init-pmd">
        <pmd shortFilenames="true">
            <ruleset>checkstyle/pmd_minimal.xml</ruleset>
            <formatter type="xml" toFile="reports/report-pmd.xml" linkPrefix="http://pmd.sourceforge.net/xref/"/>
            <fileset dir="src/" includes="com/dmdirc/**/*.java" />
            <fileset dir="modules/plugins/src/" includes="com/dmdirc/**/*.java"/>
            <fileset dir="modules/util/src/" includes="com/dmdirc/**/*.java"/>
            <fileset dir="modules/parser/src/" includes="com/dmdirc/**/*.java"/>
        </pmd>
        <xslt in="reports/report-pmd.xml" style="checkstyle/pmd.xslt" out="reports/report-pmd.html" />
    </target>
    
    <target name="cpd" description="Run CPD." depends="-init-cpd">
        <cpd format="xml" minimumTokenCount="50" outputFile="reports/report-cpd.xml">
            <fileset dir="src/com/">
                <include name="**/*.java"/>
            </fileset>
        </cpd>
        <xslt in="reports/report-cpd.xml" style="checkstyle/cpd.xslt" out="reports/report-cpd.html" />
    </target>
    
    <target name="findbugs" depends="-init-findbugs,jar" description="Run Findbugs.">
        <mkdir dir="reports/findbugs" />
        <findbugs home="/usr/share/java/findbugs/" effort="max"
                            jvmargs="-Xmx512M"
                            output="xml:withMessages"
                             outputFile="reports/findbugs/report-fb.xml" reportLevel="low">
            <sourcePath path="src/" />
            <class location="dist/DMDirc.jar" />
        </findbugs>
        <xslt in="reports/findbugs/report-fb.xml" style="checkstyle/findbugs.xslt" out="reports/report-fb.html" />
    </target>

    <target name="-init-proguard" depends="-init-dependencies">
        <taskdef resource="proguard/ant/task.properties" classpathref="analyse-classpath"/>
    </target>

    <target name="deadcode" depends="-init-proguard">
        <proguard>
            -libraryjars ${java.home}/lib/rt.jar
            -libraryjars ${java.home}/lib/jce.jar
            -libraryjars lib/main/
            -libraryjars modules/plugins/lib/main/

            -injars dist/DMDirc.jar(com/dmdirc/**,net/miginfocom/**)
            -injars modules/plugins/dist/(com/dmdirc/**)

            -dontoptimize
            -dontobfuscate
            -dontpreverify
            -printusage reports/deadcode.txt

            -dontnote !com.dmdirc.**

            <!-- The main entry point -->
            -keep public class com.dmdirc.Main {
                public static void main(java.lang.String[]);
            }

            <!-- All plugins -->
            -keep class ** implements com.dmdirc.plugins.Plugin {
                &lt;init&gt;(...);
                public void load(com.dmdirc.plugins.PluginInfo, dagger.ObjectGraph);
		public void onLoad();
		public void showConfig(com.dmdirc.config.prefs.PreferencesDialogModel);
            }

            <!-- Dagger provider methods -->
            -keepclassmembers @dagger.Module class ** {
                @dagger.Provides *;
            }

            <!-- Dagger generated subclasses -->
            -keep class **$$ModuleAdapter
            -keep class **$$InjectAdapter
            -keep class **$$StaticInjection

            <!-- Injectable constructors -->
            -keepclassmembers class * {
                @javax.inject.Inject *;
                &lt;init&gt;();
            }

            <!-- Package info classes -->
            -keep class **.package-info

            <!-- Enum stuff. -->
            -keepclassmembers enum ** {
                *** valueOf(java.lang.String);
                *** values();
            }

            <!-- Parser callbacks -->
            -keepclassmembers class ** {
                public void on*(com.dmdirc.parser.interfaces.Parser, ...);
            }

            <!-- Serializable version fields -->
            -keepclassmembers class ** implements java.io.Serializable {
                static final long serialVersionUID;
            }

            <!-- Swing dialogs -->
            -keep class com.dmdirc.addons.ui_swing.** extends com.dmdirc.addons.ui_swing.dialogs.StandardDialog {
                &lt;init&gt;(...);
            }

            <!-- Swing window implementations -->
            -keep class ** extends com.dmdirc.addons.ui_swing.components.frames.TextFrame {
                &lt;init&gt;(...);
            }

            <!-- Swing frame managers -->
            -keep class ** implements com.dmdirc.addons.ui_swing.framemanager.FrameManager {
                &lt;init&gt;(...);
            }

            <!-- DCC plugin panels -->
            -keep class com.dmdirc.addons.dcc.ui.PlaceholderPanel { &lt;init&gt;(...); }
            -keep class com.dmdirc.addons.dcc.ui.TransferPanel { &lt;init&gt;(...); }

            <!-- Exported services -->
            -keepclassmembers class ** implements com.dmdirc.plugins.Plugin {
                <!-- Parsers -->
                public com.dmdirc.parser.interfaces.Parser getParser(com.dmdirc.parser.common.MyInfo,java.net.URI);
                public com.dmdirc.parser.interfaces.ProtocolDescription getDescription();
                <!-- Tab completers -->
                public com.dmdirc.ui.input.tabstyles.TabCompletionStyle getCompletionStyle(com.dmdirc.ui.input.TabCompleter,com.dmdirc.WritableFrameContainer);
            }

            <!-- Possibly inlined constants -->
            -keepclassmembers class * {
                static final % *;
                static final java.lang.String *;
            }

            <!-- Non-DMDirc classes -->
            -keep class org.** { *; }
            -keep class net.** { *; }
            -keep class com.sun.** { *; }
            -keep class com.google.** { *; }
            -keep class com.palintir.** { *; }
            -keep class dagger.** { *; }
        </proguard>
    </target>
</project>
