Skip to content

Commit

Permalink
feat: added optional short_app_name web config (#54) (#55)
Browse files Browse the repository at this point in the history
* feat: added optional short_app_name web config (#54)

* fix: message decoration

* fix: check short app name type
  • Loading branch information
albemala authored May 14, 2024
1 parent 6016aae commit 448f65f
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ package_rename_config:
copyright_notice: # (String) The product copyright of the macos app
web:
app_name: # (String) The title and display name of the web app and PWA
app_name: # (String) The title of the web app and PWA
short_app_name: # (String) The short display name of the PWA (Optional, defaults to app_name if not set)
description: # (String) The description of the web app and PWA
windows:
Expand Down
1 change: 1 addition & 0 deletions example/package_rename_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package_rename_config:

web:
app_name: Package Rename Demo
short_app_name: Package Rename
description: CLI tool to rename package configurations in Flutter project.

linux:
Expand Down
1 change: 1 addition & 0 deletions lib/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const _projectFileName = 'project.pbxproj';
// ! Keys
const _configKey = 'package_rename_config';
const _appNameKey = 'app_name';
const _shortAppNameKey = 'short_app_name';
const _packageNameKey = 'package_name';
const _bundleNameKey = 'bundle_name';
const _descriptionKey = 'description';
Expand Down
5 changes: 5 additions & 0 deletions lib/exceptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,9 @@ class _PackageRenameErrors {
_macOSProjectFileNotFoundMessage,
33,
);

static const invalidShortAppName = _PackageRenameException(
_invalidShortAppNameMessage,
34,
);
}
6 changes: 6 additions & 0 deletions lib/messages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ const _invalidAppNameMessage = '''
╚═══════════════════════════════════════════╝
''';

const _invalidShortAppNameMessage = '''
╔═══════════════════════════════════════════════════════╗
║ short_app_name (Short App Name) must be a String. ║
╚═══════════════════════════════════════════════════════╝
''';

const _androidMainManifestNotFoundMessage = '''
╔═══════════════════════════════════════════════════════════════╗
║ AndroidManifest.xml not found in `android/app/src/main/`. ║
Expand Down
19 changes: 14 additions & 5 deletions lib/platforms/web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ void _setWebConfigurations(dynamic webConfig) {
final webConfigMap = Map<String, dynamic>.from(webConfig);

_setWebTitle(webConfigMap[_appNameKey]);
_setPWAAppName(webConfigMap[_appNameKey]);
_setPWAAppName(
webConfigMap[_appNameKey],
webConfigMap[_shortAppNameKey],
);
_setWebDescription(webConfigMap[_descriptionKey]);
_setPWADescription(webConfigMap[_descriptionKey]);
} on _PackageRenameException catch (e) {
Expand Down Expand Up @@ -59,11 +62,16 @@ void _setWebTitle(dynamic appName) {
}
}

void _setPWAAppName(dynamic appName) {
void _setPWAAppName(dynamic appName, dynamic shortAppName) {
try {
if (appName == null) return;
if (appName is! String) throw _PackageRenameErrors.invalidAppName;

if (shortAppName != null && shortAppName is! String) {
throw _PackageRenameErrors.invalidShortAppName;
}
final actualShortAppName = shortAppName is String ? shortAppName : appName;

final webManifestFile = File(_webManifestFilePath);
if (!webManifestFile.existsSync()) {
_logger.w('Web manifest.json not found!!!');
Expand All @@ -76,21 +84,22 @@ void _setPWAAppName(dynamic appName) {
) as Map<String, dynamic>;

webManifestJson['name'] = appName;
webManifestJson['short_name'] = appName;
webManifestJson['short_name'] = actualShortAppName;

const encoder = JsonEncoder.withIndent(' ');
webManifestFile.writeAsStringSync('${encoder.convert(webManifestJson)}\n');

_logger.i('PWA name set to: `$appName` (manifest.json)');
_logger.i('PWA short name set to: `$actualShortAppName` (manifest.json)');
} on _PackageRenameException catch (e) {
_logger
..e('${e.message}ERR Code: ${e.code}')
..e('PWA Name change failed!!!');
..e('PWA Name/Short Name change failed!!!');
} catch (e) {
_logger
..w(e.toString())
..e('ERR Code: 255')
..e('PWA Name change failed!!!');
..e('PWA Name/Short Name change failed!!!');
} finally {
if (appName != null) _logger.f(_minorTaskDoneLine);
}
Expand Down

0 comments on commit 448f65f

Please sign in to comment.