-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
121 additions
and
79 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters