Skip to content

Commit

Permalink
[you_cli] clean
Browse files Browse the repository at this point in the history
  • Loading branch information
chen56 committed May 21, 2024
1 parent 4759471 commit 82287d2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
51 changes: 46 additions & 5 deletions packages/you_cli/bin/cli.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ main(List<String> args) async {
FileSystem fs = const LocalFileSystem();

var runner = CommandRunner("youc", "you flutter cli tools.");

runner.addCommand(
Cmd_gen(fs: fs)
..addSubcommand(Cmd_gen_all(fs: fs))
..addSubcommand(Cmd_gen_routes_g_dart(fs: fs)),
..addSubcommand(Cmd_gen_routes_g_dart(fs: fs))
..addSubcommand(Cmd_gen_assets(fs: fs)),
);

await runner.run(args);
Expand Down Expand Up @@ -122,9 +124,9 @@ class Cmd_gen_routes_g_dart extends Command {
return builderType.newInstance(
[literalString(node.dir.basename)],
{
if (node.file_page_dart.existsSync()) "page": refer(YouCli.pageFunctionName, node.pagePackageUrl),
if (node.path_page_dart.existsSync()) "page": refer(YouCli.pageFunctionName, node.pagePackageUrl),
if (node.pageAnno != null) "pageAnno": refer("_Pages").property(node.flatName),
if (node.file_layout_dart.existsSync()) "layout": refer(YouCli.layoutFunctionName, node.layoutPackageUrl),
if (node.path_layout_dart.existsSync()) "layout": refer(YouCli.layoutFunctionName, node.layoutPackageUrl),
if (node.children.isNotEmpty) "children": literalList(node.children.map((e) => _genRootRouteExpression(e))),
},
);
Expand Down Expand Up @@ -167,7 +169,7 @@ class Cmd_gen_routes_g_dart extends Command {
..assignment = _genRootRouteExpression(rootRoute).code),
)
..fields.addAll(
routes.where((e) => e.file_page_dart.existsSync()).map(
routes.where((e) => e.path_page_dart.existsSync()).map(
(routeDir) => Field((f) => f
..modifier = FieldModifier.final$
..late = true
Expand Down Expand Up @@ -210,7 +212,46 @@ class Cmd_gen_routes_g_dart extends Command {
}
}

// ignore: camel_case_types
class Cmd_gen_assets extends Command {
Cmd_gen_assets({required this.fs}) : libMode = false {
argParser.addOption("dir", mandatory: true, help: "要生成的flutter note项目根目录");
}

Cmd_gen_assets.libMode({
required this.fs,
required this.dir,
}) : libMode = true;

bool libMode;
late Directory dir;

@override
final name = "routes";
@override
final description = "gen assets";
final FileSystem fs;
YouCli? _cli;

YouCli get cli => _cli != null ? _cli! : _cli = YouCli(projectDir: dir);

@override
Future<void> run() async {
if (!libMode) {
String dirOpt = argResults!["dir"];
dir = fs.directory(path.absolute(dirOpt));
}
if (!dir.existsSync()) {
throw AssertionError("【--dir $dir】 not exists");
}

var rootRoute = await cli.rootRoute;
Iterable<RouteNode> routes = rootRoute.toList();
routes.toList().map((e) => e.dir);
}
}

_log(Object? o) {
// ignore: avoid_print
print("${DateTime.now()} - $o");
print("you_cli - ${DateTime.now()} - $o");
}
10 changes: 5 additions & 5 deletions packages/you_cli/lib/src/cli_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ class RouteNode {

bool get isRoot => _parent == this;

File get file_page_dart => dir.childFile("page.dart");
File get path_page_dart => dir.childFile("page.dart");

File get file_layout_dart => dir.childFile("layout.dart");
File get path_layout_dart => dir.childFile("layout.dart");

String get routePath {
if (isRoot) {
Expand All @@ -156,11 +156,11 @@ class RouteNode {
}

String get pagePackageUrl {
return "package:${cli.pubspec.name}/${path.relative(file_page_dart.path, from: cli.path_lib.path)}";
return "package:${cli.pubspec.name}/${path.relative(path_page_dart.path, from: cli.path_lib.path)}";
}

String get layoutPackageUrl {
return "package:${cli.pubspec.name}/${path.relative(file_layout_dart.path, from: cli.path_lib.path)}";
return "package:${cli.pubspec.name}/${path.relative(path_layout_dart.path, from: cli.path_lib.path)}";
}

/// note name平整化,可作为变量名:
Expand Down Expand Up @@ -209,7 +209,7 @@ class RouteNode {
}

RouteNode? findLayoutSync() {
if (file_layout_dart.existsSync()) {
if (path_layout_dart.existsSync()) {
return this;
}
if (isRoot) {
Expand Down

0 comments on commit 82287d2

Please sign in to comment.