Access to pubspec.yaml attributes (version) from Dart app

dart

Solution

you can install the "dart_config" package and use this code to parse a pubspec.yaml file:

import 'package:dart_config/default_server.dart';
import 'dart:async';

void main() {
  Future<Map> conf = loadConfig("../pubspec.yaml");
  conf.then((Map config) {
    print(config['name']);
    print(config['description']);
    print(config['version']);
    print(config['author']);
    print(config['homepage']);
    print(config['dependencies']);
  });
}

The output looks similar to this:

test_cli
A sample command-line application
0.0.1
Robert Hartung
URL
{dart_config: any}

EDIT

You can do it with the Yaml package itself:

*NOTE: this will not work on Flutter Web

import 'package:yaml/yaml.dart';
import 'dart:io'; // *** NOTE *** This will not work on Flutter Web

void main() {      
    File f = new File("../pubspec.yaml");
    f.readAsString().then((String text) {
      Map yaml = loadYaml(text);
      print(yaml['name']);
      print(yaml['description']);
      print(yaml['version']);
      print(yaml['author']);
      print(yaml['homepage']);
      print(yaml['dependencies']);
    });
}

Regards Robert

Problem

Is there any way to access some of the attributes listed in a pubspec.yaml file in that files Dart application? In particular, the version and description attributes may be quite useful to see in a version info dialog, or even a '--version' when using a console app. I haven't been able to find a way to access in the API. I'm not sure if Mirrors would have anything appropriate, but if a web app is compiled to JS, then I don't see the description anywhere in the output JS. Thanks. EDIT feature request: https://code.google.com/p/dart/issues/detail?id=18769

Original source