mirror of
https://github.com/flutter/samples.git
synced 2025-11-08 13:58:47 +00:00
Fix <base href="/"> tags in hosted samples, if required. (#867)
* Fix <base href="/"> tags in hosted samples, if required. * add trailing slash
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
import 'package:path/path.dart' as p;
|
import 'package:path/path.dart' as p;
|
||||||
import 'common.dart';
|
import 'common.dart';
|
||||||
|
import 'fix_base_tags.dart';
|
||||||
|
|
||||||
final ignoredDirectories = ['_tool', 'samples_index'];
|
final ignoredDirectories = ['_tool', 'samples_index'];
|
||||||
|
|
||||||
@@ -40,6 +41,9 @@ main() async {
|
|||||||
await _run(directory, 'flutter', ['build', 'web']);
|
await _run(directory, 'flutter', ['build', 'web']);
|
||||||
await _run(directory, 'mv', [sourceBuildDir, targetDirectory]);
|
await _run(directory, 'mv', [sourceBuildDir, targetDirectory]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update the <base href> tags in each index.html file
|
||||||
|
await fixBaseTags();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Invokes run() and exits if the sub-process failed.
|
// Invokes run() and exits if the sub-process failed.
|
||||||
|
|||||||
50
web/_tool/fix_base_tags.dart
Normal file
50
web/_tool/fix_base_tags.dart
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
import 'dart:io';
|
||||||
|
import 'package:path/path.dart' as p;
|
||||||
|
|
||||||
|
Future<void> main() async {
|
||||||
|
await fixBaseTags();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Changes each sample's `<base href="/">` tag in index.html to
|
||||||
|
/// `<base href="/samples/web/<SAMPLE_DIR_NAME>/">`
|
||||||
|
///
|
||||||
|
/// For example, after building using `build_ci.dart,
|
||||||
|
/// `../samples_index/public/web/navigation_and_routing/index.html` should
|
||||||
|
/// contain `<base href="/samples/web/navigation_and_routing/">
|
||||||
|
Future<void> fixBaseTags() async {
|
||||||
|
print('currentDir = ${Directory.current.path}');
|
||||||
|
var builtSamplesDir = Directory(p.joinAll([
|
||||||
|
// Parent directory
|
||||||
|
...p.split(Directory.current.path),
|
||||||
|
// path to built samples
|
||||||
|
...p.split('samples_index/public/web')
|
||||||
|
]));
|
||||||
|
if (!await builtSamplesDir.exists()) {
|
||||||
|
print('${builtSamplesDir.path} does not exist.');
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
await for (var builtSample in builtSamplesDir.list()) {
|
||||||
|
if (builtSample is Directory) {
|
||||||
|
var index = File(p.join(builtSample.path, 'index.html'));
|
||||||
|
if (!await index.exists()) {
|
||||||
|
throw ('no index.html file found in ${builtSample.path}');
|
||||||
|
}
|
||||||
|
|
||||||
|
var sampleDirName = p
|
||||||
|
.split(builtSample.path)
|
||||||
|
.last;
|
||||||
|
|
||||||
|
if (await index.exists()) {
|
||||||
|
final regex = RegExp('<base href="(.*)">');
|
||||||
|
var contents = await index.readAsString();
|
||||||
|
if (!contents.contains(regex)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
var newContents = contents.replaceFirst(
|
||||||
|
regex, '<base href="/samples/web/$sampleDirName/">');
|
||||||
|
await index.writeAsString(newContents);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -54,7 +54,7 @@ packages:
|
|||||||
name: meta
|
name: meta
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.3.0"
|
version: "1.7.0"
|
||||||
path:
|
path:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ packages:
|
|||||||
name: meta
|
name: meta
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.3.0"
|
version: "1.7.0"
|
||||||
path:
|
path:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ packages:
|
|||||||
name: meta
|
name: meta
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.3.0"
|
version: "1.7.0"
|
||||||
path:
|
path:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@@ -110,4 +110,4 @@ packages:
|
|||||||
source: hosted
|
source: hosted
|
||||||
version: "2.1.0"
|
version: "2.1.0"
|
||||||
sdks:
|
sdks:
|
||||||
dart: ">=2.12.0-0.0 <3.0.0"
|
dart: ">=2.12.0 <3.0.0"
|
||||||
|
|||||||
Reference in New Issue
Block a user