Skip to content

Commit

Permalink
💚 Fix the run script (#38)
Browse files Browse the repository at this point in the history
- Use `path.join` for paths instead of hard-coded `/` as much as
possible.
- Use `flutter.bat` for Windows.
- Determine whether the script is running under the root path using
`name: tool_metadata` in the `pubspec.yaml`.

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read the [Flutter Style Guide] _recently_, and have followed its
advice.
- [x] I signed the [CLA].
- [ ] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [ ] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] All existing and new tests are passing.

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#overview
[Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene
[test-exempt]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes
[Discord]: https://github.com/flutter/flutter/wiki/Chat
  • Loading branch information
AlexV525 committed Aug 21, 2023
1 parent 75d129a commit 8c2163c
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 37 deletions.
24 changes: 13 additions & 11 deletions tool/colors/generate_files.dart
Expand Up @@ -13,7 +13,7 @@ import 'generated/colors_cupertino.dart' as cupertino;
import 'generated/colors_material.dart' as material;
import 'generated/colors_css.dart' as css;

const String outputFolder = 'resources/colors';
final String outputFolder = path.join('resources', 'colors');

Future<void> main(List<String> args) async {
// Verify that we're running from the project root.
Expand Down Expand Up @@ -43,15 +43,15 @@ Future<void> exitWith(int code) async {
}

void generatePropertiesFiles() {
generateProperties(material.colors, '$outputFolder/material.properties');
generateProperties(cupertino.colors, '$outputFolder/cupertino.properties');
generateProperties(css.colors, '$outputFolder/css.properties');
generateProperties(material.colors, 'material.properties');
generateProperties(cupertino.colors, 'cupertino.properties');
generateProperties(css.colors, 'css.properties');
}

void generateJsonFiles() {
generateJson(material.colors, '$outputFolder/material.json');
generateJson(cupertino.colors, '$outputFolder/cupertino.json');
generateJson(css.colors, '$outputFolder/css.json');
generateJson(material.colors, 'material.json');
generateJson(cupertino.colors, 'cupertino.json');
generateJson(css.colors, 'css.json');
}

const List<int> validShades = <int>[
Expand Down Expand Up @@ -79,9 +79,10 @@ void generateProperties(Map<String, Color> colors, String filename) {
writeColors(colors,
(String name, Color color) => buf.writeln('$name=${color.toHex()}'));

File(filename).writeAsStringSync(buf.toString());
final dest = path.join(outputFolder, filename);
File(dest).writeAsStringSync(buf.toString());

print('wrote $filename');
print('wrote $dest');
}

void generateJson(Map<String, Color> colors, String filename) {
Expand All @@ -94,9 +95,10 @@ void generateJson(Map<String, Color> colors, String filename) {
buf.writeln(lines.join(',\n'));
buf.writeln('}');

File(filename).writeAsStringSync(buf.toString());
final dest = path.join(outputFolder, filename);
File(dest).writeAsStringSync(buf.toString());

print('wrote $filename');
print('wrote $dest');
}

void writeColors(Map<String, Color> colors,
Expand Down
6 changes: 4 additions & 2 deletions tool/colors/update_colors.dart
Expand Up @@ -19,7 +19,7 @@ const String generatedFilesPath = 'tool/colors/generated';

Future<void> main(List<String> args) async {
// Verify that we're running from the project root.
if (path.basename(Directory.current.path) != 'tools_metadata') {
if (!await fromTheProjectRoot()) {
print('Please run this script from the directory root.');
exit(1);
}
Expand Down Expand Up @@ -89,7 +89,9 @@ final Map<String, Color> colors = <String, Color>{''');

buf.writeln('};');

final File out = File('$generatedFilesPath/colors_$colorType.dart');
final File out = File(
path.join(generatedFilesPath, 'colors_$colorType.dart'),
);
out.writeAsStringSync(buf.toString());

print('wrote ${out.path}');
Expand Down
35 changes: 30 additions & 5 deletions tool/common.dart
Expand Up @@ -6,14 +6,21 @@ import 'dart:convert';
import 'dart:io';

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

const String flutterBranch = 'beta';

final String flutterSdkPath = _getFlutterSdkPath();

final String flutterPath = path.join(
flutterSdkPath,
path.join('bin', Platform.isWindows ? 'flutter.bat' : 'flutter'),
);

String _getFlutterSdkPath() {
// This depends on the dart SDK being in <flutter-sdk>/bin/cache/dart-sdk/bin.
if (!Platform.resolvedExecutable.contains('bin/cache/dart-sdk')) {
if (!Platform.resolvedExecutable
.contains(path.join('bin', 'cache', 'dart-sdk'))) {
throw 'Please run this script from the version of dart in the Flutter SDK.';
}

Expand All @@ -22,9 +29,10 @@ String _getFlutterSdkPath() {
}

Map<String, String> calculateFlutterVersion() {
final String flutterPath = path.join(flutterSdkPath, 'bin/flutter');
final ProcessResult result =
Process.runSync(flutterPath, <String>['--version', '--machine']);
final ProcessResult result = Process.runSync(
flutterPath,
<String>['--version', '--machine'],
);
if (result.exitCode != 0) {
throw 'Error from flutter --version';
}
Expand All @@ -35,7 +43,9 @@ Map<String, String> calculateFlutterVersion() {

Future<void> flutterRun(String script) async {
final Process proc = await Process.start(
'flutter', <String>['run', '-d', 'flutter-tester', '-t', script]);
flutterPath,
<String>['run', '-d', 'flutter-tester', '-t', script],
);
proc.stdout
.transform(utf8.decoder)
.transform(const LineSplitter())
Expand All @@ -49,3 +59,18 @@ Future<void> flutterRun(String script) async {
throw 'Process exited with code $exitCode';
}
}

/// Determine whether the environment is based from the project root
/// by validate the name of the pubspec if it exists.
Future<bool> fromTheProjectRoot([String? rootPath]) async {
final yamlPath = path.join(
rootPath ?? Directory.current.path,
'pubspec.yaml',
);
if (!File(yamlPath).existsSync()) {
return false;
}
final yamlMap =
(await loadYaml(await File(yamlPath).readAsString()) as YamlMap);
return yamlMap['name'] == 'tool_metadata';
}
41 changes: 31 additions & 10 deletions tool/icon_generator/lib/main.dart
Expand Up @@ -10,6 +10,7 @@ import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:path/path.dart' as path;
import 'package:yaml/yaml.dart';

import 'cupertino.dart' as cupertino;
import 'material.dart' as material;
Expand All @@ -20,7 +21,7 @@ final String resourcesFolder = path.join(toolsRoot, 'resources/icons');

Future main() async {
// Verify that we're running from the project root.
if (path.basename(toolsRoot) != 'tools_metadata') {
if (!await _fromTheProjectRoot(toolsRoot)) {
print('Script must be run from tool/icon_generator');
exit(1);
}
Expand All @@ -37,20 +38,28 @@ Future main() async {

for (material.IconTuple icon in material.icons) {
await findAndSave(
icon.smallKey, '$resourcesFolder/material/${icon.name}.png',
small: true);
icon.smallKey,
path.join(resourcesFolder, 'material', '${icon.name}.png'),
small: true,
);
await findAndSave(
icon.largeKey, '$resourcesFolder/material/${icon.name}@2x.png',
small: false);
icon.largeKey,
path.join(resourcesFolder, 'material', '${icon.name}@2x.png'),
small: false,
);
}

for (cupertino.IconTuple icon in cupertino.icons) {
await findAndSave(
icon.smallKey, '$resourcesFolder/cupertino/${icon.name}.png',
small: true);
icon.smallKey,
path.join(resourcesFolder, 'cupertino', '${icon.name}.png'),
small: true,
);
await findAndSave(
icon.largeKey, '$resourcesFolder/cupertino/${icon.name}@2x.png',
small: false);
icon.largeKey,
path.join(resourcesFolder, 'cupertino', '${icon.name}@2x.png'),
small: false,
);
}

print('Finished generating icons, quitting...');
Expand Down Expand Up @@ -163,9 +172,21 @@ Future<ui.Image> _captureImage(Element element) {
assert(element.renderObject != null);
RenderObject renderObject = element.renderObject!;
while (!renderObject.isRepaintBoundary) {
renderObject = renderObject.parent! as RenderObject;
renderObject = renderObject.parent!;
}
assert(!renderObject.debugNeedsPaint);
final OffsetLayer layer = renderObject.debugLayer! as OffsetLayer;
return layer.toImage(renderObject.paintBounds);
}

/// Determine whether the environment is based from the project root
/// by validate the name of the pubspec if it exists.
Future<bool> _fromTheProjectRoot(String rootPath) async {
final yamlPath = path.join(rootPath, 'pubspec.yaml');
if (!File(yamlPath).existsSync()) {
return false;
}
final yamlMap =
(await loadYaml(await File(yamlPath).readAsString()) as YamlMap);
return yamlMap['name'] == 'tool_metadata';
}
1 change: 1 addition & 0 deletions tool/icon_generator/pubspec.yaml
Expand Up @@ -12,6 +12,7 @@ dependencies:
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.4
yaml: ^3.1.1

dev_dependencies:
flutter_test:
Expand Down
36 changes: 27 additions & 9 deletions tool/icons/update_icons.dart
Expand Up @@ -5,6 +5,8 @@
import 'dart:convert';
import 'dart:io';

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

import '../common.dart';

const String outputFolder = 'resources/icons';
Expand All @@ -26,18 +28,32 @@ Future<void> main() async {

// generate .properties files
generateProperties(
materialIcons, '$outputFolder/material.properties', 'material');
materialIcons,
path.join(outputFolder, 'material.properties'),
'material',
);
generateProperties(
cupertinoIcons, '$outputFolder/cupertino.properties', 'cupertino');
cupertinoIcons,
path.join(outputFolder, 'cupertino.properties'),
'cupertino',
);

// generate dart code
generateDart(materialIcons, 'tool/icon_generator/lib/material.dart', 'Icons',
'material');
generateDart(cupertinoIcons, 'tool/icon_generator/lib/cupertino.dart',
'CupertinoIcons', 'cupertino');
generateDart(
materialIcons,
path.join('tool', 'icon_generator', 'lib', 'material.dart'),
'Icons',
'material',
);
generateDart(
cupertinoIcons,
path.join('tool', 'icon_generator', 'lib', 'cupertino.dart'),
'CupertinoIcons',
'cupertino',
);

// generate the icons using the flutter app
await generateIcons('tool/icon_generator');
await generateIcons(path.join('tool', 'icon_generator'));
}

Future<String> downloadUrl(String url) async {
Expand Down Expand Up @@ -121,8 +137,10 @@ final List<IconTuple> icons = [''');

Future<void> generateIcons(String appFolder) async {
final Process proc = await Process.start(
'flutter', <String>['run', '-d', 'flutter-tester'],
workingDirectory: appFolder);
flutterPath,
<String>['run', '-d', 'flutter-tester'],
workingDirectory: appFolder,
);
// Errors in the Flutter app will not set the exit code, so we need to
// watch stdout/stderr for errors.
bool hasError = false;
Expand Down

0 comments on commit 8c2163c

Please sign in to comment.