liveRecordingPath function

String liveRecordingPath({
  1. required String root,
  2. required List<String> names,
  3. required String device,
  4. required DateTime at,
  5. String? requested,
  6. String? deviceId,
  7. bool several = false,
})

Where a recording of names on device is saved.

By default that's build/playbooks/<names>-<device>-<time>.mp4 in root. requested is the file from --record=<file>, relative to root; when several apps are recorded, each gets its device name added. deviceId is added after the device name when another app being recorded has the same one, so their files can't collide.

Implementation

String liveRecordingPath({
  required String root,
  required List<String> names,
  required String device,
  required DateTime at,
  String? requested,
  String? deviceId,
  bool several = false,
}) {
  final String id = (deviceId ?? '')
      .replaceAll(RegExp(r'[^A-Za-z0-9]'), '')
      .toLowerCase();
  final String slug = [
    _slug(device),
    if (id.isNotEmpty) id.substring(0, min(4, id.length)),
  ].where((String part) => part.isNotEmpty).join('-');

  if (requested != null) {
    final String path = File(requested).isAbsolute
        ? requested
        : '$root${Platform.pathSeparator}$requested';
    if (!several) return path;
    final int dot = path.lastIndexOf('.');
    return '${path.substring(0, dot)}-$slug${path.substring(dot)}';
  }

  String two(int n) => n.toString().padLeft(2, '0');
  final String stamp =
      '${at.year}${two(at.month)}${two(at.day)}-'
      '${two(at.hour)}${two(at.minute)}${two(at.second)}';
  final String playbooks = names
      .map((String name) => name.replaceAll(RegExp(r'[^A-Za-z0-9_]+'), '-'))
      .join('+');
  final String separator = Platform.pathSeparator;
  return '$root${separator}build${separator}playbooks$separator'
      '$playbooks-$slug-$stamp.mp4';
}