@@ -24,16 +24,20 @@ class CompletionInstallation {
2424
2525 /// Creates a [CompletionInstallation] given the current [SystemShell] .
2626 factory CompletionInstallation .fromSystemShell ({
27- required SystemShell systemShell,
27+ required SystemShell ? systemShell,
2828 required Logger logger,
2929 bool ? isWindowsOverride,
3030 Map <String , String >? environmentOverride,
3131 }) {
3232 final isWindows = isWindowsOverride ?? Platform .isWindows;
3333 final environment = environmentOverride ?? Platform .environment;
3434
35+ final configuration = systemShell == null
36+ ? null
37+ : ShellCompletionConfiguration .fromSystemShell (systemShell);
38+
3539 return CompletionInstallation (
36- configuration: ShellCompletionConfiguration . fromSystemShell (systemShell) ,
40+ configuration: configuration ,
3741 logger: logger,
3842 isWindows: isWindows,
3943 environment: environment,
@@ -51,7 +55,7 @@ class CompletionInstallation {
5155 final Map <String , String > environment;
5256
5357 /// The associated [ShellCompletionConfiguration] .
54- final ShellCompletionConfiguration configuration;
58+ final ShellCompletionConfiguration ? configuration;
5559
5660 /// Define the [Directory] in which the
5761 /// completion configuration files will be stored.
@@ -71,6 +75,15 @@ class CompletionInstallation {
7175 /// Install completion configuration hooks for a [rootCommand] in the
7276 /// current shell.
7377 void install (String rootCommand) {
78+ final configuration = this .configuration;
79+
80+ if (configuration == null ) {
81+ throw CompletionInstallationException (
82+ message: 'Unknown shell.' ,
83+ rootCommand: rootCommand,
84+ );
85+ }
86+
7487 logger.detail (
7588 'Installing completion for the command $rootCommand '
7689 'on ${configuration .name }' ,
@@ -87,13 +100,13 @@ class CompletionInstallation {
87100 void createCompletionConfigDir () {
88101 final completionConfigDirPath = completionConfigDir.path;
89102
90- logger.detail (
103+ logger.info (
91104 'Creating completion configuration directory '
92105 'at $completionConfigDirPath ' ,
93106 );
94107
95108 if (completionConfigDir.existsSync ()) {
96- logger.detail (
109+ logger.warn (
97110 'A ${completionConfigDir .path } directory was already found.' ,
98111 );
99112 return ;
@@ -106,20 +119,21 @@ class CompletionInstallation {
106119 /// identified shell.
107120 @visibleForTesting
108121 void writeCompletionScriptForCommand (String rootCommand) {
122+ final configuration = this .configuration! ;
109123 final completionConfigDirPath = completionConfigDir.path;
110124 final commandScriptName = '$rootCommand .${configuration .name }' ;
111125 final commandScriptPath = path.join (
112126 completionConfigDirPath,
113127 commandScriptName,
114128 );
115- logger.detail (
129+ logger.info (
116130 'Writing completion script for $rootCommand on $commandScriptPath ' ,
117131 );
118132
119133 final scriptFile = File (commandScriptPath);
120134
121135 if (scriptFile.existsSync ()) {
122- logger.detail (
136+ logger.warn (
123137 'A script file for $rootCommand was already found on '
124138 '$commandScriptPath .' ,
125139 );
@@ -133,18 +147,19 @@ class CompletionInstallation {
133147 /// [writeCompletionScriptForCommand] the the global completion config file.
134148 @visibleForTesting
135149 void writeCompletionConfigForShell (String rootCommand) {
150+ final configuration = this .configuration! ;
136151 final completionConfigDirPath = completionConfigDir.path;
137152
138153 final configPath = path.join (
139154 completionConfigDirPath,
140155 configuration.completionConfigForShellFileName,
141156 );
142- logger.detail ('Adding config for $rootCommand config entry to $configPath ' );
157+ logger.info ('Adding config for $rootCommand config entry to $configPath ' );
143158
144159 final configFile = File (configPath);
145160
146161 if (! configFile.existsSync ()) {
147- logger.detail ('No file found at $configPath , creating one now' );
162+ logger.info ('No file found at $configPath , creating one now' );
148163 configFile.createSync ();
149164 }
150165 final commandScriptName = '$rootCommand .${configuration .name }' ;
@@ -153,7 +168,7 @@ class CompletionInstallation {
153168 configFile.readAsStringSync ().contains (commandScriptName);
154169
155170 if (containsLine) {
156- logger.detail (
171+ logger.warn (
157172 'A config entry for $rootCommand was already found on $configPath .' ,
158173 );
159174 return ;
@@ -167,13 +182,15 @@ class CompletionInstallation {
167182 }
168183
169184 String get _shellRCFilePath =>
170- _resolveHome (configuration.shellRCFile, environment);
185+ _resolveHome (configuration! .shellRCFile, environment);
171186
172187 /// Write a source to the completion global script in the shell configuration
173188 /// file, which its location is described by the [configuration]
174189 @visibleForTesting
175190 void writeToShellConfigFile (String rootCommand) {
176- logger.detail (
191+ final configuration = this .configuration! ;
192+
193+ logger.info (
177194 'Adding dart cli completion config entry '
178195 'to $_shellRCFilePath ' ,
179196 );
@@ -190,15 +207,15 @@ class CompletionInstallation {
190207 if (! shellRCFile.existsSync ()) {
191208 throw CompletionInstallationException (
192209 rootCommand: rootCommand,
193- message: 'No file found at ${shellRCFile .path }' ,
210+ message: 'No configuration file found at ${shellRCFile .path }' ,
194211 );
195212 }
196213
197214 final containsLine =
198215 shellRCFile.readAsStringSync ().contains (completionConfigPath);
199216
200217 if (containsLine) {
201- logger.detail ('A completion config entry was found on'
218+ logger.warn ('A completion config entry was already found on'
202219 ' $_shellRCFilePath .' );
203220 return ;
204221 }
@@ -235,13 +252,13 @@ class CompletionInstallation {
235252 '''
236253## [$scriptName ]
237254## $description
238- ${configuration .sourceLineTemplate (scriptPath )}
255+ ${configuration ! .sourceLineTemplate (scriptPath )}
239256## [/$scriptName ]
240257
241258''' ,
242259 );
243260
244- logger.detail ('Added config to $configFilePath ' );
261+ logger.info ('Added config to $configFilePath ' );
245262 }
246263}
247264
0 commit comments