[go: nahoru, domu]

Skip to content

Commit

Permalink
extract and validate the snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
devoncarew committed Oct 10, 2015
1 parent eff887c commit b4f8963
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
_site
.sass-cache

# Dart ignores.
.packages
.pub/
pubspec.lock
packages
example/*.dart
2 changes: 2 additions & 0 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ markdown: kramdown
kramdown:
input: GFM
hard_wrap: false

exclude: [example, packages, tool]
1 change: 1 addition & 0 deletions example/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Note: All Dart files in this directory are git ignored.
3 changes: 3 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
name: flutter_github_io
version: 0.0.1
dev_dependencies:
path: any
sky: any
109 changes: 109 additions & 0 deletions tool/extract.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import 'dart:io';

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

/// Extract Dart snippets from the markdown documentation.
void main(List<String> args) {
// Validate our cwd.
File pubspec = new File('pubspec.yaml');
if (!pubspec.existsSync()) {
fail('This tool must the run from the project root.');
}

// Remove any previously generated files.
clean();

// Traverse markdown files in the root.
List<File> markdownFiles = Directory.current.listSync().where((entity) {
return entity is File && entity.path.endsWith('.md');
}).toList();

markdownFiles.forEach(_processFile);
}

void _processFile(File file) {
print(file.path);

String name = p.basename(file.path);

// Look for ```dart sections.
String source = file.readAsStringSync();
List<String> lines = source.split('\n');

int index = 1;
int count = 0;

String lastComment;

while (index < lines.length) {
// Look for ```
if (lines[index].startsWith('```dart') && lastComment != 'skip') {
int startIndex = index + 1;
index++;
while (index < lines.length && !lines[index].startsWith('```')) {
index++;
}
_extract(name, ++count, startIndex, lines.sublist(startIndex, index),
includeSource: lastComment);
} else if (lines[index].startsWith('<!--')) {
int startIndex = index;
while (!lines[index].endsWith('-->')) {
index++;
}

lastComment = lines.sublist(startIndex, index + 1).join('\n');
lastComment = lastComment.substring(4);
lastComment = lastComment.substring(0, lastComment.length - 3).trim();
} else {
lastComment = null;
}

index++;
}
}

void _extract(String filename, int snippet, int startLine, List<String> lines,
{String includeSource}) {
String first = lines.first.trim();
bool hasImport = first.startsWith('import ');
// // TODO: Many of these should be analyzed as well.
// print(' skipping ${filename} line ${startLine}, no import statement');
// return;

String path =
'example/${filename.replaceAll('-', '_').replaceAll('.', '_')}_${snippet}.dart';

String source =
'// Extracted from ${filename} line ${startLine}.\n';

int adjust = 1;

if (!hasImport) {
source += "import 'package:sky/material.dart';\n";
adjust++;
}

if (includeSource != null) {
source += "${includeSource}\n";
adjust += includeSource.split('\n').length;
}

source +=
'${''.padRight(startLine - adjust, '\n')}'
'${lines.join('\n')}\n';

print(' extracting ${path} from line ${startLine}');
new File(path).writeAsStringSync(source);
}

void clean() {
Iterable<File> files = new Directory('example').listSync().where((entity) {
return entity is File && entity.path.endsWith('.dart');
});
files.forEach((file) => file.deleteSync());
}

void fail(String message) {
print(message);
exit(1);
}
11 changes: 11 additions & 0 deletions tool/travis.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

# Fast fail the script on failures.
set -e

# Extract Dart snippets from the markdown documentation.
dart tool/extract.dart

# Analyze the extracted Dart libraries.
pub global activate tuneup
pub global run tuneup check
5 changes: 0 additions & 5 deletions tool/validate.dart

This file was deleted.

0 comments on commit b4f8963

Please sign in to comment.