project.dart 2.71 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5
// 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';
6
import 'package:flutter_tools/src/artifacts.dart';
7
import 'package:flutter_tools/src/web/file_generators/flutter_js.dart';
8 9

import '../test_utils.dart';
10
import 'deferred_components_config.dart';
11

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

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

29
  late Directory dir;
30 31

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

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

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

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

63 64 65 66
    final String fileGeneratorsPath =
        Artifacts.test().getArtifactPath(Artifact.flutterToolsFileGenerators);
    final String flutterJsContents = generateFlutterJsFile(fileGeneratorsPath);

67 68
    // Setup for different flutter web initializations
    writeFile(fileSystem.path.join(dir.path, 'web', 'index.html'), indexHtml);
69
    writeFile(fileSystem.path.join(dir.path, 'web', 'flutter.js'), flutterJsContents);
70
    writeFile(fileSystem.path.join(dir.path, 'web', 'flutter_service_worker.js'), '');
71
    writePackages(dir.path);
72 73 74
    await getPackages(dir.path);
  }

75 76
  int lineContaining(String contents, String search) {
    final int index = contents.split('\n').indexWhere((String l) => l.contains(search));
77
    if (index == -1) {
78
      throw Exception("Did not find '$search' inside the file");
79
    }
80
    return index + 1; // first line is line 1, not line 0
81
  }
82
}