getOutputUri method

Uri getOutputUri(
  1. String extension
)

Resolves the output URI for a file with the given extension.

When generateToCache is true, outputs are written to the build cache. Otherwise, they're written alongside their inputs.

@param extension The extension for the output file @return The URI where the output file should be written

Implementation

Uri getOutputUri(String extension) {
  if (generateToCache) {
    final Uri shortUri = asset.shortUri;
    final isPackageUri = shortUri.scheme == 'package';
    assert(
      isPackageUri || shortUri.scheme == 'asset',
      'Only package and asset URIs are supported',
    );
    final parts = isPackageUri
        ? [...shortUri.pathSegments.take(1), 'lib', ...shortUri.pathSegments.skip(1)]
        : shortUri.pathSegments;
    final path = p.joinAll(parts);
    final Uri outputUri = shortUri.replace(
      path: '${p.withoutExtension(path)}$extension',
    );
    _validateOutput(outputUri);
    final String filename = p.basename(outputUri.path);
    final Directory outputDir = Directory(
      p.join(p.current, generatedDir, p.dirname(outputUri.path)),
    );
    if (!outputDir.existsSync()) {
      outputDir.createSync(recursive: true);
    }
    return Uri.file(p.join(outputDir.path, filename));
  } else {
    final Uri outputUri = asset.uriWithExtension(extension);
    _validateOutput(outputUri);
    final Directory dir = Directory.fromUri(
      outputUri.replace(path: p.dirname(outputUri.path)),
    );
    if (!dir.existsSync()) {
      dir.createSync(recursive: true);
    }
    return outputUri;
  }
}