fromListLine static method

PubGlobalPackage? fromListLine(
  1. String line
)

return null if it cannot be parsed

Implementation

static PubGlobalPackage? fromListLine(String line) {
  // split the line by spaces to get the arguments
  var parts = line.split(' ');

  String? name;

  Version? version;
  _PubGlobalPackageMixin? package;
  // pub.dartlang.org hosted package
  // ignore name
  if (parts.length >= 2) {
    // first is package name, last is path
    name = parts[0];
    try {
      version = Version.parse(parts[1]);
    } catch (_) {
      return null;
    }
  } else {
    // ignore
    return null;
  }

  //
  // hosted
  // pub.dartlang.org hosted package
  // ignore name
  if (parts.length == 2) {
    return PubGlobalHostedPackage(name, version: version);
  } else if (parts.length >= 4) {
    //
    // handle git first
    //
    bool isPartGit(int index) {
      return parts[index].toLowerCase() == 'git';
    }

    // look for git in the 2 arguments preceding the source
    if (isPartGit(parts.length - 2) || isPartGit(parts.length - 3)) {
      package = PubGlobalGitPackage(name, version: version);
    }

    if (package == null) {
      //
      // handle part
      //
      bool isPartPath(int index) {
        return parts[index].toLowerCase() == 'path';
      }

      if (isPartPath(parts.length - 2) || isPartPath(parts.length - 3)) {
        package = PubGlobalPathPackage(name, version: version);
      }
    }

    // Git and path are source pacjage
    if (package is PubGlobalSourcePackage) {
      package._source = _extractSource(parts.last);
    }
  }

  return package;
}