david.mihola
david.mihola

Reputation: 12992

How to run `source_gen`?

I am currently trying to use built_value in my Flutter project but I am confused on what I need to do...

I put the following lines in my pubspec.yaml (copied from an example project):

dev_dependencies:
  build: ^0.7.0
  build_runner: ^0.3.0
  built_value_generator: ^1.0.0

But, so far, nothing is happening... In this video demonstration you can see that the generated code is updated/regenerated on the fly, while the developer is changing his code.

Do I need to do anything else to have the code generation work? Start some kind of server that watches for changes and trigger code generation? Register source_gen to be run at some point?

EDIT: OK, so now I have a tool folder next to the lib folder. The tool folder contains both build.dart and watch.dart, both changed to match the package name: in my pubspec.yaml. The lib folder contains a file user.dart, which I copied from this tutorial.

library user;

import 'package:built_value/built_value.dart';

part 'user.g.dart';

abstract class User implements Built<User, UserBuilder> {
  String get name;

  @nullable
  String get nickname;

  User._();

  factory User([updates(UserBuilder b)]) = _$User;
}

Shouldn't I now be seeing a user.g.dart file appear in lib that contains the generated code? There's nothing wrong with the glob pattern, is it?

EDIT 2: This is the output of flutter --version:

dmta@elite:~/flutter$ flutter --version
Flutter • channel master • https://github.com/flutter/flutter.git
Framework • revision e65d47d4ba (11 hours ago) • 2017-05-15 12:40:20 -0700
Engine • revision 7dd359e165
Tools • Dart 1.23.0-dev.11.11

Upvotes: 4

Views: 1539

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657536

You need a file tool/build.dart like, to generate the model classes

// Copyright (c) 2016, Google Inc. Please see the AUTHORS file for details.
// All rights reserved. Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

import 'dart:async';

import 'package:build_runner/build_runner.dart';
import 'package:built_value_generator/built_value_generator.dart';
import 'package:source_gen/source_gen.dart';

/// Build the generated files in the built_value chat example.
Future main(List<String> args) async {
  await build(
      new PhaseGroup.singleAction(
          new GeneratorBuilder([
            new BuiltValueGenerator(),
          ]),
          new InputSet('chat_example', const ['lib/**/*.dart'])),
      deleteFilesByDefault: true);
}

or a watch.dart file like also shown in https://github.com/google/built_value.dart/tree/master/chat_example/tool that watches for changes in the source files and regenerates the built_value model classes whenever one of the source files was modified.

Upvotes: 3

Related Questions