-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
4,283 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
library md_proc.build_file; | ||
|
||
import 'package:source_gen/source_gen.dart'; | ||
import 'package:md_proc/generators/embed_tests_generator.dart'; | ||
|
||
void main(List<String> args) { | ||
build(args, const [ | ||
const EmbedTestsGenerator() | ||
], librarySearchPaths: [ | ||
'test' | ||
]).then((msg) { | ||
print(msg); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
library md_proc.generators.embed_tests; | ||
|
||
class EmbedTests { | ||
final String path; | ||
|
||
const EmbedTests(this.path); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
library md_proc.generators.embed_tests_generator; | ||
|
||
import 'dart:async'; | ||
import 'dart:io'; | ||
|
||
import 'package:analyzer/src/generated/element.dart'; | ||
import 'package:analyzer/src/generated/source_io.dart'; | ||
import 'package:path/path.dart' as path; | ||
import 'package:source_gen/source_gen.dart'; | ||
|
||
import 'embed_tests.dart'; | ||
|
||
|
||
class EmbedTestsGenerator extends GeneratorForAnnotation<EmbedTests> { | ||
static const int stateWait = 0; | ||
static const int stateSource = 1; | ||
static const int stateDestination = 2; | ||
|
||
@override | ||
final AssociatedFileSet associatedFileSet; | ||
|
||
/// If [associatedFileSet] is not set, the default value of | ||
/// [AssociatedFileSet.sameDirectory] is used. | ||
const EmbedTestsGenerator( | ||
{AssociatedFileSet associatedFileSet: AssociatedFileSet.sameDirectory}) | ||
: this.associatedFileSet = associatedFileSet; | ||
|
||
Map<String, String> readFile(fileName) { | ||
Map<String, String> result = <String, String>{}; | ||
|
||
File file = new File(fileName); | ||
int state = stateWait; | ||
List<String> destination = []; | ||
List<String> source = []; | ||
List<String> lines = file.readAsLinesSync(); | ||
for (String line in lines) { | ||
if (line == ".") { | ||
state++; | ||
if (state == 3) { | ||
result[source.map((line) => line + "\n").join()] = destination.map((line) => line + "\n").join(); | ||
state = stateWait; | ||
destination = []; | ||
source = []; | ||
} | ||
} else if (state == stateSource) { | ||
source.add(line); | ||
} else if (state == stateDestination) { | ||
destination.add(line); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
@override | ||
Future<String> generateForAnnotatedElement( | ||
Element element, EmbedTests annotation) async { | ||
if (path.isAbsolute(annotation.path)) { | ||
throw 'must be relative path to the source file'; | ||
} | ||
|
||
var source = element.source as FileBasedSource; | ||
var sourcePath = source.file.getAbsolutePath(); | ||
|
||
var sourcePathDir = path.dirname(sourcePath); | ||
|
||
var filePath = path.join(sourcePathDir, annotation.path); | ||
|
||
if (!await FileSystemEntity.isFile(filePath)) { | ||
throw 'Not a file! - $filePath'; | ||
} | ||
|
||
var content = readFile(filePath); | ||
|
||
var result = 'final Map<String, String> _\$${element.displayName}Tests = {\n'; | ||
content.forEach((k, v) { | ||
result += "r'''" + k + "''': r'''" + v + "''',\n"; | ||
}); | ||
result += '};\n'; | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,4 @@ dev_dependencies: | |
test: any | ||
linter: any | ||
dart_coveralls: any | ||
|
||
source_gen: '>=0.4.2 <0.5.0' |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
library md_proc.test.data.test_data; | ||
|
||
import 'package:md_proc/generators/embed_tests.dart'; | ||
|
||
part 'test_data.g.dart'; | ||
|
||
@EmbedTests('spec.txt') | ||
final Map<String, String> specification = _$specificationTests; | ||
|
||
@EmbedTests('smart_punct.txt') | ||
final Map<String, String> smartPunctuation = _$smartPunctuationTests; | ||
|
||
@EmbedTests('markdownToMarkdown.txt') | ||
final Map<String, String> markdownToMarkdown = _$markdownToMarkdownTests; | ||
|
||
@EmbedTests('additionalMarkdownToHtml.txt') | ||
final Map<String, String> additionalMarkdownToHtml = _$additionalMarkdownToHtmlTests; |
Oops, something went wrong.