-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from fundacionjala/develop
new build version 1.0.2
- Loading branch information
Showing
101 changed files
with
2,265 additions
and
918 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
...in/groovy/org/fundacionjala/gradle/plugins/enforce/filemonitor/ComponentComparable.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/groovy/org/fundacionjala/gradle/plugins/enforce/filemonitor/ComponentHash.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
176 changes: 176 additions & 0 deletions
176
src/main/groovy/org/fundacionjala/gradle/plugins/enforce/filemonitor/ComponentMonitor.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
...in/groovy/org/fundacionjala/gradle/plugins/enforce/filemonitor/ComponentSerializer.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.