<commit_before>package org.chtijbug.drools.platform.runtime.builder;

import com.google.common.base.Throwables;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;

import static org.apache.commons.io.FileUtils.copyFileToDirectory;
import static org.apache.commons.io.FileUtils.write;

public class MavenProject {
    private static final String JAVA_EXT = ".java";
    private static Logger logger = LoggerFactory.getLogger(MavenProject.class);
    protected final File pomFile;
    protected final File srcFolder;
    protected final File resourcesFolder;
    protected final File webappFolder;
    protected final File webinfFolder;
    protected final File wsdlFolder;

    public MavenProject(File projectFolder, String pomFileContent) {
        this.srcFolder = addSubFolder(projectFolder, "src/main/java");
        this.resourcesFolder = addSubFolder(projectFolder, "src/main/resources");
        this.wsdlFolder = addSubFolder(resourcesFolder, "wsdl");
        this.webappFolder = addSubFolder(projectFolder, "src/main/webapp");
        this.webinfFolder = addSubFolder(webappFolder, "WEB-INF");
        this.pomFile = new File(projectFolder, "pom.xml");
        try {
            if (!pomFile.exists() && !pomFile.createNewFile())
                throw new RuntimeException("Unable to create temporary directory. Execution will stop");
            write(pomFile, pomFileContent);
        } catch (IOException e) {
            logger.error("Exception thrown while copying content into the destination file pom.xml");
            throw Throwables.propagate(e);
        }
    }

    public void generateSources() {
        try {
            ProcessBuilder builder = new ProcessBuilder("mvn", "-f", this.pomFile.getAbsolutePath(), "generate-sources");
            Process process = builder.start();
            int exitStatus = process.waitFor();
            if (exitStatus != 0)
                throw new RuntimeException("Error while running mvn generate-sources command");
        } catch (IOException e) {
            logger.error("Error while running mvn clean command");
            throw Throwables.propagate(e);
        } catch (InterruptedException e) {
            throw Throwables.propagate(e);
        }
    }

    public InputStream buildUpPackage() {
        try {
            ProcessBuilder builder = new ProcessBuilder("mvn", "-f", this.pomFile.getAbsolutePath(), "package");
            Process process = builder.start();
            int exitStatus = process.waitFor();
            if (exitStatus != 0)
                throw new RuntimeException("Error while running mvn package command");
            //___ TODO Look up for the generated war file and open an input stream on it.
            return null; // TODO
        } catch (IOException e) {
            logger.error("Error while running mvn clean command");
            throw Throwables.propagate(e);
        } catch (InterruptedException e) {
            throw Throwables.propagate(e);
        }
    }

    private File addSubFolder(File parentFolder, String folderName) {
        File subFolder = new File(parentFolder, folderName);
        if (!subFolder.mkdirs())
            throw new RuntimeException("Unable to create temporary directory. Execution will stop");
        return subFolder;
    }

    public void addWSDL(String filename, byte[] wsdlContentAsByteArray) {
        createNewFile(wsdlFolder, filename, new ByteArrayInputStream(wsdlContentAsByteArray));
    }

    public void addXSD(String modelFilename, InputStream businessModelAsXsd) {<commit_msg>fixing the distribution url<commit_after>package org.chtijbug.drools.platform.runtime.builder;

import com.google.common.base.Throwables;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.Collection;

import static org.apache.commons.io.FileUtils.copyFileToDirectory;
import static org.apache.commons.io.FileUtils.write;

public class MavenProject {
    private static final String JAVA_EXT = ".java";
    private static Logger logger = LoggerFactory.getLogger(MavenProject.class);
    protected final File pomFile;
    protected final File srcFolder;
    protected final File resourcesFolder;
    protected final File webappFolder;
    protected final File webinfFolder;
    protected final File wsdlFolder;

    public MavenProject(File projectFolder, String pomFileContent) {
        this.srcFolder = addSubFolder(projectFolder, "src/main/java");
        this.resourcesFolder = addSubFolder(projectFolder, "src/main/resources");
        this.wsdlFolder = addSubFolder(resourcesFolder, "wsdl");
        this.webappFolder = addSubFolder(projectFolder, "src/main/webapp");
        this.webinfFolder = addSubFolder(webappFolder, "WEB-INF");
        this.pomFile = new File(projectFolder, "pom.xml");
        try {
            if (!pomFile.exists() && !pomFile.createNewFile())
                throw new RuntimeException("Unable to create temporary directory. Execution will stop");
            write(pomFile, pomFileContent);
        } catch (IOException e) {
            logger.error("Exception thrown while copying content into the destination file pom.xml");
            throw Throwables.propagate(e);
        }
    }

    public void generateSources() {
        launchMavenCommand("generate-sources");
    }

    public InputStream buildUpPackage() {
        launchMavenCommand("package");
        //___ TODO Look up for the generated war file and open an input stream on it.
        return null; // TODO

    }

    private void launchMavenCommand(String lifeCycle) {
        try {
            ProcessBuilder builder = new ProcessBuilder("mvn", "-f", this.pomFile.getAbsolutePath(), lifeCycle);
            Process process = builder.start();
            BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));

            String line;
            while ((line = br.readLine()) != null) {
                logger.debug(line);
            }
            int exitStatus = process.waitFor();
            if (exitStatus != 0)
                throw new RuntimeException("Error while running mvn "+lifeCycle+" command");
        } catch (IOException | InterruptedException e) {
            logger.error("Error while running mvn clean command");
            throw Throwables.propagate(e);
        }
    }

    private File addSubFolder(File parentFolder, String folderName) {
        File subFolder = new File(parentFolder, folderName);
        if (!subFolder.mkdirs())
            throw new RuntimeException("Unable to create temporary directory. Execution will stop");
        return subFolder;
    }

    public void addWSDL(String filename, byte[] wsdlContentAsByteArray) {
        createNewFile(wsdlFolder, filename, new ByteArrayInputStream(wsdlContentAsByteArray));
    }

    public void addXSD(String modelFilename, InputStream businessModelAsXsd) {
{'fraction_non_alphanumeric': 0.0933277027027027, 'fraction_numerical': 0.0009853603603603603, 'mean_word_length': 2.971492453884852, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '988520', 'n_tokens_mistral': 1878, 'n_tokens_neox': 1820, 'n_words': 502}