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

Update #7

Closed
wants to merge 8 commits into from
Closed
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
2 changes: 1 addition & 1 deletion .analysis_options
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
analyzer:
strong-mode: true
exclude:
- bin/template
- bin/template/**
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
## 0.2.0
* Add `init` command to create a config file inside an existing project
* Update `Angular2`
* move all libraries export to `lib/`
* simplify generated file name (no '_component.dart', '_service.dart'...)
* `sass` support
* `less` support
* refactor `Route` defintion
* bugfix


## 0.1.0

### Add full application generation with `new` command
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub global run ng2gen:new app
- `ng2gen service my_service`
- `ng2gen pipe my_pipe`
- `ng2gen route home /home_path`
- `ng2gen init`

## Create an Application

Expand Down
4 changes: 2 additions & 2 deletions bin/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import "dart:io";
import 'dart:async';

import "package:dev_string_converter/dev_string_converter.dart";
import "utils.dart";
import "package:stagehand/stagehand.dart";
import "package:stagehand/src/common.dart";
import "app_template_data.dart" as template;
import 'package:path/path.dart' as path;
import "app_template_data.dart" as template;
import "utils.dart";

class _DirectoryGeneratorTarget extends GeneratorTarget {
final Directory dir;
Expand Down
63 changes: 21 additions & 42 deletions bin/app_template_data.dart

Large diffs are not rendered by default.

25 changes: 13 additions & 12 deletions bin/component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,39 @@ main(List<String> args) async {

if (config?.componentsPath != null) {
path = "${config.componentsPath}/${toTableName(name)}";
lib = "${config.componentsPath}/components.dart";
lib = "lib/components.dart";
}

String dartPath = '$path/${toTableName(name)}_component.dart';
String htmlPath = '$path/${toTableName(name)}_component.html';
String cssPath = '$path/${toTableName(name)}_component.css';
String prefix = config?.componentsPath != null ? "lib/" : "";
String filePath = '$prefix$path/${toTableName(name)}';

String dartPath = '$filePath.dart';
String htmlPath = '$filePath.html';
String cssPath = '$filePath.${config.styleFileType}';

await writeInFile(dartPath, componentTemplateDart(name));
await writeInFile(htmlPath, componentTemplateHtml(name));
await createFile(cssPath);

if (lib != null) {
addToLibrary("${toTableName(name)}/${toTableName(name)}_component.dart", lib);
addToLibrary("$path/${toTableName(name)}.dart", lib);
}

}

String componentTemplateDart(String name) =>
'''// Copyright (c) 2016, <your name>. All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.

import 'package:angular2/core.dart';
'''import 'package:angular2/core.dart';

@Component(
selector: '${toPolyName(name)}',
templateUrl: '${toTableName(name)}_component.html',
styleUrls: const ['${toTableName(name)}_component.css'])
templateUrl: '${toTableName(name)}.html',
styleUrls: const <String>['${toTableName(name)}.css'])
class ${toUpperCamelCase(name)} implements OnInit {

${toUpperCamelCase(name)}();

ngOnInit() {}
@override
void ngOnInit() {}

}
''';
Expand Down
21 changes: 11 additions & 10 deletions bin/component_inline.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@ main(List<String> args) async {

if (config?.componentsPath != null) {
path = "${config.componentsPath}/${toTableName(name)}";
lib = "${config.componentsPath}/components.dart";
lib = "lib/components.dart";
}

String dartPath = '$path/${toTableName(name)}_component.dart';
String cssPath = '$path/${toTableName(name)}_component.css';
String prefix = config?.componentsPath != null ? "lib/" : "";
String filePath = '$prefix$path/${toTableName(name)}';

String dartPath = '$filePath.dart';
String cssPath = '$filePath.${config.styleFileType}';

await writeInFile(dartPath, componentTemplateDart(name));
await createFile(cssPath);

if (lib != null) {
addToLibrary("${toTableName(name)}/${toTableName(name)}_component.dart", lib);
addToLibrary("$path/${toTableName(name)}.dart", lib);
}

}

String componentTemplateDart(String name) =>
'''// Copyright (c) 2016, <your name>. All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.

import 'package:angular2/core.dart';
'''import 'package:angular2/core.dart';

@Component(
selector: '${toPolyName(name)}',
Expand All @@ -39,12 +39,13 @@ import 'package:angular2/core.dart';
${toPolyName(name)} works!
</p>
\'\'\',
styleUrls: const ['${toTableName(name)}_component.css'])
styleUrls: const <String>['${toTableName(name)}.css'])
class ${toUpperCamelCase(name)} implements OnInit {

${toUpperCamelCase(name)}();

ngOnInit() {}
@override
void ngOnInit() {}

}
''';
11 changes: 5 additions & 6 deletions bin/directive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,22 @@ main(List<String> args) async {

if (config?.directivesPath != null) {
path = "${config.directivesPath}";
lib = "${config.directivesPath}/directives.dart";
lib = "lib/directives.dart";
}

String dartPath = '$path/${toTableName(name)}_directive.dart';
String prefix = config?.directivesPath != null ? "lib/" : "";

String dartPath = '$prefix$path/${toTableName(name)}.dart';

await writeInFile(dartPath, directiveTemplate(name));

if (lib != null) {
addToLibrary("${toTableName(name)}_directive.dart", lib);
addToLibrary("$path/${toTableName(name)}.dart", lib);
}

}

String directiveTemplate(String name) => '''
// Copyright (c) 2016, <your name>. All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.

import "package:angular2/core.dart";

@Directive(
Expand Down
29 changes: 29 additions & 0 deletions bin/init.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'dart:io';
import 'package:yaml/yaml.dart';
import "utils.dart";

main(List<String> _) async {
File pubspec = new File("pubspec.yaml");
if (!(await pubspec.exists())) {
output("'pubspec.yaml' not found.", Color.red);
} else {
File config = new File(config_file_name);
if (config.existsSync()) {
output("'$config_file_name' already exist.", Color.red);
} else {
config.writeAsString(configYaml);
}
}
}

String get configYaml =>
'''project:
root: "app.dart"
components: "lib/components"
directives: "lib/directives"
services: "lib/services"
routes: "lib/routes"
pipes: "lib/pipes"
sass: false
less: false
''';
6 changes: 6 additions & 0 deletions bin/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ import 'pipe.dart' as pipe;
import 'app.dart' as app;
import 'component_inline.dart' as component_inline;
import 'route.dart' as route;
import 'init.dart' as init;

/// Creates a
main(List<String> args) {
_showHelp(args, 0);
switch(args[0]) {
case 'init':
_showHelp(args, 0);
init.main(args.getRange(1, args.length).toList());
break;
case 'component':
_showHelp(args, 1);
component.main(args.getRange(1, args.length).toList());
Expand Down Expand Up @@ -59,6 +64,7 @@ USAGE:
service <service_name> : create a new service.
pipe <pipe_name> : create a new pipe.
route <route_name> <route_path> : create a new route.
init : init an existing project with ng2gen
''');
exit(0);
}
Expand Down
14 changes: 7 additions & 7 deletions bin/pipe.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,33 @@ main(List<String> args) async {

if (config?.pipesPath != null) {
path = "${config.pipesPath}";
lib = "${config.pipesPath}/pipes.dart";
lib = "lib/pipes.dart";
}

String dartPath = '$path/${toTableName(name)}_pipe.dart';
String prefix = config?.pipesPath != null ? "lib/" : "";

String dartPath = '$prefix$path/${toTableName(name)}.dart';

await writeInFile(dartPath, pipeTemplate(name));

if (lib != null) {
addToLibrary("${toTableName(name)}_pipe.dart", lib);
addToLibrary("$path/${toTableName(name)}.dart", lib);
}

}

String pipeTemplate(String name) => '''
// Copyright (c) 2016, <your name>. All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.

import "package:angular2/core.dart";

@Pipe(
name: '${toUpperCamelCase(name)}'
name: '${toLowerCamelCase(name)}'
)
@Injectable()
class ${toUpperCamelCase(name)} implements PipeTransform {

const ${toUpperCamelCase(name)}();

@override
dynamic transform(dynamic value, [List<dynamic> args = null]) {
return value;
}
Expand Down
84 changes: 56 additions & 28 deletions bin/route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,69 @@ import "component.dart";
ConfigFile config = new ConfigFile();

main(List<String> args) async {
String name = args[0];
String routePath = args[1];
String lib;
String path = "${toTableName(name)}";

String name = "${args[0]}-route";
String lib;
String path = "${toTableName(name)}";
if (config?.routesPath != null) {
path = "${config.routesPath}/${toTableName(name)}";
lib = "lib/routes.dart";
}

if (config?.routesPath != null) {
path = "${config.routesPath}/${toTableName(name)}";
lib = "${config.routesPath}/routes.dart";
}
String prefix = config?.routesPath != null ? "lib/" : "";
String filePath = '$prefix$path/${toTableName(name)}';

String dartPath = '$path/${toTableName(name)}_component.dart';
String htmlPath = '$path/${toTableName(name)}_component.html';
String cssPath = '$path/${toTableName(name)}_component.css';
String dartPath = '$filePath.dart';
String htmlPath = '$filePath.html';
String cssPath = '$filePath.${config.styleFileType}';

await writeInFile(dartPath, componentTemplateDart(name));
await writeInFile(htmlPath, componentTemplateHtml(name));
await createFile(cssPath);
await writeInFile(dartPath, componentRouteTemplateDart(name, routePath));
await writeInFile(htmlPath, componentTemplateHtml(name));
await createFile(cssPath);

if (lib != null) {
addToLibrary("${toTableName(name)}/${toTableName(name)}_component.dart", lib);
addToRouteConfig(toUpperCamelCase(name), toUpperCamelCase(args[0]), args[1]);
}
if (lib != null) {
addToLibrary("$path/${toTableName(name)}.dart", lib);
addToRouteConfig(toUpperCamelCase(name), config?.rootPath);
}
}

String componentRouteTemplateDart(String name, String path) => '''import 'package:angular2/core.dart';
import 'package:angular2/router.dart';

@Component(
selector: '${name.replaceAll("_", "-")}',
templateUrl: '${toTableName(name)}.html',
styleUrls: const <String>['${toTableName(name)}.css'])
class ${toUpperCamelCase(name)} implements OnInit {

static const String route_name = "${toUpperCamelCase(name)}";
static const String route_path = "$path";
static const Route route = const Route(path: ${toUpperCamelCase(name)}.route_path,
component: ${toUpperCamelCase(name)},
name: ${toUpperCamelCase(name)}.route_name);

${toUpperCamelCase(name)}();

@override
void ngOnInit() {}

}
''';

addToRouteConfig(String className, String name, String path) {
File rootComponent = new File("lib/${config.projectName}.dart");
addToRouteConfig(String className, String rootPath) {
File rootComponent = new File("lib/$rootPath");

if (rootComponent.existsSync()) {
String content = rootComponent.readAsStringSync();
content = formatter.format(content);
content = content.replaceFirst("@RouteConfig(const [", '''
@RouteConfig(const [
const Route(useAsDefault: false, path: '$path', name: '$name', component: $className),
if (rootComponent.existsSync()) {
String content = rootComponent.readAsStringSync();
content = formatter.format(content);
content = content.replaceFirst(
"/*Insert Routes here*/",
'''
/*Insert Routes here*/
$className.route,
''');
rootComponent.writeAsStringSync(formatter.format(content));
}
}
rootComponent.writeAsStringSync(formatter.format(content));
output("Route Inserted into root component.\n", Color.yellow);
}
}
Loading