-
Notifications
You must be signed in to change notification settings - Fork 35
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
2 changed files
with
72 additions
and
80 deletions.
There are no files selected for viewing
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,8 +1,74 @@ | ||
/// Support for doing something awesome. | ||
/// | ||
/// More dartdocs go here. | ||
library message_shrinker; | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
|
||
export 'src/messages_shrinker.dart'; | ||
import 'package:messages/package_intl_object.dart'; | ||
import 'package:messages_deserializer/messages_deserializer_json.dart'; | ||
import 'package:messages_serializer/messages_serializer.dart'; | ||
|
||
// TODO: Export any libraries intended for clients of this package. | ||
class MessageShrinker { | ||
void shrink( | ||
String dataFile, | ||
String constInstancesFile, | ||
String outputFile, | ||
) { | ||
final constInstances = | ||
_parseConstInstances(File(constInstancesFile).readAsStringSync()); | ||
|
||
final file = File(dataFile); | ||
if (dataFile.endsWith('.json')) { | ||
final buffer = file.readAsStringSync(); | ||
final newBuffer = shrinkJson(buffer, constInstances); | ||
final newFile = File(outputFile)..createSync(recursive: true); | ||
newFile.writeAsStringSync(newBuffer); | ||
} else { | ||
throw ArgumentError('Not a valid Message file'); | ||
} | ||
} | ||
|
||
/// Shrink a json message file by de- and reserializing, keeping only the | ||
/// message indices in [messagesToKeep]. | ||
String shrinkJson(String buffer, List<int> messagesToKeep) { | ||
final sizeBefore = buffer.length; | ||
final json = JsonDeserializer(buffer).deserialize(OldIntlObject()); | ||
final data = JsonSerializer(json.preamble.hasIds) | ||
.serialize( | ||
json.preamble.hash, | ||
json.preamble.locale, | ||
json.messages, | ||
messagesToKeep, | ||
) | ||
.data; | ||
final sizeAfter = data.length; | ||
final change = (sizeBefore - sizeAfter) / sizeBefore; | ||
final changeInPercent = (change * 100).toStringAsFixed(2); | ||
print('Reduced size from $sizeBefore to $sizeAfter by $changeInPercent %'); | ||
return data; | ||
} | ||
|
||
/// Receive a file emitted by const_finder from the SDK, which has the | ||
/// structure: | ||
/// ```json | ||
/// { | ||
/// "constantInstances": [ | ||
/// { | ||
/// "index": 1, | ||
/// "_name": "constantNameOne" | ||
/// }, | ||
/// { | ||
/// "index": 2, | ||
/// "_name": "constantNameTwo" | ||
/// }, | ||
/// ... | ||
/// ], | ||
/// "nonConstantLocations": [] | ||
/// } | ||
/// ``` | ||
List<int> _parseConstInstances(String fileContents) { | ||
final decoded = jsonDecode(fileContents) as Map<String, dynamic>; | ||
final instances = decoded['constantInstances'] as List; | ||
return instances | ||
.map((e) => e as Map<String, dynamic>) | ||
.map((e) => e['index'] as int) | ||
.toList(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.