Skip to content

Adds Flutter 3.3 support for flutter plugins library manager #6331

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

Merged
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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ Hrishikesh Kadam <[email protected]>
Sander Kersten <[email protected]>
Pradumna Saraf <[email protected]>
Pedro Massango <[email protected]>
Wagner Silvestre <[email protected]>
16 changes: 16 additions & 0 deletions flutter-idea/src/io/flutter/pub/PubRoot.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.jetbrains.lang.dart.util.DotPackagesFileUtil;
import io.flutter.FlutterUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Map;

/**
* A snapshot of the root directory of a pub package.
Expand Down Expand Up @@ -279,6 +281,20 @@ public VirtualFile getPackagesFile() {
return null;
}

public @Nullable Map<String, String> getPackagesMap() {
final var packageConfigFile = getPackageConfigFile();
if (packageConfigFile != null) {
return DotPackagesFileUtil.getPackagesMapFromPackageConfigJsonFile(packageConfigFile);
}

final var packagesFile = getPackagesFile();
if (packagesFile != null) {
return DotPackagesFileUtil.getPackagesMap(packagesFile);
}

return null;
}

/**
* Returns true if the packages are up to date wrt pubspec.yaml.
*/
Expand Down
92 changes: 19 additions & 73 deletions flutter-idea/src/io/flutter/sdk/FlutterPluginsLibraryManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,23 @@ protected PersistentLibraryKind<FlutterPluginLibraryProperties> getLibraryKind()
return FlutterPluginLibraryType.LIBRARY_KIND;
}

private void fileChanged(@NotNull final Project project, @NotNull final VirtualFile file) {
if (!DotPackagesFileUtil.DOT_PACKAGES.equals(file.getName())) return;
if (LocalFileSystem.getInstance() != file.getFileSystem() && !ApplicationManager.getApplication().isUnitTestMode()) return;
private boolean isPackagesFile(@NotNull final VirtualFile file) {
final VirtualFile parent = file.getParent();
return file.getName().equals(DotPackagesFileUtil.DOT_PACKAGES) && parent != null && parent.findChild(PUBSPEC_YAML) != null;
}

private boolean isPackageConfigFile(@NotNull final VirtualFile file) {
final VirtualFile parent = file.getParent();
final VirtualFile pubspec = parent == null ? null : parent.findChild(PUBSPEC_YAML);
return file.getName().equals(DotPackagesFileUtil.PACKAGE_CONFIG_JSON)
&& parent != null
&& parent.getName().equals(DotPackagesFileUtil.DART_TOOL_DIR);
}

if (pubspec != null) {
scheduleUpdate();
}
private void fileChanged(@NotNull final Project project, @NotNull final VirtualFile file) {
if (!isPackageConfigFile(file) && !isPackagesFile(file)) return;
if (LocalFileSystem.getInstance() != file.getFileSystem() && !ApplicationManager.getApplication().isUnitTestMode()) return;

scheduleUpdate();
}

private void scheduleUpdate() {
Expand Down Expand Up @@ -132,24 +139,12 @@ private static Set<String> getFlutterPluginPaths(List<PubRoot> roots) {
final Set<String> paths = new HashSet<>();

for (PubRoot pubRoot : roots) {

final Map<String, String> map;
if (pubRoot.getPackagesFile() == null) {
@Nullable VirtualFile configFile = pubRoot.getPackageConfigFile();
if (configFile == null) {
continue;
}
// TODO(messick) Use the code in the Dart plugin when available.
// This is just a backup in case we need it. It does not have a proper cache, but the Dart plugin does.
map = loadPackagesMap(configFile);
}
else {
map = DotPackagesFileUtil.getPackagesMap(pubRoot.getPackagesFile());
if (map == null) {
continue;
}
final var packagesMap = pubRoot.getPackagesMap();
if (packagesMap == null) {
continue;
}
for (String packagePath : map.values()) {

for (String packagePath : packagesMap.values()) {
final VirtualFile libFolder = LocalFileSystem.getInstance().findFileByPath(packagePath);
if (libFolder == null) {
continue;
Expand All @@ -167,53 +162,4 @@ private static Set<String> getFlutterPluginPaths(List<PubRoot> roots) {

return paths;
}

private static Map<String, String> loadPackagesMap(@NotNull VirtualFile root) {
Map<String, String> result = new HashMap<>();
try {
JsonElement element = JsonUtils.parseString(new String(root.contentsToByteArray(), StandardCharsets.UTF_8));
if (element != null) {
JsonElement packages = element.getAsJsonObject().get("packages");
if (packages != null) {
JsonArray array = packages.getAsJsonArray();
for (int i = 0; i < array.size(); i++) {
JsonObject pkg = array.get(i).getAsJsonObject();
String name = pkg.get("name").getAsString();
String rootUri = pkg.get("rootUri").getAsString();
if (name != null && rootUri != null) {
// need to protect '+' chars because URLDecoder.decode replaces '+' with space
final String encodedUriWithoutPluses = StringUtil.replace(rootUri, "+", "%2B");
final String uri = URLUtil.decode(encodedUriWithoutPluses);
final String packageUri = getAbsolutePackageRootPath(root.getParent().getParent(), uri);
result.put(name, packageUri);
}
}
}
}
}
catch (IOException | JsonSyntaxException ignored) {
}
return result;
}

@Nullable
private static String getAbsolutePackageRootPath(@NotNull final VirtualFile baseDir, @NotNull final String uri) {
// Copied from the Dart plugin.
if (uri.startsWith("file:/")) {
final String pathAfterSlashes = StringUtil.trimEnd(StringUtil.trimLeading(StringUtil.trimStart(uri, "file:/"), '/'), "/");
if (SystemInfo.isWindows && !ApplicationManager.getApplication().isUnitTestMode()) {
if (pathAfterSlashes.length() > 2 && Character.isLetter(pathAfterSlashes.charAt(0)) && ':' == pathAfterSlashes.charAt(1)) {
return pathAfterSlashes;
}
}
else {
return "/" + pathAfterSlashes;
}
}
else {
return FileUtil.toCanonicalPath(baseDir.getPath() + "/" + uri);
}

return null;
}
}