updateExistingProjectImports static method
기존 프로젝트의 import 경로 업데이트 pubspec.yaml에서 패키지 이름을 읽어서 모든 Dart 파일의 import 경로를 업데이트합니다
Implementation
static Future<void> updateExistingProjectImports({
required String projectPath,
required Logger logger,
}) async {
try {
final pubspecFile = File(path.join(projectPath, 'pubspec.yaml'));
if (!pubspecFile.existsSync()) {
logger.err('❌ pubspec.yaml을 찾을 수 없습니다: $projectPath');
return;
}
// pubspec.yaml에서 패키지 이름 읽기
final pubspecContent = await pubspecFile.readAsString();
final nameMatch = RegExp(r'^name:\s*([^\s]+)', multiLine: true)
.firstMatch(pubspecContent);
if (nameMatch == null) {
logger.err('❌ pubspec.yaml에서 패키지 이름을 찾을 수 없습니다.');
return;
}
final packageName = nameMatch.group(1)?.trim();
if (packageName == null || packageName.isEmpty) {
logger.err('❌ 유효하지 않은 패키지 이름입니다.');
return;
}
logger.info('📦 패키지 이름: $packageName');
logger.info('🔄 import 경로 업데이트 중...');
final libDir = Directory(path.join(projectPath, 'lib'));
if (!libDir.existsSync()) {
logger.warn('⚠️ lib 폴더를 찾을 수 없습니다.');
return;
}
int updatedCount = 0;
// lib 폴더 내의 모든 .dart 파일 찾기
await for (final entity in libDir.list(recursive: true)) {
if (entity is File && entity.path.endsWith('.dart')) {
try {
var content = await entity.readAsString();
final originalContent = content;
// package:naeil_flutter_init/를 새로운 패키지 이름으로 교체
content = content.replaceAll(
RegExp(r"package:naeil_flutter_init/"),
'package:$packageName/',
);
// 내용이 변경되었으면 파일에 저장
if (content != originalContent) {
await entity.writeAsString(content);
updatedCount++;
logger.detail('업데이트: ${path.relative(entity.path, from: projectPath)}');
}
} catch (e) {
logger.warn('⚠️ 파일 업데이트 실패: ${entity.path} - $e');
}
}
}
if (updatedCount > 0) {
logger.success('✅ 총 $updatedCount개 Dart 파일의 import 경로를 업데이트했습니다.');
} else {
logger.info('ℹ️ 업데이트할 파일이 없습니다.');
}
} catch (e, stackTrace) {
logger.err('❌ import 경로 업데이트 중 오류 발생: $e');
logger.detail('스택 트레이스: $stackTrace');
rethrow;
}
}