|
| 1 | +/* |
| 2 | + * Copyright 2002-2022 the original author or authors. |
| 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 | + * https://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 | + |
| 17 | +package org.springframework.test.context.aot; |
| 18 | + |
| 19 | +import java.nio.file.Files; |
| 20 | +import java.nio.file.Path; |
| 21 | +import java.util.Set; |
| 22 | +import java.util.concurrent.Callable; |
| 23 | +import java.util.stream.Stream; |
| 24 | + |
| 25 | +import picocli.CommandLine; |
| 26 | +import picocli.CommandLine.Command; |
| 27 | +import picocli.CommandLine.Option; |
| 28 | +import picocli.CommandLine.Parameters; |
| 29 | + |
| 30 | +import org.springframework.aot.generate.FileSystemGeneratedFiles; |
| 31 | +import org.springframework.aot.generate.GeneratedFiles; |
| 32 | +import org.springframework.aot.nativex.FileNativeConfigurationWriter; |
| 33 | +import org.springframework.aot.nativex.NativeConfigurationWriter; |
| 34 | + |
| 35 | +/** |
| 36 | + * Command-line application that scans the provided classpath roots for Spring |
| 37 | + * integration test classes and then generates AOT artifacts for those test |
| 38 | + * classes in the provided output directories. |
| 39 | + * |
| 40 | + * @author Sam Brannen |
| 41 | + * @since 6.0 |
| 42 | + * @see TestClassScanner |
| 43 | + * @see TestContextAotGenerator |
| 44 | + * @see FileNativeConfigurationWriter |
| 45 | + */ |
| 46 | +@Command(mixinStandardHelpOptions = true, description = "Process test classes ahead of time") |
| 47 | +public class ProcessTestsAheadOfTimeCommand implements Callable<Integer> { |
| 48 | + |
| 49 | + @Parameters(index = "0", arity = "1..*", description = "Classpath roots for compiled test classes.") |
| 50 | + private Path[] testClasspathRoots; |
| 51 | + |
| 52 | + @Option(names = {"--packages"}, required = false, description = "Test packages to scan. This is optional any only intended for testing purposes.") |
| 53 | + private String[] packagesToScan = new String[0]; |
| 54 | + |
| 55 | + @Option(names = {"--sources-out"}, required = true, description = "Output path for the generated sources.") |
| 56 | + private Path sourcesOutputPath; |
| 57 | + |
| 58 | + @Option(names = {"--resources-out"}, required = true, description = "Output path for the generated resources.") |
| 59 | + private Path resourcesOutputPath; |
| 60 | + |
| 61 | + |
| 62 | + @Override |
| 63 | + public Integer call() throws Exception { |
| 64 | + TestClassScanner testClassScanner = new TestClassScanner(Set.of(this.testClasspathRoots)); |
| 65 | + Stream<Class<?>> testClasses = testClassScanner.scan(this.packagesToScan); |
| 66 | + |
| 67 | + // TODO Determine if we need to support CLASS output path. |
| 68 | + Path tempDir = Files.createTempDirectory("classes"); |
| 69 | + GeneratedFiles generatedFiles = new FileSystemGeneratedFiles(kind -> switch(kind) { |
| 70 | + case SOURCE -> this.sourcesOutputPath; |
| 71 | + case RESOURCE -> this.resourcesOutputPath; |
| 72 | + case CLASS -> tempDir; |
| 73 | + }); |
| 74 | + TestContextAotGenerator generator = new TestContextAotGenerator(generatedFiles); |
| 75 | + generator.processAheadOfTime(testClasses); |
| 76 | + |
| 77 | + NativeConfigurationWriter writer = new FileNativeConfigurationWriter(this.resourcesOutputPath); |
| 78 | + writer.write(generator.getRuntimeHints()); |
| 79 | + |
| 80 | + return 0; |
| 81 | + } |
| 82 | + |
| 83 | + static int execute(String[] args) throws Exception { |
| 84 | + return new CommandLine(new ProcessTestsAheadOfTimeCommand()).execute(args); |
| 85 | + } |
| 86 | + |
| 87 | + public static void main(String[] args) throws Exception { |
| 88 | + System.exit(execute(args)); |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments