kordamp / jdeps-gradle-plugin

Run JDeps on a Gradle build
Apache License 2.0
35 stars 10 forks source link

Code to add -include option #13

Closed clarosoft closed 2 years ago

clarosoft commented 4 years ago

Tried new branch commit but permission denied. If it helps here is the code I wrote to get the -include option working. Needed this option to restrict jdeps package scans to our own code, because JDK 11 jdeps was failing on our pre-Java 9 code due to multiple dependency files (not controlled by us) using the jdk.unsupported classes.

`class JDepsReportTask extends DefaultTask { @Input boolean verbose = false @Input boolean summary = false @Input boolean profile = false @Input boolean recursive = false @Input boolean jdkinternals = true @Input boolean consoleOutput = true @Input @Optional List configurations = ['runtime'] @Input @Optional List classpaths = ['compileClasspath', 'runtimeClasspath', 'testCompileClasspath', 'testRuntimeClasspath'] @Input @Optional List sourceSets = ['main'] @Input @Optional Integer multiRelease @Input @Optional Map<String, Integer> multiReleaseJars = [:] @Input @Optional String include = null

private Object reportDir

JDepsReportTask() {
    extensions.create('moduleOptions', ModuleOptions)
}

@TaskAction
void evaluate() {
    ModuleOptions moduleOptions = extensions.getByType(ModuleOptions)
    JavaCompile compileJava = project.tasks.findByName(JavaPlugin.COMPILE_JAVA_TASK_NAME)
    String classpath = compileJava.classpath.asPath
    List<String> compilerArgs = compileJava.options.compilerArgs
    /** #prevent global leak with per module record output for multimodule. 
     For much larger project, Could be further improved with a buffer so it won't eat the heap further...
     **/
    List<String> commandOutput = []

    final List<String> baseCmd = ['jdeps']
    if (summary) baseCmd << '-s'
    if (verbose) baseCmd << '-v'
    if (profile) baseCmd << '-profile'
    if (recursive) baseCmd << '-recursive'
    if (jdkinternals) baseCmd << '-jdkinternals'
    if (include) {
        baseCmd << '-include'
        Regex regex = new Regex(include)
        baseCmd << regex.pattern
    }

    if (JavaVersion.current().java9Compatible) {
        if (multiRelease) {
            baseCmd << '--multi-release'
            baseCmd << multiRelease.toString()
        }

        if (classpath) {
            baseCmd << '--module-path'
            baseCmd << classpath
        } else {
            int modulePathIndex = compilerArgs.indexOf('--module-path')
            if (modulePathIndex > -1) {
                baseCmd << '--module-path'
                baseCmd << compilerArgs[modulePathIndex + 1]
            }
        }

        if (!moduleOptions.addModules.empty) {
            baseCmd << '--add-modules'
            baseCmd << moduleOptions.addModules.join(',')
        } else {
            int addModulesIndex = compilerArgs.indexOf('--add-modules')
            if (addModulesIndex > -1) {
                baseCmd << '--add-modules'
                baseCmd << compilerArgs[addModulesIndex + 1]
            }
        }
    }

    compileJava.classpath = project.files()

    project.logger.info("jdeps version is ${executeCommand(['jdeps', '-version'])}")

    sourceSets.each { sc ->
        SourceSet sourceSet = project.sourceSets[sc]
        project.logger.info("Running jdeps on sourceSet ${sourceSet.name}")
        sourceSet.output.files.each { File file ->
            if (!file.exists()) {
                return // skip
            }

            project.logger.info("jdeps command set to ${baseCmd.join(' ')}")
            String output = JDepsReportTask.executeCommandOn(baseCmd, file.absolutePath)
            if (output) {
                commandOutput << "\nProject: ${project.name}\n${output}".toString()
            }
        }
    }

    for (String c : configurations) {
        inspectConfiguration(project.configurations[c], baseCmd, commandOutput)
    }

    for (String c : classpaths) {
        inspectConfiguration(project.configurations[c], baseCmd, commandOutput)
    }

    if (commandOutput) {
        commandOutput = commandOutput.unique()
        if (consoleOutput) println commandOutput.join('\n')

        File parentFile = getReportsDir()
        if (!parentFile.exists()) parentFile.mkdirs()
        File logFile = new File(parentFile, 'jdeps-report.txt')
        logFile.append(commandOutput)
    }
}

`

aalmiray commented 4 years ago

Hmm, that's weird. Did you try to push a branch to this repository? If so then the way to make this work is

  1. for this repository to your own user/org.
  2. create a branch.
  3. make modifications to code as needed.
  4. push the branch to your repository.
  5. create a pull request
clarosoft commented 4 years ago

OK - thanks. Was using the wrong method, Here's the pull request here:

https://github.com/kordamp/jdeps-gradle-plugin/pull/14