11import 'dart:io' ;
22
33import 'package:slang/src/builder/builder/slang_file_collection_builder.dart' ;
4- import 'package:slang/src/builder/builder/translation_map_builder.dart' ;
5- import 'package:slang/src/builder/generator_facade.dart' ;
64import 'package:slang/src/builder/model/raw_config.dart' ;
75import 'package:slang/src/builder/model/slang_file_collection.dart' ;
8- import 'package:slang/src/builder/utils/file_utils.dart' ;
96import 'package:slang/src/builder/utils/path_utils.dart' ;
107import 'package:slang/src/runner/analyze.dart' ;
118import 'package:slang/src/runner/apply.dart' ;
129import 'package:slang/src/runner/clean.dart' ;
1310import 'package:slang/src/runner/configure.dart' ;
1411import 'package:slang/src/runner/edit.dart' ;
12+ import 'package:slang/src/runner/generate.dart' ;
1513import 'package:slang/src/runner/help.dart' ;
1614import 'package:slang/src/runner/migrate.dart' ;
1715import 'package:slang/src/runner/normalize.dart' ;
1816import 'package:slang/src/runner/stats.dart' ;
19- import 'package:slang/src/runner/utils/format.dart' ;
2017import 'package:slang/src/runner/wip.dart' ;
2118import 'package:slang/src/utils/log.dart' as log;
19+ import 'package:slang/src/utils/stopwatch.dart' ;
2220import 'package:watcher/watcher.dart' ;
2321
2422/// Determines what the runner will do
@@ -168,14 +166,23 @@ void main(List<String> arguments) async {
168166 arguments: filteredArguments,
169167 );
170168 break ;
171- case RunnerMode .generate:
172169 case RunnerMode .stats:
170+ await runStats (
171+ fileCollection: fileCollection,
172+ stopwatch: stopwatch,
173+ );
174+ break ;
173175 case RunnerMode .analyze:
176+ await runAnalyzeTranslations (
177+ fileCollection: fileCollection,
178+ arguments: filteredArguments,
179+ stopwatch: stopwatch,
180+ );
181+ break ;
182+ case RunnerMode .generate:
174183 await generateTranslations (
175- mode: mode,
176184 fileCollection: fileCollection,
177185 stopwatch: stopwatch,
178- arguments: filteredArguments,
179186 );
180187 break ;
181188 case RunnerMode .migrate:
@@ -282,7 +289,6 @@ Future<void> _generateTranslationsFromWatch({
282289 bool success = true ;
283290 try {
284291 await generateTranslations (
285- mode: RunnerMode .watch,
286292 fileCollection: SlangFileCollectionBuilder .fromFileModel (
287293 config: config,
288294 files: newFiles,
@@ -308,114 +314,6 @@ Future<void> _generateTranslationsFromWatch({
308314 }
309315}
310316
311- /// Reads the translations from hard drive and generates the g.dart file
312- /// The [files] are already filtered (only translation files!).
313- Future <void > generateTranslations ({
314- required RunnerMode mode,
315- required SlangFileCollection fileCollection,
316- Stopwatch ? stopwatch,
317- List <String >? arguments,
318- }) async {
319- if (fileCollection.files.isEmpty) {
320- log.error ('No translation file found.' );
321- return ;
322- }
323-
324- // STEP 1: determine base name and output file name / path
325- final outputFilePath = fileCollection.determineOutputPath ();
326-
327- // STEP 2: scan translations
328- log.verbose ('Scanning translations...\n ' );
329-
330- final translationMap = await TranslationMapBuilder .build (
331- fileCollection: fileCollection,
332- );
333-
334- if (mode == RunnerMode .stats) {
335- getStats (
336- rawConfig: fileCollection.config,
337- translationMap: translationMap,
338- ).printResult ();
339- if (stopwatch != null ) {
340- log.info ('\n Scan done. (${stopwatch .elapsed })' );
341- }
342- return ; // skip generation
343- } else if (mode == RunnerMode .analyze) {
344- runAnalyzeTranslations (
345- rawConfig: fileCollection.config,
346- translationMap: translationMap,
347- arguments: arguments ?? [],
348- );
349- if (stopwatch != null ) {
350- log.info ('Analysis done. ${stopwatch .elapsedSeconds }' );
351- }
352- return ; // skip generation
353- }
354-
355- // STEP 3: generate .g.dart content
356- final result = GeneratorFacade .generate (
357- rawConfig: fileCollection.config,
358- translationMap: translationMap,
359- inputDirectoryHint: fileCollection.determineInputPath (),
360- );
361-
362- // STEP 4: write output to hard drive
363- FileUtils .createMissingFolders (filePath: outputFilePath);
364-
365- FileUtils .writeFile (
366- path: BuildResultPaths .mainPath (outputFilePath),
367- content: result.main,
368- );
369- for (final entry in result.translations.entries) {
370- final locale = entry.key;
371- final localeTranslations = entry.value;
372- FileUtils .writeFile (
373- path: BuildResultPaths .localePath (
374- outputPath: outputFilePath,
375- locale: locale,
376- ),
377- content: localeTranslations,
378- );
379- }
380-
381- if (log.level == log.Level .verbose) {
382- log.verbose ('\n Output:' );
383- log.verbose (' -> $outputFilePath ' );
384- for (final locale in result.translations.keys) {
385- log.verbose (' -> ${BuildResultPaths .localePath (
386- outputPath : outputFilePath ,
387- locale : locale ,
388- )}' );
389- }
390- }
391-
392- if (fileCollection.config.format.enabled) {
393- final formatDir = PathUtils .getParentPath (outputFilePath)! ;
394- Stopwatch ? formatStopwatch;
395- if (log.level == log.Level .verbose) {
396- log.verbose ('\n Formatting "$formatDir " ...' );
397- if (stopwatch != null ) {
398- formatStopwatch = Stopwatch ()..start ();
399- }
400- }
401- await runDartFormat (
402- dir: formatDir,
403- width: fileCollection.config.format.width,
404- );
405- if (formatStopwatch != null ) {
406- log.verbose ('Format done. ${formatStopwatch .elapsedSeconds }' );
407- }
408- }
409-
410- if (stopwatch != null ) {
411- if (log.level == log.Level .verbose) {
412- log.verbose ('' );
413- }
414- log.info (
415- '${_green }Translations generated successfully. ${stopwatch .elapsedSeconds }$_reset ' );
416- }
417- }
418-
419317// returns current time in HH:mm:ss
420318String get currentTime {
421319 final now = DateTime .now ();
@@ -444,9 +342,3 @@ const _green = '\x1B[32m';
444342const _yellow = '\x 1B[33m' ;
445343const _red = '\x 1B[31m' ;
446344const _reset = '\x 1B[0m' ;
447-
448- extension on Stopwatch {
449- String get elapsedSeconds {
450- return '(${elapsed .inMilliseconds / 1000 } seconds)' ;
451- }
452- }
0 commit comments