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

Adapt to resources identifier #734

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions pkgs/asset_bundle/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# https://dart.dev/guides/libraries/private-files
# Created by `dart pub`
.dart_tool/

# Avoid committing pubspec.lock for library packages; see
# https://dart.dev/guides/libraries/private-files#pubspeclock.
pubspec.lock
9 changes: 9 additions & 0 deletions pkgs/asset_bundle/lib/asset_bundle.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// Support for doing something awesome.
///
/// More dartdocs go here.
library;

export 'src/asset_bundle_base.dart';
export 'src/resources.dart';

// TODO: Export any libraries intended for clients of this package.
83 changes: 83 additions & 0 deletions pkgs/asset_bundle/lib/src/asset_bundle_base.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import 'dart:io';
import 'dart:typed_data';

abstract class ResourcesAssets<T> extends Assets {
final Map<T, int> assetToPart;

ResourcesAssets(this.assetToPart);

ResourcesAssets.fromJsonString(this.assetToPart) : super.fromJsonString();

@override
Map<String, dynamic> serialize();
}

abstract class Assets<T> {
Assets();

Assets.fromJsonString();

Map<String, dynamic> serialize();
}

class NetworkAssetBundle extends CachedAssetBundle {
NetworkAssetBundle._privateConstructor();

static final NetworkAssetBundle instance =
NetworkAssetBundle._privateConstructor();

final client = HttpClient();

@override
Future<Uint8List> fetchAsset(String key) async {
var request = await client.get('host', 72, 'assets/$key');
var response = await request.done;
var b = BytesBuilder();
response.forEach((element) {
b.add(element);
});
return b.toBytes();
}

// Mock, to be called after kernel compilation, after linking.
@override
void registerAsset<T extends Object>(String key, T asset) {}
}

class CachedAssetBundle extends AssetBundle {
CachedAssetBundle._privateConstructor();

static final CachedAssetBundle instance =
CachedAssetBundle._privateConstructor();

CachedAssetBundle();

final Map<String, Uint8List> assets = {};

@override
Future<Uint8List> load(String key) async {
var fileContent = assets[key];
if (fileContent == null) {
fileContent = await fetchAsset(key);
assets[key] = fileContent;
}
return fileContent;
}

Future<Uint8List> fetchAsset(String key) async {
return await File('assets/$key').readAsBytes();
}

// Mock, to be called after kernel compilation, after linking.
@override
void registerAsset<T extends Object>(String key, T asset) {
//store asset at filepath
}
}

abstract class AssetBundle {
Future<Uint8List> load(String key);

// Mock, to be called after kernel compilation, after linking.
void registerAsset<T extends Object>(String key, T asset) {}
}
115 changes: 115 additions & 0 deletions pkgs/asset_bundle/lib/src/resources.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import 'dart:convert';

class Resources {
factory Resources.fromString(String jsonString) {
var decoded = jsonDecode(jsonString) as Map<String, dynamic>;
return Resources(
identifiers: (decoded['identifiers'] as List)
.map((e) => Identifier.fromJson(e))
.toList(),
);
}

final List<Identifier> identifiers;

Resources({required this.identifiers});
}

class Identifier {
final String name;
final String id;
final String uri;
final bool nonConstant;
final List<ResourceFile> files;

Identifier({
required this.name,
required this.id,
required this.uri,
required this.nonConstant,
required this.files,
});

@override
String toString() {
return 'Identifier(name: $name, uri: $uri, nonConstant: $nonConstant, files: $files)';
}

factory Identifier.fromJson(Map<String, dynamic> map) {
return Identifier(
name: map['name'] ?? '',
id: map['id'] ?? '',
uri: map['uri'] ?? '',
nonConstant: map['nonConstant'] ?? false,
files: List<ResourceFile>.from(
map['files']?.map((x) => ResourceFile.fromJson(x))),
);
}
}

class ResourceFile {
final int part;
final List<ResourceReference> references;

ResourceFile({required this.part, required this.references});

Map<String, dynamic> toJson() {
return {
'part': part,
'references': references.map((x) => x.toJson()).toList(),
};
}

@override
String toString() => 'ResourceFile(part: $part, references: $references)';

factory ResourceFile.fromJson(Map<String, dynamic> map) {
return ResourceFile(
part: map['part']?.toInt() ?? 0,
references: List<ResourceReference>.from(
map['references']?.map((x) => ResourceReference.fromJson(x))),
);
}
}

