Skip to content

Commit

Permalink
Merge pull request #15 from ARK4579/develop
Browse files Browse the repository at this point in the history
v0.0.12
  • Loading branch information
ARK4579 authored Aug 8, 2021
2 parents af25367 + 986231c commit 9a59eeb
Show file tree
Hide file tree
Showing 12 changed files with 294 additions and 41 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.0.12

- dj_io moved here
- improved formatting of generated code using dart_style package
- added support for json serialization in generated code without using JsonSerializer

## 0.0.11

- Simple Minor update
Expand Down
18 changes: 18 additions & 0 deletions lib/main/dj_io/base_dj_io.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:dj/dj.dart';

/// IO Wrapper for BaseDJ from 'dj' library.
class BaseDjIo {
final Map<String, dynamic> baseDjMap;

BaseDjIo({
required this.baseDjMap,
});

void write() {
print('Writing to Disk...');
var baseDj = BaseDj.fromJson(baseDjMap);

var directoryDjIo = DirectoryDjIo(directoryDj: baseDj.node);
directoryDjIo.create(baseDj.path);
}
}
57 changes: 57 additions & 0 deletions lib/main/dj_io/directory_dj_io.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import 'package:universal_io/io.dart';

import 'package:dj/main/main.dart';
import 'package:path/path.dart' as p;

import 'node_dj_io.dart';
import 'file_dj_io.dart';

class DirectoryDjIo extends NodeDjIo {
final DirectoryDj directoryDj;
final bool shouldFormat;

DirectoryDjIo({
required this.directoryDj,
this.shouldFormat = true,
});

@override
void create(String basePath) {
super.create(basePath);

var dirPath = p.join(basePath, directoryDj.name);

var directory = Directory(dirPath);

directory.createSync();

directoryDj.nodes?.forEach((node) {
var nodeDjIo = getNodeDj(node, shouldFormat);
if (nodeDjIo != null) {
nodeDjIo.create(dirPath);
nodeDjIo.write();
}
});
}
}

NodeDjIo? getNodeDj(NodeDj? node, bool shouldFormat) {
if (node == null) return null;

NodeDjIo? nodeDjIo;
if (node.type == StructureType.Directory) {
nodeDjIo = DirectoryDjIo(
directoryDj: DirectoryDj.fromJson(node.toJson()),
shouldFormat: shouldFormat,
);
} else if (node.type == StructureType.File) {
nodeDjIo = FileDjIo(
fileDj: FileDj.fromJson(node.toJson()),
shouldFormat: shouldFormat,
);
} else {
print('${node.runtimeType} Not Handeled');
}

return nodeDjIo;
}
4 changes: 4 additions & 0 deletions lib/main/dj_io/dj_io.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export 'node_dj_io.dart';
export 'file_dj_io.dart';
export 'directory_dj_io.dart';
export 'base_dj_io.dart';
60 changes: 60 additions & 0 deletions lib/main/dj_io/file_dj_io.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import 'package:universal_io/io.dart';
import 'package:dart_style/dart_style.dart';
import 'package:path/path.dart' as p;

import 'package:dj/main/main.dart';

import 'node_dj_io.dart';

class FileDjIo extends NodeDjIo {
final FileDj fileDj;
final bool shouldFormat;

FileDjIo({
required this.fileDj,
this.shouldFormat = true,
});

File? _handler;

@override
void create(String basePath) {
if (_handler == null) {
super.create(basePath);

var fullFileName = '${fileDj.name}.${fileDj.fileExtension}';
var fileAbsolutePath = p.join(basePath, fullFileName);

_handler = File(fileAbsolutePath);
}

_handler!.createSync();
}

@override
void write() {
if (_handler != null) {
var codeLines = <String>[];

fileDj.codeParts?.forEach((codePart) {
codePart.toCode().forEach((codePartLine) {
codeLines.add(codePartLine);
});
});

var singleCodeLine = codeLines.join('\n');

if (shouldFormat) {
try {
singleCodeLine = DartFormatter().format(singleCodeLine);
} catch (e) {
print(e);
}
}

var fileWriter = _handler!.openWrite();
fileWriter.writeln(singleCodeLine);
fileWriter.close();
}
}
}
9 changes: 9 additions & 0 deletions lib/main/dj_io/node_dj_io.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'package:universal_io/io.dart';

