Skip to content

Commit

Permalink
Clean-up and tweaks of the firehose project.
Browse files Browse the repository at this point in the history
Cleaned up and reduced RegExp use.
Simplified some functions.
Upped SDK constraint to 3.0.0, to use `firstOrNull` from SDK.
  • Loading branch information
lrhn committed Jun 28, 2023
1 parent 9ef5948 commit ff1726a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 57 deletions.
17 changes: 10 additions & 7 deletions pkgs/firehose/lib/firehose.dart
Original file line number Diff line number Diff line change
Expand Up @@ -288,18 +288,21 @@ class VerificationResults {

bool get hasError => results.any((r) => r.severity == Severity.error);

String describeAsMarkdown([bool withTag = true]) {
String describeAsMarkdown({bool withTag = true}) {
results.sort((a, b) => Enum.compareByIndex(a.severity, b.severity));

return results.map((r) {
var sev = r.severity == Severity.error ? '(error) ' : '';
var tag = r.gitTag == null ? '' : '`${r.gitTag}`';
var publishReleaseUri = r.publishReleaseUri;
if (publishReleaseUri != null) {
tag = '[$tag]($publishReleaseUri)';
}
var tagColumn = '';
if (withTag) {
var tag = r.gitTag == null ? '' : '`${r.gitTag}`';
var publishReleaseUri = r.publishReleaseUri;
if (publishReleaseUri != null) {
tag = '[$tag]($publishReleaseUri)';
}

var tagColumn = withTag ? ' | $tag' : '';
tagColumn = ' | $tag';
}
return '| package:${r.package.name} | ${r.package.version} | '
'$sev${r.message}$tagColumn |';
}).join('\n');
Expand Down
53 changes: 24 additions & 29 deletions pkgs/firehose/lib/src/changelog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,69 +4,64 @@

import 'dart:io';

import 'package:collection/collection.dart';

class Changelog {
static const _headerLinePrefix = '## ';

final File file;

Changelog(this.file);

bool get exists => file.existsSync();

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();
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 {
Expand Down
30 changes: 11 additions & 19 deletions pkgs/firehose/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,41 +25,33 @@ 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));

return process.exitCode;
}

class Tag {
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+(?:\+.*)?)');

final String tag;

Tag(this.tag);

bool get valid => version != null;

String? get package {
var match = packageVersionTag.firstMatch(tag);
return match?.group(1);
}
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);
}
String? get version => packageVersionTag.firstMatch(tag)?[2];

@override
String toString() => tag;
Expand Down
3 changes: 1 addition & 2 deletions pkgs/firehose/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: firehose
description: A tool to automate publishing of Pub packages from GitHub actions.
version: 0.3.18
version: 0.3.19
repository: https://github.com/dart-lang/ecosystem/tree/main/pkgs/firehose

environment:
Expand All @@ -11,7 +11,6 @@ executables:

dependencies:
args: ^2.3.0
collection: ^1.17.0
http: ^0.13.0
path: ^1.8.0
pub_semver: ^2.1.0
Expand Down

0 comments on commit ff1726a

Please sign in to comment.