<commit_before>/*
 * Minecraft Dev for IntelliJ
 *
 * https://minecraftdev.org
 *
 * Copyright (c) 2017 minecraft-dev
 *
 * MIT License
 */

package com.demonwav.mcdev.platform

import com.demonwav.mcdev.util.MinecraftFileTemplateGroupFactory
import com.intellij.codeInsight.actions.ReformatCodeProcessor
import com.intellij.ide.fileTemplates.FileTemplateManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VfsUtil
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiManager
import java.io.IOException
import java.util.Locale
import java.util.Properties

object BaseTemplate {

    fun applyBuildGradleTemplate(project: Project,
                                 file: VirtualFile,
                                 groupId: String,
                                 artifactId: String,
                                 pluginVersion: String,
                                 buildVersion: String): String? {

        val buildGradleProps = Properties()
        buildGradleProps.setProperty("BUILD_VERSION", buildVersion)

        val manager = FileTemplateManager.getInstance(project)
        val template = manager.getJ2eeTemplate(MinecraftFileTemplateGroupFactory.BUILD_GRADLE_TEMPLATE)

        // This method should be never used for Sponge projects so we just pass false
        applyGradlePropertiesTemplate(project, file, groupId, artifactId, pluginVersion, false)

        try {
            return template.getText(buildGradleProps)
        } catch (e: IOException) {
            e.printStackTrace()
        }

        return null
    }

    fun applyMultiModuleBuildGradleTemplate(project: Project,
                                            file: VirtualFile,
                                            prop: VirtualFile,
                                            groupId: String,
                                            artifactId: String,
                                            pluginVersion: String,
                                            buildVersion: String,
                                            hasSponge: Boolean) {

        val properties = Properties()
        properties.setProperty("BUILD_VERSION", buildVersion)

        applyGradlePropertiesTemplate(project, prop, groupId, artifactId, pluginVersion, hasSponge)

        try {
            applyTemplate(project, file, MinecraftFileTemplateGroupFactory.MULTI_MODULE_BUILD_GRADLE_TEMPLATE, properties)
        } catch (e: IOException) {
            e.printStackTrace()
        }

    }

    fun applyGradlePropertiesTemplate(project: Project,
                                      file: VirtualFile,
                                      groupId: String,
                                      artifactId: String,
                                      pluginVersion: String,
                                      hasSponge: Boolean) {

        val gradleProps = Properties()

        gradleProps.setProperty("GROUP_ID", groupId)
        gradleProps.setProperty("PLUGIN_VERSION", pluginVersion)

        if (hasSponge) {
            gradleProps.setProperty("PLUGIN_ID", artifactId.toLowerCase(Locale.ENGLISH))
        }

        // create gradle.properties
        try {
            applyTemplate(project, file, MinecraftFileTemplateGroupFactory.GRADLE_PROPERTIES_TEMPLATE, gradleProps)
        } catch (e: IOException) {
            e.printStackTrace()
        }

    }

    fun applySettingsGradleTemplate(project: Project,
                                    file: VirtualFile,
                                    projectName: String,
                                    includes: String) {

        val properties = Properties()
        properties.setProperty("PROJECT_NAME", projectName)
        properties.setProperty("INCLUDES", includes)

        try {
            applyTemplate(project, file, MinecraftFileTemplateGroupFactory.SETTINGS_GRADLE_TEMPLATE, properties)
        } catch (e: IOException) {
            e.printStackTrace()
        }

    }

    fun applySubmoduleBuildGradleTemplate(project: Project,
                                          commonProjectName: String): String? {

        val properties = Properties()
        properties.setProperty("COMMON_PROJECT_NAME", commonProjectName)

        val manager = FileTemplateManager.getInstance(project)
        val template = manager.getJ2eeTemplate(MinecraftFileTemplateGroupFactory.SUBMODULE_BUILD_GRADLE_TEMPLATE)

        try {
            return template.getText(properties)
        } catch (e: IOException) {
            e.printStackTrace()
        }

        return null
    }

