initialize static method

Future<void> initialize({
  1. required String targetPath,
  2. required String samplePath,
  3. required String projectName,
  4. required String packageId,
  5. required List<String> platforms,
})

sample 폴더 구조를 새 프로젝트에 복사하고 설정

Implementation

static Future<void> initialize({
  required String targetPath,
  required String samplePath,
  required String projectName,
  required String packageId,
  required List<String> platforms,
}) async {
  final logger = Logger();

  try {
    final targetDir = Directory(targetPath);
    final sampleDir = Directory(samplePath);

    if (!sampleDir.existsSync()) {
      logger.err('❌ sample 폴더를 찾을 수 없습니다: $samplePath');
      return;
    }

    if (!targetDir.existsSync()) {
      logger.err('❌ 대상 프로젝트 폴더를 찾을 수 없습니다: $targetPath');
      return;
    }

    final hasWeb = platforms.contains('web');

    // 1. lib 폴더 복사
    logger.detail('lib 폴더 구조 복사 중...');
    await _copyDirectory(
      source: path.join(samplePath, 'lib'),
      target: path.join(targetPath, 'lib'),
      logger: logger,
    );

    // 2. web 폴더 복사 (web이 선택된 경우에만)
    if (hasWeb) {
      final sampleWebDir = Directory(path.join(samplePath, 'web'));
      if (sampleWebDir.existsSync()) {
        logger.detail('web 폴더 복사 중...');
        await _copyDirectory(
          source: path.join(samplePath, 'web'),
          target: path.join(targetPath, 'web'),
          logger: logger,
        );
      }
    } else {
      logger.detail('web이 선택되지 않아 web 폴더를 복사하지 않습니다.');
      // lib/web 폴더가 있으면 삭제
      final libWebDir = Directory(path.join(targetPath, 'lib', 'web'));
      if (libWebDir.existsSync()) {
        logger.detail('lib/web 폴더 삭제 중...');
        await libWebDir.delete(recursive: true);
      }
    }

    // 3. pubspec.yaml 업데이트
    logger.detail('pubspec.yaml 업데이트 중...');
    await _updatePubspec(
      targetPath: targetPath,
      samplePath: samplePath,
      projectName: projectName,
      packageId: packageId,
      logger: logger,
    );

    // 4. 모든 Dart 파일의 import 경로 업데이트
    logger.detail('모든 Dart 파일의 import 경로 업데이트 중...');
    await _updateAllDartImports(
      targetPath: targetPath,
      packageId: packageId,
      hasWeb: hasWeb,
      logger: logger,
    );

    // 5. Android 패키지 이름 업데이트
    logger.detail('Android 패키지 이름 업데이트 중...');
    await _updateAndroidPackage(
      targetPath: targetPath,
      packageId: packageId,
      logger: logger,
    );

    // 6. iOS 번들 ID 업데이트
    logger.detail('iOS 번들 ID 업데이트 중...');
    await _updateIOSBundleId(
      targetPath: targetPath,
      packageId: packageId,
      logger: logger,
    );

    logger.success('✅ 프로젝트 초기화 완료');
  } catch (e, stackTrace) {
    logger.err('❌ 프로젝트 초기화 중 오류 발생: $e');
    logger.detail('스택 트레이스: $stackTrace');
    rethrow;
  }
}