Skip to content

Commit

Permalink
Merge pull request #63 from fundacionjala/develop
Browse files Browse the repository at this point in the history
new build version 1.0.2
  • Loading branch information
marcocdlv committed May 16, 2015
2 parents ab250ee + 078d357 commit 8d19237
Show file tree
Hide file tree
Showing 101 changed files with 2,265 additions and 918 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ repo/
.gradle/
.sass-cache/
_site/

*.fileTracker.data
*file.data
src/test/groovy/org/fundacionjala/gradle/plugins/enforce/tasks/credentialmanager/resources/secretKeyGenerated.text
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@

package org.fundacionjala.gradle.plugins.enforce

import org.fundacionjala.gradle.plugins.enforce.tasks.credentialmanager.CredentialAdder
import org.fundacionjala.gradle.plugins.enforce.tasks.credentialmanager.CredentialUpdater
import org.fundacionjala.gradle.plugins.enforce.tasks.filemonitor.FilesStatus
import org.fundacionjala.gradle.plugins.enforce.tasks.filemonitor.Reset
import org.fundacionjala.gradle.plugins.enforce.tasks.salesforce.deployment.Deploy
import org.fundacionjala.gradle.plugins.enforce.tasks.salesforce.deployment.Undeploy
import org.fundacionjala.gradle.plugins.enforce.tasks.salesforce.deployment.Update
import org.fundacionjala.gradle.plugins.enforce.tasks.salesforce.deployment.Upload
import org.fundacionjala.gradle.plugins.enforce.tasks.salesforce.execute.ApexExecutor
import org.fundacionjala.gradle.plugins.enforce.tasks.salesforce.managepackage.InstallPackageTask
import org.fundacionjala.gradle.plugins.enforce.tasks.salesforce.managepackage.UninstallPackageTask
import org.fundacionjala.gradle.plugins.enforce.tasks.salesforce.retrieve.Retrieve
import org.fundacionjala.gradle.plugins.enforce.tasks.salesforce.unittest.RunTestTask
import org.fundacionjala.gradle.plugins.enforce.wsc.Credential
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.fundacionjala.gradle.plugins.enforce.tasks.credentialmanager.CredentialAdder
import org.fundacionjala.gradle.plugins.enforce.tasks.credentialmanager.CredentialUpdater
import org.fundacionjala.gradle.plugins.enforce.tasks.filemonitor.FilesStatus
import org.fundacionjala.gradle.plugins.enforce.tasks.salesforce.deployment.Deploy
import org.fundacionjala.gradle.plugins.enforce.tasks.salesforce.deployment.Undeploy
import org.fundacionjala.gradle.plugins.enforce.tasks.salesforce.managepackage.InstallPackageTask
import org.fundacionjala.gradle.plugins.enforce.tasks.salesforce.managepackage.UninstallPackageTask

/**
* This class creates a Force Plugin that represents an extension to Gradle.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
package org.fundacionjala.gradle.plugins.enforce.credentialmanagement

import org.fundacionjala.gradle.plugins.enforce.exceptions.CredentialException
import org.fundacionjala.gradle.plugins.enforce.wsc.Credential
import org.fundacionjala.gradle.plugins.enforce.utils.CharsetUtil
import org.fundacionjala.gradle.plugins.enforce.wsc.Credential
import sun.misc.BASE64Decoder

import javax.crypto.Cipher
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

package org.fundacionjala.gradle.plugins.enforce.credentialmanagement

import org.fundacionjala.gradle.plugins.enforce.exceptions.CredentialException
import org.fundacionjala.gradle.plugins.enforce.wsc.Credential
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
import groovy.json.internal.LazyMap
import org.fundacionjala.gradle.plugins.enforce.exceptions.CredentialException
import org.fundacionjala.gradle.plugins.enforce.wsc.Credential

/**
* Reads and writes credentials in credentials file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

package org.fundacionjala.gradle.plugins.enforce.credentialmanagement

import org.fundacionjala.gradle.plugins.enforce.exceptions.CredentialException
import groovy.json.JsonSlurper
import groovy.json.internal.LazyMap
import org.fundacionjala.gradle.plugins.enforce.exceptions.CredentialException
import org.fundacionjala.gradle.plugins.enforce.utils.AnsiColor
import org.fundacionjala.gradle.plugins.enforce.utils.Util
import org.fundacionjala.gradle.plugins.enforce.wsc.Credential
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.fundacionjala.gradle.plugins.enforce.filemonitor

public interface ComponentComparable<T> {
ResultTracker compare(T componentTracker)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.fundacionjala.gradle.plugins.enforce.filemonitor

public class ComponentHash implements ComponentComparable<ComponentHash> {
String fileName
String hash

/**
* Sets component name and component hash
* @param fileName is a component name
* @param hash is a hash of a component
*/
public ComponentHash(String fileName, String hash) {
this.fileName = fileName
this.hash = hash
}

