Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added optional short_app_name web config (#54) #55

Merged
merged 3 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
OutdatedGuy marked this conversation as resolved.
Show resolved Hide resolved
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
15 changes: 10 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,10 +62,11 @@ 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;
final actualShortAppName = shortAppName is String ? shortAppName : appName;
OutdatedGuy marked this conversation as resolved.
Show resolved Hide resolved

final webManifestFile = File(_webManifestFilePath);
if (!webManifestFile.existsSync()) {
Expand All @@ -76,21 +80,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