diff --git a/assets/fonts/Random/Random-Regular-800 copy.ttf b/assets/fonts/Random/Random-Regular-800.ttf similarity index 100% rename from assets/fonts/Random/Random-Regular-800 copy.ttf rename to assets/fonts/Random/Random-Regular-800.ttf diff --git a/bin/asset_manager.dart b/bin/asset_manager.dart index 54b9f18..1666cdf 100644 --- a/bin/asset_manager.dart +++ b/bin/asset_manager.dart @@ -1,82 +1,135 @@ import 'dart:io'; import 'package:args/args.dart'; +import 'package:mason_logger/mason_logger.dart'; -void main(List arguments) { - // find the structure of directories within asset folder - final parser = ArgParser()..addFlag("build", negatable: false, abbr: 'b'); - final isBuild = parser.parse(arguments)['build']; - stdout.write(isBuild); - if (isBuild) { - // final cwd = Directory.current.path; +void main(List arguments) async { + final parser = ArgParser()..addCommand("add"); + final logger = Logger(); + final ArgResults argResults; + + try { + argResults = parser.parse(arguments); + } catch (e) { + logger.err(e.toString()); + exit(1); + } + + if (argResults.arguments.isEmpty) { + logger.info(''' +asset_manager + +Usage: asset_manager [arguments] + + +Available commands: + add Generates and adds assets code into pubspec.yaml + + '''); + exit(0); + } + final bool isBuild = argResults.command?.name == "add"; + + if (!isBuild) { + final unknownCommand = argResults.arguments[0]; + logger.err( + "Command not found $unknownCommand\nDid you mean one of these commands?\nadd"); + + exit(1); + } + + final progress = logger.progress("Generating assets code"); + try { final assetDir = Directory('assets'); + final fontsDir = Directory('assets/fonts'); final pubspecFile = File('pubspec.yaml'); final subdirectories = assetDir.listSync(); - final openedFile = pubspecFile.openWrite(mode: FileMode.append); - openedFile.writeln("\n assets:"); - for (var directory in subdirectories) { - if (directory is Directory) { - final assetPath = directory.path; - final subDirectories = Directory(assetPath).listSync(); + final openedFile = pubspecFile.readAsLinesSync(); + final addAfterLine = + openedFile.indexWhere(((element) => element.contains("flutter:"))); - if (!assetPath.contains('fonts')) { - openedFile.writeln(" - ${directory.path}"); - } + openedFile.insert(addAfterLine + 1, "\n assets:"); - for (var subDir in subDirectories) { - final subDirPath = subDir.path; - if (subDir is Directory) { - if (!subDirPath.contains('fonts')) { - openedFile.writeln(" - $subDirPath"); - } - } + List assetsString = []; + // add assets code except fonts + for (var i = 0; i < subdirectories.length; i++) { + final subdirectory = subdirectories[i]; + if (subdirectory is Directory) { + if (!subdirectory.path.contains("fonts")) { + assetsString.add(" - ${subdirectory.path}/"); + + assetsString.addAll(addToAssets(subdirectory)); } } } - openedFile.writeln(" fonts:"); - - for (var directory in subdirectories) { - if (directory is Directory) { - final assetPath = directory.path; - final subFontDirectories = Directory(assetPath).listSync(); - - if (assetPath.contains('fonts')) { - final fontFiles = Directory(assetPath).listSync(); - - for (var fontDir in subFontDirectories) { - if (fontDir is Directory) { - stdout.writeln(fontDir.path); - final fontFamily = fontDir.path.split('/').last; - openedFile.writeln(" - family: $fontFamily"); - openedFile.writeln(" fonts:"); - final fontFiles = Directory(fontDir.path).listSync(); - for (var fontFile in fontFiles) { - final fontFileExtension = - fontFile.path.split('/').last.split(".").last; - - if (fontFileExtension == "ttf") { - final fontFilePath = fontFile.path; - final fontProperties = - fontFilePath.split('/').last.split('.').first.split('-'); - final fontFileWeight = fontProperties.last; - final fontFileStyle = fontProperties[1]; - openedFile.writeln( - " - asset: $fontFilePath", - ); - openedFile.writeln( - " weight: $fontFileWeight", - ); - openedFile.writeln( - " style: ${fontFileStyle.toLowerCase()}", - ); - } + + openedFile.insertAll(addAfterLine + 2, assetsString); + + // add fonts code + + openedFile.insert(addAfterLine + 2 + assetsString.length, "\n fonts:"); + + final fonts = fontsDir.listSync(); + List fontsString = []; + + for (var font in fonts) { + if (font is Directory) { + final fontFamily = font.path.split('/').last; + fontsString.add(" - family: $fontFamily"); + fontsString.add(" fonts:"); + final fontFiles = Directory(font.path).listSync(); + for (var fontFile in fontFiles) { + if (fontFile is File) { + final fontFileExtension = + fontFile.path.split('/').last.split(".").last; + if (fontFileExtension == "ttf") { + final fontFilePath = fontFile.path; + final fontProperties = + fontFilePath.split('/').last.split('.').first.split('-'); + final fontFileWeight = fontProperties.last; + final fontFileStyle = fontProperties[1].toLowerCase(); + fontsString.add( + " - asset: $fontFilePath", + ); + fontsString.add( + " weight: $fontFileWeight", + ); + if (fontFileStyle != "regular") { + fontsString.add( + " style: $fontFileStyle", + ); } } } } } } + + openedFile.insertAll(addAfterLine + 3 + assetsString.length, fontsString); + pubspecFile.writeAsString(openedFile.join("\n")); + progress.complete("Assets added to pubspec.yaml"); + } catch (e) { + progress.fail("Error: $e"); + exit(1); + } +} + +List addToAssets(Directory directory) { + List assetString = []; + + final subDirectories = directory.listSync(); + + for (var subDir in subDirectories) { + if (subDir is Directory) { + final subDirPath = subDir.path; + if (!subDirPath.contains('fonts')) { + assetString.add(" - $subDirPath/"); + } + assetString.addAll(addToAssets(subDir)); + } } + + return assetString; } diff --git a/pubspec.lock b/pubspec.lock index fb3f8dc..d070225 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -127,6 +127,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.0.2" + mason_logger: + dependency: "direct main" + description: + name: mason_logger + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.2" matcher: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 6222afa..8f2fef8 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -8,6 +8,7 @@ environment: dependencies: args: ^2.3.1 + mason_logger: ^0.1.2 executables: asset_manager: @@ -17,22 +18,3 @@ dev_dependencies: flutter: - - assets: - - assets/images - - assets/images/pokemon - - assets/images/beyblade - fonts: - - family: Permanent_Marker - fonts: - - asset: assets/fonts/Permanent_Marker/PermanentMarker-Regular-800.ttf - weight: 800 - style: regular - - family: Random - fonts: - - asset: assets/fonts/Random/Random-Italic-700.ttf - weight: 700 - style: italic - - asset: assets/fonts/Random/Random-Regular-800 copy.ttf - weight: 800 copy - style: regular