• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2018 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'dart:io' hide Platform;
6
7import 'package:meta/meta.dart';
8import 'package:path/path.dart' as path;
9
10/// What type of snippet to produce.
11enum SnippetType {
12  /// Produces a snippet that includes the code interpolated into an application
13  /// template.
14  application,
15
16  /// Produces a nicely formatted sample code, but no application.
17  sample,
18}
19
20/// Return the name of an enum item.
21String getEnumName(dynamic enumItem) {
22  final String name = '$enumItem';
23  final int index = name.indexOf('.');
24  return index == -1 ? name : name.substring(index + 1);
25}
26
27/// A class to compute the configuration of the snippets input and output
28/// locations based in the current location of the snippets main.dart.
29class Configuration {
30  Configuration({@required this.flutterRoot}) : assert(flutterRoot != null);
31
32  final Directory flutterRoot;
33
34  /// This is the configuration directory for the snippets system, containing
35  /// the skeletons and templates.
36  @visibleForTesting
37  Directory get configDirectory {
38    _configPath ??= Directory(
39        path.canonicalize(path.join(flutterRoot.absolute.path, 'dev', 'snippets', 'config')));
40    return _configPath;
41  }
42
43  Directory _configPath;
44
45  /// This is where the snippets themselves will be written, in order to be
46  /// uploaded to the docs site.
47  Directory get outputDirectory {
48    _docsDirectory ??= Directory(
49        path.canonicalize(path.join(flutterRoot.absolute.path, 'dev', 'docs', 'doc', 'snippets')));
50    return _docsDirectory;
51  }
52
53  Directory _docsDirectory;
54
55  /// This makes sure that the output directory exists.
56  void createOutputDirectory() {
57    if (!outputDirectory.existsSync()) {
58      outputDirectory.createSync(recursive: true);
59    }
60  }
61
62  /// The directory containing the HTML skeletons to be filled out with metadata
63  /// and returned to dartdoc for insertion in the output.
64  Directory get skeletonsDirectory => Directory(path.join(configDirectory.path,'skeletons'));
65
66  /// The directory containing the code templates that can be referenced by the
67  /// dartdoc.
68  Directory get templatesDirectory => Directory(path.join(configDirectory.path, 'templates'));
69
70  /// Gets the skeleton file to use for the given [SnippetType].
71  File getHtmlSkeletonFile(SnippetType type) {
72    return File(path.join(skeletonsDirectory.path, '${getEnumName(type)}.html'));
73  }
74}
75