|
| 1 | +/* |
| 2 | + * Copyright 2023 Red Hat, Inc. and/or its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.kie.kogito.quarkus.serverless.workflow.deployment.livereload; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.io.UncheckedIOException; |
| 20 | +import java.nio.file.Files; |
| 21 | +import java.nio.file.Path; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.Collection; |
| 24 | +import java.util.HashSet; |
| 25 | +import java.util.List; |
| 26 | +import java.util.ServiceLoader; |
| 27 | +import java.util.stream.Stream; |
| 28 | + |
| 29 | +import javax.inject.Inject; |
| 30 | + |
| 31 | +import org.drools.codegen.common.GeneratedFile; |
| 32 | +import org.drools.codegen.common.GeneratedFileType; |
| 33 | +import org.drools.drl.quarkus.util.deployment.DroolsQuarkusResourceUtils; |
| 34 | +import org.eclipse.microprofile.config.ConfigProvider; |
| 35 | +import org.jboss.jandex.IndexView; |
| 36 | +import org.jboss.jandex.Indexer; |
| 37 | +import org.kie.kogito.codegen.api.context.KogitoBuildContext; |
| 38 | +import org.kie.kogito.quarkus.common.deployment.KogitoAddonsPreGeneratedSourcesBuildItem; |
| 39 | +import org.kie.kogito.quarkus.common.deployment.KogitoBuildContextBuildItem; |
| 40 | +import org.kie.kogito.quarkus.common.deployment.KogitoQuarkusResourceUtils; |
| 41 | +import org.kie.kogito.quarkus.common.deployment.LiveReloadExecutionBuildItem; |
| 42 | + |
| 43 | +import io.quarkus.arc.deployment.GeneratedBeanBuildItem; |
| 44 | +import io.quarkus.bootstrap.model.ApplicationModel; |
| 45 | +import io.quarkus.bootstrap.prebuild.CodeGenException; |
| 46 | +import io.quarkus.deployment.CodeGenContext; |
| 47 | +import io.quarkus.deployment.IsDevelopment; |
| 48 | +import io.quarkus.deployment.annotations.BuildProducer; |
| 49 | +import io.quarkus.deployment.annotations.BuildStep; |
| 50 | +import io.quarkus.deployment.builditem.CombinedIndexBuildItem; |
| 51 | +import io.quarkus.deployment.builditem.LiveReloadBuildItem; |
| 52 | +import io.quarkus.deployment.index.IndexingUtil; |
| 53 | +import io.quarkus.deployment.pkg.builditem.CurateOutcomeBuildItem; |
| 54 | +import io.quarkus.deployment.pkg.builditem.OutputTargetBuildItem; |
| 55 | + |
| 56 | +/** |
| 57 | + * This class adds live reload support for {@link io.quarkus.deployment.CodeGenProvider} objects. |
| 58 | + */ |
| 59 | +public class LiveReloadProcessor { |
| 60 | + |
| 61 | + private final LiveReloadBuildItem liveReloadBuildItem; |
| 62 | + |
| 63 | + private final ApplicationModel applicationModel; |
| 64 | + |
| 65 | + private final Path workDir; |
| 66 | + |
| 67 | + private final IndexView computingIndex; |
| 68 | + |
| 69 | + private final IndexView index; |
| 70 | + |
| 71 | + private final KogitoBuildContext kogitoBuildContext; |
| 72 | + |
| 73 | + @Inject |
| 74 | + public LiveReloadProcessor( |
| 75 | + CombinedIndexBuildItem combinedIndexBuildItem, |
| 76 | + LiveReloadBuildItem liveReloadBuildItem, |
| 77 | + CurateOutcomeBuildItem curateOutcomeBuildItem, |
| 78 | + OutputTargetBuildItem outputTargetBuildItem, |
| 79 | + KogitoBuildContextBuildItem contextBuildItem) { |
| 80 | + this.liveReloadBuildItem = liveReloadBuildItem; |
| 81 | + applicationModel = curateOutcomeBuildItem.getApplicationModel(); |
| 82 | + workDir = outputTargetBuildItem.getOutputDirectory(); |
| 83 | + computingIndex = combinedIndexBuildItem.getComputingIndex(); |
| 84 | + index = combinedIndexBuildItem.getIndex(); |
| 85 | + kogitoBuildContext = contextBuildItem.getKogitoBuildContext(); |
| 86 | + } |
| 87 | + |
| 88 | + @BuildStep(onlyIf = IsDevelopment.class) |
| 89 | + public LiveReloadExecutionBuildItem liveReload(BuildProducer<KogitoAddonsPreGeneratedSourcesBuildItem> sourcesProducer) { |
| 90 | + Collection<GeneratedFile> generatedFiles = new ArrayList<>(); |
| 91 | + List<IndexView> indexViews = new ArrayList<>(); |
| 92 | + if (liveReloadBuildItem.isLiveReload()) { |
| 93 | + if (shouldSkipLiveReload()) { |
| 94 | + dontSkipNextLiveReload(); |
| 95 | + } else { |
| 96 | + ServiceLoader.load(LiveReloadableCodeGenProvider.class).stream() |
| 97 | + .map(ServiceLoader.Provider::get) |
| 98 | + .map(this::generateCode) |
| 99 | + .forEach(codeGenerationResult -> { |
| 100 | + generatedFiles.addAll(codeGenerationResult.getGeneratedFiles()); |
| 101 | + indexViews.add(codeGenerationResult.getIndexView()); |
| 102 | + }); |
| 103 | + } |
| 104 | + } |
| 105 | + if (!generatedFiles.isEmpty()) { |
| 106 | + sourcesProducer.produce(new KogitoAddonsPreGeneratedSourcesBuildItem(generatedFiles)); |
| 107 | + skipNextLiveReload(); |
| 108 | + return new LiveReloadExecutionBuildItem(KogitoQuarkusResourceUtils.generateAggregatedIndexNew(computingIndex, indexViews)); |
| 109 | + } else { |
| 110 | + dontSkipNextLiveReload(); |
| 111 | + return new LiveReloadExecutionBuildItem(computingIndex); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private CodeGenerationResult generateCode(LiveReloadableCodeGenProvider codeGenProvider) { |
| 116 | + try { |
| 117 | + Collection<GeneratedFile> generatedFiles = new ArrayList<>(generateSources(codeGenProvider)); |
| 118 | + if (!generatedFiles.isEmpty()) { |
| 119 | + Collection<GeneratedBeanBuildItem> generatedBeanBuildItems = compileGeneratedSources(generatedFiles); |
| 120 | + return new CodeGenerationResult(generatedFiles, indexCompiledSources(generatedBeanBuildItems)); |
| 121 | + } else { |
| 122 | + return new CodeGenerationResult(List.of(), computingIndex); |
| 123 | + } |
| 124 | + } catch (CodeGenException e) { |
| 125 | + throw new IllegalStateException(e); |
| 126 | + } catch (IOException e) { |
| 127 | + throw new UncheckedIOException(e); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private IndexView indexCompiledSources(Collection<GeneratedBeanBuildItem> generatedBeanBuildItems) { |
| 132 | + Indexer kogitoIndexer = new Indexer(); |
| 133 | + |
| 134 | + for (GeneratedBeanBuildItem generatedBeanBuildItem : generatedBeanBuildItems) { |
| 135 | + IndexingUtil.indexClass( |
| 136 | + generatedBeanBuildItem.getName(), |
| 137 | + kogitoIndexer, |
| 138 | + index, |
| 139 | + new HashSet<>(), |
| 140 | + kogitoBuildContext.getClassLoader(), |
| 141 | + generatedBeanBuildItem.getData()); |
| 142 | + } |
| 143 | + |
| 144 | + return kogitoIndexer.complete(); |
| 145 | + } |
| 146 | + |
| 147 | + private Collection<GeneratedBeanBuildItem> compileGeneratedSources(Collection<GeneratedFile> sources) { |
| 148 | + return DroolsQuarkusResourceUtils.compileGeneratedSources( |
| 149 | + kogitoBuildContext, |
| 150 | + applicationModel.getRuntimeDependencies(), |
| 151 | + sources, |
| 152 | + true); |
| 153 | + } |
| 154 | + |
| 155 | + private Collection<GeneratedFile> generateSources(LiveReloadableCodeGenProvider codeGenProvider) |
| 156 | + throws CodeGenException, IOException { |
| 157 | + Path outDir = workDir.resolve("generated-sources").resolve(codeGenProvider.providerId()); |
| 158 | + var generatedFiles = new ArrayList<GeneratedFile>(); |
| 159 | + for (Path sourcePath : kogitoBuildContext.getAppPaths().getSourcePaths()) { |
| 160 | + Path inputDir = sourcePath.resolve("main").resolve(codeGenProvider.inputDirectory()); |
| 161 | + var codeGenContext = new CodeGenContext(applicationModel, outDir, workDir, inputDir, false, ConfigProvider.getConfig(), false); |
| 162 | + if (codeGenProvider.trigger(codeGenContext)) { |
| 163 | + try (Stream<Path> sources = Files.walk(outDir)) { |
| 164 | + sources.filter(Files::isRegularFile) |
| 165 | + .filter(path -> path.toString().endsWith(".java")) |
| 166 | + .map(path -> { |
| 167 | + try { |
| 168 | + return new GeneratedFile(GeneratedFileType.SOURCE, outDir.relativize(path), Files.readAllBytes(path)); |
| 169 | + } catch (IOException e) { |
| 170 | + throw new UncheckedIOException(e); |
| 171 | + } |
| 172 | + }) |
| 173 | + .forEach(generatedFiles::add); |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + return generatedFiles; |
| 179 | + } |
| 180 | + |
| 181 | + private void skipNextLiveReload() { |
| 182 | + liveReloadBuildItem.setContextObject(SkipLiveReload.class, SkipLiveReload.TRUE); |
| 183 | + } |
| 184 | + |
| 185 | + private void dontSkipNextLiveReload() { |
| 186 | + liveReloadBuildItem.setContextObject(SkipLiveReload.class, SkipLiveReload.FALSE); |
| 187 | + } |
| 188 | + |
| 189 | + private boolean shouldSkipLiveReload() { |
| 190 | + if (liveReloadBuildItem.getContextObject(SkipLiveReload.class) != null) { |
| 191 | + return liveReloadBuildItem.getContextObject(SkipLiveReload.class) == SkipLiveReload.TRUE; |
| 192 | + } |
| 193 | + return false; |
| 194 | + } |
| 195 | + |
| 196 | + @BuildStep(onlyIfNot = IsDevelopment.class) |
| 197 | + public LiveReloadExecutionBuildItem executeWhenNotDevelopment() { |
| 198 | + return new LiveReloadExecutionBuildItem(computingIndex); |
| 199 | + } |
| 200 | +} |
0 commit comments