<commit_before>/*
 * Copyright 2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gradle.build

import org.gradle.api.DefaultTask
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.TaskAction
import org.gradle.api.file.FileCollection
import com.tonicsystems.jarjar.JarJarTask as JarJarAntTask
import com.tonicsystems.jarjar.Rule;
import com.tonicsystems.jarjar.Keep;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.ZipFileSet

class JarJar extends DefaultTask {
    @InputFiles FileCollection inputFiles
    @OutputFile outputFile

    def rules = [] //todo: mark as input property
    def keeps = [] //todo: mark as input property

    @TaskAction
    void nativeJarJar() {
        JarJarAntTask jjTask = new JarJarAntTask();
        jjTask.setProject(new Project());
        jjTask.setDestFile(new File("jaroutput.jar"));

        addInputLibs(jjTask)

        rules.each { rule ->
            jjTask.addConfiguredRule(rule);
        }
        rules.each { keep ->
            jjTask.addConfiguredRule(keep);
        }
        jjTask.execute();
    }

    private void addInputLibs(JarJarAntTask jjTask) {
        inputFiles.each { inputFile ->
            ZipFileSet zipFileSet = new ZipFileSet();
            zipFileSet.setSrc(inputFile);
            jjTask.addZipfileset(zipFileSet);
        }
    }

    void addRule(String pattern, String result) {
        rules.add(new Rule(pattern: pattern, result: result))
    }

    void keep(String pattern) {
        this.keeps << new Keep(pattern: pattern)
    }
}<commit_msg>GRADLE-2238: refactor jarjar task draft to use the Main method instead of using api directly. - needed because of https://code.google.com/p/jarjar/issues/detail?id=54&sort=-id - todo: current implementation does not yet handle multiple input jars. we need our own ueberjar mechanism here since Main does not support multiple input jars atm<commit_after>/*
 * Copyright 2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.build

import com.tonicsystems.jarjar.Main as JarJarMain

import com.tonicsystems.jarjar.Keep
import com.tonicsystems.jarjar.Rule
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.file.FileCollection
import org.gradle.api.internal.file.TemporaryFileProvider
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction

class JarJar extends DefaultTask {
    @InputFiles FileCollection inputFiles
    @OutputFile File outputFile

    def patterns = [] //todo mark these patterns as task input

    @TaskAction
    void nativeJarJar() {
        try {
            File tempRuleFile = createRuleFile()
            JarJarMain.main("process", tempRuleFile .absolutePath, inputFiles.getSingleFile().absolutePath, outputFile.absolutePath)
        } catch (IOException e) {
            throw new GradleException("unable to run jarjar task", e);
        }
    }

    private File createRuleFile() {
        TemporaryFileProvider tmpFileProvider = getServices().get(TemporaryFileProvider);
        File tempRuleFile = tmpFileProvider.createTemporaryFile("jarjar", "jar")
        tempRuleFile.withPrintWriter { writer ->
            patterns.findAll {it instanceof Rule}.each {rule ->
                writer.println("rule ${rule.pattern} ${rule.result}")
            }
        }

        return tempRuleFile;
    }

    void rule(String pattern, String result) {
        patterns << new Rule(pattern: pattern, result: result)
    }

    void keep(String pattern) {
        patterns << new Keep(pattern: pattern)
    }

}
{'fraction_non_alphanumeric': 0.08608020155364267, 'fraction_numerical': 0.004618937644341801, 'mean_word_length': 3.1973568281938327, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 8, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, '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': '1367670', 'n_tokens_mistral': 1349, 'n_tokens_neox': 1295, 'n_words': 417}