mirror of
https://github.com/flutter/samples.git
synced 2025-11-08 13:58:47 +00:00
Add flutter_web samples (#75)
This commit is contained in:
committed by
Andrew Brogdon
parent
42f2dce01b
commit
3fe927cb29
202
web/_tool/peanut_post_build.dart
Normal file
202
web/_tool/peanut_post_build.dart
Normal file
@@ -0,0 +1,202 @@
|
||||
// Copyright 2019 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// Called by https://pub.dartlang.org/packages/peanut to generate example pages
|
||||
// for hosting.
|
||||
//
|
||||
// Requires at least v3.2.0 of `package:peanut`
|
||||
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:markdown/markdown.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
|
||||
void main(List<String> args) {
|
||||
final buildDir = args[0];
|
||||
final fileMap =
|
||||
(jsonDecode(args[1]) as Map<String, dynamic>).cast<String, String>();
|
||||
|
||||
if (fileMap.length < 2) {
|
||||
throw StateError('We are assuming there is more than one sample!');
|
||||
}
|
||||
|
||||
// This is USUALLY the case – where we have more than one demo
|
||||
for (var exampleDir in fileMap.values) {
|
||||
for (var htmlFile in Directory(p.join(buildDir, exampleDir))
|
||||
.listSync()
|
||||
.whereType<File>()
|
||||
.where((f) => p.extension(f.path) == '.html')) {
|
||||
_writeAnalytics(htmlFile, buildDir);
|
||||
}
|
||||
}
|
||||
|
||||
final tocFile = File(p.join(buildDir, 'index.html'));
|
||||
if (!tocFile.existsSync()) {
|
||||
throw StateError('$tocFile should exist!');
|
||||
}
|
||||
|
||||
tocFile.writeAsStringSync(
|
||||
_tocTemplate(
|
||||
fileMap.entries.map(
|
||||
(entry) => _Demo(
|
||||
_prettyName(entry.value),
|
||||
entry.key,
|
||||
entry.value,
|
||||
),
|
||||
),
|
||||
),
|
||||
flush: true);
|
||||
}
|
||||
|
||||
void _writeAnalytics(File htmlFile, String buildDir) {
|
||||
final content = htmlFile.readAsStringSync();
|
||||
final newContent = content.replaceFirst('<head>', '<head>\n$_analytics');
|
||||
|
||||
final filePath = p.relative(htmlFile.path, from: buildDir);
|
||||
|
||||
if (newContent == content) {
|
||||
print('!!! Did not replace contents in $filePath');
|
||||
} else {
|
||||
print('Replaced contents in $filePath');
|
||||
htmlFile.writeAsStringSync(newContent, flush: true);
|
||||
}
|
||||
}
|
||||
|
||||
class _Demo {
|
||||
final String name, pkgDir, buildDir;
|
||||
|
||||
_Demo(this.name, this.pkgDir, this.buildDir);
|
||||
|
||||
String get content {
|
||||
final path = p.normalize(p.join(pkgDir, '..', 'README.md'));
|
||||
|
||||
final readmeFile = File(path);
|
||||
|
||||
if (!readmeFile.existsSync()) {
|
||||
print(' $path – No readme!');
|
||||
return '';
|
||||
}
|
||||
|
||||
var readmeContent = readmeFile.readAsStringSync();
|
||||
|
||||
final tripleLineIndex = readmeContent.indexOf('\n\n\n');
|
||||
var secondDoubleIndex = readmeContent.indexOf('\n\n');
|
||||
|
||||
if (secondDoubleIndex >= 0) {
|
||||
secondDoubleIndex = readmeContent.indexOf('\n\n', secondDoubleIndex + 1);
|
||||
}
|
||||
|
||||
final endIndices =
|
||||
([tripleLineIndex, secondDoubleIndex].where((i) => i >= 0).toList()
|
||||
..sort());
|
||||
|
||||
final endIndex =
|
||||
endIndices.isEmpty ? readmeContent.length : endIndices.first;
|
||||
|
||||
return markdownToHtml(readmeContent.substring(0, endIndex - 1));
|
||||
}
|
||||
|
||||
String get html => '''
|
||||
<div>
|
||||
<a href='$buildDir'>
|
||||
<img src='${p.url.join(buildDir, 'preview.png')}' width="300" alt="$name">
|
||||
</a>
|
||||
<a class='demo-title' href='$buildDir'>$name</a>
|
||||
<div>
|
||||
${_indent(content, 2)}
|
||||
</div>
|
||||
</div>
|
||||
''';
|
||||
}
|
||||
|
||||
final _underscoreOrSlash = RegExp('_|/');
|
||||
|
||||
String _prettyName(String input) =>
|
||||
input.split(_underscoreOrSlash).where((e) => e.isNotEmpty).map((e) {
|
||||
return e.substring(0, 1).toUpperCase() + e.substring(1);
|
||||
}).join(' ');
|
||||
|
||||
// flutter.github.io
|
||||
const _analyticsId = 'UA-67589403-8';
|
||||
|
||||
const _analytics = '''
|
||||
<script async src="https://www.googletagmanager.com/gtag/js?id=$_analyticsId"></script>
|
||||
<script>
|
||||
window.dataLayer = window.dataLayer || [];
|
||||
function gtag(){dataLayer.push(arguments);}
|
||||
gtag('js', new Date());
|
||||
gtag('config', '$_analyticsId');
|
||||
</script>''';
|
||||
|
||||
String _indent(String content, int spaces) =>
|
||||
LineSplitter.split(content).join('\n' + ' ' * spaces);
|
||||
|
||||
const _itemsReplace = r'<!-- ITEMS -->';
|
||||
|
||||
String _tocTemplate(Iterable<_Demo> items) => '''
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
${_indent(_analytics, 2)}
|
||||
<title>Examples</title>
|
||||
<meta name="generator" content="https://pub.dartlang.org/packages/peanut">
|
||||
<style>
|
||||
body {
|
||||
font-family: "Google Sans", "Roboto", sans-serif;
|
||||
text-align: center;
|
||||
}
|
||||
a {
|
||||
text-decoration: none;
|
||||
color: #1389FD;
|
||||
}
|
||||
a:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
#toc {
|
||||
text-align: left;
|
||||
max-width: 1050px;
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-self: center;
|
||||
margin: 0 auto;
|
||||
align-content: space-between;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
#toc > div {
|
||||
width: 300px;
|
||||
padding: 1rem;
|
||||
margin: 0.5rem;
|
||||
border: 1px solid rgba(0, 0, 0, 0.125);
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
#toc > div img {
|
||||
display: block;
|
||||
margin: 0 auto 1rem;
|
||||
}
|
||||
|
||||
.demo-title {
|
||||
font-size: 1.25rem;
|
||||
}
|
||||
|
||||
#toc > div p {
|
||||
margin-top: 0.5rem;
|
||||
margin-bottom: 0;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h2><a href='https://github.com/flutter/flutter_web'>Flutter for web</a> samples</h2>
|
||||
<a href='https://github.com/flutter/samples/tree/master/web'>Sample source code</a>
|
||||
<div id="toc">
|
||||
$_itemsReplace
|
||||
</div>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
'''
|
||||
.replaceAll(_itemsReplace, _indent(items.map((d) => d.html).join('\n'), 4));
|
||||
33
web/_tool/pubspec.lock
Normal file
33
web/_tool/pubspec.lock
Normal file
@@ -0,0 +1,33 @@
|
||||
# Generated by pub
|
||||
# See https://www.dartlang.org/tools/pub/glossary#lockfile
|
||||
packages:
|
||||
args:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: args
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.5.1"
|
||||
charcode:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: charcode
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.1.2"
|
||||
markdown:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: markdown
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "2.0.3"
|
||||
path:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: path
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.6.2"
|
||||
sdks:
|
||||
dart: ">=2.0.0 <3.0.0"
|
||||
6
web/_tool/pubspec.yaml
Normal file
6
web/_tool/pubspec.yaml
Normal file
@@ -0,0 +1,6 @@
|
||||
name: tool
|
||||
publish_to: none
|
||||
|
||||
dependencies:
|
||||
markdown: ^2.0.3
|
||||
path: ^1.6.2
|
||||
87
web/_tool/verify_packages.dart
Normal file
87
web/_tool/verify_packages.dart
Normal file
@@ -0,0 +1,87 @@
|
||||
// Copyright 2019 The Chromium 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:path/path.dart' as p;
|
||||
|
||||
const _ansiGreen = 32;
|
||||
const _ansiRed = 31;
|
||||
const _ansiMagenta = 35;
|
||||
|
||||
void main() async {
|
||||
final packageDirs = _listPackageDirs(Directory.current)
|
||||
.map((path) => p.relative(path, from: Directory.current.path))
|
||||
.toList();
|
||||
|
||||
print('Package dirs:\n${packageDirs.map((path) => ' $path').join('\n')}');
|
||||
|
||||
final results = <bool>[];
|
||||
for (var i = 0; i < packageDirs.length; i++) {
|
||||
final dir = packageDirs[i];
|
||||
_logWrapped(_ansiMagenta, '\n$dir (${i + 1} of ${packageDirs.length})');
|
||||
results.add(await _run(dir, 'pub', ['upgrade', '--no-precompile']));
|
||||
results.add(await _run(
|
||||
dir,
|
||||
'dartanalyzer',
|
||||
['--fatal-infos', '--fatal-warnings', '.'],
|
||||
));
|
||||
_printStatus(results);
|
||||
}
|
||||
|
||||
if (results.any((v) => !v)) {
|
||||
exitCode = 1;
|
||||
}
|
||||
}
|
||||
|
||||
void _printStatus(List<bool> results) {
|
||||
var successCount = results.where((t) => t).length;
|
||||
var success = (successCount == results.length);
|
||||
var pct = 100 * successCount / results.length;
|
||||
|
||||
_logWrapped(success ? _ansiGreen : _ansiRed,
|
||||
'$successCount of ${results.length} (${pct.toStringAsFixed(2)}%)');
|
||||
}
|
||||
|
||||
void _logWrapped(int code, String message) {
|
||||
print('\x1B[${code}m$message\x1B[0m');
|
||||
}
|
||||
|
||||
Future<bool> _run(
|
||||
String workingDir, String commandName, List<String> args) async {
|
||||
var commandDescription = '`${([commandName]..addAll(args)).join(' ')}`';
|
||||
|
||||
_logWrapped(_ansiMagenta, ' Running $commandDescription');
|
||||
|
||||
var proc = await Process.start(
|
||||
commandName,
|
||||
args,
|
||||
workingDirectory: Directory.current.path + '/' + workingDir,
|
||||
mode: ProcessStartMode.inheritStdio,
|
||||
);
|
||||
|
||||
var exitCode = await proc.exitCode;
|
||||
|
||||
if (exitCode != 0) {
|
||||
_logWrapped(
|
||||
_ansiRed, ' Failed! ($exitCode) – $workingDir – $commandDescription');
|
||||
return false;
|
||||
} else {
|
||||
_logWrapped(_ansiGreen, ' Success! – $workingDir – $commandDescription');
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Iterable<String> _listPackageDirs(Directory dir) sync* {
|
||||
if (File('${dir.path}/pubspec.yaml').existsSync()) {
|
||||
yield dir.path;
|
||||
} else {
|
||||
for (var subDir in dir
|
||||
.listSync(followLinks: false)
|
||||
.whereType<Directory>()
|
||||
.where((d) => !Uri.file(d.path).pathSegments.last.startsWith('.'))) {
|
||||
yield* _listPackageDirs(subDir);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user