getUnusedAssets method
Get list of unused assets from
Implementation
Future<List<UnusedAssetModel>> getUnusedAssets() async {
String currentPath = FileUtils.instance.getCurrentPath;
final fileUtils = FileUtils.instance;
/// Read assets path from pubspec.yaml
List<String> assetsPath = await fileUtils.getPubspecAsset();
/// Remove if path not start with assets
assetsPath.removeWhere((element) => !element.startsWith("assets"));
if (assetsPath.isEmpty) return [];
/// Remove duplicate assets from [assetsPath]
assetsPath = assetsPath.toSet().toList();
/// Get config from assets_cleaner.yaml
fileUtils.getConfig();
if (fileUtils.config == null) return [];
/// Load all assets from pubspec.yaml
/// and store in [assets]
List<String> assets = [];
for (var path in assetsPath) {
final files = await fileUtils.loadAssets(path);
for (var file in files) {
/// Remove '/' on the first character
String assetsPath = file.path.replaceAll(currentPath, "");
if (assetsPath.startsWith("/")) {
assetsPath = assetsPath.substring(1);
assets.add(assetsPath);
} else {
assets.add(assetsPath);
}
}
}
/// Get exclude file and extensions
final excludeFile = fileUtils.getExcludeFile();
final excludeExtension = fileUtils.getExcludeExtension();
/// Remove exclude extension and file from assets
assets = _removeExcludedExt(excludeExtension, assets);
assets = _removeExcludedFile(excludeFile, assets);
List<UnusedAssetModel> unusedAssets = [];
if (assets.isNotEmpty) {
print("-----------------------------");
print("Scanning ${assets.length} assets file");
for (var asset in assets) {
final fileName = asset.split("/").last;
if (await CodeUtils.instance.containsAsset(fileName) == false) {
unusedAssets.add(
UnusedAssetModel(fileName: fileName, filePath: asset),
);
}
}
}
/// Read fluttergen path from pubspec.yaml
String genPath = await fileUtils.getFlutterGenPath();
/// Get list of assets from flutter_gen
List<GenModel> genAssets =
await FlutterGenUtils.instance.extractAssetVariables(genPath);
if (genAssets.isNotEmpty) {
print("-----------------------------");
print("Scanning ${genAssets.length} flutter_gen assets variables");
MsgUtils.showInfo("Find unused assets from flutter_gen");
for (var genAsset in genAssets) {
if (await CodeUtils.instance.containsFlutterGenAssets(genAsset) ==
false) {
MsgUtils.showList("${genAsset.variable}");
unusedAssets.add(UnusedAssetModel(
fileName: genAsset.path.split("/").last,
filePath: genAsset.path,
));
}
}
}
/// Remove duplicate assets from [unusedAssets] by filePath
unusedAssets = _removeDuplicateAssets(unusedAssets);
return unusedAssets;
}