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

Add package:messages_builder #721

Merged
merged 47 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
1f06f9b
First commit
mosuem Sep 11, 2023
9f0d30c
Small changes
mosuem Sep 11, 2023
8203fe3
Add to main readme
mosuem Sep 11, 2023
f240e3e
Add license headers
mosuem Sep 11, 2023
d10927b
Merge branch 'main' into addMessagesWeb
mosuem Sep 11, 2023
4667657
Add some workflows
mosuem Sep 11, 2023
5905e52
Some small changes
mosuem Sep 11, 2023
8347d5d
Remove gitignores
mosuem Sep 11, 2023
6c61b58
Dart fix
mosuem Sep 11, 2023
22fc064
Merge remote-tracking branch 'refs/remotes/origin/addMessagesWeb' int…
mosuem Sep 11, 2023
9b2e191
Remove whitespace
mosuem Sep 11, 2023
dc34797
Format
mosuem Sep 11, 2023
348d9bd
Remove varint
mosuem Sep 11, 2023
9d706e2
refactor test
mosuem Sep 11, 2023
0c063dc
Remove nesting in dart file
mosuem Sep 11, 2023
59ae0e3
Rename workflows
mosuem Sep 11, 2023
c16b34e
Merge branch 'main' into addMessagesWeb
mosuem Sep 12, 2023
ff3fb2a
Remove unneeded variables
mosuem Sep 12, 2023
721ba2d
Add pubspec descriptions
mosuem Sep 12, 2023
e983e0d
Make version lowercase
mosuem Sep 12, 2023
8fd60cd
Introduce preamble
mosuem Sep 12, 2023
01001ea
Fix
mosuem Sep 12, 2023
884a6a5
Initial commit
mosuem Sep 12, 2023
b1e3b86
Small fixes
mosuem Sep 12, 2023
29b6f67
Fix example in readme
mosuem Sep 12, 2023
2fa7926
Add license headers
mosuem Sep 12, 2023
08c52b9
Add more license headers and remove test
mosuem Sep 12, 2023
2a7e672
Merge branch 'addMessagesWeb' into addMessagesBuilder
mosuem Sep 12, 2023
4eb8aa8
Add workflows
mosuem Sep 12, 2023
e414bc9
retrigger checks
mosuem Sep 12, 2023
a4d696b
retrigger checks
mosuem Sep 12, 2023
5607d9e
Run pub get in example
mosuem Sep 12, 2023
df71f3a
run pub get in example
mosuem Sep 12, 2023
ea09f26
Fix analyzer
mosuem Sep 12, 2023
f2e80b0
Format file
mosuem Sep 12, 2023
5c65c0a
Merge branch 'main' into addMessagesBuilder
mosuem Sep 15, 2023
2c53f48
Merge branch 'main' into addMessagesBuilder
mosuem Sep 15, 2023
0690abb
Small adjustments
mosuem Sep 15, 2023
e3f2e94
Some fixes
mosuem Sep 15, 2023
29a0877
Fix readme
mosuem Sep 15, 2023
a5623c8
Remove cleaner
mosuem Sep 15, 2023
e9a5b7f
Remove inline
mosuem Sep 15, 2023
ad0467d
Remove async
mosuem Sep 15, 2023
99cd3a0
Adapt pubspec
mosuem Sep 15, 2023
2846e44
Rebuild example
mosuem Sep 18, 2023
e6eca85
Changes as per review
mosuem Sep 21, 2023
0990e2b
Add comments
mosuem Sep 21, 2023
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: 3 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@

'package:messages_deserializer':
- 'pkgs/messages_deserializer/**'

'package:messages_builder':
- 'pkgs/messages_builder/**'
2 changes: 2 additions & 0 deletions .github/workflows/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ jobs:

- run: dart pub get

- run: (cd example_json; dart pub get)

- run: dart analyze --fatal-infos

- run: dart format --output=none --set-exit-if-changed .
Expand Down
44 changes: 44 additions & 0 deletions .github/workflows/messages_builder.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: package:messages_builder
permissions: read-all

