addAssetsToPubSpec function

void addAssetsToPubSpec()

Adds the json files directory. to pubspec.yaml assets.

Implementation

void addAssetsToPubSpec() {
  if (pubSpecStr.isEmpty) {
    readPubSpec();
  }
  var addAsset = '${p.dirname(config.outputJsonTemplate)}/';
  var out = pubSpecStr;
  var assets = pubSpecMap['flutter']?['assets'];
  var replacer = '';
  if (assets == null) {
    replacer = kNoAssetsReplace.replaceAll('##replace', addAsset);
    out = out.replaceAll(kNoAssetsKey, replacer);
  } else {
    // check if the key is already added
    var hasAsset = false;
    if (assets is List) {
      for (String asset in assets) {
        if (asset.contains(addAsset)) {
          hasAsset = true;
        }
      }
    }
    if (!hasAsset) {
      replacer = kHasAssetsReplace.replaceAll('##replace', addAsset);
      out = out.replaceAll(kHasAssetsKey, replacer);
    }
  }
  if (out != pubSpecStr) {
    saveString('pubspec.yaml', out);
  }
}