Skip to content

Commit 749c3d8

Browse files
authored
Adds Flutter 3.3 support for flutter plugins library manager (#6331)
* Adds support for flutter 3.3 in the flutter plugin library manager which now updates the flutter plugin library when .dart_tool/package_config.json or .packages files are changed * Fix formatting errors on FlutterPluginsLibraryManager and PubRoot
1 parent 92b3d27 commit 749c3d8

File tree

3 files changed

+36
-73
lines changed

3 files changed

+36
-73
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ Hrishikesh Kadam <[email protected]>
2020
Sander Kersten <[email protected]>
2121
Pradumna Saraf <[email protected]>
2222
Pedro Massango <[email protected]>
23+
Wagner Silvestre <[email protected]>

flutter-idea/src/io/flutter/pub/PubRoot.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
import com.intellij.openapi.roots.ProjectRootManager;
1818
import com.intellij.openapi.vfs.VirtualFile;
1919
import com.intellij.psi.PsiFile;
20+
import com.jetbrains.lang.dart.util.DotPackagesFileUtil;
2021
import io.flutter.FlutterUtils;
2122
import org.jetbrains.annotations.NotNull;
2223
import org.jetbrains.annotations.Nullable;
2324

2425
import java.util.List;
26+
import java.util.Map;
2527

2628
/**
2729
* A snapshot of the root directory of a pub package.
@@ -279,6 +281,20 @@ public VirtualFile getPackagesFile() {
279281
return null;
280282
}
281283

284+
public @Nullable Map<String, String> getPackagesMap() {
285+
final var packageConfigFile = getPackageConfigFile();
286+
if (packageConfigFile != null) {
287+
return DotPackagesFileUtil.getPackagesMapFromPackageConfigJsonFile(packageConfigFile);
288+
}
289+
290+
final var packagesFile = getPackagesFile();
291+
if (packagesFile != null) {
292+
return DotPackagesFileUtil.getPackagesMap(packagesFile);
293+
}
294+
295+
return null;
296+
}
297+
282298
/**
283299
* Returns true if the packages are up to date wrt pubspec.yaml.
284300
*/

flutter-idea/src/io/flutter/sdk/FlutterPluginsLibraryManager.java

Lines changed: 19 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,23 @@ protected PersistentLibraryKind<FlutterPluginLibraryProperties> getLibraryKind()
8585
return FlutterPluginLibraryType.LIBRARY_KIND;
8686
}
8787

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

93+
private boolean isPackageConfigFile(@NotNull final VirtualFile file) {
9294
final VirtualFile parent = file.getParent();
93-
final VirtualFile pubspec = parent == null ? null : parent.findChild(PUBSPEC_YAML);
95+
return file.getName().equals(DotPackagesFileUtil.PACKAGE_CONFIG_JSON)
96+
&& parent != null
97+
&& parent.getName().equals(DotPackagesFileUtil.DART_TOOL_DIR);
98+
}
9499

95-
if (pubspec != null) {
96-
scheduleUpdate();
97-
}
100+
private void fileChanged(@NotNull final Project project, @NotNull final VirtualFile file) {
101+
if (!isPackageConfigFile(file) && !isPackagesFile(file)) return;
102+
if (LocalFileSystem.getInstance() != file.getFileSystem() && !ApplicationManager.getApplication().isUnitTestMode()) return;
103+
104+
scheduleUpdate();
98105
}
99106

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

134141
for (PubRoot pubRoot : roots) {
135-
136-
final Map<String, String> map;
137-
if (pubRoot.getPackagesFile() == null) {
138-
@Nullable VirtualFile configFile = pubRoot.getPackageConfigFile();
139-
if (configFile == null) {
140-
continue;
141-
}
142-
// TODO(messick) Use the code in the Dart plugin when available.
143-
// This is just a backup in case we need it. It does not have a proper cache, but the Dart plugin does.
144-
map = loadPackagesMap(configFile);
145-
}
146-
else {
147-
map = DotPackagesFileUtil.getPackagesMap(pubRoot.getPackagesFile());
148-
if (map == null) {
149-
continue;
150-
}
142+
final var packagesMap = pubRoot.getPackagesMap();
143+
if (packagesMap == null) {
144+
continue;
151145
}
152-
for (String packagePath : map.values()) {
146+
147+
for (String packagePath : packagesMap.values()) {
153148
final VirtualFile libFolder = LocalFileSystem.getInstance().findFileByPath(packagePath);
154149
if (libFolder == null) {
155150
continue;
@@ -167,53 +162,4 @@ private static Set<String> getFlutterPluginPaths(List<PubRoot> roots) {
167162

168163
return paths;
169164
}
170-
171-
private static Map<String, String> loadPackagesMap(@NotNull VirtualFile root) {
172-
Map<String, String> result = new HashMap<>();
173-
try {
174-
JsonElement element = JsonUtils.parseString(new String(root.contentsToByteArray(), StandardCharsets.UTF_8));
175-
if (element != null) {
176-
JsonElement packages = element.getAsJsonObject().get("packages");
177-
if (packages != null) {
178-
JsonArray array = packages.getAsJsonArray();
179-
for (int i = 0; i < array.size(); i++) {
180-
JsonObject pkg = array.get(i).getAsJsonObject();
181-
String name = pkg.get("name").getAsString();
182-
String rootUri = pkg.get("rootUri").getAsString();
183-
if (name != null && rootUri != null) {
184-
// need to protect '+' chars because URLDecoder.decode replaces '+' with space
185-
final String encodedUriWithoutPluses = StringUtil.replace(rootUri, "+", "%2B");
186-
final String uri = URLUtil.decode(encodedUriWithoutPluses);
187-
final String packageUri = getAbsolutePackageRootPath(root.getParent().getParent(), uri);
188-
result.put(name, packageUri);
189-
}
190-
}
191-
}
192-
}
193-
}
194-
catch (IOException | JsonSyntaxException ignored) {
195-
}
196-
return result;
197-
}
198-
199-
@Nullable
200-
private static String getAbsolutePackageRootPath(@NotNull final VirtualFile baseDir, @NotNull final String uri) {
201-
// Copied from the Dart plugin.
202-
if (uri.startsWith("file:/")) {
203-
final String pathAfterSlashes = StringUtil.trimEnd(StringUtil.trimLeading(StringUtil.trimStart(uri, "file:/"), '/'), "/");
204-
if (SystemInfo.isWindows && !ApplicationManager.getApplication().isUnitTestMode()) {
205-
if (pathAfterSlashes.length() > 2 && Character.isLetter(pathAfterSlashes.charAt(0)) && ':' == pathAfterSlashes.charAt(1)) {
206-
return pathAfterSlashes;
207-
}
208-
}
209-
else {
210-
return "/" + pathAfterSlashes;
211-
}
212-
}
213-
else {
214-
return FileUtil.toCanonicalPath(baseDir.getPath() + "/" + uri);
215-
}
216-
217-
return null;
218-
}
219165
}

0 commit comments

Comments
 (0)