on:
pull_request:
branches: [ main ]
paths:
- '.github/workflows/messages_builder.yml'
- 'pkgs/messages_builder/**'
push:
branches: [ main ]
paths:
- '.github/workflows/messages_builder.yml'
- 'pkgs/messages_builder/**'
schedule:
- cron: '0 0 * * 0' # weekly

jobs:
build:
runs-on: ubuntu-latest
defaults:
run:
working-directory: pkgs/messages_builder
strategy:
matrix:
sdk: [stable, dev] # {pkgs.versions}
include:
- sdk: stable
run-tests: true
steps:
- uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9
- uses: dart-lang/setup-dart@d6a63dab3335f427404425de0fbfed4686d93c4f
with:
sdk: ${{matrix.sdk}}

- run: dart pub get

- run: dart analyze --fatal-infos

- run: dart format --output=none --set-exit-if-changed .
if: ${{matrix.run-tests}}

- run: dart test
if: ${{matrix.run-tests}}
6 changes: 6 additions & 0 deletions pkgs/messages/example_json/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Files and directories created by pub.
.dart_tool/
.packages

# Conventional directory for build output.
build/
11 changes: 11 additions & 0 deletions pkgs/messages/example_json/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
An example project using `package:messages`.

To regenerate, run
```bash
dart run build_runner build
```

or use `dart run tools/daemon.dart` for a faster rebuild.


The options in the `pubspec.yaml` can be adjusted by changes the values for the `messages` key.
9 changes: 9 additions & 0 deletions pkgs/messages/example_json/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
include: package:dart_flutter_team_lints/analysis_options.yaml

linter:
rules:
- conditional_uri_does_not_exist
- prefer_const_constructors
- prefer_final_locals
- prefer_relative_imports
- unnecessary_parenthesis
38 changes: 38 additions & 0 deletions pkgs/messages/example_json/bin/example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// ignore_for_file: prefer_function_declarations_over_variables

import 'dart:io';

import 'package:example_json/testarbctx2.g.dart';
import 'package:messages/package_intl_object.dart';

Future<void> main(List<String> arguments) async {
final messages = AboutPageMessages(
(String id) => File('lib/$id').readAsStringSync(),
OldIntlObject(),
);
// final index = AboutPageMessagesEnum.aboutMessage;
mosuem marked this conversation as resolved.
Show resolved Hide resolved

messages.loadLocale('en');
print('AboutMessage en:');
print('\t${messages.aboutMessage(websitename: 'typesafe.en')}');

///To enable this, add `generateFindById: true` to the pubspec section
// print('\t${messages.getById('aboutMessage', ['get-by-id.en'])}');

///To enable this, add `generateFindBy: enumerate` to the pubspec section
// print('\t${messages.getByEnum(index, ['get-by-index.en'])}');

messages.loadLocale('fr');
print('AboutMessage fr:');
print('\t${messages.aboutMessage(websitename: 'typesafe.fr')}');

///To enable this, add `generateFindById: true` to the pubspec section
// print('\t${messages.getById('aboutMessage', ['get-by-id.fr'])}');

///To enable this, add `generateFindBy: enumerate` to the pubspec section
// print('\t${messages.getByEnum(index, ['get-by-index.fr'])}');
}
40 changes: 40 additions & 0 deletions pkgs/messages/example_json/lib/testarb.arb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"@@locale": "en",
"@@x-reference": true,
"@@context": "HomePage",
"helloAndWelcome": "Welcome {firstName} von {lastName}!",
"@helloAndWelcome": {
"description": "Initial welcome message",
"placeholders": {
"firstName": {
"type": "String"
},
"lastName": {
"type": "String"
}
}
},
"newMessages": "test {newMessages, plural, =0{No new messages} =1 {One new message} two{Two new Messages} other {test {newMessages} new messages}}",
"@newMessages": {
"type": "text",
"description": "Number of new messages in inbox.",
"placeholders": {
"newMessages": {
"type": "int"
}
}
},
"newMessages2": "test {gender, select, male{No new messages} female {One new message} other{Two new Messages} other {test {gender} new messages of type {newVar}}}",
"@newMessages2": {
"type": "text",
"placeholders": {
"gender": {
"type": "String"
},
"newVar": {
"type": "int"
}
}
},
"helloAndWelcome2": "Welcome {firstName} von {lastName}!"
}
80 changes: 80 additions & 0 deletions pkgs/messages/example_json/lib/testarb.g.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Generated by package:messages_builder

