use of org.revapi.simple.FileArchive in project revapi by revapi.
the class Main method run.
@SuppressWarnings("ConstantConditions")
private static void run(File cacheDir, String[] extensionGAVs, List<FileArchive> oldArchives, List<FileArchive> oldSupplementaryArchives, List<FileArchive> newArchives, List<FileArchive> newSupplementaryArchives, String[] configFiles, Map<String, String> additionalConfig) throws Exception {
ProjectModule.Builder bld = ProjectModule.build();
bld.localRepository(cacheDir);
if (extensionGAVs != null) {
for (String gav : extensionGAVs) {
bld.addDependency(gav);
}
}
Properties libraryVersionsProps = new Properties();
libraryVersionsProps.load(Main.class.getResourceAsStream("/library.versions"));
Set<Artifact> globalArtifacts = libraryVersionsProps.stringPropertyNames().stream().map(p -> {
String gav = p + ':' + libraryVersionsProps.get(p);
return new DefaultArtifact(gav);
}).collect(toSet());
bld.moduleSpecController(new ModuleSpecController() {
private boolean override;
private String currentModuleName;
@Override
public void start(String moduleName) {
currentModuleName = moduleName;
}
// TODO add warnings when the deps depend on another revapi version than the one bundled...
@Override
public DependencySpec modifyDependency(String dependencyName, DependencySpec original) {
boolean overrideThis = false;
Artifact a = new DefaultArtifact(dependencyName);
for (Artifact ga : globalArtifacts) {
if (ga.getGroupId().equals(a.getGroupId()) && ga.getArtifactId().equals(a.getArtifactId()) && !ga.getVersion().equals(a.getVersion())) {
LOG.warn("Detected version conflict in dependencies of extension " + currentModuleName + ". The extension depends on " + a + " while the CLI has " + ga + " on global" + " classpath. This will likely cause problems.");
}
}
override = override || overrideThis;
return overrideThis ? null : original;
}
@Override
public void modify(ModuleSpec.Builder bld) {
if (override) {
Set<String> revapiPaths = new HashSet<>(Arrays.asList("org/revapi", "org/revapi/configuration", "org/revapi/query", "org/revapi/simple"));
bld.addDependency(DependencySpec.createSystemDependencySpec(revapiPaths));
override = false;
}
}
@Override
public void end(String moduleName) {
currentModuleName = null;
}
});
LOG.info("Downloading extensions");
Module project = bld.create();
Revapi revapi = Revapi.builder().withAllExtensionsFrom(project.getClassLoader()).withAllExtensionsFromThreadContextClassLoader().build();
AnalysisContext.Builder ctxBld = AnalysisContext.builder(revapi).withOldAPI(API.of(oldArchives).supportedBy(oldSupplementaryArchives).build()).withNewAPI(API.of(newArchives).supportedBy(newSupplementaryArchives).build());
if (configFiles != null) {
for (String cf : configFiles) {
File f = new File(cf);
checkCanRead(f, "Configuration file");
try (FileInputStream is = new FileInputStream(f)) {
ctxBld.mergeConfigurationFromJSONStream(is);
}
}
}
for (Map.Entry<String, String> e : additionalConfig.entrySet()) {
String[] keyPath = e.getKey().split("\\.");
ModelNode additionalNode = new ModelNode();
ModelNode key = additionalNode.get(keyPath);
String value = e.getValue();
if (value.startsWith("[") && value.endsWith("]")) {
String[] values = value.substring(1, value.length() - 1).split("\\s*,\\s*");
for (String v : values) {
key.add(v);
}
} else {
key.set(value);
}
ctxBld.mergeConfiguration(additionalNode);
}
LOG.info("Starting analysis");
try (AnalysisResult result = revapi.analyze(ctxBld.build())) {
if (!result.isSuccess()) {
throw result.getFailure();
}
}
}
use of org.revapi.simple.FileArchive in project revapi by revapi.
the class Main method convertPaths.
private static List<FileArchive> convertPaths(String[] paths, String errorMessagePrefix) {
List<FileArchive> archives = new ArrayList<>(paths.length);
for (String path : paths) {
File f = new File(path);
checkCanRead(f, errorMessagePrefix);
archives.add(new FileArchive(f));
}
return archives;
}
use of org.revapi.simple.FileArchive in project revapi by revapi.
the class Main method main.
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
if (args.length < 2) {
usage(null);
System.exit(1);
}
String scriptFileName = args[0];
String baseDir = args[1];
String[] realArgs = new String[args.length - 2];
System.arraycopy(args, 2, realArgs, 0, realArgs.length);
String[] extensionGAVs = null;
String[] oldArchivePaths = null;
String[] oldGavs = null;
String[] newArchivePaths = null;
String[] newGavs = null;
String[] oldSupplementaryArchivePaths = null;
String[] newSupplementaryArchivePaths = null;
Map<String, String> additionalConfigOptions = new HashMap<>();
String[] configFiles = null;
File cacheDir = new File(baseDir, "cache");
LongOpt[] longOpts = new LongOpt[12];
longOpts[0] = new LongOpt("usage", LongOpt.NO_ARGUMENT, null, 'u');
longOpts[1] = new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h');
longOpts[2] = new LongOpt("extensions", LongOpt.REQUIRED_ARGUMENT, null, 'e');
longOpts[3] = new LongOpt("old", LongOpt.REQUIRED_ARGUMENT, null, 'o');
longOpts[4] = new LongOpt("new", LongOpt.REQUIRED_ARGUMENT, null, 'n');
longOpts[5] = new LongOpt("old-supplementary", LongOpt.REQUIRED_ARGUMENT, null, 's');
longOpts[6] = new LongOpt("new-supplementary", LongOpt.REQUIRED_ARGUMENT, null, 't');
longOpts[7] = new LongOpt("D", LongOpt.REQUIRED_ARGUMENT, null, 'D');
longOpts[8] = new LongOpt("config-files", LongOpt.REQUIRED_ARGUMENT, null, 'c');
longOpts[9] = new LongOpt("cache-dir", LongOpt.REQUIRED_ARGUMENT, null, 'd');
longOpts[10] = new LongOpt("old-gavs", LongOpt.REQUIRED_ARGUMENT, null, 'a');
longOpts[11] = new LongOpt("new-gavs", LongOpt.REQUIRED_ARGUMENT, null, 'b');
Getopt opts = new Getopt(scriptFileName, realArgs, "uhe:o:n:s:t:D:c:d:a:b:", longOpts);
int c;
while ((c = opts.getopt()) != -1) {
switch(c) {
case 'u':
case 'h':
usage(scriptFileName);
System.exit(0);
case 'e':
extensionGAVs = opts.getOptarg().split(",");
break;
case 'o':
oldArchivePaths = opts.getOptarg().split(",");
break;
case 'n':
newArchivePaths = opts.getOptarg().split(",");
break;
case 's':
oldSupplementaryArchivePaths = opts.getOptarg().split(",");
break;
case 't':
newSupplementaryArchivePaths = opts.getOptarg().split(",");
break;
case 'c':
configFiles = opts.getOptarg().split(",");
break;
case 'D':
String[] keyValue = opts.getOptarg().split("=");
additionalConfigOptions.put(keyValue[0], keyValue.length > 1 ? keyValue[1] : null);
break;
case 'd':
cacheDir = new File(opts.getOptarg());
break;
case 'a':
oldGavs = opts.getOptarg().split(",");
break;
case 'b':
newGavs = opts.getOptarg().split(",");
break;
case ':':
System.err.println("Argument required for option " + (char) opts.getOptopt());
break;
case '?':
System.err.println("The option '" + (char) opts.getOptopt() + "' is not valid");
System.exit(1);
break;
default:
System.err.println("getopt() returned " + c);
System.exit(1);
break;
}
}
if (extensionGAVs == null || (oldArchivePaths == null && oldGavs == null) || (newArchivePaths == null && newGavs == null)) {
usage(scriptFileName);
System.exit(1);
}
List<FileArchive> oldArchives = null;
List<FileArchive> newArchives = null;
List<FileArchive> oldSupplementaryArchives = null;
List<FileArchive> newSupplementaryArchives = null;
LOG.info("Downloading checked archives");
if (oldArchivePaths == null) {
ArchivesAndSupplementaryArchives res = convertGavs(oldGavs, "Old API Maven artifact", cacheDir);
oldArchives = res.archives;
oldSupplementaryArchives = res.supplementaryArchives;
} else {
oldArchives = convertPaths(oldArchivePaths, "Old API files");
oldSupplementaryArchives = oldSupplementaryArchivePaths == null ? emptyList() : convertPaths(oldSupplementaryArchivePaths, "Old API supplementary files");
}
if (newArchivePaths == null) {
ArchivesAndSupplementaryArchives res = convertGavs(newGavs, "New API Maven artifact", cacheDir);
newArchives = res.archives;
newSupplementaryArchives = res.supplementaryArchives;
} else {
newArchives = convertPaths(newArchivePaths, "New API files");
newSupplementaryArchives = newSupplementaryArchivePaths == null ? emptyList() : convertPaths(newSupplementaryArchivePaths, "New API supplementary files");
}
try {
run(cacheDir, extensionGAVs, oldArchives, oldSupplementaryArchives, newArchives, newSupplementaryArchives, configFiles, additionalConfigOptions);
} catch (Exception e) {
e.printStackTrace();
}
System.exit(0);
}
use of org.revapi.simple.FileArchive in project revapi by revapi.
the class Main method convertGavs.
private static ArchivesAndSupplementaryArchives convertGavs(String[] gavs, String errorMessagePrefix, File localRepo) {
RepositorySystem repositorySystem = MavenBootstrap.newRepositorySystem();
DefaultRepositorySystemSession session = MavenBootstrap.newRepositorySystemSession(repositorySystem, localRepo);
session.setDependencySelector(new ScopeDependencySelector("compile", "provided"));
session.setDependencyTraverser(new ScopeDependencyTraverser("compile", "provided"));
RemoteRepository mavenCentral = new RemoteRepository.Builder("@@forced-maven-central@@", "default", "http://repo.maven.apache.org/maven2/").build();
List<RemoteRepository> remoteRepositories = singletonList(mavenCentral);
ArtifactResolver resolver = new ArtifactResolver(repositorySystem, session, remoteRepositories);
List<FileArchive> archives = new ArrayList<>();
List<FileArchive> supplementaryArchives = new ArrayList<>();
for (String gav : gavs) {
try {
archives.add(new FileArchive(resolver.resolveArtifact(gav).getFile()));
ArtifactResolver.CollectionResult res = resolver.collectTransitiveDeps(gav);
res.getResolvedArtifacts().forEach(a -> supplementaryArchives.add(new FileArchive(a.getFile())));
if (!res.getFailures().isEmpty()) {
LOG.warn("Failed to resolve some transitive dependencies: " + res.getFailures().toString());
}
} catch (RepositoryException e) {
throw new IllegalArgumentException(errorMessagePrefix + " " + e.getMessage());
}
}
return new ArchivesAndSupplementaryArchives(archives, supplementaryArchives);
}
use of org.revapi.simple.FileArchive in project revapi by revapi.
the class TextReporterTest method testDefaultTemplate.
@Test
public void testDefaultTemplate() throws Exception {
TextReporter reporter = new TextReporter();
Revapi r = new Revapi(emptySet(), singleton(TextReporter.class), emptySet(), emptySet());
AnalysisContext ctx = AnalysisContext.builder(r).withOldAPI(API.of(new FileArchive(new File("old-dummy.archive"))).build()).withNewAPI(API.of(new FileArchive(new File("new-dummy.archive"))).build()).build();
AnalysisContext reporterCtx = r.prepareAnalysis(ctx).getFirstConfigurationOrNull(TextReporter.class);
reporter.initialize(reporterCtx);
buildReports().forEach(reporter::report);
StringWriter out = new StringWriter();
PrintWriter wrt = new PrintWriter(out);
reporter.setOutput(wrt);
reporter.close();
String expected = "Analysis results\n" + "----------------\n" + "\n" + "Old API: old-dummy.archive\n" + "New API: new-dummy.archive\n" + "old: old1\n" + "new: new1\n" + "code1: descr1\n" + "SOURCE: BREAKING\n" + "\n" + "old: old2\n" + "new: new2\n" + "code2: descr2\n" + "BINARY: BREAKING\n\n";
Assert.assertEquals(expected, out.toString());
}
Aggregations