downloadRepository method

Future<void> downloadRepository(
  1. String githubUrl, {
  2. String outputPath = '',
})

Downloads content from a GitHub repository.

The method can handle both single files and entire directories. For directories, it will recursively download all files.

githubUrl The GitHub URL to download from (e.g., 'https://github.com/owner/repo/tree/branch/path') outputPath The output path where to save the downloaded files (defaults to current directory)

Throws InvalidGitHubUrlException if the URL is invalid Throws GitHubApiException if there's an error with the GitHub API Throws DownloadException if there's an error downloading or saving files

Implementation

Future<void> downloadRepository(
  String githubUrl, {
  String outputPath = '',
}) async {
  try {
    final repoInfo = GitHubRepositoryInfo.fromUrl(githubUrl);
    final contentPath = repoInfo.targetPath.isNotEmpty
        ? '${repoInfo.targetPath}/${repoInfo.target}'
        : repoInfo.target;
    final contentUrl =
        'https://api.github.com/repos/${repoInfo.owner}/${repoInfo.repo}/contents/$contentPath?ref=${repoInfo.branch}';

    final contents = await _apiClient.getContents(contentUrl);

    if (contents.length == 1) {
      // For single files, determine if outputPath is a filename or directory
      final path = _getSingleFileOutputPath(outputPath, repoInfo.target);
      await _downloadSingleFile(
        content: contents.first,
        outputPath: path,
        repoName: repoInfo.repo,
      );
    } else {
      // For directories, use the output directory directly to extract contents
      await _downloadMultipleFiles(
        contents: contents,
        outputPath: outputPath,
      );
    }
  } finally {
    reset();
    _apiClient.dispose();
  }
}