import 'package:messages/messages_json.dart';
mosuem marked this conversation as resolved.
Show resolved Hide resolved

class HomePageMessages {
HomePageMessages(
this._fileLoader,
this.intlObject,
);

final String Function(String id) _fileLoader;

String _currentLocale = 'en';

final Map<String, MessageList> _messages = {};

final _carbs = {'de': 'testarb_de.json', 'en': 'testarb.json'};

final _messageListHashes = {
'testarb_de.json': '8qk919',
'testarb.json': 's69t31'
};

IntlObject intlObject;

String get currentLocale => _currentLocale;
MessageList get _currentMessages => _messages[currentLocale]!;
Iterable<String> get knownLocales => _carbs.keys;
void loadLocale(String locale) {
if (!_messages.containsKey(locale)) {
final carb = _carbs[locale];
if (carb == null) {
throw ArgumentError('Locale $locale is not in $knownLocales');
}
final data = _fileLoader(carb);
final messageList = MessageListJson.fromString(data, intlObject);
if (messageList.preamble.hash != _messageListHashes[carb]) {
throw ArgumentError('''
Messages file has different hash "${messageList.preamble.hash}" than generated code "${_messageListHashes[carb]}".''');
}
_messages[locale] = messageList;
}
_currentLocale = locale;
}

void loadAllLocales() {
for (var locale in knownLocales) {
loadLocale(locale);
}
}

String helloAndWelcome({
required String firstName,
required String lastName,
}) =>
_currentMessages.generateStringAtIndex(
HomePageMessagesEnum.helloAndWelcome.index, [firstName, lastName]);
String newMessages({required int newMessages}) =>
mosuem marked this conversation as resolved.
Show resolved Hide resolved
_currentMessages.generateStringAtIndex(
HomePageMessagesEnum.newMessages.index, [newMessages]);
String newMessages2({
required String gender,
required int newVar,
}) =>
_currentMessages.generateStringAtIndex(
HomePageMessagesEnum.newMessages2.index, [gender, newVar]);
String helloAndWelcome2({
required String firstName,
required String lastName,
}) =>
_currentMessages.generateStringAtIndex(
HomePageMessagesEnum.helloAndWelcome2.index, [firstName, lastName]);
}

enum HomePageMessagesEnum {
helloAndWelcome,
newMessages,
newMessages2,
helloAndWelcome2
}
1 change: 1 addition & 0 deletions pkgs/messages/example_json/lib/testarb.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[0,"en","s69t31",0,["Welcome von !",["8","0"],["d","1"]],[6,"test ",[3,0,["test new messages",["5","0"]],[2,"No new messages",4,"One new message",5,"Two new Messages"]]],[6,"test ",[4,0,"Two new Messages",{"male":"No new messages","female":"One new message"}]],["Welcome von !",["8","0"],["d","1"]]]
14 changes: 14 additions & 0 deletions pkgs/messages/example_json/lib/testarb_de.arb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"@@locale":"de",
"@@context": "HomePage",
"helloAndWelcome": "Willkommen {firstName} von {lastName}",
"newMessages": "testde {newMessages, plural, =0 {No new messages} =1 {One new message} two{Two new Messages} other {test {newMessages} new messages}}",
"@newMessages": {
"placeholders": {
"newMessages" : {
"type":"int"
}
}
},
"newMessages2": "testdse is just a simple message"
}
1 change: 1 addition & 0 deletions pkgs/messages/example_json/lib/testarb_de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[0,"de","8qk919",0,["Willkommen von ",["b","0"],["g","1"]],[6,"testde ",[3,0,["test new messages",["5","0"]],[2,"No new messages",4,"One new message",5,"Two new Messages"]]],"testdse is just a simple message"]
42 changes: 42 additions & 0 deletions pkgs/messages/example_json/lib/testarbctx2.arb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"@@locale": "en",
"@@x-reference": true,
"@@context": "AboutPage",
"helloAndWelcome": "Welcome {firstName} von {lastName} <",
"@helloAndWelcome": {
"description": "Initial welcome message",
"placeholders": {
"firstName": {},
"lastName": {}
}
},
"aboutMessage": "About {websitename}",
"@aboutMessage": {
"description": "About message",
"placeholders": {
"websitename": {}
}
},
"newMessages": "test {newMessages, plural, =0 {No new messages} =1 {One new message} two{Two new Messages} other {test {newMessages} new messages}}",
"@newMessages": {
"type": "text",
"description": "Number of new messages in inbox.",
"placeholders": {
"newMessages": {
"type": "int"
}
}
},
"newMessages2": "test {gender, select,male {No new messages} female {One new message} other{Two new Messages} other {test {gender} new messages of type {newVar}}}",
"@newMessages2": {
"type": "text",
"placeholders": {
"gender": {
"type": "String"
},
"newVar": {
"type": "int"
}
}
}
}
77 changes: 77 additions & 0 deletions pkgs/messages/example_json/lib/testarbctx2.g.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Generated by package:messages_builder

