Skip to content

Codelab rebuild to markdown #2309

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion tooling/codelab_rebuild/bin/codelab_rebuild.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ void main(List<String> arguments) {

final source = arguments.single;
final blueprint = Blueprint.load(File(source));
blueprint.rebuild(File(source).parent);
var basedir = File(source).parent;
blueprint.rebuild(basedir);
blueprint.regenerateCodelabRebuildMd(basedir);
}
26 changes: 25 additions & 1 deletion tooling/codelab_rebuild/lib/src/blueprint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,29 @@ import 'package:json_annotation/json_annotation.dart';
import 'package:logging/logging.dart';

import 'rebuild_blueprint.dart';
import 'regenerate_codelab_rebuild_md.dart';

part 'blueprint.g.dart';

final _logger = Logger('blueprint');

@JsonSerializable(anyMap: true, checked: true, disallowUnrecognizedKeys: true)
class Blueprint {
/// The name of the codelab.
@JsonKey(required: true)
final String name;

@JsonKey(name: 'generate-markdown')
final bool generateMarkdown;

@JsonKey(required: true)
final List<BlueprintStep> steps;

Blueprint({required this.name, required this.steps}) {
Blueprint({
required this.name,
required this.steps,
this.generateMarkdown = false,
}) {
if (name.isEmpty) {
throw ArgumentError.value(name, 'name', 'Cannot be empty.');
}
Expand Down Expand Up @@ -64,6 +74,10 @@ class Blueprint {
/// Rebuild a blueprint in a target directory.
Future<void> rebuild(Directory cwd) => rebuildFromBlueprint(cwd, this);

/// Regenerate the `codelab_rebuild.md` from the blueprint.
Future<void> regenerateCodelabRebuildMd(Directory cwd) =>
regenerateCodelabRebuildMdFromBlueprint(cwd, this);

Map<String, dynamic> toJson() => _$BlueprintToJson(this);

@override
Expand All @@ -76,6 +90,11 @@ class BlueprintStep {
final String name;
final List<BlueprintStep> steps;

@JsonKey(name: 'markdown-content')
final String? markdownContent;
@JsonKey(name: 'markdown-ignore')
final bool markdownIgnore;

final String? path;

@JsonKey(name: 'base64-contents')
Expand Down Expand Up @@ -175,6 +194,8 @@ class BlueprintStep {
this.iphoneosDeploymentTarget,
this.macosxDeploymentTarget,
this.protoc,
this.markdownContent,
this.markdownIgnore = false,
}) {
if (name.isEmpty) {
throw ArgumentError.value(name, 'name', 'Cannot be empty.');
Expand All @@ -190,6 +211,9 @@ class BlueprintStep {
// Stop is for debugging only.
if (stop != null && stop == true) return true;

// Markdown content is stand alone valid
if (markdownContent != null) return true;

// If there aren't sub-steps, then there should be something else to do.
if (steps.isEmpty &&
patch == null &&
Expand Down
20 changes: 18 additions & 2 deletions tooling/codelab_rebuild/lib/src/blueprint.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions tooling/codelab_rebuild/lib/src/rebuild_blueprint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ Future<void> _buildBlueprintStep(Directory cwd, BlueprintStep step) async {
exit(0);
}

final markdownContent = step.markdownContent;
if (markdownContent != null) {
return;
}

final platforms = step.platforms;
if (platforms != null) {
if (!platforms.contains(Platform.operatingSystem)) {
Expand Down
63 changes: 63 additions & 0 deletions tooling/codelab_rebuild/lib/src/regenerate_codelab_rebuild_md.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import 'dart:io';

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

import 'blueprint.dart';

Future<void> regenerateCodelabRebuildMdFromBlueprint(
Directory cwd,
Blueprint blueprint,
) async {
if (blueprint.generateMarkdown != true) return;

final buff = StringBuffer();

buff.writeln('# ${blueprint.name}');
buff.writeln();

for (final step in blueprint.steps) {
_handleStep(step, buff, 2);
}

await File(
path.join(cwd.path, 'codelab_rebuild.md'),
).writeAsString(buff.toString(), flush: true);
}

void _handleStep(BlueprintStep step, StringBuffer buff, int depth) {
// Skip certain steps
if (step.rmdir != null || step.stripLinesContaining != null) return;

buff.writeln('${'#' * depth} ${step.name}');

if (step.steps.length > 1) {
for (final subStep in step.steps) {
_handleStep(subStep, buff, depth + 1);
}
return;
}

final flutter = step.flutter;
if (flutter != null) {
buff.write('''
Run the following Flutter command:

```console
\$ flutter $flutter
```
''');
return;
}

final dart = step.dart;
if (dart != null) {
buff.write('''
Run the following Dart command:

```console
\$ dart $dart
```
''');
return;
}
}