Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#120: moved code to lib #150

Merged
Merged
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
134 changes: 2 additions & 132 deletions bin/model_generator.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
import 'dart:async';
import 'dart:io';

import 'package:args/args.dart';
import 'package:model_generator/config/pubspec_config.dart';
import 'package:model_generator/config/yml_generator_config.dart';
import 'package:model_generator/model/model/custom_model.dart';
import 'package:model_generator/model/model/enum_model.dart';
import 'package:model_generator/model/model/json_converter_model.dart';
import 'package:model_generator/model/model/object_model.dart';
import 'package:model_generator/run_process/run_process.dart';
import 'package:model_generator/writer/enum_model_writer.dart';
import 'package:model_generator/writer/object_model_writer.dart';
import 'package:path/path.dart';
import 'package:model_generator/main.dart';

Future<void> main(List<String> args) async {
final argParser = ArgParser()
Expand All @@ -26,123 +14,5 @@ Future<void> main(List<String> args) async {
print(argParser.usage);
return;
}

final pubspecYaml = File(join(Directory.current.path, 'pubspec.yaml'));
if (!pubspecYaml.existsSync()) {
throw Exception(
'This program should be run from the root of a flutter/dart project');
}
final pubspecContent = pubspecYaml.readAsStringSync();
final pubspecConfig = PubspecConfig(pubspecContent);

final configPath = results['path'] ?? pubspecConfig.configPath;
String absolutePath;
if (isAbsolute(configPath)) {
absolutePath = configPath;
} else {
absolutePath = join(Directory.current.path, configPath);
}
final FileSystemEntity configEntity;
switch (FileSystemEntity.typeSync(absolutePath)) {
case FileSystemEntityType.directory:
configEntity = Directory(absolutePath);
break;
case FileSystemEntityType.file:
default:
configEntity = File(absolutePath);
break;
}

if (!configEntity.existsSync()) {
throw Exception(
'This program requires a config file/dir. `$configPath` does not exist');
}
final YmlGeneratorConfig modelGeneratorConfig;
if (configEntity is Directory) {
modelGeneratorConfig =
readConfigFilesInDirectory(pubspecConfig, configEntity, configPath);
} else {
final modelGeneratorContent = (configEntity as File).readAsStringSync();
modelGeneratorConfig =
YmlGeneratorConfig(pubspecConfig, modelGeneratorContent, configPath);
}
modelGeneratorConfig.checkIfTypesAvailable();
if (modelGeneratorConfig.models.isEmpty) {
print('No models defined in config files, skipping generation');
}

writeToFiles(pubspecConfig, modelGeneratorConfig);
await generateJsonGeneratedModels(useFvm: pubspecConfig.useFvm);
print('Done!!!');
}

YmlGeneratorConfig readConfigFilesInDirectory(
PubspecConfig config, Directory configEntity, String directoryPath) {
final configFiles = configEntity
.listSync(recursive: true)
.whereType<File>()
.where((element) =>
extension(element.path) == '.yaml' ||
extension(element.path) == '.yml');
final configs = configFiles.map((e) =>
YmlGeneratorConfig(config, e.readAsStringSync(), relative(e.path)));
return YmlGeneratorConfig.merge(configs, directoryPath);
}

void writeToFiles(
PubspecConfig pubspecConfig, YmlGeneratorConfig modelGeneratorConfig) {
for (final model in modelGeneratorConfig.models) {
final modelDirectory = Directory(join('lib', model.baseDirectory));
if (!modelDirectory.existsSync()) {
modelDirectory.createSync(recursive: true);
}
String? content;
if (model is ObjectModel) {
content = ObjectModelWriter(
pubspecConfig,
model,
modelGeneratorConfig,
).write();
} else if (model is EnumModel) {
content = EnumModelWriter(model).write();
} else if (model is JsonConverterModel) {
continue;
} else if (model is CustomModel) {
continue;
}
if (content == null) {
throw Exception(
'content is null for ${model.name}. File a bug report on github. This is not normal. https://github.com/icapps/flutter-model-generator/issues');
}
File file;
if (model.path == null) {
file = File(join('lib', model.baseDirectory, '${model.fileName}.dart'));
} else {
file = File(join(
'lib', model.baseDirectory, model.path, '${model.fileName}.dart'));
}
if (!file.existsSync()) {
file.createSync(recursive: true);
}
file.writeAsStringSync(content);
}
}

Future<void> generateJsonGeneratedModels({required bool useFvm}) async {
final arguments = [
if (useFvm) ...[
'fvm',
],
'flutter',
'packages',
'pub',
'run',
'build_runner',
'build',
'--delete-conflicting-outputs',
];
await ProcessRunner.runProcessVerbose(
arguments.first,
arguments.skip(1).toList(),
);
ModelGenerator.generate(results);
}
140 changes: 140 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import 'dart:async';
import 'dart:io';

