Skip to content

Commit

Permalink
auto-update check support and refactoring support with docs updated
Browse files Browse the repository at this point in the history
  • Loading branch information
rutvik110 committed Sep 10, 2022
1 parent ca403fc commit 41ccb68
Show file tree
Hide file tree
Showing 7 changed files with 363 additions and 110 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
122 changes: 17 additions & 105 deletions bin/asset_manager.dart
Original file line number Diff line number Diff line change
@@ -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<String> 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);
}

Expand All @@ -23,113 +29,19 @@ Usage: asset_manager <command> [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<String> 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<String> 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<String> addToAssets(Directory directory) {
List<String> 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;
}
142 changes: 140 additions & 2 deletions lib/asset_manager_cli.dart
Original file line number Diff line number Diff line change
@@ -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<void> 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<String> 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<String> 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<String> generateDirAssetsCode(Directory directory) {
List<String> 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<void> updateCLI() async {
await pubUpdater.update(packageName: "asset_manager_cli");
}
2 changes: 2 additions & 0 deletions lib/src/version.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 41ccb68

Please sign in to comment.