Skip to content

Commit

Permalink
Merge pull request #157 from Workiva/WINDOWS#@$
Browse files Browse the repository at this point in the history
Fix intl_message_migration issue with Windows
  • Loading branch information
rmconsole2-wf authored Jul 6, 2022
2 parents 7e06e1a + 2c615b3 commit 60633a9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 41 deletions.
4 changes: 0 additions & 4 deletions lib/src/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
/// suggestor.
library over_react_codemod.src.constants;

import 'dart:io';

const String generatedPrefix = r'$';
const String privatePrefix = r'_';
const String privateGeneratedPrefix = '$privatePrefix$generatedPrefix';
Expand Down Expand Up @@ -137,5 +135,3 @@ final RegExp dependencyOverrideRegExp = RegExp(
r'^dependency_overrides:\s*$',
multiLine: true,
);

final String slash = Platform.pathSeparator;
73 changes: 36 additions & 37 deletions lib/src/executables/intl_message_migration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,22 @@
// limitations under the License.

import 'dart:io';
import 'package:file/file.dart';
import 'package:file/local.dart';
import 'package:over_react_codemod/src/constants.dart';
import 'package:over_react_codemod/src/intl_suggestors/intl_configs_migrator.dart';
import 'package:over_react_codemod/src/intl_suggestors/intl_importer.dart';
import 'package:over_react_codemod/src/intl_suggestors/intl_migrator.dart';

import 'package:analyzer/dart/analysis/utilities.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:args/args.dart';
import 'package:codemod/codemod.dart';
import 'package:file/file.dart';
import 'package:file/local.dart';
import 'package:glob/glob.dart';
import 'package:glob/list_local_fs.dart';
import 'package:logging/logging.dart';
import 'package:over_react_codemod/src/intl_suggestors/intl_configs_migrator.dart';
import 'package:over_react_codemod/src/intl_suggestors/intl_importer.dart';
import 'package:over_react_codemod/src/intl_suggestors/intl_migrator.dart';
import 'package:over_react_codemod/src/intl_suggestors/utils.dart';
import 'package:over_react_codemod/src/util/package_util.dart';
import 'package:path/path.dart' as p;

import '../util.dart';

Expand Down Expand Up @@ -157,20 +158,19 @@ void main(List<String> args) async {
// TODO - reference analyzer issue for this once it's created
final packageRoots = dartPaths.map(findPackageRootFor).toSet().toList();
packageRoots.sort((packageA, packageB) =>
packageB.split(slash).length - packageA.split(slash).length);

Map<String, String> packageNameLookup = Map.fromIterable(
pubspecYamlPaths(),
key: (path) => findPackageRootFor(path).split(slash).last,
value: (path) {
final pubspec = fs.file(path);
List<String> pubspecLines = pubspec.readAsLinesSync();
String nameLine =
pubspecLines.firstWhere((line) => line.startsWith('name'));
var nameLineParts = nameLine.split(':');
return nameLineParts[1].trim();
},
);
p.split(packageB).length - p.split(packageA).length);

// TODO: Use packageConfig and utilities for reading that rather than manually parsing pubspec..
Map<String, String> packageNameLookup = {
for (String path in pubspecYamlPaths())
p.basename(findPackageRootFor(path)): fs
.file(path)
.readAsLinesSync()
.firstWhere((line) => line.startsWith('name'))
.split(':')
.last
.trim()
};

final processedPackages = Set<String>();
await pubGetForAllPackageRoots(dartPaths);
Expand All @@ -187,15 +187,15 @@ void main(List<String> args) async {
for (String package in packageRoots) {
_log.info('Starting migration for $package');

final packageRoot = package.split(slash).last;
final packageRoot = p.basename(package);
final packageName = packageNameLookup[packageRoot] ?? 'fix_me_bad_name';
_log.info('Starting migration for $packageName');
final packageDartPath =
dartFilesToMigrateForPackage(package, processedPackages).toList();
sortPartsLast(packageDartPath);

final File outputFile = fs.currentDirectory
.childFile('${package}${slash}lib${slash}src${slash}intl${slash}${packageName}_intl.dart');
final File outputFile = fs.currentDirectory.childFile(
p.join(package, 'lib', 'src', 'intl', '${packageName}_intl.dart'));
final bool existingOutputFile = outputFile.existsSync();

final className = toClassName('${packageName}');
Expand All @@ -216,7 +216,6 @@ void main(List<String> args) async {
final importMigrator =
(FileContext context) => intlImporter(context, packageName, className);


exitCode = await runCodemodSequences(packageDartPath, [
[intlPropMigrator],
[displayNameMigrator],
Expand All @@ -228,12 +227,12 @@ void main(List<String> args) async {
outputFile.deleteSync();
} else {
List<String> lines = outputFile.readAsLinesSync();
final functions = lines.sublist(4);
functions.removeWhere((string) => string == '');
functions.sort();
lines.replaceRange(4, lines.length, functions);
lines.add('}');
outputFile.writeAsStringSync(lines.join('\n'), mode: FileMode.write);
final functions = lines.sublist(4);
functions.removeWhere((string) => string == '');
functions.sort();
lines.replaceRange(4, lines.length, functions);
lines.add('}');
outputFile.writeAsStringSync(lines.join('\n'), mode: FileMode.write);
}
}
}
Expand Down Expand Up @@ -293,7 +292,7 @@ Iterable<String> dartFilesToMigrate() => Glob('**.dart', recursive: true)

Iterable<String> dartFilesToMigrateForPackage(
String package, Set<String> processedPackages) =>
Glob('${slash}$package${slash}lib${slash}*${slash}**.dart', recursive: true)
Glob(p.join(package, 'lib', '*', '**.dart'), recursive: true)
.listSync()
.whereType<File>()
.where((file) => !file.path.contains('.sg.g.dart'))
Expand All @@ -304,10 +303,10 @@ Iterable<String> dartFilesToMigrateForPackage(
.where(isNotWithinTopLevelBuildOutputDir)
.where(isNotWithinTopLevelToolDir)
.where((file) => !processedPackages.contains(file.path))
.map((e) => e.path);
.map((e) => e.path)
.toList();

Iterable<String> experienceConfigDartFiles() => Glob('**.dart', recursive: true)
.listSync()
.whereType<File>()
.where((file) => file.path.contains('_experience_config.dart'))
.map((e) => e.path);
Iterable<String> experienceConfigDartFiles() => [
for (var f in Glob('**.dart', recursive: true).listSync())
if (f is File && f.path.contains('_experience_config.dart')) f.path
];

0 comments on commit 60633a9

Please sign in to comment.