protected ProjectDefinition loadChildProject(ProjectDefinition parentProject, Map<String, String> moduleProps, String moduleId) { final File baseDir; if (moduleProps.containsKey(PROPERTY_PROJECT_BASEDIR)) { baseDir = resolvePath(parentProject.getBaseDir(), moduleProps.get(PROPERTY_PROJECT_BASEDIR)); setProjectBaseDir(baseDir, moduleProps, moduleId); } else { baseDir = new File(parentProject.getBaseDir(), moduleId); setProjectBaseDir(baseDir, moduleProps, moduleId); } setModuleKeyAndNameIfNotDefined(moduleProps, moduleId, parentProject.getKey()); // and finish checkMandatoryProperties(moduleProps, MANDATORY_PROPERTIES_FOR_CHILD); validateDirectories(moduleProps, baseDir, moduleId); mergeParentProperties(moduleProps, parentProject.properties()); return createModuleDefinition(moduleProps, parentProject); }
private void defineChildren(ProjectDefinition parentProject, Map<String, Map<String, String>> propertiesByModuleIdPath, String parentModuleIdPath) { Map<String, String> parentProps = parentProject.properties(); if (parentProps.containsKey(PROPERTY_MODULES)) { for (String moduleId : getListFromProperty(parentProps, PROPERTY_MODULES)) { String moduleIdPath = parentModuleIdPath.isEmpty() ? moduleId : (parentModuleIdPath + "." + moduleId); Map<String, String> moduleProps = propertiesByModuleIdPath.get(moduleIdPath); ProjectDefinition childProject = loadChildProject(parentProject, moduleProps, moduleId); // check the uniqueness of the child key checkUniquenessOfChildKey(childProject, parentProject); // the child project may have children as well defineChildren(childProject, propertiesByModuleIdPath, moduleIdPath); // and finally add this child project to its parent parentProject.addSubProject(childProject); } } }
public ProjectReactor provide(ProjectReactorBuilder builder) { if (reactor == null) { reactor = builder.execute(); } return reactor; } }
protected static void validateDirectories(Map<String, String> props, File baseDir, String projectId) { if (!props.containsKey(PROPERTY_MODULES)) { // SONARPLUGINS-2285 Not an aggregator project so we can validate that paths are correct if defined // Check sonar.tests String[] testPaths = getListFromProperty(props, PROPERTY_TESTS); checkExistenceOfPaths(projectId, baseDir, testPaths, PROPERTY_TESTS); } }
private static void logMissingSourcesAndTests(ProjectDefinition project) { Map<String, String> properties = project.properties(); File baseDir = project.getBaseDir(); logMissingPaths("source", baseDir, getListFromProperty(properties, PROPERTY_SOURCES)); logMissingPaths("test", baseDir, getListFromProperty(properties, PROPERTY_TESTS)); }
protected ProjectDefinition createModuleDefinition(Map<String, String> moduleProperties, @Nullable ProjectDefinition parent) { if (moduleProperties.containsKey(PROPERTY_MODULES)) { checkMandatoryProperties(moduleProperties, MANDATORY_PROPERTIES_FOR_MULTIMODULE_PROJECT); } else { checkMandatoryProperties(moduleProperties, MANDATORY_PROPERTIES_FOR_SIMPLE_PROJECT); } File baseDir = new File(moduleProperties.get(PROPERTY_PROJECT_BASEDIR)); final String projectKey = moduleProperties.get(CoreProperties.PROJECT_KEY_PROPERTY); File workDir; if (parent == null) { validateDirectories(moduleProperties, baseDir, projectKey); workDir = initRootProjectWorkDir(baseDir, moduleProperties); } else { workDir = initModuleWorkDir(baseDir, moduleProperties); checkUnsupportedIssueExclusions(moduleProperties); } return ProjectDefinition.create().setProperties(moduleProperties) .setBaseDir(baseDir) .setWorkDir(workDir) .setBuildDir(initModuleBuildDir(baseDir, moduleProperties)); }
private static void logMissingPaths(String label, File baseDir, String[] paths) { for (String path : paths) { File file = resolvePath(baseDir, path); if (!file.exists()) { LOG.debug("Path '{}' does not exist, will not be used as {}", file, label); } } }
String[] moduleIds = getListFromProperty(currentModuleProperties, PROPERTY_MODULES); extractPropertiesByModule(propertiesByModuleIdPath, moduleId, subModuleIdPath, currentModuleProperties);
@Test public void shouldInitRootWorkDirWithCustomAbsoluteFolder() { Map<String, String> props = Maps.<String, String>newHashMap(); props.put("sonar.working.directory", new File("src").getAbsolutePath()); ProjectReactorBuilder builder = new ProjectReactorBuilder(new ScannerProperties(props), mock(AnalysisWarnings.class)); File baseDir = new File("target/tmp/baseDir"); File workDir = builder.initRootProjectWorkDir(baseDir, props); assertThat(workDir).isEqualTo(new File("src").getAbsoluteFile()); }
@Test public void shouldGetList() { Map<String, String> props = new HashMap<>(); props.put("prop", " foo ,, bar , toto,tutu"); assertThat(ProjectReactorBuilder.getListFromProperty(props, "prop")).containsOnly("foo", "bar", "toto", "tutu"); }
private ProjectDefinition loadProjectDefinition(String projectFolder) { Map<String, String> props = loadProps(projectFolder); ScannerProperties bootstrapProps = new ScannerProperties(props); ProjectReactor projectReactor = new ProjectReactorBuilder(bootstrapProps, mock(AnalysisWarnings.class)).execute(); return projectReactor.getRoot(); }
@Test public void shouldNotFailIfMandatoryPropertiesArePresent() { Map<String, String> props = new HashMap<>(); props.put("foo1", "bla"); props.put("foo4", "bla"); ProjectReactorBuilder.checkMandatoryProperties(props, new String[] {"foo1"}); // No exception should be thrown }
@Test public void shouldSetModuleKeyIfNotPresent() { Map<String, String> props = new HashMap<>(); props.put("sonar.projectVersion", "1.0"); // should be set ProjectReactorBuilder.setModuleKeyAndNameIfNotDefined(props, "foo", "parent"); assertThat(props.get("sonar.moduleKey")).isEqualTo("parent:foo"); assertThat(props.get("sonar.projectName")).isEqualTo("foo"); // but not this 2nd time ProjectReactorBuilder.setModuleKeyAndNameIfNotDefined(props, "bar", "parent"); assertThat(props.get("sonar.moduleKey")).isEqualTo("parent:foo"); assertThat(props.get("sonar.projectName")).isEqualTo("foo"); }
@Test public void shouldMergeParentProperties() { // Use a random value to avoid VM optimization that would create constant String and make s1 and s2 the same object int i = (int) Math.random() * 10; String s1 = "value" + i; String s2 = "value" + i; Map<String, String> parentProps = new HashMap<>(); parentProps.put("toBeMergeProps", "fooParent"); parentProps.put("existingChildProp", "barParent"); parentProps.put("duplicatedProp", s1); parentProps.put("sonar.projectDescription", "Desc from Parent"); Map<String, String> childProps = new HashMap<>(); childProps.put("existingChildProp", "barChild"); childProps.put("otherProp", "tutuChild"); childProps.put("duplicatedProp", s2); ProjectReactorBuilder.mergeParentProperties(childProps, parentProps); assertThat(childProps).hasSize(4); assertThat(childProps.get("toBeMergeProps")).isEqualTo("fooParent"); assertThat(childProps.get("existingChildProp")).isEqualTo("barChild"); assertThat(childProps.get("otherProp")).isEqualTo("tutuChild"); assertThat(childProps.get("sonar.projectDescription")).isNull(); assertThat(childProps.get("duplicatedProp")).isSameAs(parentProps.get("duplicatedProp")); }
@Test public void shouldFailIf2ModulesWithSameKey() { Map<String, String> props = new HashMap<>(); props.put("sonar.projectKey", "root"); ProjectDefinition root = ProjectDefinition.create().setProperties(props); Map<String, String> props1 = new HashMap<>(); props1.put("sonar.projectKey", "mod1"); root.addSubProject(ProjectDefinition.create().setProperties(props1)); // Check uniqueness of a new module: OK Map<String, String> props2 = new HashMap<>(); props2.put("sonar.projectKey", "mod2"); ProjectDefinition mod2 = ProjectDefinition.create().setProperties(props2); ProjectReactorBuilder.checkUniquenessOfChildKey(mod2, root); // Now, add it and check again root.addSubProject(mod2); thrown.expect(MessageException.class); thrown.expectMessage("Project 'root' can't have 2 modules with the following key: mod2"); ProjectReactorBuilder.checkUniquenessOfChildKey(mod2, root); }
protected ProjectDefinition createModuleDefinition(Map<String, String> moduleProperties, @Nullable ProjectDefinition parent) { if (moduleProperties.containsKey(PROPERTY_MODULES)) { checkMandatoryProperties(moduleProperties, MANDATORY_PROPERTIES_FOR_MULTIMODULE_PROJECT); } else { checkMandatoryProperties(moduleProperties, MANDATORY_PROPERTIES_FOR_SIMPLE_PROJECT); } File baseDir = new File(moduleProperties.get(PROPERTY_PROJECT_BASEDIR)); final String projectKey = moduleProperties.get(CoreProperties.PROJECT_KEY_PROPERTY); File workDir; if (parent == null) { validateDirectories(moduleProperties, baseDir, projectKey); workDir = initRootProjectWorkDir(baseDir, moduleProperties); } else { workDir = initModuleWorkDir(baseDir, moduleProperties); checkUnsupportedIssueExclusions(moduleProperties); } return ProjectDefinition.create().setProperties(moduleProperties) .setBaseDir(baseDir) .setWorkDir(workDir) .setBuildDir(initModuleBuildDir(baseDir, moduleProperties)); }
@VisibleForTesting protected static void cleanAndCheckModuleProperties(ProjectDefinition project) { Map<String, String> properties = project.properties(); // We need to check the existence of source directories String[] sourcePaths = getListFromProperty(properties, PROPERTY_SOURCES); checkExistenceOfPaths(project.getKey(), project.getBaseDir(), sourcePaths, PROPERTY_SOURCES); }
@VisibleForTesting protected static void checkExistenceOfPaths(String moduleRef, File baseDir, String[] paths, String propName) { for (String path : paths) { File sourceFolder = resolvePath(baseDir, path); if (!sourceFolder.exists()) { LOG.error(MessageFormat.format(INVALID_VALUE_OF_X_FOR_Y, propName, moduleRef)); throw MessageException.of("The folder '" + path + "' does not exist for '" + moduleRef + "' (base directory = " + baseDir.getAbsolutePath() + ")"); } } }
private static void logMissingSourcesAndTests(ProjectDefinition project) { Map<String, String> properties = project.properties(); File baseDir = project.getBaseDir(); logMissingPaths("source", baseDir, getListFromProperty(properties, PROPERTY_SOURCES)); logMissingPaths("test", baseDir, getListFromProperty(properties, PROPERTY_TESTS)); }
String[] moduleIds = getListFromProperty(currentModuleProperties, PROPERTY_MODULES); extractPropertiesByModule(propertiesByModuleIdPath, moduleId, subModuleIdPath, currentModuleProperties);