fingerprint.dart 4.3 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:crypto/crypto.dart' show md5;
import 'package:meta/meta.dart';

8
import '../convert.dart' show json;
9
import 'file_system.dart';
10
import 'logger.dart';
11
import 'utils.dart';
12 13 14 15

/// A tool that can be used to compute, compare, and write [Fingerprint]s for a
/// set of input files and associated build settings.
///
16 17
/// This class should only be used in situations where `assemble` is not appropriate,
/// such as checking if Cocoapods should be run.
18 19 20 21
class Fingerprinter {
  Fingerprinter({
    @required this.fingerprintPath,
    @required Iterable<String> paths,
22 23
    @required FileSystem fileSystem,
    @required Logger logger,
24 25 26
  }) : _paths = paths.toList(),
       assert(fingerprintPath != null),
       assert(paths != null && paths.every((String path) => path != null)),
27 28
       _logger = logger,
       _fileSystem = fileSystem;
29 30 31

  final String fingerprintPath;
  final List<String> _paths;
32 33
  final Logger _logger;
  final FileSystem _fileSystem;
34

35 36
  Fingerprint buildFingerprint() {
    final List<String> paths = _getPaths();
37
    return Fingerprint.fromBuildInputs(paths, _fileSystem);
38 39
  }

40
  bool doesFingerprintMatch() {
41
    try {
42
      final File fingerprintFile = _fileSystem.file(fingerprintPath);
43
      if (!fingerprintFile.existsSync()) {
44
        return false;
45
      }
46

47
      final List<String> paths = _getPaths();
48
      if (!paths.every(_fileSystem.isFileSync)) {
49
        return false;
50
      }
51

52 53
      final Fingerprint oldFingerprint = Fingerprint.fromJson(fingerprintFile.readAsStringSync());
      final Fingerprint newFingerprint = buildFingerprint();
54
      return oldFingerprint == newFingerprint;
55
    } on Exception catch (e) {
56
      // Log exception and continue, fingerprinting is only a performance improvement.
57
      _logger.printTrace('Fingerprint check error: $e');
58 59 60 61
    }
    return false;
  }

62
  void writeFingerprint() {
63
    try {
64
      final Fingerprint fingerprint = buildFingerprint();
65
      _fileSystem.file(fingerprintPath).writeAsStringSync(fingerprint.toJson());
66
    } on Exception catch (e) {
67
      // Log exception and continue, fingerprinting is only a performance improvement.
68
      _logger.printTrace('Fingerprint write error: $e');
69 70 71
    }
  }

72
  List<String> _getPaths() => _paths;
73 74 75 76 77 78
}

/// A fingerprint that uniquely identifies a set of build input files and
/// properties.
///
/// See [Fingerprinter].
79
@immutable
80
class Fingerprint {
81 82
  const Fingerprint._({
    Map<String, String> checksums,
83
  })  : _checksums = checksums;
84

85 86
  factory Fingerprint.fromBuildInputs(Iterable<String> inputPaths, FileSystem fileSystem) {
    final Iterable<File> files = inputPaths.map<File>(fileSystem.file);
87
    final Iterable<File> missingInputs = files.where((File file) => !file.existsSync());
88
    if (missingInputs.isNotEmpty) {
89
      throw Exception('Missing input files:\n' + missingInputs.join('\n'));
90
    }
91 92 93 94 95 96
    return Fingerprint._(
      checksums: <String, String>{
        for (final File file in files)
          file.path: md5.convert(file.readAsBytesSync()).toString(),
      },
    );
97 98 99 100
  }

  /// Creates a Fingerprint from serialized JSON.
  ///
101
  /// Throws [Exception], if there is a version mismatch between the
102
  /// serializing framework and this framework.
103
  factory Fingerprint.fromJson(String jsonData) {
104
    final Map<String, dynamic> content = castStringKeyedMap(json.decode(jsonData));
105 106 107
    return Fingerprint._(
      checksums: castStringKeyedMap(content['files'])?.cast<String,String>() ?? <String, String>{},
    );
108 109
  }

110
  final Map<String, String> _checksums;
111 112 113 114 115 116

  String toJson() => json.encode(<String, dynamic>{
    'files': _checksums,
  });

  @override
117
  bool operator==(Object other) {
118
    return other is Fingerprint
119
        && _equalMaps(other._checksums, _checksums);
120 121 122 123 124 125 126 127 128
  }

  bool _equalMaps(Map<String, String> a, Map<String, String> b) {
    return a.length == b.length
        && a.keys.every((String key) => a[key] == b[key]);
  }

  @override
  // Ignore map entries here to avoid becoming inconsistent with equals
129 130
  // due to differences in map entry order. This is a really bad hash
  // function and should eventually be deprecated and removed.
131
  int get hashCode => _checksums.length.hashCode;
132 133

  @override
134
  String toString() => '{checksums: $_checksums}';
135
}