Search in sources :

Example 1 with FileArchive

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();
        }
    }
}
Also used : Arrays(java.util.Arrays) FileArchive(org.revapi.simple.FileArchive) MavenBootstrap(pw.krejci.modules.maven.MavenBootstrap) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) DependencySpec(org.jboss.modules.DependencySpec) ModuleSpec(org.jboss.modules.ModuleSpec) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Map(java.util.Map) ArtifactResolver(org.revapi.maven.utils.ArtifactResolver) Getopt(gnu.getopt.Getopt) Collectors.toSet(java.util.stream.Collectors.toSet) Nullable(javax.annotation.Nullable) LongOpt(gnu.getopt.LongOpt) API(org.revapi.API) Properties(java.util.Properties) AnalysisContext(org.revapi.AnalysisContext) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Collections.emptyList(java.util.Collections.emptyList) Set(java.util.Set) Artifact(org.eclipse.aether.artifact.Artifact) AnalysisResult(org.revapi.AnalysisResult) FileInputStream(java.io.FileInputStream) Revapi(org.revapi.Revapi) File(java.io.File) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) List(java.util.List) Module(org.jboss.modules.Module) RepositoryException(org.eclipse.aether.RepositoryException) ProjectModule(pw.krejci.modules.maven.ProjectModule) ModuleSpecController(pw.krejci.modules.maven.ModuleSpecController) ScopeDependencyTraverser(org.revapi.maven.utils.ScopeDependencyTraverser) ModelNode(org.jboss.dmr.ModelNode) ScopeDependencySelector(org.revapi.maven.utils.ScopeDependencySelector) RepositorySystem(org.eclipse.aether.RepositorySystem) HashSet(java.util.HashSet) Collectors.toSet(java.util.stream.Collectors.toSet) Set(java.util.Set) Revapi(org.revapi.Revapi) AnalysisContext(org.revapi.AnalysisContext) Properties(java.util.Properties) ModuleSpec(org.jboss.modules.ModuleSpec) DependencySpec(org.jboss.modules.DependencySpec) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) FileInputStream(java.io.FileInputStream) AnalysisResult(org.revapi.AnalysisResult) ProjectModule(pw.krejci.modules.maven.ProjectModule) ModuleSpecController(pw.krejci.modules.maven.ModuleSpecController) Module(org.jboss.modules.Module) ProjectModule(pw.krejci.modules.maven.ProjectModule) ModelNode(org.jboss.dmr.ModelNode) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Example 2 with FileArchive

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;
}
Also used : ArrayList(java.util.ArrayList) FileArchive(org.revapi.simple.FileArchive) File(java.io.File)

Example 3 with FileArchive

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);
}
Also used : HashMap(java.util.HashMap) RepositoryException(org.eclipse.aether.RepositoryException) Getopt(gnu.getopt.Getopt) LongOpt(gnu.getopt.LongOpt) FileArchive(org.revapi.simple.FileArchive) File(java.io.File)

Example 4 with FileArchive

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);
}
Also used : ArrayList(java.util.ArrayList) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) RepositoryException(org.eclipse.aether.RepositoryException) ArtifactResolver(org.revapi.maven.utils.ArtifactResolver) ScopeDependencyTraverser(org.revapi.maven.utils.ScopeDependencyTraverser) RepositorySystem(org.eclipse.aether.RepositorySystem) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) FileArchive(org.revapi.simple.FileArchive) ScopeDependencySelector(org.revapi.maven.utils.ScopeDependencySelector)

Example 5 with FileArchive

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());
}
Also used : Revapi(org.revapi.Revapi) StringWriter(java.io.StringWriter) FileArchive(org.revapi.simple.FileArchive) AnalysisContext(org.revapi.AnalysisContext) File(java.io.File) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Aggregations

FileArchive (org.revapi.simple.FileArchive)6 File (java.io.File)5 ArrayList (java.util.ArrayList)3 RepositoryException (org.eclipse.aether.RepositoryException)3 AnalysisContext (org.revapi.AnalysisContext)3 Revapi (org.revapi.Revapi)3 Getopt (gnu.getopt.Getopt)2 LongOpt (gnu.getopt.LongOpt)2 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 HashMap (java.util.HashMap)2 DefaultRepositorySystemSession (org.eclipse.aether.DefaultRepositorySystemSession)2 RepositorySystem (org.eclipse.aether.RepositorySystem)2 RemoteRepository (org.eclipse.aether.repository.RemoteRepository)2 Test (org.junit.Test)2 ArtifactResolver (org.revapi.maven.utils.ArtifactResolver)2 ScopeDependencySelector (org.revapi.maven.utils.ScopeDependencySelector)2 ScopeDependencyTraverser (org.revapi.maven.utils.ScopeDependencyTraverser)2 FileInputStream (java.io.FileInputStream)1 Path (java.nio.file.Path)1