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

5 6
// @dart = 2.8

7 8 9
import 'package:file/file.dart';

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
  Directory dir;

  String get pubspec;
  String get main;
28
  String get test => null;
29
  String get generatedFile => null;
30
  DeferredComponentsConfig get deferredComponents => null;
31

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

  Future<void> setUpIn(Directory dir) async {
    this.dir = dir;
36
    writeFile(fileSystem.path.join(dir.path, 'pubspec.yaml'), pubspec);
37
    if (main != null) {
38
      writeFile(fileSystem.path.join(dir.path, 'lib', 'main.dart'), main);
39
    }
40
    if (test != null) {
41
      writeFile(fileSystem.path.join(dir.path, 'test', 'test.dart'), test);
42
    }
43
    if (generatedFile != null) {
44
      writeFile(fileSystem.path.join(dir.path, '.dart_tool', 'flutter_gen', 'flutter_gen.dart'), generatedFile);
45
    }
46 47 48
    if (deferredComponents != null) {
      deferredComponents.setUpIn(dir);
    }
49
    writeFile(fileSystem.path.join(dir.path, 'web', 'index.html'), _kDefaultHtml);
50
    writePackages(dir.path);
51 52 53
    await getPackages(dir.path);
  }

54 55
  int lineContaining(String contents, String search) {
    final int index = contents.split('\n').indexWhere((String l) => l.contains(search));
56
    if (index == -1) {
57
      throw Exception("Did not find '$search' inside the file");
58
    }
59
    return index + 1; // first line is line 1, not line 0
60
  }
61
}