Skip to content

Commit

Permalink
dbeaver/dbeaver-infra#184 exclude target of repositories & mark resou…
Browse files Browse the repository at this point in the history
…rces (#35)

Additionally checkstyle improvement were made
Closes dbeaver/dbeaver-infra#184, dbeaver/dbeaver-infra#162
  • Loading branch information
Destrolaric authored Dec 18, 2024
1 parent 7382f98 commit 4d84325
Showing 1 changed file with 131 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import com.dbeaver.osgi.dependency.processing.*;
import com.dbeaver.osgi.dependency.processing.inter.IImportListener;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import com.dbeaver.osgi.dependency.processing.p2.P2RepositoryManager;
import com.dbeaver.osgi.dependency.processing.p2.repository.RemoteP2BundleInfo;
import org.jkiss.tools.rcplaunchconfig.producers.DevPropertiesProducer;
import com.dbeaver.osgi.dependency.processing.util.FileUtils;
import com.dbeaver.osgi.dependency.processing.util.Version;
import com.dbeaver.osgi.dependency.processing.util.VersionRange;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.tools.rcplaunchconfig.producers.DevPropertiesProducer;
import org.jkiss.utils.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -24,13 +24,15 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;


// TODO this class should be decomposed in the future
public class IMLConfigurationProducer implements IImportListener {

public static final IMLConfigurationProducer INSTANCE = new IMLConfigurationProducer();

private static final Logger log = LoggerFactory.getLogger(IMLConfigurationProducer.class);
private static final String TEST_FOLDER = "src/test/java";
private static final String[] RESOURCE_FOLDERS = new String[]{"src/main/resources", "src/test/resources"};


private static final Map<String, String> eclipseToIdeaJavaMappings = Map.of(
"JavaSE-11", "JDK_11",
Expand All @@ -43,7 +45,7 @@ public class IMLConfigurationProducer implements IImportListener {
private final Set<Path> rootModules = new LinkedHashSet<>();
private final Set<ModuleInfo> modules = new LinkedHashSet<>();

Set<Path> createdModules = new LinkedHashSet<>();
private final Set<Path> createdModules = new LinkedHashSet<>();

private final Lock lock = new ReentrantLock();

Expand All @@ -67,20 +69,24 @@ public void generateIMLFiles(@NotNull Result result, @Nullable Path productPath)
}
if (DevPropertiesProducer.isBundleAcceptable(bundleInfo.getBundleName())) {
String moduleConfig = generateIMLBundleConfig(bundleInfo, result);
modules.add(bundleInfo);
Path imlFilePath = getImlModulePath(bundleInfo);
createConfigFile(imlFilePath, moduleConfig);
if (moduleConfig != null) {
modules.add(bundleInfo);
Path imlFilePath = getImlModulePath(bundleInfo);
createConfigFile(imlFilePath, moduleConfig);
}
}
}
}

// Features
for (FeatureInfo featureInfo : result.getResolvedFeatures().values()) {
if (DevPropertiesProducer.isBundleAcceptable(featureInfo.getFeatureName())) {
String moduleConfig = generateIMLFeatureConfig(featureInfo, result);
modules.add(featureInfo);
Path imlFilePath = getImlModulePath(featureInfo);
createConfigFile(imlFilePath, moduleConfig);
String moduleConfig = generateIMLFeatureConfig(featureInfo);
if (moduleConfig != null) {
modules.add(featureInfo);
Path imlFilePath = getImlModulePath(featureInfo);
createConfigFile(imlFilePath, moduleConfig);
}
}
}

Expand All @@ -93,6 +99,12 @@ public void generateIMLFiles(@NotNull Result result, @Nullable Path productPath)
rootModules.addAll(generateRootModules());
}

