Skip to content

Commit

Permalink
CI: Site Variable Scanner
Browse files Browse the repository at this point in the history
  • Loading branch information
AmosHuKe committed Jun 26, 2024
1 parent 83999cf commit 3280bfd
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 1 deletion.
17 changes: 16 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,21 @@ jobs:
- name: Check for broken internal links
run: dart run flutter_site check-links

site-variable-scanner:
name: Check if text can be replaced with site variables
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29
with:
submodules: recursive
- uses: dart-lang/setup-dart@f0ead981b4d9a35b37f30d36160575d60931ec30
with:
sdk: stable
- name: Fetch Dart packages
run: dart pub get
- name: Check if text can be replaced with site variables
run: dart run dart_site check-site-variable

firebase-validate:
name: Validate Firebase configuration
runs-on: ubuntu-latest
Expand All @@ -119,7 +134,7 @@ jobs:

deploy:
name: Deploy to production
needs: [test, excerpts, linkcheck, firebase-validate]
needs: [test, excerpts, linkcheck, site-variable-scanner, firebase-validate]
runs-on: ubuntu-latest
if: |
github.event_name == 'push' &&
Expand Down
75 changes: 75 additions & 0 deletions tool/config/site_variable_scanner.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
yaml_variable_scanner:

# File path for YAML variables
yamlFilePath:
- path: "src/_data/site.yml" # YAML path (Glob syntax)
variablePrefix: "site." # Variable prefix. e.g. `a.` -> `a.x.xx`

# Ignore YAML file path
#
# (Glob Syntax)
ignoreYamlFilePath:

# Ignore YAML Key
#
# e.g. "^a\.bb$"
#
# (RegExp Syntax)
ignoreYamlKey:
- ^site\.title$
- ^site\.url$
- ^site\.main-url$
- ^site\.github_username$
- ^site\.branch$
- ^site\.dart\.sdk\.channel$
- ^site\.sdk\.channel$
- ^site\.appmin\.
- ^site\.devmin\.
- ^site\.targetmin\.
- ^site\.appnow\.
- ^site\.bili\.std-size$

# File path for check file contents
#
# (Glob Syntax)
checkFilePath:
- "src/content/**/*.md"
- "src/_includes/**/*.md"

# Ignore file paths to check
#
# (Glob Syntax)
ignoreCheckFilePath:
- "src/content/release/release-notes/**"
- "src/content/tools/devtools/release-notes/**"
- "src/content/community/china-old.md"
- "src/content/community/china/**"
- "src/content/community/tutorials/**"
- "src/content/posts/**"

# Ignore text that doesn't need to match
#
# e.g.
# - `r"^\s*---([\s\S]*?)---$"`
# - `r"^\s*{%-?\s*comment\s*-?%}([\s\S]*?){%-?\s*endcomment\s*-?%}$"`
# - `r"^\s*<!---?\s*([\s\S]*?)\s*-?-->$"`
#
# (RegExp Syntax)
ignoreCheckText:
# <!-- ignore-variable-check --> xxx <!-- end-ignore-variable-check -->
- ^\s*<!---?\s*ignore-variable-check\s*-?-->([\s\S]*?)<!---?\s*end-ignore-variable-check\s*-?-->$

# --- xxx ---
- ^\s*---([\s\S]*?)---$

# {%- comment %} xxx {% endcomment -%}
- ^\s*{%-?\s*comment\s*-?%}([\s\S]*?){%-?\s*endcomment\s*-?%}$

# <!-- xxx -->
- ^\s*<!---?\s*([\s\S]*?)\s*-?-->$

# ``` xxx ```
- ^\s*```([\s\S]*?)```$

# ` xxx `
- '`([^`]*)`'
2 changes: 2 additions & 0 deletions tool/flutter_site/lib/flutter_site.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'src/commands/build.dart';
import 'src/commands/check_all.dart';
import 'src/commands/check_link_references.dart';
import 'src/commands/check_links.dart';
import 'src/commands/check_site_variable.dart';
import 'src/commands/format_dart.dart';
import 'src/commands/refresh_excerpts.dart';
import 'src/commands/serve.dart';
Expand All @@ -29,6 +30,7 @@ final class FlutterSiteCommandRunner extends CommandRunner<int> {
addCommand(CheckAllCommand());
addCommand(CheckLinksCommand());
addCommand(CheckLinkReferencesCommand());
addCommand(CheckSiteVariableCommand());
addCommand(FormatDartCommand());
addCommand(RefreshExcerptsCommand());
addCommand(ServeSiteCommand());
Expand Down
56 changes: 56 additions & 0 deletions tool/flutter_site/lib/src/commands/check_site_variable.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2024 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:io';

import 'package:args/command_runner.dart';
import 'package:yaml_variable_scanner/yaml_variable_scanner.dart';

import '../utils.dart';

final class CheckSiteVariableCommand extends Command<int> {
static const String _printModeFlag = 'print-mode';

CheckSiteVariableCommand() {
argParser.addOption(
_printModeFlag,
help: 'Configure the amount of information output.',
allowed: ['none', 'detail', 'stats', 'detailAndStats'],
allowedHelp: {
'none': 'No content.',
'detail': 'Detail to file lines and columns.',
'stats': 'Total statistics.',
'detailAndStats': 'detail & stats.',
},
defaultsTo: 'detail',
);
}

@override
String get description =>
'Scan multiple files for text that can use site variables.';

@override
String get name => 'check-site-variable';

@override
Future<int> run() async {
final printMode = argResults.get<String>(_printModeFlag, 'detail');

try {
final checkResultAll = await YamlVariableScanner.run(
'./tool/config/site_variable_scanner.yaml',
stdout,
printMode: PrintMode.values.byName(printMode),
);
if (checkResultAll.isNotEmpty) return 2;
return 0;
} catch (e, stackTrace) {
stderr.writeln('Error: YamlVariableScanner failed to execute properly!');
stderr.writeln(e);
stderr.writeln(stackTrace);
return 1;
}
}
}
1 change: 1 addition & 0 deletions tool/flutter_site/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ dependencies:
args: ^2.4.2
io: ^1.0.4
linkcheck: ^3.0.0
yaml_variable_scanner: ^0.0.5
path: ^1.9.0

dev_dependencies:
Expand Down

0 comments on commit 3280bfd

Please sign in to comment.