From 2f2864f124769ab5985aa81b836a4409fd939d41 Mon Sep 17 00:00:00 2001 From: shenyong Date: Wed, 6 May 2020 17:32:54 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E5=BF=BD=E7=95=A5.DS=5FStore?= =?UTF-8?q?=E6=96=87=E4=BB=B6=EF=BC=9B=E6=94=AF=E6=8C=81Module=E7=AD=89?= =?UTF-8?q?=E5=85=B6=E4=BB=96=E7=B1=BB=E5=9E=8BFlutter=E5=B7=A5=E7=A8=8B?= =?UTF-8?q?=EF=BC=9B3=E3=80=81=E6=8F=90=E7=82=BC=E5=B7=A5=E7=A8=8B?= =?UTF-8?q?=E7=BB=93=E6=9E=84=E6=A3=80=E6=9F=A5=E4=BB=A3=E7=A0=81=EF=BC=9B?= =?UTF-8?q?4=E3=80=81=E6=B7=BB=E5=8A=A0=E5=B7=A5=E7=A8=8B=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 - .idea/inspectionProfiles/Project_Default.xml | 36 +++ .idea/misc.xml | 6 + .idea/modules.xml | 8 + .idea/vcs.xml | 6 + .idea/workspace.xml | 248 ++++++++++++++++++ AssetsRefGenerator.iml | 13 + resources/META-INF/plugin.xml | 17 +- .../shenyong/flutter/AssetsRefGenerator.java | 80 ++---- .../flutter/checker/AssetsChecker.java | 43 +++ src/com/shenyong/flutter/checker/ICheck.java | 17 ++ .../shenyong/flutter/checker/ProjChecker.java | 43 +++ 12 files changed, 451 insertions(+), 68 deletions(-) create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml create mode 100644 AssetsRefGenerator.iml create mode 100644 src/com/shenyong/flutter/checker/AssetsChecker.java create mode 100644 src/com/shenyong/flutter/checker/ICheck.java create mode 100644 src/com/shenyong/flutter/checker/ProjChecker.java diff --git a/.gitignore b/.gitignore index 8117b8f..89f9ac0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1 @@ -.idea/ out/ -*.iml diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..6560a98 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,36 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..0f5cec6 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..b861402 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..fd3db6b --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,248 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1588731283433 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/AssetsRefGenerator.iml b/AssetsRefGenerator.iml new file mode 100644 index 0000000..e025b20 --- /dev/null +++ b/AssetsRefGenerator.iml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml index dd348d1..36b13dd 100644 --- a/resources/META-INF/plugin.xml +++ b/resources/META-INF/plugin.xml @@ -33,6 +33,15 @@ ]]> 1.0.1 + 1.0.0
Implement basic functions. @@ -59,9 +68,11 @@ - + diff --git a/src/com/shenyong/flutter/AssetsRefGenerator.java b/src/com/shenyong/flutter/AssetsRefGenerator.java index a9d46fa..ab5047b 100644 --- a/src/com/shenyong/flutter/AssetsRefGenerator.java +++ b/src/com/shenyong/flutter/AssetsRefGenerator.java @@ -29,6 +29,8 @@ import com.intellij.openapi.actionSystem.PlatformDataKeys; import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.Messages; +import com.shenyong.flutter.checker.AssetsChecker; +import com.shenyong.flutter.checker.ProjChecker; import java.io.*; import java.text.SimpleDateFormat; @@ -51,44 +53,22 @@ */ public class AssetsRefGenerator extends AnAction { - private static ArrayList projFiles; - private static ArrayList projModelFiles; - private static ArrayList assetFiles; private static final String PUBSPEC = "pubspec.yaml"; private static final String RES_FILE = "res.dart"; + private static final String MAC_OS_DS_STORE = ".DS_Store"; - private static final String NOT_FLUTTER = "Current directory does not seem to be a valid Flutter project directory."; - - static { - projFiles = new ArrayList<>(); - projFiles.add("android"); - projFiles.add("ios"); - projFiles.add("lib"); - projFiles.add("pubspec.lock"); - projFiles.add(PUBSPEC); - - projModelFiles = new ArrayList<>(); - projModelFiles.add(".android"); - projModelFiles.add(".ios"); - projModelFiles.add("lib"); - projModelFiles.add("pubspec.lock"); - projModelFiles.add(PUBSPEC); - - assetFiles = new ArrayList<>(); - assetFiles.add("asset"); - assetFiles.add("assets"); - assetFiles.add("images"); - } + private ProjChecker projChecker = new ProjChecker(); + private AssetsChecker assetsChecker = new AssetsChecker(); @Override public void actionPerformed(AnActionEvent e) { Project project = e.getData(PlatformDataKeys.PROJECT); String path = Objects.requireNonNull(project).getBasePath(); - if (!checkFlutterProj(path)) { - showErrMsg(NOT_FLUTTER); + if (!projChecker.check(path)) { + showErrMsg("Current directory does not seem to be a valid Flutter project directory."); return; } - if (!checkAssets(path)) { + if (!assetsChecker.check(path)) { showErrMsg("No asset directory named asset, assets or images was found."); return; } @@ -105,44 +85,12 @@ private void showSuccessInfo() { "Flutter Assets Reference Generator", Messages.getInformationIcon()); } - private boolean isAllFilesContained(String[] files, ArrayList checkFiles) { - int cnt = 0; - for (String f : files) { - if (checkFiles.contains(f)) { - cnt++; - } - } - return cnt >= checkFiles.size(); - } - - private boolean checkFlutterProj(String path) { - if (path == null || path.isEmpty()) { - return false; - } - File dir = new File(path); - if (!dir.exists() || !dir.isDirectory()) { - showErrMsg(NOT_FLUTTER); - } - return isAllFilesContained(Objects.requireNonNull(dir.list()), projFiles) || isAllFilesContained(Objects.requireNonNull(dir.list()), projModelFiles); - } - - private boolean checkAssets(String path) { - File dir = new File(path); - String[] files = Objects.requireNonNull(dir.list()); - int cnt = 0; - for (String f : files) { - if (assetFiles.contains(f)) { - cnt++; - } - } - return cnt > 0; - } - private List getAssets(String path) { System.out.println("Scanning asset files under asset, assets and images..."); assetsNames.clear(); + List assetsDirs = assetsChecker.getAssetsDirs(); List assets = new ArrayList<>(); - for (String name : assetFiles) { + for (String name : assetsDirs) { File dir = new File(path, name); getAssets(assets, dir, name); } @@ -155,7 +103,13 @@ private void getAssets(List assets, File dir, String prefix) { if (!dir.exists() || !dir.isDirectory()) { return; } - File[] files = dir.listFiles(); + File[] files = dir.listFiles(new FilenameFilter() { + @Override + public boolean accept(File dir, String name) { + // 忽略 MacOS 中的 .DS_Store 文件 + return !MAC_OS_DS_STORE.equals(name); + } + }); if (files == null) { return; } diff --git a/src/com/shenyong/flutter/checker/AssetsChecker.java b/src/com/shenyong/flutter/checker/AssetsChecker.java new file mode 100644 index 0000000..3752538 --- /dev/null +++ b/src/com/shenyong/flutter/checker/AssetsChecker.java @@ -0,0 +1,43 @@ +package com.shenyong.flutter.checker; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +/** + * @author ShenYong + * @date 2020/5/6 + */ +public class AssetsChecker implements ICheck { + + private ArrayList assetFiles; + + { + assetFiles = new ArrayList<>(); + assetFiles.add("asset"); + assetFiles.add("assets"); + assetFiles.add("images"); + } + + @Override + public boolean check(String path) { + if (path == null || path.isEmpty()) { + return false; + } + + File dir = new File(path); + String[] files = Objects.requireNonNull(dir.list()); + int cnt = 0; + for (String f : files) { + if (assetFiles.contains(f)) { + cnt++; + } + } + return cnt > 0; + } + + public List getAssetsDirs() { + return assetFiles; + } +} diff --git a/src/com/shenyong/flutter/checker/ICheck.java b/src/com/shenyong/flutter/checker/ICheck.java new file mode 100644 index 0000000..b6683c8 --- /dev/null +++ b/src/com/shenyong/flutter/checker/ICheck.java @@ -0,0 +1,17 @@ +package com.shenyong.flutter.checker; + +import java.io.File; + +/** + * @author ShenYong + * @date 2020/5/6 + */ +public interface ICheck { + + /** + * 校验方法 + * @param path 待检查路径 + * @return 校验结果,true 为 通过 + */ + boolean check(String path); +} diff --git a/src/com/shenyong/flutter/checker/ProjChecker.java b/src/com/shenyong/flutter/checker/ProjChecker.java new file mode 100644 index 0000000..520351b --- /dev/null +++ b/src/com/shenyong/flutter/checker/ProjChecker.java @@ -0,0 +1,43 @@ +package com.shenyong.flutter.checker; + +import java.io.File; +import java.util.ArrayList; +import java.util.Objects; + +/** + * 项目结构检查,看是否是一个有效的 Flutter 工程目录。 + * @author ShenYong + * @date 2020/5/6 + */ +public class ProjChecker implements ICheck { + + private ArrayList checkFiles; + + { + checkFiles = new ArrayList<>(); + checkFiles.add("lib"); + checkFiles.add(".metadata"); + checkFiles.add(".packages"); + checkFiles.add("pubspec.lock"); + checkFiles.add("pubspec.yaml"); + } + + @Override + public boolean check(String path) { + if (path == null || path.isEmpty()) { + return false; + } + File dir = new File(path); + if (!dir.exists() || !dir.isDirectory()) { + return false; + } + String[] files = Objects.requireNonNull(dir.list()); + int cnt = 0; + for (String f : files) { + if (checkFiles.contains(f)) { + cnt++; + } + } + return cnt == checkFiles.size(); + } +}