1// Copyright 2014 The Flutter 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 'package:file/file.dart';
6import 'package:file/local.dart';
7import 'package:path/path.dart' as path;
8
9// Represents the locations of all of the data for snippets.
10class SnippetConfiguration {
11 const SnippetConfiguration({
12 required this.configDirectory,
13 required this.skeletonsDirectory,
14 this.filesystem = const LocalFileSystem(),
15 });
16
17 final FileSystem filesystem;
18
19 /// This is the configuration directory for the snippets system, containing
20 /// the skeletons and templates.
21 final Directory configDirectory;
22
23 /// The directory containing the HTML skeletons to be filled out with metadata
24 /// and returned to dartdoc for insertion in the output.
25 final Directory skeletonsDirectory;
26
27 /// Gets the skeleton file to use for the given [SampleType] and DartPad
28 /// preference.
29 File getHtmlSkeletonFile(String type) {
30 final String filename = type == 'dartpad' ? 'dartpad-sample.html' : '$type.html';
31 return filesystem.file(path.join(skeletonsDirectory.path, filename));
32 }
33}
34
35/// A class to compute the configuration of the snippets input and output
36/// locations based in the current location of the snippets main.dart.
37class FlutterRepoSnippetConfiguration extends SnippetConfiguration {
38 FlutterRepoSnippetConfiguration({required this.flutterRoot, super.filesystem})
39 : super(
40 configDirectory: _underRoot(filesystem, flutterRoot, const <String>[
41 'dev',
42 'snippets',
43 'config',
44 ]),
45 skeletonsDirectory: _underRoot(filesystem, flutterRoot, const <String>[
46 'dev',
47 'snippets',
48 'config',
49 'skeletons',
50 ]),
51 );
52
53 final Directory flutterRoot;
54
55 static Directory _underRoot(FileSystem fs, Directory flutterRoot, List<String> dirs) =>
56 fs.directory(path.canonicalize(path.joinAll(<String>[flutterRoot.absolute.path, ...dirs])));
57}
58