desktop.dart 2.69 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:meta/meta.dart';

import '../../base/file_system.dart';
import '../depfile.dart';

10 11 12 13 14 15 16
/// Unpack the engine artifact list [artifacts] from [engineSourcePath] and
/// [clientSourcePath] (if provided) into a directory [outputDirectory].
///
/// Returns a [Depfile] including all copied files.
///
/// Throws an [Exception] if [artifacts] includes missing files, directories,
/// or links.
17 18 19 20
Depfile unpackDesktopArtifacts({
  @required FileSystem fileSystem,
  @required List<String> artifacts,
  @required Directory outputDirectory,
21 22
  @required String engineSourcePath,
  String clientSourcePath,
23 24 25 26
}) {
  final List<File> inputs = <File>[];
  final List<File> outputs = <File>[];
  for (final String artifact in artifacts) {
27
    final String entityPath = fileSystem.path.join(engineSourcePath, artifact);
28 29 30
    final FileSystemEntityType entityType = fileSystem.typeSync(entityPath);

    if (entityType == FileSystemEntityType.notFound
31
     || entityType == FileSystemEntityType.directory
32 33 34
     || entityType == FileSystemEntityType.link) {
      throw Exception('Unsupported file type: $entityType');
    }
35 36 37 38 39 40 41 42
    assert(entityType == FileSystemEntityType.file);
    final String outputPath = fileSystem.path.join(
      outputDirectory.path,
      fileSystem.path.relative(entityPath, from: engineSourcePath),
    );
    final File destinationFile = fileSystem.file(outputPath);
    if (!destinationFile.parent.existsSync()) {
      destinationFile.parent.createSync(recursive: true);
43
    }
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    final File inputFile = fileSystem.file(entityPath);
    inputFile.copySync(destinationFile.path);
    inputs.add(inputFile);
    outputs.add(destinationFile);
  }
  if (clientSourcePath == null) {
    return Depfile(inputs, outputs);
  }
  final Directory clientSourceDirectory = fileSystem.directory(clientSourcePath);
  if (!clientSourceDirectory.existsSync()) {
    throw Exception('Missing clientSourceDirectory: $clientSourcePath');
  }
  for (final File input in clientSourceDirectory
    .listSync(recursive: true)
    .whereType<File>()) {
    final String outputPath = fileSystem.path.join(
      outputDirectory.path,
      fileSystem.path.relative(input.path, from: clientSourceDirectory.parent.path),
    );
    final File destinationFile = fileSystem.file(outputPath);
    if (!destinationFile.parent.existsSync()) {
      destinationFile.parent.createSync(recursive: true);
66
    }
67 68 69 70
    final File inputFile = fileSystem.file(input);
    inputFile.copySync(destinationFile.path);
    inputs.add(inputFile);
    outputs.add(destinationFile);
71 72 73
  }
  return Depfile(inputs, outputs);
}