/**
* Generates workspace configuration files
* and transfers existing workspace files from designated folder
*
* @throws IOException issue during configuration creation/transfer
*/
public void generateImplConfiguration() throws IOException {
String modulesConfig = generateModulesConfig();
createConfigFile(getImplModuleConfigPath(), modulesConfig);
Expand Down Expand Up @@ -136,7 +148,9 @@ private void createRunConfiguration() throws IOException {
private String generateLaunchConfig(Path productPath, Result result) {
StringBuilder config = new StringBuilder();
config.append("<component name=\"ProjectRunConfigurationManager\">\n");
config.append(String.format(" <configuration default=\"false\" name=\"Run " + result.getProductName() + " \" type=\"Application\" factoryName=\"Application\">\n"));
config.append(
" <configuration default=\"false\" name=\"Run %s \" type=\"Application\" factoryName=\"Application\">\n"
.formatted(result.getProductName()));
config.append(" <option name=\"ALTERNATIVE_JRE_PATH\" value=\"17\" />\n");
config.append(" <option name=\"ALTERNATIVE_JRE_PATH_ENABLED\" value=\"true\" />\n");
config.append(" <option name=\"MAIN_CLASS_NAME\" value=\"org.jkiss.dbeaver.launcher.DBeaverLauncher\" />\n");
Expand Down Expand Up @@ -201,7 +215,10 @@ private void buildProgramParameters(StringBuilder config, Path productPath, Resu
}

private Set<Path> generateRootModules() throws IOException {
Set<Path> presentModules = PathsManager.INSTANCE.getModulesRoots().stream().filter(it -> it.toFile().exists()).collect(Collectors.toSet());
Set<Path> presentModules = PathsManager.INSTANCE.getModulesRoots()
.stream()
.filter(it -> it.toFile().exists())
.collect(Collectors.toSet());
Set<Path> rootModules = new LinkedHashSet<>();
Path imlModuleRoot = PathsManager.INSTANCE.getImlModulesPath();
for (Path presentModule : presentModules) {
Expand All @@ -216,7 +233,7 @@ private Set<Path> generateRootModules() throws IOException {
return rootModules;
}

public static boolean isMacOS() {
private static boolean isMacOS() {
String osName = System.getProperty("os.name").toLowerCase();
return osName.contains("mac");
}
Expand All @@ -233,7 +250,7 @@ private String generateImlRepositoryRootModule(@NotNull Path imlRoot) {
"</module>";
}

private String generateRootModule(@NotNull Path presentModule) {
private String generateRootModule(@NotNull Path presentModule) throws IOException {
StringBuilder productExcludes = new StringBuilder();
Map<Path, String> productsPathsAndWorkDirs = PathsManager.INSTANCE.getProductsPathsAndWorkDirs();
for (Path productConfigPath : productsPathsAndWorkDirs.keySet()) {
Expand All @@ -252,6 +269,24 @@ private String generateRootModule(@NotNull Path presentModule) {
.append("\"/>\n");
}
}
for (Path additionalRepositoriesPath : PathsManager.INSTANCE.getAdditionalRepositoriesPaths()) {
try (Stream<Path> stream = Files.walk(additionalRepositoriesPath, 2)) {
List<Path> categoryXMLS = stream.filter(Files::isRegularFile)
.filter(path -> path.getFileName().toString().equals("category.xml"))
.toList();
for (Path categoryXML : categoryXMLS) {
if (categoryXML.getParent().startsWith(presentModule)) {
productExcludes.append(" <excludeFolder url=\"")
.append(getFormattedRelativePath(presentModule, false, false))
.append("/").append(presentModule.relativize(categoryXML.getParent())
.resolve("target")
.toString()
.replace("\\", "/"))
.append("\"/>\n");
}
}
}
}
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<module type=\"JAVA_MODULE\" version=\"4\">\n" +
" <component name=\"NewModuleRootManager\">\n" +
Expand Down Expand Up @@ -329,8 +364,11 @@ private void processAdditionalIMLModules(StringBuilder builder) throws IOExcepti
}
}

private void processModulePath(StringBuilder builder, Path sourceFile, Path imlModulesPath)
throws IOException {
private void processModulePath(
StringBuilder builder,
Path sourceFile,
Path imlModulesPath
) throws IOException {
if (sourceFile.toFile().isDirectory()) {
try (Stream<Path> walk = Files.walk(sourceFile)) {
for (Path path : walk.toList()) {
Expand All @@ -343,14 +381,16 @@ private void processModulePath(StringBuilder builder, Path sourceFile, Path imlM
StandardCopyOption.REPLACE_EXISTING
);
} catch (DirectoryNotEmptyException | FileAlreadyExistsException ignore) {

// No need to bother with that we replace all data
} catch (IOException e) {
log.error("Error transferring data", e);
}
if (!path.toFile().isDirectory()) {
builder.append(" <module fileurl=\"file://$PROJECT_DIR$/")
.append(imlModulesPath.relativize(destination).toString().replace("\\", "/")).append("\"")
.append(" filepath=\"$PROJECT_DIR$/").append(imlModulesPath.relativize(destination).toString().replace("\\", "/")).append("\"/>\n");
.append(imlModulesPath.relativize(destination).toString().replace("\\", "/"))
.append("\"")
.append(" filepath=\"$PROJECT_DIR$/")
.append(imlModulesPath.relativize(destination).toString().replace("\\", "/")).append("\"/>\n");
}
}
}
Expand All @@ -370,14 +410,23 @@ private void appendLibraryInfo(
@NotNull Set<Pair<String, Version>> resolvedBundles,
boolean isLibrary) {
resolvedBundles.add(new Pair<>(bundleInfo.getBundleName(), new Version(bundleInfo.getBundleVersion())));
if (bundleInfo.getPath() == null) {
log.error("Error appending library info to non existing module %s, should not happen"
.formatted(bundleInfo.getBundleName()));
return;
}
if (bundleInfo.getPath().toFile().isDirectory()) {
List<String> classpathLibs = bundleInfo.getClasspathLibs();
if (!classpathLibs.isEmpty()) {
for (String classpathLib : classpathLibs) {
appendClasspathLib(builder, bundleInfo, classpathLib, isLibrary);
}
} else {
builder.append(" <root url=\"").append(getFormattedRelativePath(bundleInfo.getPath(), false, isLibrary)).append("\"/>\n");
builder.append(" <root url=\"").append(getFormattedRelativePath(
bundleInfo.getPath(),
false,
isLibrary
)).append("\"/>\n");
}
} else {
builder.append(" <root url=\"").append(getFormattedRelativePath(bundleInfo.getPath(), true, isLibrary)).append("\"/>\n");
Expand All @@ -388,12 +437,18 @@ private void appendLibraryInfo(
if (!bundleInfo.getReexportedBundles().isEmpty()) {
for (String reexportedBundle : bundleInfo.getReexportedBundles()) {
Set<BundleInfo> bundlesByName = result.getBundlesByName(reexportedBundle);
Optional<Pair<String, VersionRange>> bundleRequirements = bundleInfo.getRequireBundles().stream().filter(it -> it.getFirst().equals(reexportedBundle)).findFirst();
Optional<Pair<String, VersionRange>> bundleRequirements = bundleInfo.getRequireBundles()
.stream()
.filter(it -> it.getFirst().equals(reexportedBundle))
.findFirst();
if (bundleRequirements.isEmpty() || bundlesByName == null) {
log.warn("Missing reexported bundle " + reexportedBundle);
continue;
}
BundleInfo suitableBundle = bundlesByName.stream().filter(it -> VersionRange.isVersionsCompatible(bundleRequirements.get().getSecond(), new Version(it.getBundleVersion()))).findFirst().orElseThrow();
BundleInfo suitableBundle = bundlesByName.stream().filter(it -> VersionRange.isVersionsCompatible(
bundleRequirements.get().getSecond(),
new Version(it.getBundleVersion())
)).findFirst().orElseThrow();
appendLibraryInfo(builder, suitableBundle, result, resolvedBundles, isLibrary);
}
for (Pair<String, VersionRange> importPackage : bundleInfo.getImportPackages()) {
Expand Down Expand Up @@ -440,6 +495,10 @@ private void appendClasspathLib(@NotNull StringBuilder builder,
@NotNull BundleInfo bundleInfo,
@NotNull String classpathLib,
boolean isLibrary) {
if (bundleInfo.getPath() == null) {
log.error("Error appending classpath to non-existing module, should not happen");
return;
}
builder.append(" <root url=\"")
.append(getFormattedRelativePath(bundleInfo.getPath().resolve(classpathLib), true, isLibrary))
.append("\"/>\n");
Expand Down Expand Up @@ -514,6 +573,9 @@ private String generateIMLBundleConfig(@NotNull BundleInfo bundleInfo, @NotNull
if (bundleInfo.getPath() != null) {
appendTestSources(bundleInfo.getPath(), builder);
}
if (bundleInfo.getPath() != null) {
appendResourceSources(bundleInfo.getPath(), builder);
}
builder.append(" </content>").append("\n");
}
builder.append(" <orderEntry type=\"inheritedJdk\" />").append("\n");
Expand Down Expand Up @@ -603,6 +665,17 @@ private String generateIMLBundleConfig(@NotNull BundleInfo bundleInfo, @NotNull
return builder.toString();
}

private void appendResourceSources(Path path, StringBuilder builder) {
for (String resourceFolder : RESOURCE_FOLDERS) {
Path testFolder = path.resolve(resourceFolder);
if (testFolder.toFile().exists()) {
builder.append(" <sourceFolder url=\"")
.append(getFormattedRelativePath(path.resolve(testFolder), false, false))
.append("\" type=\"java-resource\"/>").append("\n");
}
}
}

private void appendTestSources(@NotNull Path bundlePath, StringBuilder builder) {
Path testFolder = bundlePath.resolve(TEST_FOLDER);
if (testFolder.toFile().exists()) {
Expand All @@ -613,8 +686,7 @@ private void appendTestSources(@NotNull Path bundlePath, StringBuilder builder)
}

@Nullable
private String generateIMLFeatureConfig(@NotNull FeatureInfo featureInfo, @NotNull Result result)
throws IOException {
private String generateIMLFeatureConfig(@NotNull FeatureInfo featureInfo) {
if (featureInfo.getFeatureXmlFile() == null) {
log.warn("Feature doesn't contain any data");
return null;
Expand Down Expand Up @@ -657,7 +729,13 @@ private void appendBundleInfo(
}
return;
}
BundleInfo bundle = bundleByName.stream().filter(it -> VersionRange.isVersionsCompatible(requireBundle.getSecond(), new Version(it.getBundleVersion()))).findFirst().orElseThrow();
BundleInfo bundle = bundleByName.stream()
.filter(it -> VersionRange.isVersionsCompatible(
requireBundle.getSecond(),
new Version(it.getBundleVersion())
))
.findFirst()
.orElseThrow();
if (resolvedBundles.contains(new Pair<>(requireBundle.getFirst(), new Version(bundle.getBundleVersion())))) {
return;
}
Expand All @@ -667,7 +745,13 @@ private void appendBundleInfo(
builder.append(" <orderEntry type = \"module\" module-name=\"").append(requireBundle.getFirst())
.append(isExported ? "\" exported=\"\"" : "\"").append("/>").append("\n");
} else {
addModuleLibrary(new Pair<>(requireBundle.getFirst(), new Version(bundle.getBundleVersion())), builder, result, resolvedBundles, isExported);
addModuleLibrary(
new Pair<>(requireBundle.getFirst(), new Version(bundle.getBundleVersion())),
builder,
result,
resolvedBundles,
isExported
);
}
}

Expand All @@ -685,9 +769,12 @@ private void addModuleLibrary(
}
BundleInfo bundleByName;
if (requiredLibrary.getSecond() != null) {
bundleByName = bundles.stream().filter(it -> new Version(it.getBundleVersion()).compareTo(requiredLibrary.getSecond()) == 0).findFirst().orElseThrow();
bundleByName = bundles.stream()
.filter(it -> new Version(it.getBundleVersion()).compareTo(requiredLibrary.getSecond()) == 0)
.findFirst()
.orElseThrow();
} else {
bundleByName = bundles.stream().findFirst().get();
bundleByName = bundles.stream().findFirst().orElseThrow();
}
if (bundleByName.getPath() == null) {
log.warn("Missing reexported bundle " + requiredLibrary);
Expand Down Expand Up @@ -729,7 +816,14 @@ private String generateXMLLibraryConfig(@NotNull BundleInfo bundleInfo, Result r
for (Pair<String, Version> libraryObject : libraryObjects) {
Set<BundleInfo> sources = result.getBundlesByName(libraryObject.getFirst() + ".source");
if (sources != null) {
appendLibraryInfo(builder, sources.stream().filter(it -> it.getBundleVersion().equals(bundleInfo.getBundleVersion())).findFirst().get(), result, libraryObjects, true);
appendLibraryInfo(
builder,
sources.stream().filter(it -> it.getBundleVersion()
.equals(bundleInfo.getBundleVersion())).findFirst().orElseThrow(),
result,
libraryObjects,
true
);
}
}
builder.append(" </SOURCES>").append("\n");
Expand All @@ -750,8 +844,11 @@ private void endLibraryEntry(
builder.append(" <JAVADOC />\n");
builder.append(" <SOURCES>\n");
for (Pair<String, Version> resolvedBundle : resolvedBundles) {
Collection<RemoteP2BundleInfo> sources = P2RepositoryManager.INSTANCE.getLookupCache().getRemoteBundlesByName(resolvedBundle.getFirst() + ".source");
Optional<RemoteP2BundleInfo> source = sources.stream().filter(it -> new Version(it.getBundleVersion()).compareTo(resolvedBundle.getSecond()) == 0).findFirst();
Collection<RemoteP2BundleInfo> sources = P2RepositoryManager.INSTANCE.getLookupCache()
.getRemoteBundlesByName(resolvedBundle.getFirst() + ".source");
Optional<RemoteP2BundleInfo> source = sources.stream()
.filter(it -> new Version(it.getBundleVersion()).compareTo(resolvedBundle.getSecond()) == 0)
.findFirst();
if (source.isPresent()) {
source.get().resolveBundle();
appendLibraryInfo(builder, source.get(), result, new LinkedHashSet<>(), false);
Expand Down Expand Up @@ -792,6 +889,7 @@ private Path getImplModuleConfigPath() {
private Path getIdeaConfigsPath() {
return PathsManager.INSTANCE.getImlModulesPath().resolve(".idea/");
}

private Path getLibraryConfigPath() {
return getIdeaConfigsPath().resolve("libraries/");
}
Expand Down

0 comments on commit 4d84325

Please sign in to comment.