Skip to content

Commit

Permalink
Merge pull request #12 from etendosoftware/feature/EML-377
Browse files Browse the repository at this point in the history
Feature EML-377: Create a Gradle configuration to manage copilot projects
  • Loading branch information
RubenEtendo authored Oct 10, 2023
2 parents 4e660ea + 760f859 commit efbbbf4
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/main/groovy/com/etendoerp/copilot/Constants.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ class Constants {
static final String OPENAI_API_KEY_PROPERTY = "openaiAPIKey"
static final String BASTIAN_URL_PROPERTY = "bastianPort"
static final String COPILOT_DOCKER_REPO = "etendo_copilot_core"
static final String TOOLS_CONFIG_FILE = "tools_config.json"
}
2 changes: 2 additions & 0 deletions src/main/groovy/com/etendoerp/copilot/CopilotLoader.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.etendoerp.copilot

import com.etendoerp.copilot.configuration.CopilotConfigurationLoader
import com.etendoerp.copilot.dockerenv.CopilotEnvironmentVerification
import com.etendoerp.copilot.dockerenv.CopilotStart
import com.etendoerp.copilot.dockerenv.CopilotStop
Expand All @@ -8,6 +9,7 @@ import org.gradle.api.Project

class CopilotLoader {
static void load(Project project) {
CopilotConfigurationLoader.load(project)
CopilotEnvironmentVerification.load(project)
CopilotStart.load(project)
CopilotStop.load(project)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package com.etendoerp.copilot.configuration

import com.etendoerp.copilot.Constants
import com.etendoerp.publication.PublicationUtils
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import org.gradle.api.Project

class CopilotConfigurationLoader {

/**
* This class makes al the necessary copilot configurations at the start of a build.
*
* The initialization of any gradle task will trigger the following:
* <ul>
* <li> Generate a <i>build/copilot</i> directory, in which the following will be placed:
* <ul>
* <li> All of the <i>com.etendoerp.copilot</i> module's files
* <li> Files from any module related to copilot - this is, one of the copilot tools -. They will be placed in <i>build/copilot/tools</i>
* </ul>
* <li> Modify the copilot module's `tools_config.json`, adding dinamically the tools, like so:
* <pre>
{
"native_tools": {
"BastianFetcher": true,
},
"third_party_tools": {
"HelloWorldTool": true,
"XMLTranslatorTool": true,
...
}
}
* </pre>
* </ul>
* @param project
*/
static void load(Project project) {
project.afterEvaluate {
File copilotDir = new File(project.buildDir.path, "copilot")
copilotDir.deleteDir()

Project moduleProject = project.findProject(":${PublicationUtils.BASE_MODULE_DIR}")
if (moduleProject != null) {
Project copilotProject = moduleProject.findProject(Constants.COPILOT_MODULE)
File jarModulesLocation = new File(project.buildDir, "etendo" + File.separator + Constants.MODULES_PROJECT)
File copilotJarModule = new File(jarModulesLocation, Constants.COPILOT_MODULE)

boolean copilotExists = false
if (copilotProject != null) { // Copilot found in SOURCES
copilotExists = true
project.copy {
from {
copilotProject.projectDir.path
}
into "${project.buildDir.path}${File.separator}copilot"
includeEmptyDirs false
}
} else if (copilotJarModule.exists()) { // Copilot found in JARS
copilotExists = true
project.copy {
from {
copilotJarModule.path
}
into "${project.buildDir.path}${File.separator}copilot"
includeEmptyDirs false
}
}

if (copilotExists) {
File toolsConfigFile = new File(project.buildDir, "copilot" + File.separator + Constants.TOOLS_CONFIG_FILE)
def toolsConfigJson = new JsonSlurper().parseText(toolsConfigFile.readLines().join(" "))

// Get tools in SOURCES
moduleProject.subprojects.each { subproject ->
File toolsDir = new File(subproject.projectDir, "tools")
if (toolsDir.exists() && !subproject.name.equals(Constants.COPILOT_MODULE)) {
project.copy {
from {
toolsDir.path
}
into "${project.buildDir.path}${File.separator}copilot${File.separator}tools"
}
toolsDir.listFiles().each { file ->
toolsConfigJson.third_party_tools[file.name.replaceFirst(~/\.[^\.]+$/, '')] = true
}
def json_data = JsonOutput.toJson(toolsConfigJson)
toolsConfigFile.write(JsonOutput.prettyPrint(json_data))
}
}

// Get tools in JARS
jarModulesLocation.listFiles().each { jarModule ->
File jarModuleToolsDir = new File(jarModule, "tools")
if (jarModuleToolsDir.exists() && !jarModule.name.equals(Constants.COPILOT_MODULE)) {
project.copy {
from {
jarModuleToolsDir.path
}
into "${project.buildDir.path}${File.separator}copilot${File.separator}tools"
}
jarModuleToolsDir.listFiles().each { file ->
toolsConfigJson.third_party_tools[file.name.replaceFirst(~/\.[^\.]+$/, '')] = true
}
def json_data = JsonOutput.toJson(toolsConfigJson)
toolsConfigFile.write(JsonOutput.prettyPrint(json_data))
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,22 @@ class CopilotEnvironmentVerification {

static void verifyModuleIsInstalled(Project project) {
String errorMsg = "${COPILOT_MODULE_ABSENT} \n"

// Check sources for copilot
Project modules = project.findProject(Constants.MODULES_PROJECT)
Project copilot = modules.findProject(Constants.COPILOT_MODULE)
boolean copilotInSrc = modules.findProject(Constants.COPILOT_MODULE) != null

// Check jars for copilot
FileFilter fileFilter = new FileFilter() {
@Override
boolean accept(File file) {
return file.name == Constants.COPILOT_MODULE
}
}
File jarsDir = new File(project.buildDir.path, "etendo" + File.separator + Constants.MODULES_PROJECT)
boolean copilotInJars = jarsDir.listFiles(fileFilter)?.size() > 0

if (!copilot)
if (!copilotInSrc && !copilotInJars)
throw new CopilotEnvironmentConfException(errorMsg)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ class CopilotStart {
if (bastianUrl)
dockerEnvVars += ' -e BASTIAN_URL=' + "\"${bastianUrl}\""
String dockerCommand = dockerEnvVars + ' -p ' + "${copilotPort}" + ':' + "${copilotPort}" +
' -v ' + "\$(pwd)/modules/${Constants.COPILOT_MODULE}/:/app/ " +
'-v ' + "\$(pwd)/modules:/modules/ etendo/${Constants.COPILOT_DOCKER_REPO}:develop"
' -v ' + "${project.buildDir.path}/copilot/:/app/ " +
'-v ' + "\$(pwd)/modules:/modules/ etendo/${Constants.COPILOT_DOCKER_REPO}:develop"

project.exec {
workingDir '.'
commandLine 'sh', '-c', 'docker pull etendo/' + "${Constants.COPILOT_DOCKER_REPO}:develop"
}
project.exec {
workingDir '.'
commandLine 'sh', '-c', dockerCommand
}
}
Expand Down

0 comments on commit efbbbf4

Please sign in to comment.