Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Preview fix #1

Open
wants to merge 16 commits into
base: base-v2023.03.21-aswb-stable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions aspect/intellij_info_impl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,19 @@ def get_source_jars(output):
return [output.source_jar]
return []

def library_artifact(java_output):
def get_rule_source_jar(rule):
if hasattr(rule.attr, "srcjar"):
if rule.attr.srcjar != None:
return rule.attr.srcjar.files.to_list()
return []

def library_artifact(java_output, rule=None):
"""Creates a LibraryArtifact representing a given java_output."""
if java_output == None or java_output.class_jar == None:
return None
src_jars = get_source_jars(java_output)
if rule and not src_jars:
src_jars = get_rule_source_jar(rule)
return struct_omit_none(
interface_jar = artifact_location(java_output.ijar),
jar = artifact_location(java_output.class_jar),
Expand Down Expand Up @@ -591,7 +599,7 @@ def collect_java_info(target, ctx, semantics, ide_info, ide_info_file, output_gr

ide_info_files = []
sources = sources_from_target(ctx)
jars = [library_artifact(output) for output in java_outputs]
jars = [library_artifact(output, ctx.rule) for output in java_outputs]
class_jars = [output.class_jar for output in java_outputs if output and output.class_jar]
output_jars = [jar for output in java_outputs for jar in jars_from_output(output)]
resolve_files = output_jars
Expand Down
1 change: 1 addition & 0 deletions aswb/src/META-INF/aswb.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
</extensionPoints>

<extensions defaultExtensionNs="com.google.idea.blaze">
<MavenArtifactLocator implementation="com.google.idea.blaze.android.projectsystem.DefaultMavenArtifactLocator"/>
<LintCollector implementation="com.google.idea.blaze.android.libraries.AndroidLintCollector"/>
<SyncPlugin implementation="com.google.idea.blaze.android.sync.BlazeAndroidSyncPlugin"/>
<SyncListener implementation="com.google.idea.blaze.android.sync.BlazeAndroidSyncListener"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
public class ExperimentComposeStatusProvider implements ComposeStatusProvider {
private static final BoolExperiment composeEnabled =
new BoolExperiment("aswb.force.enable.compose", false);
new BoolExperiment("aswb.force.enable.compose", true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this? I wasn't actually sure. Someone in the Bazel Slack suggested it, but looking through the codebase, it didn't seem totally necessary. This seemed more of a "last resort".


@Override
public boolean composeEnabled(Project project) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package com.google.idea.blaze.android.projectsystem;

import static com.google.common.collect.ImmutableList.toImmutableList;

import com.android.ide.common.util.PathString;
import com.android.projectmodel.ExternalAndroidLibrary;
import com.android.projectmodel.ExternalLibraryImpl;
Expand All @@ -26,6 +24,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.idea.blaze.android.libraries.UnpackedAars;
import com.google.idea.blaze.android.manifest.ManifestParser;
import com.google.idea.blaze.android.sync.model.AarLibrary;
import com.google.idea.blaze.android.sync.model.AndroidResourceModuleRegistry;
import com.google.idea.blaze.android.sync.model.BlazeAndroidSyncData;
Expand All @@ -44,15 +43,20 @@
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Collections;
import java.util.stream.Stream;
import org.jetbrains.annotations.Nullable;

import static com.google.common.collect.ImmutableList.toImmutableList;

/** Blaze implementation of {@link AndroidModuleSystem}. */
public class BlazeModuleSystem extends BlazeModuleSystemBase {
Expand Down Expand Up @@ -166,14 +170,26 @@ static ExternalAndroidLibrary toExternalLibrary(
library.aarArtifact));
return null;
}

File resFolder = unpackedAars.getResourceDirectory(decoder, library);
PathString resFolderPathString = resFolder == null ? null : new PathString(resFolder);
PathString manifest = resFolderPathString == null
? null
: resFolderPathString.getParentOrRoot().resolve("AndroidManifest.xml");
String resourcePackage = library.resourcePackage;
if ((resourcePackage == null || resourcePackage == "") && manifest != null) {
try (InputStream is = new FileInputStream(manifest.toFile())) {
ManifestParser.ParsedManifest parsedManifest = ManifestParser.parseManifestFromInputStream(is);
resourcePackage = parsedManifest.packageName;
} catch (IOException ioe) {
logger.warn(
String.format("Could not parse package from manifest in library %s", aarFile.getName()));
return null;
}
}
return new ExternalLibraryImpl(library.key.toString())
.withLocation(new PathString(aarFile))
.withManifestFile(
resFolderPathString == null
? null
: resFolderPathString.getParentOrRoot().resolve("AndroidManifest.xml"))
.withManifestFile(manifest)
.withResFolder(
resFolderPathString == null
? null
Expand All @@ -182,7 +198,7 @@ static ExternalAndroidLibrary toExternalLibrary(
resFolderPathString == null
? null
: resFolderPathString.getParentOrRoot().resolve("R.txt"))
.withPackageName(library.resourcePackage);
.withPackageName(resourcePackage);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@
import com.intellij.psi.PsiElement;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.ProjectScope;
import kotlin.Triple;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
Expand All @@ -78,9 +82,6 @@
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import kotlin.Triple;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;

/** Blaze implementation of {@link AndroidModuleSystem}. */
@SuppressWarnings("NullableProblems")
Expand Down Expand Up @@ -174,8 +175,6 @@ public void registerDependency(GradleCoordinate coordinate, DependencyType type)
}

// TODO: automagically edit deps instead of just opening the BUILD file?
// Need to translate Gradle coordinates into blaze targets.
// Will probably need to hardcode for each dependency.
FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
PsiElement buildTargetPsi =
BuildReferenceManager.getInstance(project).resolveLabel(targetIdeInfo.getKey().getLabel());
Expand Down Expand Up @@ -205,6 +204,10 @@ public void registerDependency(GradleCoordinate coordinate, DependencyType type)
}
}

public void clearCache() {
classFileFinder.clearCache();
}

@Nullable
@Override
public GradleCoordinate getRegisteredDependency(GradleCoordinate coordinate) {
Expand Down Expand Up @@ -327,7 +330,7 @@ private Stream<TargetKey> locateArtifactsFor(GradleCoordinate coordinate) {
// labels in order to find them.
return MavenArtifactLocator.forBuildSystem(Blaze.getBuildSystemName(module.getProject()))
.stream()
.map(locator -> locator.labelFor(coordinate))
.map(locator -> locator.labelFor(module.getProject(), coordinate))
.filter(Objects::nonNull)
.map(TargetKey::forPlainTarget);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package com.google.idea.blaze.android.projectsystem;

import static java.lang.Math.max;

import com.android.annotations.concurrency.UiThread;
import com.android.tools.idea.projectsystem.ProjectSystemBuildManager;
import com.google.idea.blaze.base.build.BlazeBuildListener;
Expand All @@ -28,9 +26,12 @@
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.messages.Topic;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.concurrent.atomic.AtomicInteger;
import org.jetbrains.annotations.NotNull;

import static java.lang.Math.max;

/** Blaze implementation of {@link ProjectSystemBuildManager} */
public class BlazeProjectSystemBuildManager implements ProjectSystemBuildManager {
Expand All @@ -51,8 +52,12 @@ public void compileProject() {
@Override
public void compileFilesAndDependencies(Collection<? extends VirtualFile> files) {
// TODO(b/191937319): Implement incremental builds for individual files
// Just compile the entire project for now.
compileProject();
// Do an incremental sync for now so that previews update appropriately
//BlazeSyncManager.getInstance(project).workingSetSync("Compile files and dependencies");
// TODO: use partialSync with targets verived from `files` (see logic in
// BlazeSyncParams.sourceFilesToSync
//compileProject();
BlazeBuildService.getInstance(project).buildFiles(files);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.google.idea.blaze.android.projectsystem;

import com.android.ide.common.repository.GradleCoordinate;
import com.google.idea.blaze.base.ideinfo.TargetMap;
import com.google.idea.blaze.base.model.primitives.Label;
import com.google.idea.blaze.base.settings.BuildSystemName;
import com.google.idea.blaze.base.sync.data.BlazeProjectDataManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;

public class DefaultMavenArtifactLocator implements MavenArtifactLocator {
colinrgodsey marked this conversation as resolved.
Show resolved Hide resolved
private static final Logger log = Logger.getInstance(DefaultMavenArtifactLocator.class);
private static final String mavenCoordinateTagPrefix = "maven_coordinates=";

/**
* Locate an artifact label by maven coordinates. This is somewhat brittle,
* but Android Studio requests specific artifacts needed for their preview
* system, so we make our best attempt here to locate them using `maven_install`
* patterned bazel artifacts.
*/
public Label labelFor(Project project, GradleCoordinate coordinate) {
TargetMap targetMap = BlazeProjectDataManager.getInstance(project)
.getBlazeProjectData().getTargetMap();

String desiredCoord = mavenCoordinateTagPrefix + coordinate.getGroupId() +
":" + coordinate.getArtifactId() + ":";
String labelSuffix = String.format(":%s_%s",
coordinate.getGroupId().replaceAll("[.-]", "_"),
coordinate.getArtifactId().replaceAll("[.-]", "_")
);

// Debug code to list all targets. Some go missing sometimes...
/*String debugString = targetMap.map().keySet().stream()
.sorted(Comparator.comparing(TargetKey::toString))
.map(x -> x.getLabel().toString())
.collect(Collectors.joining(", "));
System.out.println(debugString);*/

return targetMap.targets().stream().filter(target -> {
for (String tag : target.getTags()) {
if (tag.startsWith(desiredCoord)) {
return true;
}
}
return false;
})
.map(x -> x.getKey().getLabel())
.findFirst().orElseGet(() -> {
Label bestGuess = Label.create("@maven//" + labelSuffix);
log.warn(String.format(
"Could not find exact label for %s, returning best guess of %s",
coordinate, bestGuess));
return bestGuess;
});
}

public BuildSystemName buildSystem() {
return BuildSystemName.Bazel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.google.idea.blaze.base.model.primitives.Label;
import com.google.idea.blaze.base.settings.BuildSystemName;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.openapi.project.Project;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -41,7 +43,7 @@ public interface MavenArtifactLocator {
*
* @param coordinate GradleCoordinate for the artifact.
*/
Label labelFor(GradleCoordinate coordinate);
Label labelFor(Project project, GradleCoordinate coordinate);

/** Returns the {@link BuildSystemName} this {@link MavenArtifactLocator} supports. */
BuildSystemName buildSystem();
Expand Down
Loading