Skip to content
Draft
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
21 changes: 11 additions & 10 deletions packages/mason/lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:convert';
import 'dart:io' show Directory, File, FileMode, FileSystemEntity, Process;
import 'dart:isolate';
import 'dart:typed_data';

import 'package:checked_yaml/checked_yaml.dart';
import 'package:collection/collection.dart';
Expand Down Expand Up @@ -202,7 +203,7 @@ abstract class Generator implements Comparable<Generator> {
/// Map of partial files which will be used as includes.
///
/// Contains a Map of partial file path to partial file content.
final Map<String, List<int>> partials = {};
final Map<String, Uint8List> partials = {};

/// Add a new template file.
void addTemplateFile(TemplateFile? file) {
Expand Down Expand Up @@ -238,7 +239,7 @@ abstract class Generator implements Comparable<Generator> {
final resultFiles = await _runSubstitutionAsync(
file,
Map<String, dynamic>.of(vars),
Map<String, List<int>>.of(partials),
Map<String, Uint8List>.of(partials),
);
final root = RegExp(r'\w:\\|\w:\/');
final separator = RegExp(r'\/|\\');
Expand Down Expand Up @@ -339,7 +340,7 @@ class DirectoryGeneratorTarget extends GeneratorTarget {
@override
Future<GeneratedFile> createFile(
String path,
List<int> contents, {
Uint8List contents, {
Logger? logger,
OverwriteRule? overwriteRule,
}) async {
Expand Down Expand Up @@ -424,7 +425,7 @@ abstract class GeneratorTarget {
/// Create a file at the given path with the given contents.
Future<GeneratedFile> createFile(
String path,
List<int> contents, {
Uint8List contents, {
Logger? logger,
OverwriteRule? overwriteRule,
});
Expand All @@ -447,12 +448,12 @@ class TemplateFile {
final String path;

/// The template file content.
final List<int> content;
final Uint8List content;

/// Performs a substitution on the [path] based on the incoming [parameters].
Set<FileContents> runSubstitution(
Map<String, dynamic> parameters,
Map<String, List<int>> partials,
Map<String, Uint8List> partials,
) {
var filePath = path.replaceAll(r'\', '/');
if (_loopRegExp().hasMatch(filePath)) {
Expand Down Expand Up @@ -506,9 +507,9 @@ class TemplateFile {
}
}

List<int> _createContent(
Uint8List _createContent(
Map<String, dynamic> vars,
Map<String, List<int>> partials,
Map<String, Uint8List> partials,
) {
try {
final decoded = utf8.decode(content);
Expand All @@ -533,7 +534,7 @@ class FileContents {
final String path;

/// The contents of the file.
final List<int> content;
final Uint8List content;

@override
bool operator ==(Object other) {
Expand Down Expand Up @@ -584,7 +585,7 @@ class _Permutations<T> {
Future<Set<FileContents>> _runSubstitutionAsync(
TemplateFile file,
Map<String, dynamic> vars,
Map<String, List<int>> partials,
Map<String, Uint8List> partials,
) async {
return Isolate.run(() => file.runSubstitution(vars, partials));
}
Expand Down
6 changes: 3 additions & 3 deletions packages/mason/lib/src/hooks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class GeneratorHooks {
static Future<GeneratorHooks> fromBrickYaml(BrickYaml brick) async {
HookFile? preGenHook;
HookFile? postGenHook;
List<int>? pubspec;
Uint8List? pubspec;

final accumulator = AccumulatorSink<Digest>();
final sink = sha1.startChunkedConversion(accumulator);
Expand Down Expand Up @@ -151,7 +151,7 @@ class GeneratorHooks {
final HookFile? postGenHook;

/// Contents of the hooks `pubspec.yaml` if exists.
final List<int>? pubspec;
final Uint8List? pubspec;

/// The hash of the hooks directory and its contents.
final String checksum;
Expand Down Expand Up @@ -403,7 +403,7 @@ class HookFile {
final String path;

/// The template file content.
final List<int> content;
final Uint8List content;
}

/// A reference to core mason APIs to be used within hooks.
Expand Down
11 changes: 7 additions & 4 deletions packages/mason/lib/src/mason_bundle.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:convert';
import 'dart:isolate';
import 'dart:typed_data';

import 'package:archive/archive.dart';
import 'package:json_annotation/json_annotation.dart';
Expand Down Expand Up @@ -58,7 +59,7 @@ class MasonBundle {
_$MasonBundleFromJson(json);

/// Converts a universal bundle into a [MasonBundle] instance.
static Future<MasonBundle> fromUniversalBundle(List<int> bytes) async {
static Future<MasonBundle> fromUniversalBundle(Uint8List bytes) async {
final bundleJson = await Isolate.run(
() => json.decode(
utf8.decode(BZip2Decoder().decodeBytes(bytes)),
Expand Down Expand Up @@ -121,11 +122,13 @@ class MasonBundle {
Map<String, dynamic> toJson() => _$MasonBundleToJson(this);

/// Converts a [MasonBundle] into universal bundle bytes.
Future<List<int>> toUniversalBundle() {
Future<Uint8List> toUniversalBundle() {
return Isolate.run(() => _encodeBundle(this));
}

Future<List<int>> _encodeBundle(MasonBundle bundle) async {
return BZip2Encoder().encode(utf8.encode(json.encode(bundle.toJson())));
Future<Uint8List> _encodeBundle(MasonBundle bundle) async {
return BZip2Encoder().encodeBytes(
utf8.encode(json.encode(bundle.toJson())),
);
}
}
5 changes: 3 additions & 2 deletions packages/mason/lib/src/render.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:convert';
import 'dart:typed_data';

import 'package:mason/mason.dart';
import 'package:mustache_template/mustache_template.dart';
Expand Down Expand Up @@ -97,7 +98,7 @@ extension RenderTemplate on String {
/// {@macro render_template}
String render(
Map<String, dynamic> vars, [
Map<String, List<int>>? partials = const {},
Map<String, Uint8List>? partials = const {},
]) {
final template = Template(
_sanitizeInput(transpiled()),
Expand Down Expand Up @@ -159,7 +160,7 @@ extension on String {
/// A resolver function which given a partial name.
/// attempts to return a new [Template].
/// {@endtemplate}
extension ResolvePartial on Map<String, List<int>> {
extension ResolvePartial on Map<String, Uint8List> {
/// {@macro resolve_partial}
Template? resolve(String name) {
final content = this['{{~ $name }}'];
Expand Down
3 changes: 2 additions & 1 deletion packages/mason/test/src/generator_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// ignore_for_file: missing_whitespace_between_adjacent_strings
import 'dart:io';
import 'dart:typed_data';

import 'package:mason/mason.dart';
import 'package:mocktail/mocktail.dart';
Expand Down Expand Up @@ -1094,7 +1095,7 @@ void main() {
group('runSubstitution', () {
test('handles malformed content', () {
final tempDir = Directory.systemTemp.createTempSync();
final bytes = [0x80, 0x00];
final bytes = Uint8List.fromList([0x80, 0x00]);
final template = TemplateFile.fromBytes(
path.join(tempDir.path, 'malformed.txt'),
bytes,
Expand Down
3 changes: 2 additions & 1 deletion packages/mason_api/lib/src/mason_api.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';

import 'package:cli_util/cli_util.dart';
import 'package:http/http.dart' as http;
Expand Down Expand Up @@ -184,7 +185,7 @@ class MasonApi {
void logout() => _clearCredentials();

/// Publish universal [bundle] to remote registry.
Future<void> publish({required List<int> bundle}) async {
Future<void> publish({required Uint8List bundle}) async {
var credentials = _credentials;

if (credentials == null) {
Expand Down
5 changes: 3 additions & 2 deletions packages/mason_api/test/src/mason_api_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';

import 'package:http/http.dart' as http;
import 'package:mason_api/mason_api.dart';
Expand Down Expand Up @@ -331,7 +332,7 @@ void main() {
);

try {
await masonApi.publish(bundle: <int>[]);
await masonApi.publish(bundle: Uint8List.fromList([]));
fail('should throw');
} on MasonApiPublishFailure catch (error) {
expect(
Expand All @@ -347,7 +348,7 @@ void main() {
});

group('publish', () {
final bytes = <int>[42];
final bytes = Uint8List.fromList([42]);

test('throws when user not found', () async {
File(
Expand Down
2 changes: 2 additions & 0 deletions packages/mason_cli/test/commands/publish_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:io';
import 'dart:typed_data';

import 'package:args/args.dart';
import 'package:args/command_runner.dart';
Expand Down Expand Up @@ -28,6 +29,7 @@ void main() {

setUpAll(() {
registerFallbackValue(FakeUri());
registerFallbackValue(Uint8List.fromList([]));
});

group('PublishCommand', () {
Expand Down
Loading