resolvePluginPkg function

Future<List<Map>> resolvePluginPkg (
  1. String templateLibName
)

Implementation

Future<List<Map>> resolvePluginPkg(String templateLibName) async {
  // Config.debugLogger.d('resolvePluginPkg $templateLibName');
  // 1. look in ~/.dartrix.d/.yaml, libraries: entry
  // Rule: if listed as user lib with path it wins

  var userYaml = getUserYaml();
  if (userYaml != null) {
    var lib;
    try {
      lib = userYaml.libraries.singleWhere((lib) {
        return (lib.name == templateLibName + Config.appSfx);
      }, orElse: () => null);
    } catch (e) {
      Config.ppLogger.e(e);
      // muliple matches, should not happen
      Config.ppLogger.e(
          'User config file (${Config.home}/.dartrix.d/.yaml}) contains duplicate library entries for \'${templateLibName}_dartrix\'. Disallowed.');
      exit(1);
    }
    if (lib != null) {
      var r = {'name': lib.name, 'rootUri': lib.path};
      return [r];
    }
  }

  // // Step 1: get user's dart config (a packageConfig2)
  // //PackageConfig
  // var userPackageConfig2 = await getUserPackageConfig2();
  // // Step 2. pkgName is listed as a dep in the packageConfig2
  // // Package

  var templatePkgName = templateLibName + Config.appSfx; // '_dartrix';
  // var pkgName = templateLibName + Config.appSfx; // '_dartrix';
  // var _pkgList;
  // //Package
  // var pkgPackage;
  // try {
  //   pkgPackage =
  //       userPackageConfig2.packages.singleWhere((pkg) => pkg.name == pkgName);
  //   // this is ok - pkg config can only contain one copy of a dep
  //   _pkgList = [
  //     {
  //       'name': pkgPackage.name,
  //       'version': 'Y',
  //       'cache': 'dartrix',
  //       'rootUri': pkgPackage.root.path
  //     }
  //   ];
  // Config.ppLogger.i('found in user pkgconfig: $_pkgList');
  // } catch (e) {
  //   if (!(e.message.startsWith('No element'))) {
  //     Config.prodLogger.e(e);
  //     exit(0);
  //   }

  //   if (Config.debug) {
  //     Config.logger.d(
  //         'dartrix library pkg \'$pkgName\' not in usercache; checking syscache.');
  //   }

  var _pkgList = await searchSysCache(templatePkgName);
  if (_pkgList.isEmpty) {
    if (Config.debug) {
      Config.logger.d(
          'dartrix library pkg \'$templatePkgName\' not in syscache; checking pub.dev.');
    }
    //FIXME: only if Config.searchPubDev
    // download and install in syscache
    await fetchPackage(templatePkgName);
    // now we should find it
    var sysPkgList = await searchSysCache(templatePkgName);
    if (sysPkgList.isEmpty) {
      Config.prodLogger.i('Plugin package \'$templatePkgName\' not found.');
      if (templatePkgName.startsWith('dartrix')) {
        Config.prodLogger.i(
            'Did you forget a semi-colon? Try \':dartrix\' (or just \':d\').');
      }
    }
    // Config.ppLogger.d('fetchPackage: $sysPkgList');
    // return sysPkgList.first; //['uri']; //.path;
    // pkg = sysPkgList.first;
    _pkgList = sysPkgList;
  } else {
    if (Config.debug) {
      Config.logger.d(
          'found ${_pkgList.length} occurrences of $templatePkgName in syscache: $_pkgList');
    }
    // return p.first; //['uri']; //.path + '/'; //FIXME: remove this hack
    // pkg = p.first;
    return _pkgList;
  }
  // }
  // Config.libPkgRoot = pkg['rootUri'];
  // Config.ppLogger.d('resolved pkgList: $_pkgList');
  return _pkgList; // ['rootUri']; // pkgRootUri.path;
}