Skip to content

Commit

Permalink
bug to truncate components was fixed and logs improved
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodrigo Ruiz committed May 14, 2015
1 parent dceaf6b commit 44d8df3
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@
package org.fundacionjala.gradle.plugins.enforce.interceptor

import org.fundacionjala.gradle.plugins.enforce.utils.salesforce.MetadataComponents
import groovy.util.logging.Slf4j

/**
* This class manages all components created from source path
*/
@Slf4j
class InterceptorManager {

Map<String, MetadataInterceptor> interceptors
List truncatedDirectories = ['classes', 'objects', 'triggers', 'pages', 'components', 'workflows', 'tabs']
List<String> interceptorsToExecute


/**
* Creates a new interceptor management from source path
* @param sourcePath the source directory path
Expand Down Expand Up @@ -102,7 +106,8 @@ class InterceptorManager {
* Executes the truncate method of all the component interceptors
*/
public void executeTruncate() {
this.interceptors.values().each { interceptor ->
this.interceptors.each {String component, MetadataInterceptor interceptor ->
log.debug "----------------------" + component + "----------------------"
interceptor.executeInterceptors()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
*/

package org.fundacionjala.gradle.plugins.enforce.interceptor

import groovy.util.logging.Slf4j
/**
* This class provides a skeletal implementation of the interceptor for all metadata types supported
*/
@Slf4j
abstract class MetadataInterceptor {
List<String> interceptorsToExecute
List<File> files
Expand Down Expand Up @@ -59,6 +60,7 @@ abstract class MetadataInterceptor {
if (interceptorsToExecute.contains(interceptorName) ||
(!interceptorsToExecute.contains(interceptorName) &&
interceptorName.isNumber() && interceptorName.toInteger() == interceptor.hashCode())) {
log.debug "$interceptorName --> $file.name"
executeInterceptor(interceptor, file)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ import java.util.regex.Matcher
* Implements the truncated algorithm to truncate the component content
*/
class Component {
public static final String EMPTY_COMPONENT = '<apex:component></apex:component>'
private final String COMPONENT_REGEX = /<apex:component.*>(.*([^\n]*?\n+?)*?.*)<\/apex:component>/
private final String ATTRIBUTE_REGEX = /<apex:attribute([^\n]*?\n+?)*?.*\/>/
private final String ATTRIBUTE_BY_DEFAULT = "<apex:attribute name=%s type=\"Object\" required=\"true\" description=\"Description\"/>"
private final String ATTRIBUTE_REGEX = /<apex:attribute(.|\n)*?\/>/
private final String ATTRIBUTE_BY_DEFAULT = "<apex:attribute name=%s type=\"Object\" required=\"false\" description=\"Description\"/>"
private final String NAME_ATTRIBUTE = "name"
private final int INDEX_COMPONENT = 1
private final String COMPONENT_BY_DEFAULT = "<apex:component>%s\n</apex:component>"
private final int INDEX_ATTRIBUTE = 0

/**
Expand All @@ -27,29 +25,22 @@ class Component {
return
}
String component = file.text
Matcher componentMatcher = component =~ COMPONENT_REGEX
String content = ""
String newContent = ""
String newAttributes = ""
String attribute = ""
String attributeName = ""
int indexName
int indexIniName
int indexEndName
componentMatcher.each { componentIt ->
content = componentIt[INDEX_COMPONENT]
if (content) {
Matcher attributeMatcher = component =~ ATTRIBUTE_REGEX
attributeMatcher.each { attributeIt ->
attribute = attributeIt[INDEX_ATTRIBUTE]
indexName = attribute.indexOf(NAME_ATTRIBUTE)
indexIniName = attribute.indexOf("\"", indexName)
indexEndName = attribute.indexOf("\"", indexIniName + 1)
attributeName = attribute.substring(indexIniName, indexEndName + 1)
newContent += "\n${String.format(ATTRIBUTE_BY_DEFAULT, attributeName)}"
}
component = component.replace(content, "${newContent}\n")
}
Matcher attributeMatcher = component =~ ATTRIBUTE_REGEX
attributeMatcher.each { attributeIt ->
attribute = attributeIt[INDEX_ATTRIBUTE]
indexName = attribute.indexOf(NAME_ATTRIBUTE)
indexIniName = attribute.indexOf("\"", indexName)
indexEndName = attribute.indexOf("\"", indexIniName + 1)
attributeName = attribute.substring(indexIniName, indexEndName + 1)
newAttributes += "\n${String.format(ATTRIBUTE_BY_DEFAULT, attributeName)}"
}
file.text = component

file.text = String.format(COMPONENT_BY_DEFAULT, newAttributes)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import org.fundacionjala.gradle.plugins.enforce.interceptor.commands.Class
import org.fundacionjala.gradle.plugins.enforce.interceptor.commands.ClassAnnotation
import org.fundacionjala.gradle.plugins.enforce.utils.ManagementFile
import org.fundacionjala.gradle.plugins.enforce.utils.salesforce.MetadataComponents

import groovy.util.logging.Slf4j
/**
* Implements methods to manage interceptors and load the classes to truncate
*/
@Slf4j
class ClassInterceptor extends MetadataInterceptor {
private final String DEPRECATE_ANNOTATION = '@deprecated'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import org.fundacionjala.gradle.plugins.enforce.interceptor.MetadataInterceptor
import org.fundacionjala.gradle.plugins.enforce.interceptor.commands.Component
import org.fundacionjala.gradle.plugins.enforce.utils.ManagementFile
import org.fundacionjala.gradle.plugins.enforce.utils.salesforce.MetadataComponents

import groovy.util.logging.Slf4j
/**
* Implements methods to manage interceptors and load the components to truncate
*/
@Slf4j
class ComponentInterceptor extends MetadataInterceptor {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import org.fundacionjala.gradle.plugins.enforce.interceptor.MetadataInterceptor
import org.fundacionjala.gradle.plugins.enforce.interceptor.commands.*
import org.fundacionjala.gradle.plugins.enforce.utils.ManagementFile
import org.fundacionjala.gradle.plugins.enforce.utils.salesforce.MetadataComponents

import groovy.util.logging.Slf4j
/**
* Implements methods to manage interceptors and load the objects to truncate
*/
@Slf4j
class ObjectInterceptor extends MetadataInterceptor {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import org.fundacionjala.gradle.plugins.enforce.interceptor.MetadataInterceptor
import org.fundacionjala.gradle.plugins.enforce.interceptor.commands.Page
import org.fundacionjala.gradle.plugins.enforce.utils.ManagementFile
import org.fundacionjala.gradle.plugins.enforce.utils.salesforce.MetadataComponents

import groovy.util.logging.Slf4j
/**
* Implements methods to manage interceptors and load the pages to truncate
*/
@Slf4j
class PageInterceptor extends MetadataInterceptor {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import org.fundacionjala.gradle.plugins.enforce.interceptor.MetadataInterceptor
import org.fundacionjala.gradle.plugins.enforce.interceptor.commands.Tab
import org.fundacionjala.gradle.plugins.enforce.utils.ManagementFile
import org.fundacionjala.gradle.plugins.enforce.utils.salesforce.MetadataComponents

import groovy.util.logging.Slf4j
/**
* Implements methods to manage interceptors and load the tabs to truncate
*/
@Slf4j
class TabInterceptor extends MetadataInterceptor {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import org.fundacionjala.gradle.plugins.enforce.interceptor.MetadataInterceptor
import org.fundacionjala.gradle.plugins.enforce.interceptor.commands.Trigger
import org.fundacionjala.gradle.plugins.enforce.utils.ManagementFile
import org.fundacionjala.gradle.plugins.enforce.utils.salesforce.MetadataComponents

import groovy.util.logging.Slf4j
/**
* Implements methods to manage interceptors and load the triggers to truncate
*/
@Slf4j
class TriggerInterceptor extends MetadataInterceptor {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import org.fundacionjala.gradle.plugins.enforce.interceptor.MetadataInterceptor
import org.fundacionjala.gradle.plugins.enforce.interceptor.commands.Workflow
import org.fundacionjala.gradle.plugins.enforce.utils.ManagementFile
import org.fundacionjala.gradle.plugins.enforce.utils.salesforce.MetadataComponents

import groovy.util.logging.Slf4j
/**
* Implements methods to manage interceptors and load the workflows to truncate
*/
@Slf4j
class WorkflowInterceptor extends MetadataInterceptor {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ class Deploy extends Deployment {
void runTask() {
folderDeploy = Paths.get(buildFolderPath, FOLDER_DEPLOY).toString()
packagePathDeploy = Paths.get(folderDeploy, PACKAGE_NAME).toString()
logger.debug('Creating folder Deploy at: ' + folderDeploy)
createDeploymentDirectory(folderDeploy)
logger.debug('Created folder Deploy at: ' + folderDeploy)
if (Util.isValidProperty(project, FOLDERS_DEPLOY)) {
deployByFolder()
} else {
Expand Down Expand Up @@ -98,26 +98,24 @@ class Deploy extends Deployment {
def deployTruncateFiles() {
checkStatusTruncate()
displayFolderNoDeploy()
logger.debug('Displayed message')
if (codeTruncateOn) {
ArrayList<File> filesToTruncate = excludeFiles(fileManager.getFilesByFolders(projectPath, FOLDERS_TO_TRUNCATE))
Files.copy(Paths.get(projectPath, PACKAGE_NAME), Paths.get(packagePathDeploy), StandardCopyOption.REPLACE_EXISTING)
logger.debug('Copying files to deploy')
fileManager.copy(filesToTruncate, folderDeploy)
logger.debug('All files copied to deploy...')
logger.debug('Generating package')
writePackage(packagePathDeploy, filesToTruncate)
logger.debug('Created package...')
truncateComponents()
componentDeploy.startMessage = DEPLOYING_TRUNCATED_CODE
componentDeploy.successMessage = DEPLOYING_TRUNCATED_CODE_SUCCESSFULLY
logger.debug("Deploying to truncate components from: $folderDeploy")
executeDeploy(folderDeploy)
createDeploymentDirectory(folderDeploy)
logger.debug('Truncated components...')
}
fileManager.copy(excludeFiles(fileManager.getValidElements(projectPath)), folderDeploy)
componentDeploy.startMessage = DEPLOYING_CODE
componentDeploy.successMessage = DEPLOYING_CODE_SUCCESSFULLY
deployToSalesForce()
logger.debug('Deployed components...')
}

/**
Expand Down Expand Up @@ -145,8 +143,10 @@ class Deploy extends Deployment {
if (deprecateTruncateOn) {
interceptorsToExecute = [Interceptor.REMOVE_DEPRECATE.id]
interceptorsToExecute += interceptors
logger.debug("Truncating components from: $folderDeploy")
truncateComponents(folderDeploy)
}
logger.debug("Deploying all components from: $folderDeploy")
executeDeploy(folderDeploy)
updateFileTracker()
}
Expand Down Expand Up @@ -177,7 +177,9 @@ class Deploy extends Deployment {
*/
def updateFileTracker() {
ComponentMonitor componentMonitor = new ComponentMonitor(projectPath)
logger.debug('Getting components signatures')
Map initMapSave = componentMonitor.getComponentsSignature(fileManager.getValidElements(projectPath, excludeFilesToMonitor))
logger.debug('Saving initial file tracker')
componentMonitor.componentSerializer.save(initMapSave)
}

Expand All @@ -191,6 +193,7 @@ class Deploy extends Deployment {
Interceptor.TRUNCATE_PAGES.id, Interceptor.TRUNCATE_TRIGGERS.id, Interceptor.TRUNCATE_WORKFLOWS.id,
Interceptor.TRUNCATE_COMPONENTS.id]
interceptorsToExecute += interceptors
logger.debug("Truncating components at: $srcPath")
truncateComponents(srcPath)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ abstract class Deployment extends SalesforceTask {
*/
def executeDeploy(String sourcePath) {
String fileName = new File(sourcePath).getName()
logger.debug("Crating zip file at: $buildFolderPath/$fileName")
String pathZipToDeploy = createZip(sourcePath, buildFolderPath, fileName)
componentDeploy.setPath(pathZipToDeploy)
logger.debug('Deploying components')
componentDeploy.deploy(poll, waitTime, credential)
}

Expand All @@ -64,10 +66,14 @@ abstract class Deployment extends SalesforceTask {
* @param dirToTruncate the directory path to truncate
*/
def truncateComponents(String dirToTruncate) {
logger.debug('Loading files to truncate')
componentManager.loadFiles(dirToTruncate)
logger.debug('Adding interceptors')
componentManager.addInterceptors(interceptorsToExecute)
componentManager.addInterceptorsRegistered(project.property(Constants.FORCE_EXTENSION).interceptors as Map)
logger.debug('Validating interceptors')
componentManager.validateInterceptors()
logger.debug('Executing truncate process')
componentManager.executeTruncate()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,20 @@ class ComponentTest extends Specification {
when:
componentContent.execute(file)
then:
file.text == expectedContent
file.text.replaceAll("\\s*","") == expectedContent.replaceAll("\\s*","")
}
def "Should truncate a component with attributes"() {
given:
def expectedContent = '''<!-- MyComponent -->
<apex:component controller="MyComponentController" allowDML="true">
<apex:attribute name="record" type="Object" required="true" description="Description"/>
<apex:attribute name=" myObject " type="Object" required="true" description="Description"/>
<apex:attribute name="myClass" type="Object" required="true" description="Description"/>
def expectedContent = '''<apex:component>
<apex:attribute name="record" type="Object" required="false" description="Description"/>
<apex:attribute name=" myObject " type="Object" required="false" description="Description"/>
<apex:attribute name="myClass" type="Object" required="false" description="Description"/>
</apex:component>'''
File file = new File("${TRUNCATED_PATH}/NewComponentContent.component")
when:
componentContent.execute(file)
then:
true
file.text.replaceAll("\\s*","") == expectedContent.replaceAll("\\s*","")
}
def cleanupSpec() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<apex:component >
<!-- Begin Default Content REMOVE THIS -->
<h1>Congratulations</h1>
This is your new Component
<!-- End Default Content REMOVE THIS -->
</apex:component>

0 comments on commit 44d8df3

Please sign in to comment.