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 #595 from Parabeac/release/2.4.0
Browse files Browse the repository at this point in the history
Release/2.4.0
  • Loading branch information
ivan-015 committed Feb 22, 2022
2 parents 45fd72b + c6bf10b commit de2aff6
Show file tree
Hide file tree
Showing 32 changed files with 538 additions and 416 deletions.
3 changes: 2 additions & 1 deletion lib/configurations/configurations.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
"desktop": 1280
},
"scaling": true,
"enablePrototyping": false
"enablePrototyping": false,
"componentIsolation": "none"
}
20 changes: 13 additions & 7 deletions lib/controllers/interpret.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Interpret {

PBPrototypeLinkerService _pbPrototypeLinkerService;

final List<AITHandler> aitHandlers = [
List<AITHandler> aitHandlers = [
StateManagementNodeInterpreter(),
PBSymbolLinkerService(),
// PBPluginControlService(),
Expand All @@ -43,9 +43,13 @@ class Interpret {
Future<PBIntermediateTree> interpretAndOptimize(
PBIntermediateTree tree, PBContext context, PBProject project,
{List<AITHandler> handlers, AITServiceBuilder aitServiceBuilder}) async {
handlers ??= aitHandlers;
if (handlers == null || handlers.isEmpty) {
handlers = aitHandlers;
} else {
handlers.addAll(aitHandlers);
}

aitServiceBuilder ??= AITServiceBuilder(aitHandlers);
aitServiceBuilder ??= AITServiceBuilder(handlers);
var elementStorage = ElementStorage();

elementStorage.elementToTree[tree.rootNode.UUID] = tree.UUID;
Expand All @@ -59,10 +63,12 @@ class Interpret {
elementStorage.elementToTree[node.UUID] = tree.UUID;
return Future.value(node);
}, index: 0, id: 'Indexing ${tree.name}').addTransformation(
(PBContext context, PBIntermediateTree tree) {
tree
.whereType<BaseGroup>()
.forEach((node) => tree.remove(node, keepChildren: true));
(PBContext context, PBIntermediateTree tree) async {
//
var baseGroupList = tree.whereType<BaseGroup>();

baseGroupList.forEach((group) => tree.remove(group, keepChildren: true));

return Future.value(tree);
}, index: 1, id: 'Removing the $BaseGroup from ${tree.name}');

Expand Down
5 changes: 4 additions & 1 deletion lib/controllers/main_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ class MainInfo {
String get projectName => _projectName ?? configuration?.projectName;
set projectName(String name) => _projectName = name;

/// API needed to do API callls
/// OAuth Token to call Figma API
String figmaOauthToken;

/// API key needed to do API calls
String figmaKey;

/// Project ID on Figma
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:convert';
import 'dart:io';
import 'package:parabeac_core/generation/flutter_project_builder/file_system_analyzer.dart';
import 'package:parabeac_core/generation/flutter_project_builder/post_gen_tasks/post_gen_task.dart';
import 'package:parabeac_core/generation/generators/writers/pb_flutter_writer.dart';
import 'package:parabeac_core/interpret_and_optimize/helpers/pb_context.dart';
import 'package:parabeac_core/interpret_and_optimize/helpers/pb_intermediate_node_tree.dart';
Expand Down Expand Up @@ -41,6 +42,9 @@ class FlutterProjectBuilder {

bool _configured = false;

/// Tasks to be run after generation is complete.
List<PostGenTask> postGenTasks = [];

FlutterProjectBuilder(
this.generationConfiguration,
this.fileSystemAnalyzer, {
Expand All @@ -65,7 +69,7 @@ class FlutterProjectBuilder {
static Future<Tuple2> createFlutterProject(String flutterProjectName,
{String projectDir,
bool createAssetsDir = true,
String assetsDir = 'assets/images/'}) async {
String assetsDir = 'lib/assets/images/'}) async {
try {
var result = await Process.run('flutter', ['create', flutterProjectName],
workingDirectory: projectDir, runInShell: true);
Expand Down Expand Up @@ -123,6 +127,11 @@ class FlutterProjectBuilder {
;
}

/// Runs all the tasks in
void executePostGenTasks() {
postGenTasks.forEach((task) => task.execute());
}

Future<void> genAITree(
PBIntermediateTree tree, PBContext context, bool isDryRun) async {
if (!_configured) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:parabeac_core/generation/flutter_project_builder/import_helper.dart';
import 'package:parabeac_core/generation/generators/util/pb_generation_project_data.dart';

/// Class that represents a Component Isolation Generator.
///
/// This class sets up the Component Isolation Package (i.e. Widgetbook, Dashbook, etc.)
/// to create the necessary classes and generate the code at the end.
abstract class ComponentIsolationGenerator {
/// Method that generates the code for this generator.
String generateCode(ImportHelper importHelper);

/// Path to the file to be written, relative to the `lib` directory.
String fileName;

/// projectData used to add dependencies to the project.
PBGenerationProjectData projectData;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/// Class that represents a generic Component Isolation Node.
///
/// For instance, Widgetbook may have Categories, Folders, etc.,
/// while Dashbook has Stories and Chapters. This node can represent
/// these in a generic way.
abstract class IsolationNode {
String name;

List<IsolationNode> children;

/// Generates a string representation of how the
/// [IsolationNode] should be printed.
String generate();

IsolationNode({this.children, this.name}) {
children ??= <IsolationNode>[];
}

/// Adds a child to the [IsolationNode].
void addChild(IsolationNode child) {
children.add(child);
}

List<IsolationNode> getType<T extends IsolationNode>() =>
children.whereType<T>().toList();

/// Returns [IsolationNode] with type [T] and name [name].
///
/// Returns null if no such node exists.
IsolationNode getNamed<T extends IsolationNode>(String name) =>
children.whereType<T>().firstWhere(
(element) => element.name == name,
orElse: () => null,
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:parabeac_core/generation/flutter_project_builder/post_gen_tasks/comp_isolation/component_isolation_generator.dart';
import 'package:parabeac_core/generation/flutter_project_builder/post_gen_tasks/post_gen_task.dart';
import 'package:parabeac_core/generation/generators/value_objects/file_structure_strategy/commands/write_symbol_command.dart';
import 'package:parabeac_core/generation/generators/value_objects/generation_configuration/pb_generation_configuration.dart';

/// This class is responsible for coordinating the generation of the
/// component isolation code based on the given configuration.
class IsolationPostGenTask implements PostGenTask {
/// Specific instance of the configuration to execute
ComponentIsolationGenerator compIsoConfiguration;

/// GenerationConfiguration to get strategy and imports
GenerationConfiguration generationConfiguration;
IsolationPostGenTask(this.compIsoConfiguration, this.generationConfiguration);
@override
void execute() {
var isolationCode = compIsoConfiguration.generateCode(
generationConfiguration.generationManager.importProcessor);
var fileName = compIsoConfiguration.fileName;

/// TODO: WriteSymbolCommand was used as a workaround. We should generate a command that generically writes any file
generationConfiguration.fileStructureStrategy.commandCreated(
WriteSymbolCommand(null, fileName, isolationCode, symbolPath: 'lib/'));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import 'package:parabeac_core/generation/flutter_project_builder/post_gen_tasks/comp_isolation/isolation_node.dart';
import 'package:parabeac_core/generation/flutter_project_builder/post_gen_tasks/comp_isolation/widgetbook/entities/widgetbook_folder.dart';
import 'package:parabeac_core/generation/flutter_project_builder/post_gen_tasks/comp_isolation/widgetbook/entities/widgetbook_widget.dart';

/// Class that represents a WidgetBook Category.
class WidgetBookCategory extends IsolationNode {
WidgetBookCategory({
String name = 'Parabeac-Generated',
}) : super(name: name);

@override
String generate() {
var folders = getType<WidgetBookFolder>();
var widgets = getType<WidgetBookWidget>();

var folderGen = '';
var widgetsGen = '';

if (folders != null && folders.isNotEmpty) {
folderGen = folders.map((f) => f.generate()).join('\n');
}
if (widgets != null && widgets.isNotEmpty) {
widgetsGen = widgets.map((w) => w.generate()).join('\n');
}
return '''
WidgetbookCategory(
name: 'Parabeac-Generated',
${folderGen.isNotEmpty ? 'folders: [\n$folderGen\n],\n' : ''}
${widgetsGen.isNotEmpty ? 'widgets: [\n$widgetsGen\n],\n' : ''}
)
''';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:parabeac_core/generation/flutter_project_builder/post_gen_tasks/comp_isolation/isolation_node.dart';
import 'package:parabeac_core/generation/flutter_project_builder/post_gen_tasks/comp_isolation/widgetbook/entities/widgetbook_widget.dart';

class WidgetBookFolder extends IsolationNode {
WidgetBookFolder(String name) : super(name: name);

@override
String generate() {
var widgets = getType<WidgetBookWidget>();
var genWidgets = '';
if (widgets != null && widgets.isNotEmpty) {
genWidgets = widgets.map((node) => node.generate()).join(',\n');
}
return '''
WidgetbookFolder(
name: '$name',
${genWidgets.isNotEmpty ? 'widgets: [\n$genWidgets\n],\n' : ''}
)
''';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:parabeac_core/generation/flutter_project_builder/post_gen_tasks/comp_isolation/isolation_node.dart';

class WidgetBookUseCase extends IsolationNode {
String builderCode;
WidgetBookUseCase(String name, this.builderCode) : super(name: name);

@override
String generate() {
return '''
WidgetbookUseCase(
name: '$name',
builder: (context) => Center(child: $builderCode),
),
''';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:parabeac_core/generation/flutter_project_builder/post_gen_tasks/comp_isolation/isolation_node.dart';
import 'package:parabeac_core/generation/flutter_project_builder/post_gen_tasks/comp_isolation/widgetbook/entities/widgetbook_use_case.dart';

/// Node that represents a Widgetbook component.
class WidgetBookWidget extends IsolationNode {
WidgetBookWidget(String name) : super(name: name);

@override
String generate() {
var useCases = getType<WidgetBookUseCase>();
var useCasesGen = '';
if (useCases != null && useCases.isNotEmpty) {
useCasesGen = useCases.map((useCase) => useCase.generate()).join('\n');
}
return '''
WidgetbookWidget(
name: '$name',
${useCasesGen.isNotEmpty ? 'useCases: [\n$useCasesGen\n],\n' : ''}
)
''';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import 'package:parabeac_core/controllers/main_info.dart';
import 'package:parabeac_core/generation/flutter_project_builder/import_helper.dart';
import 'package:parabeac_core/generation/flutter_project_builder/post_gen_tasks/comp_isolation/component_isolation_generator.dart';
import 'package:parabeac_core/generation/generators/import_generator.dart';
import 'package:parabeac_core/generation/generators/util/pb_generation_project_data.dart';
import 'package:parabeac_core/interpret_and_optimize/services/component_isolation/widgetbook_service.dart';

class WidgetbookGenerator implements ComponentIsolationGenerator {
@override
String fileName = 'main_widgetbook.dart';

WidgetbookGenerator(this.projectData) {
projectData.addDependencies('widgetbook', '2.0.5-beta');
}

@override
PBGenerationProjectData projectData;

@override
String generateCode(ImportHelper helper) {
var category = WidgetBookService.category;
var treeIds = WidgetBookService.treeIds;
var generatedCode = category.generate();

var imports = treeIds
.map(
(id) => helper
.getFormattedImports(
id,
importMapper: (import) => FlutterImport(
import,
MainInfo().projectName,
),
)
.join('\n'),
)
.join('');
return '''
import 'package:widgetbook/widgetbook.dart';
import 'package:flutter/material.dart';
$imports
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context){
return Widgetbook(
themes: [
WidgetbookTheme(name: 'Light', data: ThemeData.light()),
],
devices: const [
Apple.iPhone11ProMax,
Samsung.s10,
],
categories: [
$generatedCode,
],
appInfo: AppInfo(name: 'MyApp'),
);
}
}
''';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// Abstract class for Tasks that will run post-generation.
abstract class PostGenTask {
/// Executes the [PostGenTask].
void execute();
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:parabeac_core/controllers/main_info.dart';
import 'package:parabeac_core/generation/generators/value_objects/file_structure_strategy/commands/file_structure_command.dart';
import 'package:parabeac_core/generation/generators/value_objects/file_structure_strategy/pb_file_structure_strategy.dart';

Expand All @@ -6,7 +7,8 @@ class AddDependencyCommand extends FileStructureCommand {
final _PUBSPEC_YAML_NAME = 'pubspec.yaml';

///assets yaml decleration
final String _ASSET_DECLERATION = '\t\t- assets/images/';
final String _ASSET_DECLERATION =
'\t\t- packages/${MainInfo().projectName}/assets/images/';

/// Name of the [package]
String package;
Expand Down
7 changes: 5 additions & 2 deletions lib/generation/generators/visual-widgets/pb_bitmap_gen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ class PBBitmapGenerator extends PBGenerator {
? 'assets/${source.referenceImage}' // Assuming PBDL will give us reference to image in the form of `image/<image_name>.png`
: ('assets/images/' + source.UUID + '.png');

buffer.write(
'\'$imagePath\', ${_sizehelper.generate(source, generatorContext)} $boxFit)');
buffer.write('\'$imagePath\', ');
// Point package to self (for component isolation support)
buffer.write('package: \'${MainInfo().projectName}\',');
buffer.write('${_sizehelper.generate(source, generatorContext)} $boxFit)');

return buffer.toString();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:parabeac_core/controllers/main_info.dart';
import 'package:parabeac_core/generation/generators/attribute-helper/pb_size_helper.dart';
import 'package:parabeac_core/generation/generators/pb_generator.dart';
import 'package:parabeac_core/interpret_and_optimize/entities/inherited_shape_group.dart';
Expand All @@ -15,7 +16,7 @@ class PBShapeGroupGen extends PBGenerator {
if (source is InheritedShapeGroup) {
var buffer = StringBuffer();
buffer.write(
'Image.asset(\'assets/images/${source.UUID}.png\', ${_sizehelper.generate(source)})');
'Image.asset(\'assets/images/${source.UUID}.png\', ${_sizehelper.generate(source)}), package: \'${MainInfo().projectName}\',');
return buffer.toString();
}
}
Expand Down
Loading

0 comments on commit de2aff6

Please sign in to comment.