从Dart应用程序访问pubspec.yaml属性(版本) [英] Access to pubspec.yaml attributes (version) from Dart app

查看:273
本文介绍了从Dart应用程序访问pubspec.yaml属性(版本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以访问该文件Dart应用程序中pubspec.yaml文件中列出的某些属性?

Is there any way to access some of the attributes listed in a pubspec.yaml file in that files Dart application?

特别是,在使用控制台应用程序时,在版本信息对话框中甚至在 --version中查看版本和描述属性可能非常有用。我还没有找到访问API的方法。我不确定Mirrors是否合适,但是如果将Web应用程序编译为JS,则在输出JS的任何地方都看不到该描述。

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.

谢谢。

编辑

功能请求: https://code.google.com/p/dart/issues/detail?id=18769

推荐答案

您可以安装 dart_config软件包,并使用以下代码来解析pubspec.yaml文件:

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']);
  });
}

输出类似于以下内容:

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

编辑

您可以使用Yaml软件包本身进行操作:

You can do it with the Yaml package itself:

import 'package:yaml/yaml.dart';
import 'dart:io';

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']);
    });
}

关于罗伯特

这篇关于从Dart应用程序访问pubspec.yaml属性(版本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