Skip to content

Commit

Permalink
release updates in code
Browse files Browse the repository at this point in the history
  • Loading branch information
rutvik110 committed Sep 8, 2022
1 parent 4daf926 commit 795afde
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 79 deletions.
File renamed without changes.
173 changes: 113 additions & 60 deletions bin/asset_manager.dart
Original file line number Diff line number Diff line change
@@ -1,82 +1,135 @@
import 'dart:io';

import 'package:args/args.dart';
import 'package:mason_logger/mason_logger.dart';

void main(List<String> 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<String> 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 <command> [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<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.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<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));
}
}

return assetString;
}
7 changes: 7 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
20 changes: 1 addition & 19 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ environment:

dependencies:
args: ^2.3.1
mason_logger: ^0.1.2
executables:
asset_manager:

Expand All @@ -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

0 comments on commit 795afde

Please sign in to comment.