class ResourceReference {
final String uri;
final int line;
final int column;
final Map<String, Object> arguments;

ResourceReference({
required this.uri,
required this.line,
required this.column,
required this.arguments,
});

Map<String, dynamic> toJson() {
return {
'@': {
'uri': uri,
'line': line,
'column': column,
},
...arguments,
};
}

@override
String toString() {
return 'ResourceReference(uri: $uri, line: $line, column: $column, arguments: $arguments)';
}

factory ResourceReference.fromJson(Map<String, dynamic> map) {
return ResourceReference(
uri: map['@']['uri'] ?? '',
line: map['@']['line']?.toInt() ?? 0,
column: map['@']['column']?.toInt() ?? 0,
arguments: Map<String, Object>.fromEntries(map
.map((key, value) => MapEntry(key, value as Object))
.entries
.skip(1)),
);
}
}
15 changes: 15 additions & 0 deletions pkgs/asset_bundle/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: asset_bundle
description: A starting point for Dart libraries or applications.
version: 1.0.0
# repository: https://github.com/my_org/my_repo

environment:
sdk: ^3.2.0-131.0.dev

# Add regular dependencies here.
dependencies:
# path: ^1.8.0

dev_dependencies:
lints: ^2.1.0
test: ^1.24.0
1 change: 1 addition & 0 deletions pkgs/messages/example_json/assets/about_messages_en
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[0,"en","skm01b",0,{"0":["Welcome von <",["8","0"],["d","1"]],"1":["About ",["6","0"]],"2":[6,"test ",[3,0,["test new messages",["5","0"]],[2,"No new messages",4,"One new message",5,"Two new Messages"]]],"3":[6,"test ",[4,0,"Two new Messages",{"male":"No new messages","female":"One new message"}]]}]
1 change: 1 addition & 0 deletions pkgs/messages/example_json/assets/about_messages_fr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[0,"fr","3nj3c2",0,{"0":["Welcome von <",["8","0"],["d","1"]],"1":["Sur ",["4","0"]],"2":[6,"test ",[3,0,["test new messages",["5","0"]],[2,"No new messages",4,"One new message",5,"Two new Messages"]]],"3":[6,"test ",[4,0,"Two new Messages",{"male":"No new messages","female":"One new message"}]]}]
1 change: 1 addition & 0 deletions pkgs/messages/example_json/assets/messages_resources
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions pkgs/messages/example_json/assets/name_1_en
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[0,"en","skm01b",0,{"0":null,"1":["About ",["6","0"]],"2":null,"3":null}]
14 changes: 4 additions & 10 deletions pkgs/messages/example_json/bin/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,25 @@

// 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 messages = AboutPageMessages(OldIntlObject());
// final index = AboutPageMessagesEnum.aboutMessage;

messages.loadLocale('en');
print('AboutMessage en:');
print('\t${messages.aboutMessage(websitename: 'typesafe.en')}');
print('\t${await 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')}');
messages.currentLocale = 'fr';
print('\t${await messages.aboutMessage(websitename: 'typesafe.fr')}');

///To enable this, add `generateFindById: true` to the pubspec section
// print('\t${messages.getById('aboutMessage', ['get-by-id.fr'])}');
Expand Down
26 changes: 26 additions & 0 deletions pkgs/messages/example_json/build_script.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'dart:io';

import 'package:native_assets_cli/build_files_code.dart';
import 'package:data_assets_cli/build_files_data.dart';
import 'package:path/path.dart' as path;

Future<void> main(List<String> args) async {
// Process.runSync('dart', ['compile', 'exe', 'bin/example.dart']);
final files = await Directory('lib').list().toList();

final filesPerPackage = {
'messages': files
.whereType<File>()
.map((e) => e.path)
.where((file) => path.extension(file) == '.json')
.map((event) =>
(input: path.setExtension(event, '.g.dart'), output: event))
.toList(),
};

final buildOutput = [
CodeBuildOutput(filesPerPackage: filesPerPackage).toJson(),
DataBuildOutput(filesPerPackage: filesPerPackage).toJson(),
];
print(buildOutput);
}
86 changes: 0 additions & 86 deletions pkgs/messages/example_json/lib/testarb.g.dart

This file was deleted.

Loading
Loading