-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathcreate_widget_command.dart
More file actions
118 lines (108 loc) · 3.86 KB
/
create_widget_command.dart
File metadata and controls
118 lines (108 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import 'dart:async';
import 'package:args/command_runner.dart';
import 'package:stacked_cli/src/constants/command_constants.dart';
import 'package:stacked_cli/src/constants/message_constants.dart';
import 'package:stacked_cli/src/locator.dart';
import 'package:stacked_cli/src/mixins/project_structure_validator_mixin.dart';
import 'package:stacked_cli/src/services/colorized_log_service.dart';
import 'package:stacked_cli/src/services/config_service.dart';
import 'package:stacked_cli/src/services/posthog_service.dart';
import 'package:stacked_cli/src/services/process_service.dart';
import 'package:stacked_cli/src/services/pubspec_service.dart';
import 'package:stacked_cli/src/services/template_service.dart';
import 'package:stacked_cli/src/templates/compiled_constants.dart';
import 'package:stacked_cli/src/templates/template_constants.dart';
class CreateWidgetCommand extends Command with ProjectStructureValidator {
final _log = locator<ColorizedLogService>();
final _configService = locator<ConfigService>();
final _processService = locator<ProcessService>();
final _pubspecService = locator<PubspecService>();
final _templateService = locator<TemplateService>();
final _analyticsService = locator<PosthogService>();
@override
String get description => 'Creates a widget with their model file.';
@override
String get name => kTemplateNameWidget;
CreateWidgetCommand() {
argParser
..addFlag(
ksModel,
defaultsTo: true,
help: kCommandHelpModel,
)
..addOption(
ksTemplateType,
abbr: 't',
allowed: kCompiledTemplateTypes[kTemplateNameWidget],
defaultsTo: 'empty',
help: kCommandHelpCreateWidgetTemplate,
)
..addOption(
ksConfigPath,
abbr: 'c',
help: kCommandHelpConfigFilePath,
)
..addOption(
ksPath,
abbr: 'p',
help: kCommandHelpPath,
)
..addOption(
ksLineLength,
abbr: 'l',
help: kCommandHelpLineLength,
valueHelp: '80',
)
..addOption(
ksProjectPath,
help: kCommandHelpProjectPath,
);
}
@override
Future<void> run() async {
try {
final List<String> widgetNames = argResults!.rest;
final templateType = argResults![ksTemplateType];
// load configuration
await _configService.composeAndLoadConfigFile(
configFilePath: argResults![ksConfigPath],
projectPath: argResults![ksProjectPath],
);
// override [widgets_path] value on configuration
_configService.setWidgetsPath(argResults![ksPath]);
_processService.formattingLineLength = argResults![ksLineLength];
await _pubspecService.initialise(
workingDirectory: argResults![ksProjectPath]);
await validateStructure(outputPath: argResults![ksProjectPath]);
for (var i = 0; i < widgetNames.length; i++) {
// Parse the widget name to support subdirectories
final widgetPath = widgetNames[i];
final pathParts = widgetPath.split('/');
final widgetName = pathParts.last;
final subfolders = pathParts.length > 1
? pathParts.sublist(0, pathParts.length - 1).join('/')
: null;
await _templateService.renderTemplate(
templateName: name,
name: widgetName,
subfolder: subfolders,
outputPath: argResults![ksProjectPath],
verbose: true,
hasModel: argResults![ksModel],
templateType: templateType,
);
await _analyticsService.createWidgetEvent(
name: widgetPath,
arguments: argResults!.arguments,
);
}
} catch (e, s) {
_log.error(message: e.toString());
unawaited(_analyticsService.logExceptionEvent(
runtimeType: e.runtimeType.toString(),
message: e.toString(),
stackTrace: s.toString(),
));
}
}
}