pathTo method

String pathTo(
  1. String absolute
)

The path from root to absolute: relative — with .. when it is outside the project — whenever both are on the same drive (or share), so a project and what it points at can move together; else absolute.

Implementation

String pathTo(String absolute) {
  final target = _resolveDots(absolute);
  final fromHead = _head(root);
  final toHead = _head(target);
  if (!samePath(fromHead, toHead)) return target;
  List<String> parts(String p, String head) =>
      p.substring(head.length).split('/').where((s) => s.isNotEmpty).toList();
  final from = parts(root, fromHead);
  final to = parts(target, toHead);
  var i = 0;
  while (i < from.length && i < to.length && samePath(from[i], to[i])) {
    i++;
  }
  final rel = [
    ...List.filled(from.length - i, '..'),
    ...to.skip(i),
  ].join('/');
  return rel.isEmpty ? '.' : rel;
}