1// Copyright 2016 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5import 'dart:async'; 6 7import 'package:process/process.dart'; 8import 'package:process/record_replay.dart'; 9 10import 'common.dart'; 11import 'context.dart'; 12import 'file_system.dart'; 13import 'process.dart'; 14 15const String _kRecordingType = 'process'; 16const ProcessManager _kLocalProcessManager = LocalProcessManager(); 17 18/// The active process manager. 19ProcessManager get processManager => context.get<ProcessManager>() ?? _kLocalProcessManager; 20 21/// Gets a [ProcessManager] that will record process invocation activity to the 22/// specified base recording [location]. 23/// 24/// Activity will be recorded in a subdirectory of [location] named `"process"`. 25/// It is permissible for [location] to represent an existing non-empty 26/// directory as long as there is no collision with the `"process"` 27/// subdirectory. 28RecordingProcessManager getRecordingProcessManager(String location) { 29 final Directory dir = getRecordingSink(location, _kRecordingType); 30 const ProcessManager delegate = LocalProcessManager(); 31 final RecordingProcessManager manager = RecordingProcessManager(delegate, dir); 32 addShutdownHook(() async { 33 await manager.flush(finishRunningProcesses: true); 34 }, ShutdownStage.SERIALIZE_RECORDING); 35 return manager; 36} 37 38/// Gets a [ProcessManager] that replays process activity from a previously 39/// recorded set of invocations. 40/// 41/// [location] must represent a directory to which process activity has been 42/// recorded (i.e. the result of having been previously passed to 43/// [getRecordingProcessManager]), or a [ToolExit] will be thrown. 44Future<ReplayProcessManager> getReplayProcessManager(String location) async { 45 final Directory dir = getReplaySource(location, _kRecordingType); 46 47 ProcessManager manager; 48 try { 49 manager = await ReplayProcessManager.create(dir); 50 } on ArgumentError catch (error) { 51 throwToolExit('Invalid replay-from: $error'); 52 } 53 54 return manager; 55} 56