Embedding information at compile time.

19 views
Skip to first unread message

Pomba Magar

unread,
Nov 10, 2021, 2:03:17 AM11/10/21
to Dart Misc
Newbie alert!

I'm building a cli for learning dart and i wanted to embed some information from `pubspec.yaml` file and git output to my executable but i can't seem to find a good solution nor comprehensive information to do so, maybe i need to step up my googling skill.

Anyways, what i want to do is grab `name`, `version` & `description` from `pubspec.yaml` file and output them when certain flags are passed to the cli.

Eg:
```
app --help
# output
<app name> <version>+<git rev-parse --short HEAD>
<description> 

// other stuffs
```
Closest thing i came across was https://github.com/kevmoo/build_version but it doesn't have all the things i need.

Any help is greatly appreciated.

Chris Norman

unread,
Nov 10, 2021, 3:58:56 AM11/10/21
to mi...@dartlang.org
Hi,
What I would do is parse the pubspec.yaml file (I'm sure there's a pubspecparser package on pub.dev), and write yourself a Dart file with that info in it.

as an example:

```dart
void main() {
  final pubspec - parsePubSpec(); // However you do that.
  final code = "const name = '${pubspec.name}';\nconst version = '${pubspec.version}';\nconst description = '${pubspec.description}';
  File('bin/metadata.dart').writeAsStringSync(code);
}
```

Now you can include that file in your code. Just make yourself a batch file / shell script that generates the code before compiling your app.

HTH,

Take care,

Chris Norman



--
For more ways to connect visit https://dart.dev/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/484fb7a2-221c-4e27-9a88-a2f90d49b162n%40dartlang.org.

Sigurd Meldgaard

unread,
Nov 11, 2021, 3:51:41 AM11/11/21
to mi...@dartlang.org
You could look into https://pub.dev/packages/build_version . It only supports the version, but I guess the name of your app won't change so often...

Reply all
Reply to author
Forward
0 new messages