├── .gitignore ├── CHANGELOG.md ├── README.md ├── build.yaml ├── lib └── builder.dart └── pubspec.yaml /.gitignore: -------------------------------------------------------------------------------- 1 | # Files and directories created by pub 2 | .dart_tool/ 3 | .packages 4 | # Remove the following pattern if you wish to check in your lock file 5 | pubspec.lock 6 | 7 | # Conventional directory for build outputs 8 | build/ 9 | 10 | # Directory created by dartdoc 11 | doc/api/ 12 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 1.0.0 2 | 3 | - Builder & Widget 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## This is a proof of concept and WIP 2 | 3 | #### Feedback and ideas welcome !! 4 | 5 | Access your pubspec and git commit informations like versions and commit status from **auto-generated** widgets. 6 | 7 | Configure your widgets to display pertinent and always up to date informations to your users like the last build version, 8 | expose advanced info in production or dev only to help debug specific builds. 9 | 10 | Preconfigured widgets 11 | 12 | #### Flutter 13 | 14 | - [Title](https://docs.flutter.io/flutter/widgets/Title-class.html) 15 | - [AboutDialog](https://docs.flutter.io/flutter/material/AboutDialog-class.html) 16 | - [AboutListTile](https://docs.flutter.io/flutter/material/AboutListTile-class.html) 17 | 18 | #### In house 19 | 20 | - DisplayVersion 21 | - Journal 22 | 23 | [More to come !](https://github.com/Solido/build_flutter_version/issues?q=is%3Aissue+is%3Aopen+label%3Aenhancement) 24 | 25 | _NB: I wrote this specific Flutter and Git version based on the work of https://github.com/kevmoo/build_version 26 | and will try to follow this package evolution on top of our own requirements._ 27 | 28 | ## Requirements 29 | 30 | - Git command available in current path 31 | - Working on an existing git dir 32 | 33 | ## Installation 34 | 35 | Add this builder under the `dev_dependencies` section of the `pubspec.yaml` file: 36 | 37 | ```yaml 38 | dev_dependencies: 39 | build_runner: ^1.0.0 40 | build_flutter_version: 41 | git: 42 | url: https://github.com/Solido/build_flutter_version 43 | ``` 44 | 45 | Then 46 | 47 | flutter packages pub run build_runner watch 48 | 49 | Will generate a dart file at `utils/version.dart` 50 | Just import this file to access Widgets that fits your needs. 51 | 52 | 53 | ## Configuration 54 | 55 | ```dart 56 | DisplayVersion( 57 | // Version contains infos about the pubspec and last git commit 58 | builder: (context, version) { 59 | return Text("${version.name} ${version.version} / ${version.gitInfo.message} : ${version.gitInfo.sha}"); 60 | // => MyAppName 1.2 / adding help section : c78d654678iozka8790 61 | }, 62 | ) 63 | ``` 64 | 65 | ### Check required 66 | 67 | - Inherited Title 68 | - AboutDialog 69 | - AboutListTile 70 | -------------------------------------------------------------------------------- /build.yaml: -------------------------------------------------------------------------------- 1 | # Read about `build.yaml` at https://pub.dartlang.org/packages/build_config 2 | targets: 3 | $default: 4 | builders: 5 | build_flutter_version: 6 | enabled: true 7 | 8 | builders: 9 | build_flutter_version: 10 | import: "package:build_flutter_version/builder.dart" 11 | builder_factories: ["buildFlutterVersion"] 12 | build_extensions: {"$lib$": ["version.dart"]} 13 | build_to: source 14 | auto_apply: dependents 15 | -------------------------------------------------------------------------------- /lib/builder.dart: -------------------------------------------------------------------------------- 1 | /// Configuration for using `package:build`-compatible build systems. 2 | /// 3 | /// This library is **not** intended to be imported by typical end-users unless 4 | /// you are creating a custom compilation pipeline. 5 | /// 6 | /// See [package:build_runner](https://pub.dartlang.org/packages/build_runner) 7 | /// for more information. 8 | library builder; 9 | 10 | import 'dart:async'; 11 | import 'dart:io'; 12 | 13 | import 'package:build/build.dart'; 14 | import 'package:git/git.dart'; 15 | import 'package:pubspec_parse/pubspec_parse.dart'; 16 | 17 | Builder buildFlutterVersion([BuilderOptions options]) => 18 | _FlutterVersionBuilder(); 19 | 20 | class _FlutterVersionBuilder implements Builder { 21 | @override 22 | Future build(BuildStep buildStep) async { 23 | var assetId = AssetId(buildStep.inputId.package, 'pubspec.yaml'); 24 | var content = await buildStep.readAsString(assetId); 25 | var pubspec = Pubspec.parse(content, sourceUrl: assetId.uri); 26 | 27 | if (pubspec.version == null) { 28 | throw StateError('pubspec.yaml does not have a version defined.'); 29 | } 30 | 31 | var gitDir = await GitDir.fromExisting(Directory.current.path); 32 | var commits = await gitDir.getCommits(); 33 | var lastCommit = commits.values.last; 34 | var branch = await gitDir.getCurrentBranch(); 35 | 36 | await buildStep.writeAsString( 37 | AssetId(buildStep.inputId.package, 'lib/utils/version.dart'), ''' 38 | // Generated code. Do not modify. 39 | import 'package:flutter/material.dart'; 40 | 41 | class GitInfo { 42 | final String message = '${lastCommit.message}'; 43 | final String sha = '${lastCommit.treeSha}'; 44 | final String branch = '${branch.branchName}'; 45 | 46 | const GitInfo(); 47 | } 48 | 49 | class Version { 50 | final String name = '${pubspec.name}'; 51 | final String description = '${pubspec.description ?? ''}'; 52 | final String version = '${pubspec.version}'; 53 | final String homepage = '${pubspec.homepage ?? ''}'; 54 | final String authors = '${pubspec.authors ?? ''}'; 55 | final gitInfo = const GitInfo(); 56 | } 57 | 58 | typedef Widget VersionBuilder(BuildContext context, Version version); 59 | 60 | class VersionDialog extends AboutDialog { 61 | VersionDialog({ 62 | Key key, 63 | Widget applicationIcon, 64 | String applicationLegalese, 65 | List children, 66 | }) : super(key:key, 67 | applicationName:'${pubspec.name}', 68 | applicationVersion:'${pubspec.version}', 69 | applicationIcon: applicationIcon, 70 | applicationLegalese: applicationLegalese, 71 | children: children); 72 | } 73 | 74 | /// Setup the Title widget 75 | class VersionApp extends StatelessWidget { 76 | final Widget child; 77 | final Color color; 78 | 79 | VersionApp({@required this.child, @required this.color}); 80 | 81 | @override 82 | Widget build(BuildContext context) { 83 | return Title( 84 | title: Version().name, 85 | color: color, 86 | child: child, 87 | ); 88 | } 89 | } 90 | 91 | /// A configurable Widget that display conditionally 92 | class DisplayVersion extends StatelessWidget { 93 | 94 | final VersionBuilder builder; 95 | final production = const bool.fromEnvironment("dart.vm.product"); 96 | final bool onDebugOnly; 97 | 98 | DisplayVersion({this.builder, this.onDebugOnly = true}); 99 | 100 | @override 101 | Widget build(BuildContext context) { 102 | return Visibility( 103 | visible: !onDebugOnly || !production, 104 | child: builder(context, Version()), 105 | replacement: Container(), 106 | ); 107 | } 108 | } 109 | 110 | 111 | '''); 112 | } 113 | 114 | @override 115 | final buildExtensions = const { 116 | r'$lib$': ['utils/version.dart'] 117 | }; 118 | } 119 | -------------------------------------------------------------------------------- /pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: build_flutter_version 2 | description: Provide app version and git status in a configurable Widget 3 | version: 1.0.0 4 | # homepage: https://github.com/Solido 5 | # author: Robert Felker 6 | 7 | environment: 8 | sdk: '>=2.0.0 <3.0.0' 9 | 10 | dependencies: 11 | build: '>=0.12.1 <2.0.0' 12 | # Not imported in code, but used to constrain `build.yaml` requirements 13 | build_config: ^0.3.0 14 | pubspec_parse: ^0.1.2+2 15 | git: ^0.5.1+1 16 | 17 | dev_dependencies: 18 | build_runner: ^1.0.0 19 | build_test: ^0.10.3+1 20 | build_verify: ^1.0.0 21 | test: ^1.2.0 22 | 23 | --------------------------------------------------------------------------------