Build task - collect @Incubating + logging refs

This commit is contained in:
Steve Ebersole 2022-01-22 10:53:13 -06:00
parent ef73522139
commit 692fa24b34
5 changed files with 91 additions and 38 deletions

View File

@ -9,7 +9,9 @@ package org.hibernate;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.CONSTRUCTOR; import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.PACKAGE; import static java.lang.annotation.ElementType.PACKAGE;
import static java.lang.annotation.ElementType.METHOD; import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE; import static java.lang.annotation.ElementType.TYPE;
@ -24,7 +26,7 @@ import static java.lang.annotation.RetentionPolicy.CLASS;
* *
* @author Steve Ebersole * @author Steve Ebersole
*/ */
@Target({PACKAGE, TYPE, METHOD,CONSTRUCTOR}) @Target({PACKAGE, TYPE, ANNOTATION_TYPE, METHOD, FIELD, CONSTRUCTOR})
@Retention(CLASS) @Retention(CLASS)
public @interface Incubating { public @interface Incubating {
} }

View File

@ -8,9 +8,12 @@ package org.hibernate.orm.post;
import org.gradle.api.Plugin; import org.gradle.api.Plugin;
import org.gradle.api.Project; import org.gradle.api.Project;
import org.gradle.api.file.RegularFile; import org.gradle.api.file.Directory;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.provider.Provider; import org.gradle.api.provider.Provider;
import org.gradle.jvm.tasks.Jar; import org.gradle.api.tasks.SourceSet;
import static org.gradle.api.tasks.SourceSet.MAIN_SOURCE_SET_NAME;
/** /**
* @author Steve Ebersole * @author Steve Ebersole
@ -18,29 +21,32 @@ import org.gradle.jvm.tasks.Jar;
public class CollectorPlugin implements Plugin<Project> { public class CollectorPlugin implements Plugin<Project> {
@Override @Override
public void apply(Project project) { public void apply(Project project) {
final Jar jarTask = (Jar) project.getTasks().findByName( "jar" ); final JavaPluginExtension javaPluginExtension = project.getExtensions().getByType( JavaPluginExtension.class );
final Provider<RegularFile> jarFileReferenceAccess = jarTask.getArchiveFile(); final SourceSet projectMainSourceSet = javaPluginExtension.getSourceSets().getByName( MAIN_SOURCE_SET_NAME );
final Provider<RegularFile> indexFileReferenceAccess = project.getLayout() final Provider<Directory> classesDirectory = projectMainSourceSet.getJava().getClassesDirectory();
.getBuildDirectory()
.file( "post/" + project.getName() + ".idx" );
final IndexManager indexManager = new IndexManager( jarFileReferenceAccess, indexFileReferenceAccess ); final IndexManager indexManager = new IndexManager( classesDirectory, project );
final IndexerTask indexerTask = project.getTasks().create( final IndexerTask indexerTask = project.getTasks().create(
"indexProjectJar", "indexProject",
IndexerTask.class, IndexerTask.class,
indexManager indexManager
); );
// NOTE : `indexProject` implicitly depends on the compilation task.
// it uses the `classesDirectory` from the `main` sourceSet. Gradle
// understands that the `classesDirectory` is generated from the
// compilation task and implicitly creates the task dependency
final IncubatingCollectorTask incubatingTask = project.getTasks().create( final IncubatingCollectorTask incubatingTask = project.getTasks().create(
"collectProjectIncubating", "createIncubatingReport",
IncubatingCollectorTask.class, IncubatingCollectorTask.class,
indexManager indexManager
); );
incubatingTask.dependsOn( indexerTask ); incubatingTask.dependsOn( indexerTask );
final LoggingCollectorTask loggingTask = project.getTasks().create( final LoggingCollectorTask loggingTask = project.getTasks().create(
"collectProjectLogging", "createLoggingReport",
LoggingCollectorTask.class, LoggingCollectorTask.class,
indexManager indexManager
); );

View File

@ -12,6 +12,11 @@ import java.util.TreeSet;
import javax.inject.Inject; import javax.inject.Inject;
import org.gradle.api.DefaultTask; import org.gradle.api.DefaultTask;
import org.gradle.api.Project;
import org.gradle.api.file.RegularFile;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.TaskAction; import org.gradle.api.tasks.TaskAction;
import org.jboss.jandex.AnnotationInstance; import org.jboss.jandex.AnnotationInstance;
@ -28,10 +33,22 @@ public abstract class IncubatingCollectorTask extends DefaultTask {
public static final String INCUBATING_ANN_NAME = "org.hibernate.Incubating"; public static final String INCUBATING_ANN_NAME = "org.hibernate.Incubating";
private final IndexManager indexManager; private final IndexManager indexManager;
private final Provider<RegularFile> reportFileReferenceAccess;
@Inject @Inject
public IncubatingCollectorTask(IndexManager indexManager) { public IncubatingCollectorTask(IndexManager indexManager, Project project) {
this.indexManager = indexManager; this.indexManager = indexManager;
this.reportFileReferenceAccess = project.getLayout().getBuildDirectory().file( "post/" + project.getName() + "-incubating.txt" );
}
@InputFile
public Provider<RegularFile> getIndexFileReference() {
return indexManager.getIndexFileReferenceAccess();
}
@OutputFile
public Provider<RegularFile> getReportFileReferenceAccess() {
return reportFileReferenceAccess;
} }
@TaskAction @TaskAction

View File

@ -11,10 +11,14 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.Set;
import org.gradle.api.Project;
import org.gradle.api.file.Directory;
import org.gradle.api.file.RegularFile; import org.gradle.api.file.RegularFile;
import org.gradle.api.provider.Provider; import org.gradle.api.provider.Provider;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.Index; import org.jboss.jandex.Index;
import org.jboss.jandex.IndexWriter; import org.jboss.jandex.IndexWriter;
import org.jboss.jandex.Indexer; import org.jboss.jandex.Indexer;
@ -25,22 +29,28 @@ import org.jboss.jandex.Indexer;
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public class IndexManager { public class IndexManager {
private final Provider<RegularFile> jarFileReference; private final Provider<Directory> classesDirectoryReferenceAccess;
private final Provider<RegularFile> indexFileReference; private final Provider<RegularFile> indexFileReferenceAccess;
private final Project project;
private Index index; private Index index;
public IndexManager(Provider<RegularFile> jarFileReference, Provider<RegularFile> indexFileReference) { public IndexManager(
this.jarFileReference = jarFileReference; Provider<Directory> classesDirectoryReferenceAccess,
this.indexFileReference = indexFileReference; Project project) {
this.classesDirectoryReferenceAccess = classesDirectoryReferenceAccess;
this.indexFileReferenceAccess = project.getLayout()
.getBuildDirectory()
.file( "post/" + project.getName() + ".idx" );
this.project = project;
} }
public Provider<RegularFile> getJarFileReference() { public Provider<Directory> getClassesDirectoryReferenceAccess() {
return jarFileReference; return classesDirectoryReferenceAccess;
} }
public Provider<RegularFile> getIndexFileReference() { public Provider<RegularFile> getIndexFileReferenceAccess() {
return indexFileReference; return indexFileReferenceAccess;
} }
public Index getIndex() { public Index getIndex() {
@ -55,16 +65,31 @@ public class IndexManager {
return; return;
} }
final File jarFileAsFile = jarFileReference.get().getAsFile();
final Indexer indexer = new Indexer(); final Indexer indexer = new Indexer();
try ( final FileInputStream stream = new FileInputStream( jarFileAsFile ) ) {
indexer.index( stream ); final Directory classesDirectory = classesDirectoryReferenceAccess.get();
} final Set<File> classFiles = classesDirectory.getAsFileTree().getFiles();
catch (FileNotFoundException e) { for ( File classFile : classFiles ) {
throw new RuntimeException( "Unable to locate project jar file - " + jarFileAsFile.getAbsolutePath(), e ); if ( !classFile.getName().endsWith( ".class" ) ) {
} continue;
catch (IOException e) { }
throw new RuntimeException( "Error accessing project jar file - " + jarFileAsFile.getAbsolutePath(), e );
if ( !classFile.getAbsolutePath().contains( "org/hibernate" ) ) {
continue;
}
try ( final FileInputStream stream = new FileInputStream( classFile ) ) {
final ClassInfo indexedClassInfo = indexer.index( stream );
if ( indexedClassInfo == null ) {
project.getLogger().lifecycle( "Problem indexing class file - " + classFile.getAbsolutePath() );
}
}
catch (FileNotFoundException e) {
throw new RuntimeException( "Problem locating project class file - " + classFile.getAbsolutePath(), e );
}
catch (IOException e) {
throw new RuntimeException( "Error accessing project class file - " + classFile.getAbsolutePath(), e );
}
} }
this.index = indexer.complete(); this.index = indexer.complete();
@ -72,13 +97,13 @@ public class IndexManager {
} }
private void storeIndex(Index index) { private void storeIndex(Index index) {
final File indexFile = indexFileReference.get().getAsFile(); final File indexFile = indexFileReferenceAccess.get().getAsFile();
if ( indexFile.exists() ) { if ( indexFile.exists() ) {
indexFile.delete(); indexFile.delete();
} }
try { try {
indexFile.mkdirs(); indexFile.getParentFile().mkdirs();
indexFile.createNewFile(); indexFile.createNewFile();
} }
catch (IOException e) { catch (IOException e) {

View File

@ -9,10 +9,12 @@ package org.hibernate.orm.post;
import javax.inject.Inject; import javax.inject.Inject;
import org.gradle.api.DefaultTask; import org.gradle.api.DefaultTask;
import org.gradle.api.file.Directory;
import org.gradle.api.file.RegularFile; import org.gradle.api.file.RegularFile;
import org.gradle.api.provider.Provider; import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.InputFile; import org.gradle.api.tasks.InputDirectory;
import org.gradle.api.tasks.OutputFile; import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.SkipWhenEmpty;
import org.gradle.api.tasks.TaskAction; import org.gradle.api.tasks.TaskAction;
/** /**
@ -28,14 +30,15 @@ public abstract class IndexerTask extends DefaultTask {
this.indexManager = indexManager; this.indexManager = indexManager;
} }
@InputFile @InputDirectory
public Provider<RegularFile> getJarFileReference() { @SkipWhenEmpty
return indexManager.getJarFileReference(); public Provider<Directory> getClassesDirectory() {
return indexManager.getClassesDirectoryReferenceAccess();
} }
@OutputFile @OutputFile
public Provider<RegularFile> getIndexFileReference() { public Provider<RegularFile> getIndexFileReference() {
return indexManager.getIndexFileReference(); return indexManager.getIndexFileReferenceAccess();
} }
@TaskAction @TaskAction