Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 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
15 changes: 15 additions & 0 deletions bin/intl_quick.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2021 Workiva Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

export 'package:over_react_codemod/src/executables/intl_quick_migration.dart';
104 changes: 104 additions & 0 deletions lib/src/executables/intl_quick_migration.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright 2021 Workiva Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import 'dart:io';

import 'package:args/args.dart';
import 'package:codemod/codemod.dart';
import 'package:file/file.dart';
import 'package:file/local.dart';
import 'package:over_react_codemod/src/intl_suggestors/intl_messages.dart';
import 'package:over_react_codemod/src/intl_suggestors/intl_migrator.dart';
import 'package:path/path.dart' as p;

typedef Migrator = Stream<Patch> Function(FileContext);

final FileSystem fs = const LocalFileSystem();

final parser = ArgParser()
..addFlag(
'help',
abbr: 'h',
negatable: false,
help: 'Prints this help output.',
)
..addFlag(
'verbose',
abbr: 'v',
negatable: false,
help: 'Outputs all logging to stdout/stderr.',
);

late ArgResults parsedArgs;

void main(List<String> args) async {
parsedArgs = parser.parse(args);
if (parsedArgs['help'] as bool) {
printUsage();
return;
}

if (parsedArgs.rest.isEmpty) {
print('You have to specify a file');
exit(1);
}
var intlPath = p.canonicalize(p.absolute((parsedArgs.rest.first)));

await migratePackage(fs.currentDirectory.path, intlPath);
}

void printUsage() {
stderr.writeln('Migrates a particular string to an intl message.');
stderr.writeln();
stderr.writeln('Usage:');
stderr.writeln(' intl_quick [arguments]');
stderr.writeln();
stderr.writeln('Options:');
stderr.writeln(parser.usage);
}

/// Migrate files included in [paths] within [packagePath].
///
/// We expect [paths] to be absolute.
Future<void> migratePackage(String packagePath, String path) async {
final packageName = p.basename(packagePath);

final IntlMessages messages = IntlMessages(packageName,
directory: fs.currentDirectory, packagePath: packagePath);

exitCode = await runMigrators([path], [], messages, packageName);

messages.write(force: false);
// This will leave the intl.dart file unformatted, but that takes too long, so we'll just leave it out.
}

Future<int> runMigrators(List<String> packageDartPaths,
List<String> codemodArgs, IntlMessages messages, String packageName) async {
// final intlPropMigrator = IntlMigrator(messages.className, messages);
final constantStringMigrator =
ConstantStringMigrator(messages.className, messages);
// final importMigrator = (FileContext context) =>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably run the importer too, so unwind the change to run only a single migrator. But we shouldn't (I think) need the change that runs them all sequentially. Or we could let the IDE Extension still do the import.

// intlImporter(context, packageName, messages.className);

// List<List<Migrator>> migrators = [
// [intlPropMigrator],
// [constantStringMigrator],
// [importMigrator],
// ];

var result = await runInteractiveCodemod(
packageDartPaths, constantStringMigrator,
defaultYes: true);
return result;
}
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ executables:
rmui_preparation:
rmui_bundle_update:
intl_message_migration:
intl_quick:
dependency_validator:
ignore:
- meta
Expand Down