import 'package:messages/messages_json.dart';

class AboutPageMessages {
AboutPageMessages(
this._fileLoader,
this.intlObject,
);

final String Function(String id) _fileLoader;

String _currentLocale = 'en';

final Map<String, MessageList> _messages = {};

final _carbs = {'fr': 'testarbctx2_fr.json', 'en': 'testarbctx2.json'};

final _messageListHashes = {
'testarbctx2_fr.json': '3nj3c2',
'testarbctx2.json': 'skm01b'
};

IntlObject intlObject;

String get currentLocale => _currentLocale;
MessageList get _currentMessages => _messages[currentLocale]!;
Iterable<String> get knownLocales => _carbs.keys;
void loadLocale(String locale) {
if (!_messages.containsKey(locale)) {
final carb = _carbs[locale];
if (carb == null) {
throw ArgumentError('Locale $locale is not in $knownLocales');
}
final data = _fileLoader(carb);
final messageList = MessageListJson.fromString(data, intlObject);
if (messageList.preamble.hash != _messageListHashes[carb]) {
throw ArgumentError('''
Messages file has different hash "${messageList.preamble.hash}" than generated code "${_messageListHashes[carb]}".''');
}
_messages[locale] = messageList;
}
_currentLocale = locale;
}

void loadAllLocales() {
for (var locale in knownLocales) {
loadLocale(locale);
}
}

String helloAndWelcome({
required String firstName,
required String lastName,
}) =>
_currentMessages.generateStringAtIndex(
AboutPageMessagesEnum.helloAndWelcome.index, [firstName, lastName]);
String aboutMessage({required String websitename}) =>
_currentMessages.generateStringAtIndex(
AboutPageMessagesEnum.aboutMessage.index, [websitename]);
String newMessages({required int newMessages}) =>
_currentMessages.generateStringAtIndex(
AboutPageMessagesEnum.newMessages.index, [newMessages]);
String newMessages2({
required String gender,
required int newVar,
}) =>
_currentMessages.generateStringAtIndex(
AboutPageMessagesEnum.newMessages2.index, [gender, newVar]);
}

enum AboutPageMessagesEnum {
helloAndWelcome,
aboutMessage,
newMessages,
newMessages2
}
1 change: 1 addition & 0 deletions pkgs/messages/example_json/lib/testarbctx2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[0,"en","skm01b",0,["Welcome von <",["8","0"],["d","1"]],["About ",["6","0"]],[6,"test ",[3,0,["test new messages",["5","0"]],[2,"No new messages",4,"One new message",5,"Two new Messages"]]],[6,"test ",[4,0,"Two new Messages",{"male":"No new messages","female":"One new message"}]]]
Loading
Loading