2018-10-23 20:50:24 +00:00
|
|
|
// 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,
|
2018-11-07 16:29:14 +00:00
|
|
|
|
2018-10-23 20:50:24 +00:00
|
|
|
/// 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 {
|
2018-11-07 16:29:14 +00:00
|
|
|
Configuration({@required this.flutterRoot}) : assert(flutterRoot != null);
|
2018-10-23 20:50:24 +00:00
|
|
|
|
2018-11-07 16:29:14 +00:00
|
|
|
final Directory flutterRoot;
|
2018-10-23 20:50:24 +00:00
|
|
|
|
|
|
|
/// This is the configuration directory for the snippets system, containing
|
|
|
|
/// the skeletons and templates.
|
|
|
|
@visibleForTesting
|
2018-11-07 16:29:14 +00:00
|
|
|
Directory get configDirectory {
|
|
|
|
_configPath ??= Directory(
|
|
|
|
path.canonicalize(path.join(flutterRoot.absolute.path, 'dev', 'snippets', 'config')));
|
|
|
|
return _configPath;
|
2018-10-23 20:50:24 +00:00
|
|
|
}
|
|
|
|
|
2018-11-07 16:29:14 +00:00
|
|
|
Directory _configPath;
|
|
|
|
|
2018-10-23 20:50:24 +00:00
|
|
|
/// This is where the snippets themselves will be written, in order to be
|
|
|
|
/// uploaded to the docs site.
|
|
|
|
Directory get outputDirectory {
|
2018-11-07 16:29:14 +00:00
|
|
|
_docsDirectory ??= Directory(
|
|
|
|
path.canonicalize(path.join(flutterRoot.absolute.path, 'dev', 'docs', 'doc', 'snippets')));
|
|
|
|
return _docsDirectory;
|
2018-10-23 20:50:24 +00:00
|
|
|
}
|
|
|
|
|
2018-11-07 16:29:14 +00:00
|
|
|
Directory _docsDirectory;
|
|
|
|
|
2018-10-23 20:50:24 +00:00
|
|
|
/// 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.
|
2018-11-07 16:29:14 +00:00
|
|
|
Directory get skeletonsDirectory => Directory(path.join(configDirectory.path,'skeletons'));
|
2018-10-23 20:50:24 +00:00
|
|
|
|
|
|
|
/// The directory containing the code templates that can be referenced by the
|
|
|
|
/// dartdoc.
|
2018-11-07 16:29:14 +00:00
|
|
|
Directory get templatesDirectory => Directory(path.join(configDirectory.path, 'templates'));
|
2018-10-23 20:50:24 +00:00
|
|
|
|
|
|
|
/// Gets the skeleton file to use for the given [SnippetType].
|
|
|
|
File getHtmlSkeletonFile(SnippetType type) {
|
|
|
|
return File(path.join(skeletonsDirectory.path, '${getEnumName(type)}.html'));
|
|
|
|
}
|
|
|
|
}
|