Skip to content

Commit

Permalink
JavaDocs and Refactoring between AndroidUtils and SourceSetsModelBuilder
Browse files Browse the repository at this point in the history
Changes:
- Added JavaDocs to AndroidUtils
- Added SourceSetUtils for common utility methods for SourceSets.
- Removed common method declarations from AndroidUtils and SourceSetsModelBuilder into SourceSetUtils.
  • Loading branch information
Tanish-Ranjan committed Aug 20, 2024
1 parent aca03ac commit 2cd01ff
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.stream.Collectors;

import com.microsoft.java.bs.gradle.plugin.utils.AndroidUtils;
import com.microsoft.java.bs.gradle.plugin.utils.SourceSetUtils;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.Task;
Expand All @@ -38,6 +39,7 @@
import com.microsoft.java.bs.gradle.model.impl.DefaultGradleSourceSet;
import com.microsoft.java.bs.gradle.model.impl.DefaultGradleSourceSets;
import com.microsoft.java.bs.gradle.plugin.dependency.DependencyCollector;
import org.jetbrains.annotations.NotNull;

/**
* The model builder for Gradle source sets.
Expand All @@ -49,7 +51,7 @@ public boolean canBuild(String modelName) {
}

@Override
public Object buildAll(String modelName, Project project) {
public @NotNull Object buildAll(@NotNull String modelName, @NotNull Project project) {
// mapping Gradle source set to our customized model.
List<GradleSourceSet> sourceSets;

Expand Down Expand Up @@ -77,13 +79,14 @@ private DefaultGradleSourceSet getSourceSet(Project project, SourceSet sourceSet
gradleSourceSet.setProjectDir(project.getProjectDir());
gradleSourceSet.setRootDir(project.getRootDir());
gradleSourceSet.setSourceSetName(sourceSet.getName());
String classesTaskName = getFullTaskName(projectPath, sourceSet.getClassesTaskName());
String classesTaskName =
SourceSetUtils.getFullTaskName(projectPath, sourceSet.getClassesTaskName());
gradleSourceSet.setClassesTaskName(classesTaskName);
String cleanTaskName = getFullTaskName(projectPath, "clean");
String cleanTaskName = SourceSetUtils.getFullTaskName(projectPath, "clean");
gradleSourceSet.setCleanTaskName(cleanTaskName);
Set<String> taskNames = new HashSet<>();
gradleSourceSet.setTaskNames(taskNames);
String projectName = stripPathPrefix(projectPath);
String projectName = SourceSetUtils.stripPathPrefix(projectPath);
if (projectName.isEmpty()) {
projectName = project.getName();
}
Expand All @@ -102,7 +105,8 @@ private DefaultGradleSourceSet getSourceSet(Project project, SourceSet sourceSet
LanguageExtension extension = languageModelBuilder.getExtensionFor(project, sourceSet,
gradleSourceSet.getModuleDependencies());
if (extension != null) {
String compileTaskName = getFullTaskName(projectPath, extension.getCompileTaskName());
String compileTaskName =
SourceSetUtils.getFullTaskName(projectPath, extension.getCompileTaskName());
taskNames.add(compileTaskName);

srcDirs.addAll(extension.getSourceDirs());
Expand Down Expand Up @@ -302,31 +306,6 @@ private Collection<SourceSet> getSourceSetContainer(Project project) {
return new LinkedList<>();
}

/**
* Return a project task name - [project path]:[task].
*/
private String getFullTaskName(String modulePath, String taskName) {
if (taskName == null) {
return null;
}
if (taskName.isEmpty()) {
return taskName;
}

if (modulePath == null || modulePath.equals(":")) {
// must be prefixed with ":" as taskPaths are reported back like that in progress messages
return ":" + taskName;
}
return modulePath + ":" + taskName;
}

private String stripPathPrefix(String projectPath) {
if (projectPath.startsWith(":")) {
return projectPath.substring(1);
}
return projectPath;
}