import 'package:args/args.dart';
import 'package:model_generator/config/pubspec_config.dart';
import 'package:model_generator/config/yml_generator_config.dart';
import 'package:model_generator/model/model/custom_model.dart';
import 'package:model_generator/model/model/enum_model.dart';
import 'package:model_generator/model/model/json_converter_model.dart';
import 'package:model_generator/model/model/object_model.dart';
import 'package:model_generator/run_process/run_process.dart';
import 'package:model_generator/writer/enum_model_writer.dart';
import 'package:model_generator/writer/object_model_writer.dart';
import 'package:path/path.dart';

class ModelGenerator {
ModelGenerator._();

static void generate(ArgResults results) async {
final pubspecYaml = File(join(Directory.current.path, 'pubspec.yaml'));
if (!pubspecYaml.existsSync()) {
throw Exception(
'This program should be run from the root of a flutter/dart project');
}
final pubspecContent = pubspecYaml.readAsStringSync();
final pubspecConfig = PubspecConfig(pubspecContent);

final configPath = results['path'] ?? pubspecConfig.configPath;
String absolutePath;
if (isAbsolute(configPath)) {
absolutePath = configPath;
} else {
absolutePath = join(Directory.current.path, configPath);
}
final FileSystemEntity configEntity;
switch (FileSystemEntity.typeSync(absolutePath)) {
case FileSystemEntityType.directory:
configEntity = Directory(absolutePath);
break;
case FileSystemEntityType.file:
default:
configEntity = File(absolutePath);
break;
}

if (!configEntity.existsSync()) {
throw Exception(
'This program requires a config file/dir. `$configPath` does not exist');
}
final YmlGeneratorConfig modelGeneratorConfig;
if (configEntity is Directory) {
modelGeneratorConfig =
_readConfigFilesInDirectory(pubspecConfig, configEntity, configPath);
} else {
final modelGeneratorContent = (configEntity as File).readAsStringSync();
modelGeneratorConfig =
YmlGeneratorConfig(pubspecConfig, modelGeneratorContent, configPath);
}
modelGeneratorConfig.checkIfTypesAvailable();
if (modelGeneratorConfig.models.isEmpty) {
print('No models defined in config files, skipping generation');
}

_writeToFiles(pubspecConfig, modelGeneratorConfig);
await _generateJsonGeneratedModels(useFvm: pubspecConfig.useFvm);
print('Done!!!');
}

static YmlGeneratorConfig _readConfigFilesInDirectory(
PubspecConfig config, Directory configEntity, String directoryPath) {
final configFiles = configEntity
.listSync(recursive: true)
.whereType<File>()
.where((element) =>
extension(element.path) == '.yaml' ||
extension(element.path) == '.yml');
final configs = configFiles.map((e) =>
YmlGeneratorConfig(config, e.readAsStringSync(), relative(e.path)));
return YmlGeneratorConfig.merge(configs, directoryPath);
}

static void _writeToFiles(
PubspecConfig pubspecConfig, YmlGeneratorConfig modelGeneratorConfig) {
for (final model in modelGeneratorConfig.models) {
final modelDirectory = Directory(join('lib', model.baseDirectory));
if (!modelDirectory.existsSync()) {
modelDirectory.createSync(recursive: true);
}
String? content;
if (model is ObjectModel) {
content = ObjectModelWriter(
pubspecConfig,
model,
modelGeneratorConfig,
).write();
} else if (model is EnumModel) {
content = EnumModelWriter(model).write();
} else if (model is JsonConverterModel) {
continue;
} else if (model is CustomModel) {
continue;
}
if (content == null) {
throw Exception(
'content is null for ${model.name}. File a bug report on github. This is not normal. https://github.com/icapps/flutter-model-generator/issues');
}
File file;
if (model.path == null) {
file = File(join('lib', model.baseDirectory, '${model.fileName}.dart'));
} else {
file = File(join(
'lib', model.baseDirectory, model.path, '${model.fileName}.dart'));
}
if (!file.existsSync()) {
file.createSync(recursive: true);
}
file.writeAsStringSync(content);
}
}

static Future<void> _generateJsonGeneratedModels(
{required bool useFvm}) async {
final arguments = [
if (useFvm) ...[
'fvm',
],
'flutter',
'packages',
'pub',
'run',
'build_runner',
'build',
'--delete-conflicting-outputs',
];
await ProcessRunner.runProcessVerbose(
arguments.first,
arguments.skip(1).toList(),
);
}
}