-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clean-up and tweaks of the firehose project. #117
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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 |
---|---|---|
|
@@ -4,69 +4,79 @@ | |
|
||
import 'dart:io'; | ||
|
||
import 'package:collection/collection.dart'; | ||
|
||
class Changelog { | ||
static const _headerLinePrefix = '## '; | ||
|
||
final File file; | ||
|
||
Changelog(this.file); | ||
|
||
bool get exists => file.existsSync(); | ||
|
||
/// Pattern recognizing some SemVer formats. | ||
/// | ||
/// Accepts: | ||
/// > digits '.' digits '.' digits | ||
/// | ||
/// optionally followed by `-` or `+` character | ||
/// and one or more "word characters", which are | ||
/// ASCII letters (`a`-`z`, `A`-`Z`), digits (`0`-`9`), | ||
/// underscore (`_`) and dollar-sign (`$`). | ||
/// | ||
/// This is not all complete SemVer version strings, | ||
/// since it doesn't allow `.` in the continuation, | ||
/// or a `+...` sequence after a `-` sequence. | ||
/// It should be enough for the user-cases we need it for | ||
/// in this package. | ||
static final _versionRegex = RegExp(r'\d+\.\d+\.\d+(?:[\-+]\w+)?'); | ||
|
||
String? get latestVersion { | ||
var input = latestHeading; | ||
|
||
if (input == null) { | ||
return null; | ||
if (input != null) { | ||
var match = _versionRegex.firstMatch(input); | ||
if (match != null) { | ||
var version = match[0]; | ||
return version; | ||
} | ||
} | ||
|
||
final versionRegex = RegExp(r'[0-9]+\.[0-9]+\.[0-9]+([-\+]\w+)?'); | ||
|
||
var match = versionRegex.firstMatch(input); | ||
|
||
if (match != null) { | ||
var version = match.group(0); | ||
return version; | ||
} | ||
return null; | ||
} | ||
|
||
String? get latestHeading { | ||
var sections = _parseSections(); | ||
// Remove all leading "#" | ||
return sections.firstOrNull?.title.replaceAll(RegExp(r'^#*'), '').trim(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only lines we accept below as title lines start with |
||
var section = sections.firstOrNull; | ||
if (section == null) return null; | ||
// Remove the leading `_headerLinePrefix`, then trim left-over whitespace. | ||
var title = section.title; | ||
assert(title.startsWith(_headerLinePrefix)); | ||
return title.substring(_headerLinePrefix.length).trim(); | ||
} | ||
|
||
List<String> get latestChangeEntries { | ||
var sections = _parseSections(); | ||
return sections.isEmpty ? [] : sections.first.entries; | ||
} | ||
List<String> get latestChangeEntries => | ||
_parseSections().firstOrNull?.entries ?? []; | ||
|
||
Iterable<_Section> _parseSections() sync* { | ||
if (!exists) return; | ||
|
||
_Section? section; | ||
|
||
for (var line in file.readAsLinesSync().where((line) => line.isNotEmpty)) { | ||
if (line.startsWith('## ')) { | ||
for (var line in file.readAsLinesSync()) { | ||
if (line.isEmpty) continue; | ||
if (line.startsWith(_headerLinePrefix)) { | ||
if (section != null) yield section; | ||
|
||
section = _Section(line); | ||
} else if (section != null) { | ||
section.entries.add(line); | ||
} else { | ||
section?.entries.add(line); | ||
} | ||
} | ||
|
||
if (section != null) yield section; | ||
} | ||
|
||
String get describeLatestChanges { | ||
var buf = StringBuffer(); | ||
for (var entry in latestChangeEntries) { | ||
buf.writeln(entry); | ||
} | ||
return buf.toString(); | ||
} | ||
String get describeLatestChanges => latestChangeEntries.join(); | ||
} | ||
|
||
class _Section { | ||
|
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 |
---|---|---|
|
@@ -25,41 +25,62 @@ Future<int> runCommand( | |
|
||
process.stdout | ||
.transform(utf8.decoder) | ||
.transform(LineSplitter()) | ||
.listen((line) => stdout.writeln(' $line')); | ||
.transform(const LineSplitter()) | ||
.listen((line) => stdout | ||
..write(' ') | ||
..writeln(line)); | ||
process.stderr | ||
.transform(utf8.decoder) | ||
.transform(LineSplitter()) | ||
.listen((line) => stderr.writeln(' $line')); | ||
.transform(const LineSplitter()) | ||
.listen((line) => stderr | ||
..write(' ') | ||
..writeln(line)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just don't allocate extra strings unnecessarily. |
||
|
||
return process.exitCode; | ||
} | ||
|
||
class Tag { | ||
/// RegExp matching a version tag at the start of a line. | ||
/// | ||
/// A version tag is an optional starting seqeuence | ||
/// of non-whitespace, which is the package name, | ||
/// followed by a `v` and a simplified SemVer version | ||
/// number. | ||
/// The version number accepted is | ||
/// > digits '.' digits '.' digits | ||
/// | ||
/// and if followed by a `+`, then it includes the | ||
/// rest of the line. | ||
static final RegExp packageVersionTag = | ||
RegExp(r'^(\S+)-v(\d+\.\d+\.\d+(\+.*)?)'); | ||
|
||
static final RegExp versionTag = RegExp(r'^v(\d+\.\d+\.\d+(\+.*)?)'); | ||
RegExp(r'^(?:(\S+)-)?v(\d+\.\d+\.\d+(?:\+.*)?)'); | ||
|
||
/// A package version tag. | ||
/// | ||
/// Is expected to have the format: | ||
/// > (package-name)? 'v' SemVer-version | ||
/// | ||
/// If not, the tag is not [valid], and the [package] and [version] | ||
/// will both be `null`. | ||
final String tag; | ||
|
||
Tag(this.tag); | ||
|
||
bool get valid => version != null; | ||
|
||
String? get package { | ||
var match = packageVersionTag.firstMatch(tag); | ||
return match?.group(1); | ||
} | ||
/// The package name before the `v` in the version [tag], if any. | ||
/// | ||
/// Is `null` if there is no package name before the `v`, | ||
/// or if the tag is not [valid]. | ||
String? get package => packageVersionTag.firstMatch(tag)?[1]; | ||
|
||
String? get version { | ||
var match = packageVersionTag.firstMatch(tag); | ||
if (match != null) { | ||
return match.group(2); | ||
} | ||
match = versionTag.firstMatch(tag); | ||
return match?.group(1); | ||
} | ||
/// The SemVer version string of the version [tag], if any. | ||
/// | ||
/// This is the part after the `v` of the [tag] string, | ||
/// of the form, which is a major/minor/patch version string | ||
/// optionally followed by a `+` and more characters. | ||
/// | ||
/// Is `null` if the tag is not [valid]. | ||
String? get version => packageVersionTag.firstMatch(tag)?[2]; | ||
|
||
@override | ||
String toString() => tag; | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This entire class can be optimized to not read the file from scratch on every operation.
Unless it's deliberately doing it to avoid using stale data, it probably should cache the
List<String>
lines and/orList<_Section>
sections.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suspect that worrying about stale data was not a use-case here; some simple caching seems reasonable.