private Set<Object> getArchiveSourcePaths(CopySpec copySpec) {
Set<Object> sourcePaths = new HashSet<>();
if (copySpec instanceof DefaultCopySpec) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import java.util.Set;
import java.util.stream.Collectors;

// WORK IN PROGRESS

/**
* TODO: JavaDoc.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,27 @@
import java.util.Set;

/**
* TODO: JavaDoc.
* Utility class for android related operations.
*/
public class AndroidUtils {

private AndroidUtils() {
}

/**
* TODO: JavaDoc.
* Checks if the given project is an Android project.
*
* @param project Gradle project to check
*/
public static boolean isAndroidProject(Project project) {
return getAndroidExtension(project) != null;
}

/**
* TODO: JavaDoc.
* Extracts build variants from the given Android project and converts
* them into list of GradleSourceSets.
*
* @param project Gradle project for extracting the build variants
*/
@SuppressWarnings("unchecked")
public static List<GradleSourceSet> getBuildVariantsAsGradleSourceSets(Project project) {
Expand Down Expand Up @@ -88,6 +93,12 @@ public static List<GradleSourceSet> getBuildVariantsAsGradleSourceSets(Project p

}

/**
* Returns a GradleSourceSet populated with the given Android build variant data.
*
* @param project Gradle project to populate GradleSourceSet properties
* @param variant Android Build Variant object to populate GradleSourceSet properties
*/
@SuppressWarnings("unchecked")
private static GradleSourceSet convertVariantToGradleSourceSet(Project project, Object variant) {

Expand All @@ -108,17 +119,17 @@ private static GradleSourceSet convertVariantToGradleSourceSet(Project project,

// classes task equivalent in android (assembleRelease)
gradleSourceSet.setClassesTaskName(
getFullTaskName(projectPath, "assemble" + capitalize(variantName))
SourceSetUtils.getFullTaskName(projectPath, "assemble" + capitalize(variantName))
);

gradleSourceSet.setCleanTaskName(getFullTaskName(projectPath, "clean"));
gradleSourceSet.setCleanTaskName(SourceSetUtils.getFullTaskName(projectPath, "clean"));

// compile task in android (compileReleaseJavaWithJavac)
HashSet<String> tasks = new HashSet<>();
tasks.add("compile" + capitalize(variantName) + "JavaWithJavac");
gradleSourceSet.setTaskNames(tasks);

String projectName = stripPathPrefix(projectPath);
String projectName = SourceSetUtils.stripPathPrefix(projectPath);
if (projectName.isEmpty()) {
projectName = project.getName();
}
Expand Down Expand Up @@ -222,11 +233,15 @@ private static GradleSourceSet convertVariantToGradleSourceSet(Project project,
.getMethod("getRClassOutputJar").invoke(processResourcesTask);
RegularFile file = (RegularFile) output.getClass()
.getMethod("get").invoke(output);
classpathFiles.add(file.getAsFile());
File jarFile = file.getAsFile();
if (jarFile.exists()) {
classpathFiles.add(jarFile);
}
}
gradleSourceSet.setCompileClasspath(new LinkedList<>(classpathFiles));

// TODO: Set Archive output dirs
// Archive output dirs (not relevant in case of android build variants)
gradleSourceSet.setArchiveOutputFiles(new HashMap<>());

// has tests
Object unitTestVariant = variant.getClass().getMethod("getUnitTestVariant").invoke(variant);
Expand All @@ -241,6 +256,11 @@ private static GradleSourceSet convertVariantToGradleSourceSet(Project project,

}

/**
* Extracts the AndroidExtension from the given project.
*
* @param project Gradle project to extract the AndroidExtension object.
*/
private static Object getAndroidExtension(Project project) {

Object extension = null;
Expand All @@ -258,6 +278,11 @@ private static Object getAndroidExtension(Project project) {

}

/**
* Returns the AndroidProjectType based on the plugin applied to the given project.
*
* @param project Gradle project to check for plugin and return the corresponding project type.
*/
private static AndroidProjectType getProjectType(Project project) {

if (getAndroidExtension(project) == null) {
Expand All @@ -282,11 +307,20 @@ private static AndroidProjectType getProjectType(Project project) {

}

/**
* Extracts the given property from the given object with {@code getProperty} method.
*
* @param obj object from which the property is to be extracted
* @param propertyName name of the property to be extracted
*/
public static Object getProperty(Object obj, String propertyName)
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
return obj.getClass().getMethod("getProperty", String.class).invoke(obj, propertyName);
}

/**
* Enum class representing different types of Android projects.
*/
private enum AndroidProjectType {
APPLICATION,
LIBRARY,
Expand All @@ -295,31 +329,11 @@ private enum AndroidProjectType {
ANDROID_TEST
}

private static String stripPathPrefix(String projectPath) {
if (projectPath.startsWith(":")) {
return projectPath.substring(1);
}
return projectPath;
}

/**
* Return a project task name - [project path]:[task].
* Returns the given string with its first letter capitalized.
*
* @param s String to capitalize
*/
private static String getFullTaskName(String modulePath, String taskName) {
if (taskName == null) {
return null;
}
if (taskName.isEmpty()) {
return taskName;
}

if (modulePath == null || modulePath.equals(":")) {
// must be prefixed with ":" as taskPaths are reported back like that in progress messages
return ":" + taskName;
}
return modulePath + ":" + taskName;
}

private static String capitalize(String s) {
return s.substring(0, 1).toUpperCase() + s.substring(1);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.microsoft.java.bs.gradle.plugin.utils;

/**
* Utility class for common source set operations.
*/
public class SourceSetUtils {

private SourceSetUtils() {
}

/**
* Returns the gradle project path without the initial {@code :}.
*
* @param projectPath project path to operate upon
*/
public static String stripPathPrefix(String projectPath) {
if (projectPath.startsWith(":")) {
return projectPath.substring(1);
}
return projectPath;
}

/**
* Return a project task name - [project path]:[task].
*
* @param modulePath path of project module
* @param taskName name of gradle task
*/
public static String getFullTaskName(String modulePath, String taskName) {
if (taskName == null) {
return null;
}
if (taskName.isEmpty()) {
return taskName;
}

if (modulePath == null || modulePath.equals(":")) {
// must be prefixed with ":" as taskPaths are reported back like that in progress messages
return ":" + taskName;
}
return modulePath + ":" + taskName;
}

}

0 comments on commit 2cd01ff

Please sign in to comment.