project.dart 2.39 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:file/file.dart';

import '../test_utils.dart';
8
import 'deferred_components_config.dart';
9

10 11 12 13 14 15 16 17 18 19 20
const String _kDefaultHtml  = '''
<html>
    <head>
        <title>Hello, World</title>
    </head>
    <body>
        <script src="main.dart.js"></script>
    </body>
</html>
''';

21
abstract class Project {
22 23 24 25 26
  /// Creates a flutter Project for testing.
  ///
  /// If passed, `indexHtml` is used as the contents of the web/index.html file.
  Project({this.indexHtml = _kDefaultHtml});

27
  late Directory dir;
28 29

  String get pubspec;
30 31 32 33
  String? get main => null;
  String? get test => null;
  String? get generatedFile => null;
  DeferredComponentsConfig? get deferredComponents => null;
34

35
  Uri get mainDart => Uri.parse('package:test/main.dart');
36

37 38 39 40 41 42 43
  /// The contents for the index.html file of this `Project`.
  ///
  /// Defaults to [_kDefaultHtml] via the Project constructor.
  ///
  /// (Used by [HotReloadProject].)
  final String indexHtml;

44 45
  Future<void> setUpIn(Directory dir) async {
    this.dir = dir;
46
    writeFile(fileSystem.path.join(dir.path, 'pubspec.yaml'), pubspec);
47
    final String? main = this.main;
48
    if (main != null) {
49
      writeFile(fileSystem.path.join(dir.path, 'lib', 'main.dart'), main);
50
    }
51
    final String? test = this.test;
52
    if (test != null) {
53
      writeFile(fileSystem.path.join(dir.path, 'test', 'test.dart'), test);
54
    }
55
    final String? generatedFile = this.generatedFile;
56
    if (generatedFile != null) {
57
      writeFile(fileSystem.path.join(dir.path, '.dart_tool', 'flutter_gen', 'flutter_gen.dart'), generatedFile);
58
    }
59
    deferredComponents?.setUpIn(dir);
60 61 62

    // Setup for different flutter web initializations
    writeFile(fileSystem.path.join(dir.path, 'web', 'index.html'), indexHtml);
63
    writeFile(fileSystem.path.join(dir.path, 'web', 'flutter.js'), '');
64
    writeFile(fileSystem.path.join(dir.path, 'web', 'flutter_service_worker.js'), '');
65
    writePackages(dir.path);
66 67 68
    await getPackages(dir.path);
  }

69 70
  int lineContaining(String contents, String search) {
    final int index = contents.split('\n').indexWhere((String l) => l.contains(search));
71
    if (index == -1) {
72
      throw Exception("Did not find '$search' inside the file");
73
    }
74
    return index + 1; // first line is line 1, not line 0
75
  }
76
}