candy version check

This commit is contained in:
lgrignon 2016-01-11 19:47:01 +01:00
parent ed9b8c5cdc
commit 6a139d55df
5 changed files with 59 additions and 18 deletions

View File

@ -254,7 +254,11 @@ public enum JSweetProblem {
/**
* Raised when a class is declared in a parent of a @Root package.
*/
CLASS_OUT_OF_ROOT_PACKAGE_SCOPE(Severity.ERROR);
CLASS_OUT_OF_ROOT_PACKAGE_SCOPE(Severity.ERROR),
/**
* Raised when using a candy which was generated for an older / newer version of the transpiler
*/
CANDY_VERSION_DISCREPANCY(Severity.WARNING);
private Severity severity;
@ -382,6 +386,8 @@ public enum JSweetProblem {
return String.format("invalid package hierarchy: type %s is declared in a parent of @Root package %s", params);
case WRONG_USE_OF_AMBIENT:
return String.format("wrong use of @Ambient on %s: only types and globals can be declared as ambients", params);
case CANDY_VERSION_DISCREPANCY:
return String.format("candy %s:%s was generated for an older / newer version of the transpiler. current:%s previous:%s", params);
}
return null;
}

View File

@ -563,7 +563,7 @@ public class JSweetTranspiler {
logger.error(e.getMessage(), e);
return;
}
candiesProcessor.processCandies();
candiesProcessor.processCandies(transpilationHandler);
addTsDefDir(candiesProcessor.getCandiesTsdefsDir());
if (classPath != null && !ArrayUtils.contains(classPath.split(File.pathSeparator), candiesProcessor.getCandiesProcessedDir().getPath())) {
classPath = candiesProcessor.getCandiesProcessedDir() + File.pathSeparator + classPath;

View File

@ -13,7 +13,7 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
*/
package org.jsweet.transpiler;
import java.io.File;

View File

@ -34,6 +34,8 @@ import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.jsweet.JSweetConfig;
import org.jsweet.transpiler.JSweetProblem;
import org.jsweet.transpiler.TranspilationHandler;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
@ -127,10 +129,10 @@ public class CandiesProcessor {
/**
* Do the processing for the candies jars found in the classpath.
*/
public void processCandies() throws IOException {
public void processCandies(TranspilationHandler transpilationHandler) throws IOException {
CandiesStore candiesStore = getCandiesStore();
LinkedHashMap<File, CandyDescriptor> newCandiesDescriptors = getCandiesDescriptorsFromClassPath();
LinkedHashMap<File, CandyDescriptor> newCandiesDescriptors = getCandiesDescriptorsFromClassPath(transpilationHandler);
CandiesStore newStore = new CandiesStore(new ArrayList<>(newCandiesDescriptors.values()));
if (newStore.equals(candiesStore)) {
logger.info("candies are up to date");
@ -160,7 +162,7 @@ public class CandiesProcessor {
extractSourcesForClasses(candies, mergedMixins.keySet());
}
private LinkedHashMap<File, CandyDescriptor> getCandiesDescriptorsFromClassPath() throws IOException {
private LinkedHashMap<File, CandyDescriptor> getCandiesDescriptorsFromClassPath(TranspilationHandler transpilationHandler) throws IOException {
LinkedHashMap<File, CandyDescriptor> jarFilesCollector = new LinkedHashMap<>();
for (String classPathEntry : classPath.split("[" + System.getProperty("path.separator") + "]")) {
if (classPathEntry.endsWith(".jar")) {
@ -172,7 +174,7 @@ public class CandiesProcessor {
if (isCandy) {
CandyDescriptor descriptor = CandyDescriptor.fromCandyJar(jarFileHandle);
checkCandyVersion(descriptor);
checkCandyVersion(descriptor, transpilationHandler);
jarFilesCollector.put(jarFile, descriptor);
}
}
@ -183,12 +185,18 @@ public class CandiesProcessor {
return jarFilesCollector;
}
private void checkCandyVersion(CandyDescriptor candy) {
// TODO : check major version change
// we assume candies will always remain compatible with jsweet minor
// versions changes
private void checkCandyVersion(CandyDescriptor candy, TranspilationHandler transpilationHandler) {
String actualTranspilerVersion = JSweetConfig.getVersionNumber().replace("-SNAPSHOT", "");
if (candy.transpilerVersion == null || !candy.transpilerVersion.equals(actualTranspilerVersion)) {
transpilationHandler.report(
JSweetProblem.CANDY_VERSION_DISCREPANCY, null,
JSweetProblem.CANDY_VERSION_DISCREPANCY.getMessage(
candy.name, candy.version, actualTranspilerVersion, candy.transpilerVersion));
}
}
private void extractCandies(Map<File, CandyDescriptor> candies) throws IOException {
File extractedSourcesDir = candiesSourceDir;
File extractedTsDefsDir = candiesTsdefsDir;

View File

@ -13,17 +13,23 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
*/
package org.jsweet.transpiler.candies;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import org.apache.commons.io.IOUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
/**
* A candy descriptor for the candies store.
*
@ -34,12 +40,14 @@ class CandyDescriptor {
String version;
long lastUpdateTimestamp;
String modelVersion;
String transpilerVersion;
public CandyDescriptor(String name, String version, long lastUpdateTimestamp, String modelVersion) {
public CandyDescriptor(String name, String version, long lastUpdateTimestamp, String modelVersion, String transpilerVersion) {
this.name = name;
this.version = version;
this.lastUpdateTimestamp = lastUpdateTimestamp;
this.modelVersion = modelVersion;
this.transpilerVersion = transpilerVersion;
}
@Override
@ -63,10 +71,17 @@ class CandyDescriptor {
private final static Pattern ARTIFACT_ID_PATTERN = Pattern.compile("[\\<]artifactId[\\>](.*)[\\<]/artifactId[\\>]");
private final static Pattern VERSION_PATTERN = Pattern.compile("[\\<]version[\\>](.*)[\\<]/version[\\>]");
private final static Gson gson = new GsonBuilder().setPrettyPrinting().create();
public static CandyDescriptor fromCandyJar(JarFile jarFile) throws IOException {
JarEntry pomEntry = jarFile.stream() //
.filter(e -> e.getName().endsWith("pom.xml")) //
.findFirst().get();
JarEntry pomEntry = null;
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry current = entries.nextElement();
if (current.getName().endsWith("pom.xml")) {
pomEntry = current;
}
}
String pomContent = IOUtils.toString(jarFile.getInputStream(pomEntry));
@ -95,7 +110,19 @@ class CandyDescriptor {
long lastUpdateTimestamp = jarFile.getEntry("META-INF/MANIFEST.MF").getTime();
return new CandyDescriptor(name, version, lastUpdateTimestamp, modelVersion);
String transpilerVersion = null;
ZipEntry metadataEntry = jarFile.getEntry("META-INF/candy-metadata.json");
if (metadataEntry != null) {
String metadataContent = IOUtils.toString(jarFile.getInputStream(metadataEntry));
@SuppressWarnings("unchecked")
Map<String, ?> metadata = gson.fromJson(metadataContent, Map.class);
transpilerVersion = (String) metadata.get("transpilerVersion");
}
return new CandyDescriptor(name, version, lastUpdateTimestamp, modelVersion, transpilerVersion);
}
@Override