use of com.android.tools.idea.gradle.project.model.GradleModuleModel in project android by JetBrains.
the class AndroidGradleModuleUtils method getContainingModule.
/**
* Given a file and a project, return the Module corresponding to the containing Gradle project for the file. If the file is not
* contained by any project then return null
*/
@Nullable
public static Module getContainingModule(File file, Project project) {
VirtualFile vFile = VfsUtil.findFileByIoFile(file, false);
if (vFile == null) {
return null;
}
Module bestMatch = null;
int bestMatchValue = Integer.MAX_VALUE;
for (Module module : ModuleManager.getInstance(project).getModules()) {
GradleFacet facet = GradleFacet.getInstance(module);
if (facet != null) {
GradleModuleModel gradleModuleModel = facet.getGradleModuleModel();
assert gradleModuleModel != null;
VirtualFile buildFile = gradleModuleModel.getBuildFile();
if (buildFile != null) {
VirtualFile root = buildFile.getParent();
if (VfsUtilCore.isAncestor(root, vFile, true)) {
String relativePath = VfsUtilCore.getRelativePath(vFile, root, '/');
if (relativePath != null) {
int value = Iterables.size(Splitter.on('/').split(relativePath));
if (value < bestMatchValue) {
bestMatch = module;
bestMatchValue = value;
}
}
}
}
}
}
return bestMatch;
}
use of com.android.tools.idea.gradle.project.model.GradleModuleModel in project android by JetBrains.
the class AndroidGradleProjectResolver method populateModuleContentRoots.
@Override
public void populateModuleContentRoots(@NotNull IdeaModule gradleModule, @NotNull DataNode<ModuleData> ideModule) {
ImportedModule importedModule = new ImportedModule(gradleModule);
ideModule.createChild(IMPORTED_MODULE, importedModule);
GradleProject gradleProject = gradleModule.getGradleProject();
GradleScript buildScript = null;
try {
buildScript = gradleProject.getBuildScript();
} catch (UnsupportedOperationException ignore) {
}
if (buildScript == null || !isAndroidGradleProject()) {
nextResolver.populateModuleContentRoots(gradleModule, ideModule);
return;
}
// do not derive module root dir based on *.iml file location
File moduleRootDirPath = new File(toSystemDependentName(ideModule.getData().getLinkedExternalProjectPath()));
AndroidProject androidProject = resolverCtx.getExtraProject(gradleModule, AndroidProject.class);
boolean androidProjectWithoutVariants = false;
String moduleName = gradleModule.getName();
if (androidProject != null) {
Variant selectedVariant = myVariantSelector.findVariantToSelect(androidProject);
if (selectedVariant == null) {
// If an Android project does not have variants, it would be impossible to build. This is a possible but invalid use case.
// For now we are going to treat this case as a Java library module, because everywhere in the IDE (e.g. run configurations,
// editors, test support, variants tool window, project building, etc.) we have the assumption that there is at least one variant
// per Android project, and changing that in the code base is too risky, for very little benefit.
// See https://code.google.com/p/android/issues/detail?id=170722
androidProjectWithoutVariants = true;
} else {
String variantName = selectedVariant.getName();
AndroidModuleModel model = new AndroidModuleModel(moduleName, moduleRootDirPath, androidProject, variantName);
ideModule.createChild(ANDROID_MODEL, model);
}
}
NativeAndroidProject nativeAndroidProject = resolverCtx.getExtraProject(gradleModule, NativeAndroidProject.class);
if (nativeAndroidProject != null) {
NdkModuleModel ndkModuleModel = new NdkModuleModel(moduleName, moduleRootDirPath, nativeAndroidProject);
ideModule.createChild(NDK_MODEL, ndkModuleModel);
}
File gradleSettingsFile = new File(moduleRootDirPath, FN_SETTINGS_GRADLE);
if (gradleSettingsFile.isFile() && androidProject == null && nativeAndroidProject == null) {
// This is just a root folder for a group of Gradle projects. We don't set an IdeaGradleProject so the JPS builder won't try to
// compile it using Gradle. We still need to create the module to display files inside it.
createJavaProject(gradleModule, ideModule, false);
return;
}
BuildScriptClasspathModel buildScriptModel = resolverCtx.getExtraProject(BuildScriptClasspathModel.class);
String gradleVersion = buildScriptModel != null ? buildScriptModel.getGradleVersion() : null;
File buildFilePath = buildScript.getSourceFile();
GradleModuleModel gradleModuleModel = new GradleModuleModel(moduleName, gradleProject, buildFilePath, gradleVersion);
ideModule.createChild(GRADLE_MODULE_MODEL, gradleModuleModel);
if (nativeAndroidProject == null && (androidProject == null || androidProjectWithoutVariants)) {
// This is a Java lib module.
createJavaProject(gradleModule, ideModule, androidProjectWithoutVariants);
}
}
use of com.android.tools.idea.gradle.project.model.GradleModuleModel in project android by JetBrains.
the class GradleProjectSyncData method createFrom.
@Nullable
@VisibleForTesting
static GradleProjectSyncData createFrom(@NotNull Project project) throws IOException {
GradleProjectSyncData data = new GradleProjectSyncData();
File rootDirPath = getBaseDirPath(project);
Module[] modules = ModuleManager.getInstance(project).getModules();
for (Module module : modules) {
GradleFacet gradleFacet = GradleFacet.getInstance(module);
if (gradleFacet != null) {
GradleModuleModel gradleModuleModel = gradleFacet.getGradleModuleModel();
if (gradleModuleModel != null) {
data.addFileChecksum(rootDirPath, gradleModuleModel.getBuildFile());
} else {
LOG.warn(String.format("Trying to create project data from a not initialized project '%1$s'. Abort.", project.getName()));
return null;
}
}
if (isGradleProjectModule(module)) {
data.addFileChecksum(rootDirPath, getGradleBuildFile(module));
data.addFileChecksum(rootDirPath, getGradleSettingsFile(rootDirPath));
data.addFileChecksum(rootDirPath, new File(rootDirPath, FN_GRADLE_PROPERTIES));
data.addFileChecksum(rootDirPath, new File(rootDirPath, FN_LOCAL_PROPERTIES));
data.addFileChecksum(rootDirPath, getGradleUserSettingsFile());
}
NdkModuleModel ndkModuleModel = NdkModuleModel.get(module);
if (ndkModuleModel != null) {
for (File externalBuildFile : ndkModuleModel.getAndroidProject().getBuildFiles()) {
data.addFileChecksum(rootDirPath, externalBuildFile);
}
}
}
GradleSyncState syncState = GradleSyncState.getInstance(project);
data.myLastGradleSyncTimestamp = syncState.getSummary().getSyncTimestamp();
return data;
}
use of com.android.tools.idea.gradle.project.model.GradleModuleModel in project android by JetBrains.
the class MakeBeforeRunTaskProvider method createAvailableTasks.
private List<String> createAvailableTasks() {
ModuleManager moduleManager = ModuleManager.getInstance(myProject);
List<String> gradleTasks = Lists.newArrayList();
for (Module module : moduleManager.getModules()) {
GradleFacet facet = GradleFacet.getInstance(module);
if (facet == null) {
continue;
}
GradleModuleModel gradleModuleModel = facet.getGradleModuleModel();
if (gradleModuleModel == null) {
continue;
}
gradleTasks.addAll(gradleModuleModel.getTaskNames());
}
return gradleTasks;
}
use of com.android.tools.idea.gradle.project.model.GradleModuleModel in project android by JetBrains.
the class WrapArchiveOptionsStep method getContainingModule.
@Nullable
private Module getContainingModule(@NotNull VirtualFile file) {
Module bestMatch = null;
if (myProject != null) {
int bestMatchValue = Integer.MAX_VALUE;
for (Module module : ModuleManager.getInstance(myProject).getModules()) {
GradleFacet facet = GradleFacet.getInstance(module);
if (facet != null) {
GradleModuleModel gradleModuleModel = facet.getGradleModuleModel();
assert gradleModuleModel != null;
VirtualFile buildFile = gradleModuleModel.getBuildFile();
if (buildFile != null) {
VirtualFile root = buildFile.getParent();
if (VfsUtilCore.isAncestor(root, file, true)) {
String relativePath = VfsUtilCore.getRelativePath(file, root, '/');
if (relativePath != null) {
int value = Iterables.size(Splitter.on('/').split(relativePath));
if (value < bestMatchValue) {
bestMatch = module;
}
}
}
}
}
}
}
return bestMatch;
}
Aggregations