diff --git a/CHANGELOG.md b/CHANGELOG.md index b7cfb22..ac31749 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## 0.1.2-dev.0+3 + +- Bug fix in update logic. +## 0.1.1-dev.0+3 + +- Support for update check added and some refactoring. Documentation updated. ## 0.1.0-dev.0+2 - Fixed typo in repository name in pubspec.yaml. diff --git a/README.md b/README.md index bf91976..e14b62e 100644 --- a/README.md +++ b/README.md @@ -20,11 +20,19 @@ dart pub global activate asset_manager_cli ## Overview To auto-generate and add assets code to your pubspec.yaml, run the following command at the root of your project. -```DART +``` asset_manager add ``` + +To update to latest version, run the following command. +``` + +asset_manager update + +``` + ## ❕Note The cli assumes the following structure for your assets folder. diff --git a/bin/asset_manager.dart b/bin/asset_manager.dart index 3fcef74..9f5f0c1 100644 --- a/bin/asset_manager.dart +++ b/bin/asset_manager.dart @@ -1,17 +1,23 @@ import 'dart:io'; import 'package:args/args.dart'; +import 'package:asset_manager_cli/asset_manager_cli.dart'; import 'package:mason_logger/mason_logger.dart'; void main(List arguments) async { - final parser = ArgParser()..addCommand("add"); + final parser = ArgParser() + ..addCommand("add") + ..addCommand("update"); + final logger = Logger(); + final ArgResults argResults; try { argResults = parser.parse(arguments); } catch (e) { logger.err(e.toString()); + await checkForUpdate(); exit(1); } @@ -23,113 +29,19 @@ Usage: asset_manager [arguments] Available commands: - add Generates and adds assets code into pubspec.yaml - +$commandsWithInfoString '''); 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.readAsLinesSync(); - final addAfterLine = - openedFile.indexWhere(((element) => element == "flutter:")); - - openedFile.insert(addAfterLine + 1, "\n assets:"); - - 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.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)); - } + switch (argResults.command?.name) { + case "add": + addAssetsCode(logger); + break; + case "update": + updateCLI(); + break; + default: + commandNotFound(commandsWithInfoString, argResults, logger); } - - return assetString; } diff --git a/lib/asset_manager_cli.dart b/lib/asset_manager_cli.dart index f64ad72..8db1e31 100644 --- a/lib/asset_manager_cli.dart +++ b/lib/asset_manager_cli.dart @@ -1,3 +1,141 @@ -int calculate() { - return 6 * 7; +import 'dart:io'; + +import 'package:args/args.dart'; +import 'package:asset_manager_cli/src/version.dart'; +import 'package:mason_logger/mason_logger.dart'; +import 'package:pub_updater/pub_updater.dart'; + +final pubUpdater = PubUpdater(); + +Future checkForUpdate() async { + // Create an instance of PubUpdater + + // Check whether or not packageVersion is the latest version of asset_manager_cli + final isUpToDate = await pubUpdater.isUpToDate( + packageName: 'asset_manager_cli', + currentVersion: packageVersion, + ); + + // Trigger an upgrade to the latest version if asset_manager_cli is not up to date + if (!isUpToDate) { + await pubUpdater.update(packageName: 'asset_manager_cli'); + } +} + +void commandNotFound( + String commands, ArgResults argResults, Logger logger) async { + final unknownCommand = argResults.arguments[0]; + logger.err( + "Command not found $unknownCommand\nDid you mean one of these commands?\n$commands"); + await checkForUpdate(); + exit(1); +} + +String commandsWithInfoString = ''' + add Generates and adds assets code into pubspec.yaml + update Updates asset_manager_cli to the latest version +'''; + +void addAssetsCode(Logger logger) async { + 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.readAsLinesSync(); + final addAfterLine = + openedFile.indexWhere(((element) => element == "flutter:")); + + openedFile.insert(addAfterLine + 1, "\n assets:"); + + 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(generateDirAssetsCode(subdirectory)); + } + } + } + + 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"); + await checkForUpdate(); + exit(0); + } catch (e) { + progress.fail("Error: $e"); + await checkForUpdate(); + exit(1); + } +} + +List generateDirAssetsCode(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(generateDirAssetsCode(subDir)); + } + } + + return assetString; +} + +Future updateCLI() async { + await pubUpdater.update(packageName: "asset_manager_cli"); } diff --git a/lib/src/version.dart b/lib/src/version.dart new file mode 100644 index 0000000..51266c4 --- /dev/null +++ b/lib/src/version.dart @@ -0,0 +1,2 @@ +// Generated code. Do not modify. +const packageVersion = '0.1.2-dev.0+3'; diff --git a/pubspec.lock b/pubspec.lock index d070225..eca0665 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -36,6 +36,83 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.1.0" + build: + dependency: transitive + description: + name: build + url: "https://pub.dartlang.org" + source: hosted + version: "2.3.0" + build_config: + dependency: transitive + description: + name: build_config + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0" + build_daemon: + dependency: transitive + description: + name: build_daemon + url: "https://pub.dartlang.org" + source: hosted + version: "3.1.0" + build_resolvers: + dependency: transitive + description: + name: build_resolvers + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.9" + build_runner: + dependency: "direct dev" + description: + name: build_runner + url: "https://pub.dartlang.org" + source: hosted + version: "2.2.0" + build_runner_core: + dependency: transitive + description: + name: build_runner_core + url: "https://pub.dartlang.org" + source: hosted + version: "7.2.3" + build_version: + dependency: "direct dev" + description: + name: build_version + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.1" + built_collection: + dependency: transitive + description: + name: built_collection + url: "https://pub.dartlang.org" + source: hosted + version: "5.1.1" + built_value: + dependency: transitive + description: + name: built_value + url: "https://pub.dartlang.org" + source: hosted + version: "8.4.1" + checked_yaml: + dependency: transitive + description: + name: checked_yaml + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.1" + code_builder: + dependency: transitive + description: + name: code_builder + url: "https://pub.dartlang.org" + source: hosted + version: "4.2.0" collection: dependency: transitive description: @@ -64,6 +141,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "3.0.2" + dart_style: + dependency: transitive + description: + name: dart_style + url: "https://pub.dartlang.org" + source: hosted + version: "2.2.3" file: dependency: transitive description: @@ -71,13 +155,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "6.1.4" + fixnum: + dependency: transitive + description: + name: fixnum + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.1" frontend_server_client: dependency: transitive description: name: frontend_server_client url: "https://pub.dartlang.org" source: hosted - version: "3.0.0" + version: "2.1.3" glob: dependency: transitive description: @@ -85,6 +176,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.1.0" + graphs: + dependency: transitive + description: + name: graphs + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.0" + http: + dependency: transitive + description: + name: http + url: "https://pub.dartlang.org" + source: hosted + version: "0.13.5" http_multi_server: dependency: transitive description: @@ -113,6 +218,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.6.4" + json_annotation: + dependency: transitive + description: + name: json_annotation + url: "https://pub.dartlang.org" + source: hosted + version: "4.6.0" lints: dependency: "direct dev" description: @@ -176,6 +288,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.8.2" + platform: + dependency: transitive + description: + name: platform + url: "https://pub.dartlang.org" + source: hosted + version: "3.1.0" pool: dependency: transitive description: @@ -183,6 +302,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.5.1" + process: + dependency: transitive + description: + name: process + url: "https://pub.dartlang.org" + source: hosted + version: "4.2.4" pub_semver: dependency: transitive description: @@ -190,6 +316,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.1.1" + pub_updater: + dependency: "direct main" + description: + name: pub_updater + url: "https://pub.dartlang.org" + source: hosted + version: "0.2.2" + pubspec_parse: + dependency: transitive + description: + name: pubspec_parse + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.1" shelf: dependency: transitive description: @@ -253,6 +393,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.1.0" + stream_transform: + dependency: transitive + description: + name: stream_transform + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.0" string_scanner: dependency: transitive description: @@ -288,6 +435,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.4.17" + timing: + dependency: transitive + description: + name: timing + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.0" typed_data: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index b96b276..b7419f1 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: asset_manager_cli description: Auto-generate the assets code and add it to your pubspec.yaml . -version: 0.1.0-dev.0+2 +version: 0.1.2-dev.0+3 homepage: https://github.com/rutvik110/asset_manager_cli funding: - https://www.buymeacoffee.com/takrutvik @@ -10,12 +10,45 @@ environment: dependencies: args: ^2.3.1 + mason_logger: ^0.1.2 + pub_updater: ^0.2.2 executables: asset_manager: dev_dependencies: lints: ^2.0.0 test: ^1.16.0 + build_version: ^2.1.1 + build_runner: ^2.2.0 + +flutter: + assets: + - assets/music/ + - assets/music/actions/ + - assets/music/player/ + - assets/images/ + - assets/images/pokemon/ + - assets/images/pokemon/water/ + - assets/images/pokemon/water/classic/ + - assets/images/pokemon/water/new/ + - assets/images/pokemon/fire/ + - assets/images/pokemon/fire/classic/ + - assets/images/pokemon/fire/new/ + - assets/images/beyblade/ + - assets/images/beyblade/classic/ + - assets/images/beyblade/new/ + fonts: + - family: font2 + fonts: + - asset: assets/fonts/font2/font2-Italic-700.ttf + weight: 700 + style: italic + - asset: assets/fonts/font2/font2-Regular-800.ttf + weight: 800 + - family: font1 + fonts: + - asset: assets/fonts/font1/font1-Regular-800.ttf + weight: 800 \ No newline at end of file