    fun applyTemplate(project: Project, file: VirtualFile, templateName: String, properties: Properties, trimNewlines: Boolean = false) {

        val manager = FileTemplateManager.getInstance(project)
        val template = manager.getJ2eeTemplate(templateName)

        val allProperties = manager.defaultProperties
        allProperties.putAll(properties)<commit_msg>Remove bogus try catches<commit_after>/*
 * Minecraft Dev for IntelliJ
 *
 * https://minecraftdev.org
 *
 * Copyright (c) 2017 minecraft-dev
 *
 * MIT License
 */

package com.demonwav.mcdev.platform

import com.demonwav.mcdev.util.MinecraftFileTemplateGroupFactory
import com.intellij.codeInsight.actions.ReformatCodeProcessor
import com.intellij.ide.fileTemplates.FileTemplateManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VfsUtil
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiManager
import java.util.Locale
import java.util.Properties

object BaseTemplate {

    fun applyBuildGradleTemplate(project: Project,
                                 file: VirtualFile,
                                 groupId: String,
                                 artifactId: String,
                                 pluginVersion: String,
                                 buildVersion: String): String? {

        val buildGradleProps = Properties()
        buildGradleProps.setProperty("BUILD_VERSION", buildVersion)

        val manager = FileTemplateManager.getInstance(project)
        val template = manager.getJ2eeTemplate(MinecraftFileTemplateGroupFactory.BUILD_GRADLE_TEMPLATE)

        // This method should be never used for Sponge projects so we just pass false
        applyGradlePropertiesTemplate(project, file, groupId, artifactId, pluginVersion, false)

        return template.getText(buildGradleProps)
    }

    fun applyMultiModuleBuildGradleTemplate(project: Project,
                                            file: VirtualFile,
                                            prop: VirtualFile,
                                            groupId: String,
                                            artifactId: String,
                                            pluginVersion: String,
                                            buildVersion: String,
                                            hasSponge: Boolean) {

        val properties = Properties()
        properties.setProperty("BUILD_VERSION", buildVersion)

        applyGradlePropertiesTemplate(project, prop, groupId, artifactId, pluginVersion, hasSponge)

        applyTemplate(project, file, MinecraftFileTemplateGroupFactory.MULTI_MODULE_BUILD_GRADLE_TEMPLATE, properties)
    }

    fun applyGradlePropertiesTemplate(project: Project,
                                      file: VirtualFile,
                                      groupId: String,
                                      artifactId: String,
                                      pluginVersion: String,
                                      hasSponge: Boolean) {

        val gradleProps = Properties()

        gradleProps.setProperty("GROUP_ID", groupId)
        gradleProps.setProperty("PLUGIN_VERSION", pluginVersion)

        if (hasSponge) {
            gradleProps.setProperty("PLUGIN_ID", artifactId.toLowerCase(Locale.ENGLISH))
        }

        // create gradle.properties
        applyTemplate(project, file, MinecraftFileTemplateGroupFactory.GRADLE_PROPERTIES_TEMPLATE, gradleProps)
    }

    fun applySettingsGradleTemplate(project: Project,
                                    file: VirtualFile,
                                    projectName: String,
                                    includes: String) {

        val properties = Properties()
        properties.setProperty("PROJECT_NAME", projectName)
        properties.setProperty("INCLUDES", includes)

        applyTemplate(project, file, MinecraftFileTemplateGroupFactory.SETTINGS_GRADLE_TEMPLATE, properties)
    }

    fun applySubmoduleBuildGradleTemplate(project: Project,
                                          commonProjectName: String): String? {

        val properties = Properties()
        properties.setProperty("COMMON_PROJECT_NAME", commonProjectName)

        val manager = FileTemplateManager.getInstance(project)
        val template = manager.getJ2eeTemplate(MinecraftFileTemplateGroupFactory.SUBMODULE_BUILD_GRADLE_TEMPLATE)

        return template.getText(properties)
    }

    fun applyTemplate(project: Project, file: VirtualFile, templateName: String, properties: Properties, trimNewlines: Boolean = false) {

        val manager = FileTemplateManager.getInstance(project)
        val template = manager.getJ2eeTemplate(templateName)

        val allProperties = manager.defaultProperties
        allProperties.putAll(properties)
{'fraction_non_alphanumeric': 0.0661420672054445, 'fraction_numerical': 0.0014887282007656317, 'mean_word_length': 1.9326473339569692, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 58, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6539039', 'n_tokens_mistral': 2233, 'n_tokens_neox': 2125, 'n_words': 441}