Skip to content
This repository has been archived by the owner on Jan 25, 2024. It is now read-only.

Commit

Permalink
Merge pull request #278 from Parabeac/sprint/elf
Browse files Browse the repository at this point in the history
Sprint/elf
  • Loading branch information
mergify[bot] authored Feb 23, 2021
2 parents 51e46f6 + 6368a11 commit 98969a0
Show file tree
Hide file tree
Showing 17 changed files with 113 additions and 20 deletions.
1 change: 0 additions & 1 deletion lib/controllers/interpret.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ class Interpret {
log.fine(
'Processed \'${item.name}\' in group \'${group.name}\' with item type: \'${tempTree.tree_type}\'');

tempTree.rootNode;
tempForest.add(tempTree);
}
}
Expand Down
3 changes: 3 additions & 0 deletions lib/eggs/injected_app_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,19 @@ class PBAppBarGenerator extends PBGenerator {

buffer.write('AppBar(');
if (source.leadingItem != null) {
source.leadingItem.currentContext = source.currentContext;
buffer.write(
'leading: ${source.leadingItem.generator.generate(source.leadingItem, generatorContext)},');
}
if (source.middleItem != null) {
source.middleItem.currentContext = source.currentContext;
buffer.write(
'title: ${source.middleItem.generator.generate(source.middleItem, generatorContext)},');
}

if (source.trailingItem != null) {
var trailingItem = '';
source.trailingItem.currentContext = source.currentContext;
trailingItem =
'${source.trailingItem.generator.generate(source.trailingItem, generatorContext)}';
buffer.write('actions: [$trailingItem],');
Expand Down
1 change: 1 addition & 0 deletions lib/generation/generators/layouts/pb_row_gen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class PBRowGenerator extends PBLayoutGenerator {
List<PBIntermediateNode> children = source.children;

for (var child in children) {
child.currentContext = source.currentContext;
buffer.write(child.generator.generate(child, generatorContext));
var trailing_comma = (counter + 1) == children.length ? '' : ',';
buffer.write(trailing_comma);
Expand Down
11 changes: 11 additions & 0 deletions lib/generation/generators/middleware/middleware.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import 'package:parabeac_core/interpret_and_optimize/helpers/pb_gen_cache.dart';
import 'package:recase/recase.dart';

abstract class Middleware {
static var variableNames = {};

PBGenerationManager generationManager;

Middleware(this.generationManager);
Expand All @@ -24,4 +26,13 @@ abstract class Middleware {
void addImportToCache(String id, String path) {
PBGenCache().addToCache(id, path);
}

String getVariableName(String name) {
if (!variableNames.containsKey(name)) {
variableNames[name] = 1;
return name;
} else {
return name + '_' + (++variableNames[name]).toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,21 @@ class BLoCMiddleware extends Middleware {
var generalStateName = node.functionCallName
.substring(0, node.functionCallName.lastIndexOf('/'));

var globalVariableName = node.name.snakeCase;
var globalVariableName = getVariableName(node.name.snakeCase);
managerData.addGlobalVariable(PBVariable(globalVariableName, 'var ', true,
'${generalStateName.pascalCase}Bloc()'));

addImportToCache(node.SYMBOL_ID, getImportPath(node, fileStrategy));

managerData.addToDispose('${globalVariableName}.close()');
node.generator = StringGeneratorAdapter('''
if (node.generator is! StringGeneratorAdapter) {
node.generator = StringGeneratorAdapter('''
BlocBuilder<${generalStateName.pascalCase}Bloc, ${generalStateName.pascalCase}State>(
cubit: ${globalVariableName},
builder: (context, state) => state.widget
)
''');
}
return node;
}
var parentState = getNameOfNode(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@ class ProviderMiddleware extends Middleware {
node.currentContext.project.genProjectData
.addDependencies(PACKAGE_NAME, PACKAGE_VERSION);
managerData.addImport('package:provider/provider.dart');
watcherName = node.name.snakeCase + '_notifier';
var widgetName = node.functionCallName.camelCase;

watcherName = getVariableName(node.name.snakeCase + '_notifier');
var watcher = PBVariable(watcherName, 'final ', true,
'context.watch<${getName(node.functionCallName).pascalCase}>().defaultWidget');
'context.watch<${getName(node.functionCallName).pascalCase}>().${widgetName}');
managerData.addMethodVariable(watcher);

addImportToCache(node.SYMBOL_ID, getImportPath(node, fileStrategy));

node.generator = StringGeneratorAdapter(watcherName);
if (node.generator is! StringGeneratorAdapter) {
node.generator = StringGeneratorAdapter(watcherName);
}
return node;
}
watcherName = getNameOfNode(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,19 @@ class RiverpodMiddleware extends Middleware {
node.currentContext.project.genProjectData
.addDependencies(PACKAGE_NAME, PACKAGE_VERSION);
managerData.addImport('package:flutter_riverpod/flutter_riverpod.dart');
watcherName = node.functionCallName.snakeCase;
watcherName = getVariableName(node.functionCallName.snakeCase);
var watcher = PBVariable(watcherName + '_provider', 'final ', true,
'ChangeNotifierProvider((ref) => ${getName(node.functionCallName).pascalCase}())');

managerData.addMethodVariable(watcher);
var widgetName = node.functionCallName.camelCase;

addImportToCache(node.SYMBOL_ID, getImportPath(node, fileStrategy));

node.generator = StringGeneratorAdapter(getConsumer(watcherName));
if (node.generator is! StringGeneratorAdapter) {
node.generator =
StringGeneratorAdapter(getConsumer(watcherName, widgetName));
}
return node;
}
watcherName = getNameOfNode(node);
Expand All @@ -50,12 +54,12 @@ class RiverpodMiddleware extends Middleware {
return node;
}

String getConsumer(String name) {
String getConsumer(String name, String pointTo) {
return '''
Consumer(
builder: (context, watch, child) {
final ${name} = watch(${name}_provider);
return ${name}.defaultWidget;
return ${name}.${pointTo};
},
)
''';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ class StatefulMiddleware extends Middleware {

if (node is PBSharedInstanceIntermediateNode) {
managerData.addGlobalVariable(await _getVariables(node));
node.generator = StringGeneratorAdapter(node.name.snakeCase);
if (node.generator is! StringGeneratorAdapter) {
node.generator = StringGeneratorAdapter(node.name.snakeCase);
}
addImportToCache(node.SYMBOL_ID, getImportPath(node, fileStrategy));
return node;
}
Expand All @@ -49,8 +51,10 @@ class StatefulMiddleware extends Middleware {
var symbolMaster =
PBSymbolStorage().getSharedMasterNodeBySymbolID(node.SYMBOL_ID);

var watcherName = getVariableName(node.name.snakeCase);

var tempVar = PBVariable(
node.name.snakeCase,
watcherName,
'var ',
true,
node.functionCallName == symbolMaster.name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import 'package:parabeac_core/interpret_and_optimize/entities/subclasses/pb_inte
import 'package:recase/recase.dart';

class MiddlewareUtils {
static String generateChangeNotifierClass(String defaultStateName,
PBGenerationManager manager, PBIntermediateNode node) {
static String generateChangeNotifierClass(
String defaultStateName,
PBGenerationManager manager,
PBIntermediateNode node,
) {
var overrideVars = ''; // Variables outside of initializer
var overrideAttr = ''; // Attributes that will be part of initializer
var stateInitializers = StringBuffer();
Expand Down
1 change: 1 addition & 0 deletions lib/generation/generators/visual-widgets/pb_align_gen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class PBAlignGenerator extends PBGenerator {
'alignment: Alignment(${source.alignX.toStringAsFixed(2)}, ${source.alignY.toStringAsFixed(2)}),');

try {
source.child.currentContext = source.currentContext;
buffer.write(
'child: ${source.child.generator.generate(source.child, generatorContext)},');
} catch (e, stackTrace) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class PBContainerGenerator extends PBGenerator {
Point(source.topLeftCorner.x, source.topLeftCorner.y);
source.child.bottomRightCorner =
Point(source.bottomRightCorner.x, source.bottomRightCorner.y);
source.child.currentContext = source.currentContext;
var statement = source.child != null
? 'child: ${source.child.generator.generate(source.child, generatorContext)}'
: '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class PBFlexibleGenerator extends PBGenerator {
buffer.write('Flexible(');
buffer.write('flex: ${source.flex},');
try {
source.child.currentContext = source.currentContext;
buffer.write(
'child: ${source.child.generator.generate(source.child, generatorContext)},');
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class PBPaddingGen extends PBGenerator {
buffer.write('),');

if (source.child != null) {
source.child.currentContext = source.currentContext;
buffer.write(
'child: ${source.child.generator.generate(source.child, generatorContext)}');
}
Expand Down
8 changes: 6 additions & 2 deletions lib/generation/generators/visual-widgets/pb_text_gen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'package:parabeac_core/generation/generators/attribute-helper/pb_generato
import 'package:parabeac_core/generation/generators/pb_generator.dart';
import 'package:parabeac_core/input/sketch/helper/symbol_node_mixin.dart';
import 'package:parabeac_core/interpret_and_optimize/entities/inherited_text.dart';
import 'package:parabeac_core/interpret_and_optimize/entities/pb_shared_master_node.dart';
import 'package:parabeac_core/interpret_and_optimize/entities/subclasses/pb_intermediate_node.dart';

class PBTextGen extends PBGenerator with PBColorMixin {
Expand All @@ -13,8 +12,13 @@ class PBTextGen extends PBGenerator with PBColorMixin {
String generate(
PBIntermediateNode source, GeneratorContext generatorContext) {
if (source is InheritedText) {
source.currentContext.project.genProjectData
.addDependencies('auto_size_text', '^2.1.0');

source.managerData
.addImport('package:auto_size_text/auto_size_text.dart');
var buffer = StringBuffer();
buffer.write('Text(\n');
buffer.write('AutoSizeText(\n');
var isTextParameter = source.isTextParameter;
if (isTextParameter) {
var text = source.text;
Expand Down
23 changes: 23 additions & 0 deletions lib/input/figma/entities/layers/group.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ class Group extends FigmaFrame implements AbstractFigmaNodeFactory, Image {
this.prototypeNodeUUID = tempPrototypeID;
}

boundaryRectangle = fitFrame();

children.clear();

return Future.value(
Expand All @@ -130,6 +132,27 @@ class Group extends FigmaFrame implements AbstractFigmaNodeFactory, Image {
return true;
}

Frame fitFrame() {
var heights = [];
var widths = [];
for (var child in children) {
heights.add(child.boundaryRectangle.height);
widths.add(child.boundaryRectangle.width);
}

if (heights.every((element) => element == heights[0]) &&
widths.every((element) => element == widths[0])) {
return Frame(
height: heights[0],
width: widths[0],
x: boundaryRectangle.x,
y: boundaryRectangle.y,
);
} else {
return boundaryRectangle;
}
}

String childrenHavePrototypeNode() {
for (DesignNode child in children) {
if (child.prototypeNodeUUID != null) {
Expand Down
6 changes: 3 additions & 3 deletions lib/interpret_and_optimize/entities/layouts/row.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ class PBIntermediateRowLayout extends PBLayoutIntermediateNode {

Map alignment = {};

@override
PBContext currentContext;
// @override
// PBContext currentContext;

@override
PrototypeNode prototypeNode;

PBIntermediateRowLayout(String name, this.UUID, {this.currentContext})
PBIntermediateRowLayout(String name, this.UUID, {PBContext currentContext})
: super(ROW_RULES, ROW_EXCEPTIONS, currentContext, name) {
generator = PBRowGenerator();
checkCrossAxisAlignment();
Expand Down
33 changes: 32 additions & 1 deletion test/lib/generation/generator_test.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import 'package:parabeac_core/generation/generators/attribute-helper/pb_generator_context.dart';
import 'package:parabeac_core/generation/generators/pb_flutter_generator.dart';
import 'package:parabeac_core/generation/generators/util/pb_generation_project_data.dart';
import 'package:parabeac_core/generation/generators/util/pb_generation_view_data.dart';
import 'package:parabeac_core/generation/generators/visual-widgets/pb_container_gen.dart';
import 'package:parabeac_core/generation/generators/visual-widgets/pb_text_gen.dart';
import 'package:parabeac_core/interpret_and_optimize/entities/inherited_container.dart';
import 'package:parabeac_core/interpret_and_optimize/entities/inherited_text.dart';
import 'package:parabeac_core/interpret_and_optimize/helpers/pb_context.dart';
import 'package:parabeac_core/interpret_and_optimize/helpers/pb_project.dart';
import 'package:parabeac_core/interpret_and_optimize/state_management/intermediate_auxillary_data.dart';
import 'package:parabeac_core/interpret_and_optimize/value_objects/point.dart';
import 'package:test/test.dart';
Expand All @@ -19,22 +23,49 @@ class MockContext extends Mock implements GeneratorContext {}

class MockData extends Mock implements IntermediateAuxiliaryData {}

class MockPBContext extends Mock implements PBContext {}

class MockProject extends Mock implements PBProject {}

class MockGenProjectData extends Mock implements PBGenerationProjectData {}

class MockPBGenViewData extends Mock implements PBGenerationViewData {}

void main() {
group('Generator test', () {
var mockManager, mockGenerator, mockTextGenerator;
var mockInheritedContainer, mockInheritedText;
var mockContext, mockData;
var mockPBContext;
var mockProject;
var mockGenProjectData;
var mockGenViewData;
setUp(() {
mockInheritedContainer = MockInheritedContainer();
mockInheritedText = MockInheritedText();
mockContext = MockContext();
mockData = MockData();
mockPBContext = MockPBContext();
mockProject = MockProject();
mockGenProjectData = MockGenProjectData();
mockGenViewData = MockPBGenViewData();

when(mockInheritedContainer.child).thenReturn(mockInheritedText);

when((mockInheritedContainer as MockInheritedContainer).auxiliaryData)
.thenReturn(mockData);

when((mockInheritedText as MockInheritedText).currentContext)
.thenReturn(mockPBContext);

when((mockPBContext as MockPBContext).project).thenReturn(mockProject);

when((mockProject as MockProject).genProjectData)
.thenReturn(mockGenProjectData);

when((mockInheritedText as MockInheritedText).managerData)
.thenReturn(mockGenViewData);

when(mockInheritedContainer.topLeftCorner).thenReturn(Point(0, 0));

when(mockInheritedContainer.bottomRightCorner)
Expand Down Expand Up @@ -65,7 +96,7 @@ void main() {
expect(result is String, true);

// Do not modify
expect(result, '''Container(child: Text(
expect(result, '''Container(child: AutoSizeText(
'Test Text',
style: TextStyle(
),
Expand Down

0 comments on commit 98969a0

Please sign in to comment.