platform.dart 1.52 KB
Newer Older
1 2 3 4
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
import 'dart:async';

7 8 9
import 'package:platform/platform.dart';

import 'context.dart';
10
import 'file_system.dart';
11 12 13 14

export 'package:platform/platform.dart';

const Platform _kLocalPlatform = const LocalPlatform();
15
const String _kRecordingType = 'platform';
16

17
Platform get platform => context[Platform] ?? _kLocalPlatform;
18

19 20
/// Serializes the current [platform] to the specified base recording
/// [location].
21 22 23 24 25
///
/// Platform metadata will be recorded in a subdirectory of [location] named
/// `"platform"`. It is permissible for [location] to represent an existing
/// non-empty directory as long as there is no collision with the `"platform"`
/// subdirectory.
26 27 28
///
/// Returns the existing platform.
Future<Platform> getRecordingPlatform(String location) async {
29 30
  final Directory dir = getRecordingSink(location, _kRecordingType);
  final File file = _getPlatformManifest(dir);
31
  await file.writeAsString(platform.toJson(), flush: true);
32
  return platform;
33 34
}

35
Future<FakePlatform> getReplayPlatform(String location) async {
36 37 38
  final Directory dir = getReplaySource(location, _kRecordingType);
  final File file = _getPlatformManifest(dir);
  final String json = await file.readAsString();
39
  return new FakePlatform.fromJson(json);
40 41 42
}

File _getPlatformManifest(Directory dir) {
43
  final String path = dir.fileSystem.path.join(dir.path, 'MANIFEST.txt');
44 45
  return dir.fileSystem.file(path);
}