abstract class NodeDjIo {
void create(String basePath) {
Directory(basePath).createSync(recursive: true);
}

void write() {}
}
154 changes: 116 additions & 38 deletions lib/main/djs/code_djs/class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,24 @@ class ClassDj extends CodePartDj {
final bool? isImplements;

@JsonKey(name: 'jsonSerializable')
final bool? jsonSerializable;
final bool jsonSerializable;

@JsonKey(name: 'functions')
final List<CodePartDj>? functions;

@JsonKey(name: 'selfJsonSerialization')
final bool selfJsonSerialization;

ClassDj({
descriptionDj,
this.name,
this.baseName,
this.fields,
this.isExtends,
this.isImplements,
this.jsonSerializable,
this.jsonSerializable = false,
this.functions,
this.selfJsonSerialization = false,
CodePartDjType codePartDjType = CodePartDjType.Class,
}) : super(
descriptionDj: descriptionDj,
Expand All @@ -46,6 +50,42 @@ class ClassDj extends CodePartDj {
@override
Map<String, dynamic> toJson() => _$ClassDjToJson(this);

List<String> _nameLineCode() {
var codeLines = <String>[];

var baseLine = '';
if ((isExtends ?? false) && (baseName ?? '').isNotEmpty) {
baseLine = 'extends $baseName';
}
var classStartLine = [
if (jsonSerializable) '@JsonSerializable()',
'class $name $baseLine {'
];
codeLines += classStartLine;

return codeLines;
}

List<FieldDj>? get _intrestingFields => fields
?.where((field) =>
(!(field.superOnly ?? false) && !field.isPrivate) &&
field.name != null)
.toList();

List<String> _fieldsCode() {
var codeLines = <String>[];

_intrestingFields?.forEach((field) {
if (jsonSerializable) {
codeLines.add("@JsonKey(name: '${field.name}')");
}
codeLines += field.toCode();
codeLines.add('');
});

return codeLines;
}

List<String> _constructorCode() {
var codeLines = <String>[];

Expand Down Expand Up @@ -92,6 +132,12 @@ class ClassDj extends CodePartDj {
codeLines.add(');');
}

return codeLines;
}

List<String> _functionsCode() {
var codeLines = <String>[];

if (functions != null) {
codeLines.add('\n');
functions?.forEach((function) {
Expand All @@ -102,52 +148,84 @@ class ClassDj extends CodePartDj {
return codeLines;
}

@override
List<String> toCode() {
var _lines = super.toCode();

var _jsonSerializable = jsonSerializable ?? false;
List<String> jsonSerializableCode() {
var codeLines = <String>[];

if (name == null) return _lines;
// if we are going to generate to/from json code for this class we won't be
// needing JsonSerializable
if (jsonSerializable && !selfJsonSerialization) {
codeLines.add('');
var jsFromLine1 = '$name.fromJson(Map<String, dynamic> json)';
var jsFromLine2 = '_\$${name}FromJson(json);';
var jsFromLine = 'factory $jsFromLine1 => $jsFromLine2';
codeLines.add(jsFromLine);

var baseLine = '';
if ((isExtends ?? false) && (baseName ?? '').isNotEmpty) {
baseLine = 'extends $baseName';
codeLines.add('');
codeLines.add('@override');
codeLines.add('Map<String, dynamic> toJson() => _\$${name}ToJson(this);');
}
var classStartLine = [
if (_jsonSerializable) '@JsonSerializable()',
'class $name $baseLine {'
];
_lines += classStartLine;

fields
?.where((field) => (!(field.superOnly ?? false) && !field.isPrivate))
.forEach((field) {
if (_jsonSerializable) {
_lines.add("@JsonKey(name: '${field.name}')");
}
_lines += field.toCode();
_lines.add('');
});
return codeLines;
}

List<String> _fromJsonCode() {
var codeLines = <String>[];

_lines = _lines + _constructorCode();
// TODO: Writ FromJson code generator !
if (selfJsonSerialization) {
codeLines.add('');
codeLines.add('factory $name.fromJson(Map<String, dynamic> json) {');

if (jsonSerializable ?? false) {
_lines.add('');
var jsFromLine1 = '$name.fromJson(Map<String, dynamic> json)';
var jsFromLine2 = '_\$${name}FromJson(json);';
var jsFromLine = 'factory $jsFromLine1 => $jsFromLine2';
_lines.add(jsFromLine);
codeLines.add('return $name(');
_intrestingFields?.forEach((field) {
codeLines
.add("${field.name}: json['${field.name}'] ${field.parseAsType},");
});
codeLines.add('); }');
}

if (jsonSerializable ?? false) {
_lines.add('');
_lines.add('@override');
_lines.add('Map<String, dynamic> toJson() => _\$${name}ToJson(this);');
return codeLines;
}

List<String> _toJsonCode() {
var codeLines = <String>[];

if (selfJsonSerialization) {
codeLines.add('');
codeLines.add('Map<String, dynamic> toJson() {');
codeLines.add('final val = <String, dynamic>{};');
codeLines.add('');
codeLines.add('void writeNotNull(String key, dynamic value) {');
codeLines.add('if (value != null) {');
codeLines.add('val[key] = value;');
codeLines.add('} }');
codeLines.add('');

_intrestingFields?.forEach((field) {
codeLines.add("writeNotNull('${field.name}', ${field.name});");
});

codeLines.add('return val;');
codeLines.add('}');
}

// class end line
_lines.add('}');
return codeLines;
}

@override
List<String> toCode() {
var _lines = super.toCode();

if (name == null) return _lines;

_lines += _nameLineCode() +
_fieldsCode() +
_constructorCode() +
_functionsCode() +
jsonSerializableCode() +
_fromJsonCode() +
_toJsonCode() +
['}']; // class end line

return _lines;
}
Expand Down
6 changes: 4 additions & 2 deletions lib/main/djs/code_djs/class.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9a59eeb

Please sign in to comment.