/**
* Initializes component name and component hash
*/
public ComponentHash() {
this.hash = ""
this.fileName = ""
}

/**
* Compares components status
* @param componentHash is the hash of components
* @return an resultTracker with status
*/
public ResultTracker compare(ComponentHash componentHash) {
ResultTracker resultTracker = new ResultTracker()
resultTracker.state = ComponentStates.NOT_CHANGED
if (componentHash.hash != hash) {
resultTracker.state = ComponentStates.CHANGED
}
return resultTracker
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package org.fundacionjala.gradle.plugins.enforce.filemonitor

import com.twmacinta.util.MD5
import org.fundacionjala.gradle.plugins.enforce.utils.Constants
import org.fundacionjala.gradle.plugins.enforce.utils.Util

import java.nio.file.Paths

class ComponentMonitor {
private final String FILE_TRACKING = '.fileTracker.data'
public Map<String, ComponentHash> currentFileHashCode
public Map<String, ComponentHash> recoveryFileHashCode
public String srcProject
public String fileName
ComponentSerializer componentSerializer

/**
* Sets a source project directory
* Builds a .fileTracker.data path
* Creates and instance of ComponentSerializer class
* Initializes currentHashCode map and recoveryFileHashCode map
* @param srcProject is project directory
*/
public ComponentMonitor(String srcProject) {
this.srcProject = srcProject
this.fileName = Paths.get(srcProject, FILE_TRACKING).toString()
this.componentSerializer = new ComponentSerializer(fileName)
currentFileHashCode = [:]
recoveryFileHashCode = [:]
}

/**
* Initializes currentHashCode map and recoveryFileHashCode map
*/
public ComponentMonitor() {
currentFileHashCode = [:]
recoveryFileHashCode = [:]
}

/**
* Gets a map with components that were changed
* @param arrayFiles are components of the project directory
* @return a map of components with their status changed, added or deleted
*/
public Map<String, ResultTracker> getComponentChanged(ArrayList<File> arrayFiles) throws Exception {
recoveryFileHashCode = componentSerializer.read()
currentFileHashCode = getComponentsSignature(arrayFiles)

return getFilesChanged(recoveryFileHashCode, currentFileHashCode)
}

/**
* Gets a map with components that were changed
* @param oldFiles is a map of components with their old componentHash
* @param currentFiles is a map of components with their current componentHash
* @return a map of components with their status as changed, added or deleted
*/
public Map<String, ResultTracker> getFilesChanged(Map<String, ComponentHash> oldFiles, Map<String, ComponentHash> currentFiles) {
Map<String, ResultTracker> result = [:]
currentFiles.each { String relativePath, ComponentHash currentComponentHash ->
ResultTracker resultTracker
if (!oldFiles.containsKey(relativePath)) {
resultTracker = new ResultTracker(ComponentStates.ADDED)
result.put(relativePath, resultTracker)
}

if (oldFiles.containsKey(relativePath) && currentFiles.containsKey(relativePath)) {
ComponentHash oldComponentHash = oldFiles.get(relativePath)
resultTracker = oldComponentHash.compare(currentComponentHash)
if(resultTracker.state == ComponentStates.CHANGED) {
result.put(relativePath, resultTracker)
}
}
}

oldFiles.each { String relativePath, ComponentHash oldComponentHash->
if(!currentFiles.containsKey(relativePath)) {
ResultTracker resultTracker = new ResultTracker(ComponentStates.DELETED)
result.put(relativePath, resultTracker)
}
}
return result
}

/**
* Gets components with their componentHash
* @param files is array files
* @return a map of components with their name and its componentHash
*/
public Map<String, ComponentHash> getComponentsSignature(ArrayList<File> files) {
Map<String, ComponentHash> result = [:]
files.each {File file->
ComponentHash componentHash
String signature = MD5.asHex(MD5.getHash(file))
String relativePath = getPathRelative(file)
if (Util.getFileExtension(file) == Constants.OBJECT_EXTENSION) {
ObjectParser objectParser = new ObjectParser()
componentHash = new ObjectHash(relativePath, signature, objectParser.parseByObjectXML(file))
} else {
componentHash = new ComponentHash(relativePath, signature)
}
result.put(relativePath, componentHash)
}
return result
}

/**
* Gets a path relative of the file
* @param file is the file that is tracked
* @return is a path relative
*/
public String getPathRelative(File file) {
String nameFile = file.getName()
String folderFile = file.getParentFile().getName()
Paths.get(srcProject, folderFile, nameFile).toString()
}

public void setSrcProject(String srcProject) {
fileName = Paths.get(srcProject, FILE_TRACKING).toString()
this.srcProject = srcProject
}

/**
* Verify if file directory exists
* @return boolean result of file(pathFile).exists
*/
public boolean verifyFileMap() {
return new File(fileName).exists()
}

/**
* Start file monitor
* @param arrayFiles create a file tracking according array files
*/
public void saveCurrentComponents(ArrayList<File> arrayFiles) {
currentFileHashCode = getComponentsSignature(arrayFiles)
componentSerializer.save(currentFileHashCode)
}

/**
* Update changed value hasCode file
* @throws IOException if no cant write in the disk
*/
public Map getFoldersFiltered(ArrayList<String> folders, Map<String, ResultTracker> mapFilesChanged) {
Map foldersFiltered = [:]

mapFilesChanged.each { fileName, resultTracker ->

String parentFile = new File(fileName).getParentFile().getName()
folders.each { nameFolder ->

if (parentFile == nameFolder) {
foldersFiltered.put(fileName, resultTracker)
}
}
}

return foldersFiltered
}

/**
* Save only elements changed in the map
* @param mapFileChanged is the files changed
*/
public void saveMapUpdated(Map<String, ResultTracker> mapFileChanged) {
mapFileChanged.each { String fileName, ResultTracker resultTracker ->
if (resultTracker.state == ComponentStates.DELETED) {
recoveryFileHashCode.remove(fileName)
}
else {
recoveryFileHashCode.put(fileName, currentFileHashCode.get(fileName))
}
}
componentSerializer.save(recoveryFileHashCode)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package org.fundacionjala.gradle.plugins.enforce.filemonitor

import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
import groovy.json.StringEscapeUtils
import groovy.json.internal.LazyMap
import org.fundacionjala.gradle.plugins.enforce.utils.Util

public class ComponentSerializer implements Serializable{
public String sourcePath
private final String BACK_SLASH = "\\\\"
private final String DOUBLE_BACK_SLASH = "\\\\\\\\"
private final String FILE_NAME_LABEL = 'fileName'
private final String HASH_LABEL = 'hash'
private final String SUB_COMPONENT_LABEL = 'subComponents'

public ComponentSerializer(String sourcePath) {
this.sourcePath = sourcePath
}

/**
* Saves a map into .fileTracker.data file
* @param components is map with filName and its componentHash
*/
void save(Map<String, ComponentHash> components) throws IOException {
File fileTracker = new File(sourcePath)
JsonBuilder jsonBuilder = new JsonBuilder()
jsonBuilder(components)
String content = jsonBuilder.toString().replaceAll(DOUBLE_BACK_SLASH, BACK_SLASH)
content = content.replaceAll(BACK_SLASH, DOUBLE_BACK_SLASH)
fileTracker.text = content
}

/**
* Reads a .fileTracker.data file from source project
* @return a map with .fileTracker.data file values
*/
public Map<String, ComponentHash> read() throws IOException {
Map<String, ComponentHash> result = [:]
try {
JsonSlurper jsonSlurper = new JsonSlurper()
jsonSlurper.parseText(new File(sourcePath).text)
result = readJsonTrackerFile()

} catch (Exception exception) {
result = readOldFormat()
}
return result
}

/**
* Reads a .fileTracker.data file that has old format
* @return a map with values from .fileTracker.data file
*/
private Map<String, ComponentHash> readOldFormat() throws IOException {
Map<String, ComponentHash> result
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(sourcePath))
result = convertToComponentHash((Map<String, String>) ois.readObject())
ois.close()
return result
}

/**
* Converts a map from Map<String, String> to Map<String, ComponentHash>
* @param fileTrackerMap is a map that has filename as key and its hash as value
* @return a map with fileName and its componentHash
*/
private Map<String, ComponentHash> convertToComponentHash(Map<String, String> fileTrackerMap) {
Map<String, ComponentHash> result = [:]
fileTrackerMap.each {String fileName, String fileHash ->
ComponentHash componentHash = new ComponentHash(fileName, fileHash)
result.put(fileName, componentHash)
}
return result
}

/**
* Reads .fileTracker.data file that has a json format.
* @return a map with values from .fileTracker.data file
*/
private Map<String, ComponentHash> readJsonTrackerFile() {
Map<String, ComponentHash> result = [:]
JsonSlurper jsonSlurper = new JsonSlurper()
LazyMap lazyMap = jsonSlurper.parseText(new File(sourcePath).text)
lazyMap.each { String componentName, LazyMap componentMap ->
String fileName = componentMap.get(FILE_NAME_LABEL)
String fileHash = componentMap.get(HASH_LABEL)
ComponentHash componentHash
if (componentMap.containsKey(SUB_COMPONENT_LABEL)) {
componentHash = new ObjectHash(fileName, fileHash, componentMap.get(SUB_COMPONENT_LABEL) as Map<String, String>)
} else {
componentHash = new ComponentHash(fileName, fileHash)
}
result.put(fileName, componentHash)
}
return result
}
}
Loading

0 comments on commit 8d19237

Please sign in to comment.