configuration.dart 2.46 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright 2018 The Chromium Authors. 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:io' hide Platform;

import 'package:meta/meta.dart';
import 'package:path/path.dart' as path;

/// What type of snippet to produce.
enum SnippetType {
  /// Produces a snippet that includes the code interpolated into an application
  /// template.
  application,
15

16 17 18 19 20 21 22 23 24 25 26 27 28 29
  /// Produces a nicely formatted sample code, but no application.
  sample,
}

/// Return the name of an enum item.
String getEnumName(dynamic enumItem) {
  final String name = '$enumItem';
  final int index = name.indexOf('.');
  return index == -1 ? name : name.substring(index + 1);
}

/// A class to compute the configuration of the snippets input and output
/// locations based in the current location of the snippets main.dart.
class Configuration {
30
  Configuration({@required this.flutterRoot}) : assert(flutterRoot != null);
31

32
  final Directory flutterRoot;
33 34 35 36

  /// This is the configuration directory for the snippets system, containing
  /// the skeletons and templates.
  @visibleForTesting
37 38 39 40
  Directory get configDirectory {
    _configPath ??= Directory(
        path.canonicalize(path.join(flutterRoot.absolute.path, 'dev', 'snippets', 'config')));
    return _configPath;
41 42
  }

43 44
  Directory _configPath;

45 46 47
  /// This is where the snippets themselves will be written, in order to be
  /// uploaded to the docs site.
  Directory get outputDirectory {
48 49 50
    _docsDirectory ??= Directory(
        path.canonicalize(path.join(flutterRoot.absolute.path, 'dev', 'docs', 'doc', 'snippets')));
    return _docsDirectory;
51 52
  }

53 54
  Directory _docsDirectory;

55 56 57 58 59 60 61 62 63
  /// This makes sure that the output directory exists.
  void createOutputDirectory() {
    if (!outputDirectory.existsSync()) {
      outputDirectory.createSync(recursive: true);
    }
  }

  /// The directory containing the HTML skeletons to be filled out with metadata
  /// and returned to dartdoc for insertion in the output.
64
  Directory get skeletonsDirectory => Directory(path.join(configDirectory.path,'skeletons'));
65 66 67

  /// The directory containing the code templates that can be referenced by the
  /// dartdoc.
68
  Directory get templatesDirectory => Directory(path.join(configDirectory.path, 'templates'));
69 70 71 72 73 74

  /// Gets the skeleton file to use for the given [SnippetType].
  File getHtmlSkeletonFile(SnippetType type) {
    return File(path.join(skeletonsDirectory.path, '${getEnumName(type)}.html'));
  }
}