This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Add a build command to felt #12303
Merged
Merged
Add a build command to felt #12303
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| // Copyright 2013 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:async'; | ||
|
|
||
| import 'package:args/command_runner.dart'; | ||
| import 'package:meta/meta.dart'; | ||
| import 'package:path/path.dart' as path; | ||
| import 'package:watcher/watcher.dart'; | ||
|
|
||
| import 'environment.dart'; | ||
| import 'utils.dart'; | ||
|
|
||
| class BuildCommand extends Command<bool> { | ||
| BuildCommand() { | ||
| argParser | ||
| ..addFlag( | ||
| 'watch', | ||
| abbr: 'w', | ||
| help: 'Run the build in watch mode so it rebuilds whenever a change' | ||
| 'is made.', | ||
| ); | ||
| } | ||
|
|
||
| @override | ||
| String get name => 'build'; | ||
|
|
||
| @override | ||
| String get description => 'Build the Flutter web engine.'; | ||
|
|
||
| bool get isWatchMode => argResults['watch']; | ||
|
|
||
| @override | ||
| FutureOr<bool> run() async { | ||
| final FilePath libPath = FilePath.fromWebUi('lib'); | ||
| final Pipeline buildPipeline = Pipeline(steps: <PipelineStep>[ | ||
| gn, | ||
| ninja, | ||
| ]); | ||
| await buildPipeline.start(); | ||
|
|
||
| if (isWatchMode) { | ||
| print('Initial build done!'); | ||
| print('Watching directory: ${libPath.relativeToCwd}/'); | ||
| PipelineWatcher( | ||
| dir: libPath.absolute, | ||
| pipeline: buildPipeline, | ||
| ).start(); | ||
| // Return a never-ending future. | ||
| return Completer<bool>().future; | ||
| } else { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Future<void> gn() { | ||
| print('Running gn...'); | ||
| return runProcess( | ||
| path.join(environment.flutterDirectory.path, 'tools', 'gn'), | ||
| <String>[ | ||
| '--unopt', | ||
| '--full-dart-sdk', | ||
| ], | ||
| ); | ||
| } | ||
|
|
||
| // TODO(mdebbar): Make the ninja step interruptable in the pipeline. | ||
| Future<void> ninja() { | ||
| print('Running ninja...'); | ||
| return runProcess('ninja', <String>[ | ||
| '-C', | ||
| environment.hostDebugUnoptDir.path, | ||
| ]); | ||
| } | ||
|
|
||
| enum PipelineStatus { | ||
| idle, | ||
| started, | ||
| stopping, | ||
| stopped, | ||
| error, | ||
| done, | ||
| } | ||
|
|
||
| typedef PipelineStep = Future<void> Function(); | ||
|
|
||
| class Pipeline { | ||
| Pipeline({@required this.steps}); | ||
|
|
||
| final Iterable<PipelineStep> steps; | ||
|
|
||
| Future<dynamic> _currentStepFuture; | ||
|
|
||
| PipelineStatus status = PipelineStatus.idle; | ||
|
|
||
| Future<void> start() async { | ||
| status = PipelineStatus.started; | ||
| try { | ||
| for (PipelineStep step in steps) { | ||
| if (status != PipelineStatus.started) { | ||
| break; | ||
| } | ||
| _currentStepFuture = step(); | ||
| await _currentStepFuture; | ||
| } | ||
| status = PipelineStatus.done; | ||
| } catch (_) { | ||
| status = PipelineStatus.error; | ||
| } finally { | ||
| _currentStepFuture = null; | ||
| } | ||
| } | ||
|
|
||
| Future<void> stop() { | ||
| status = PipelineStatus.stopping; | ||
| return (_currentStepFuture ?? Future<void>.value(null)).then((_) { | ||
| status = PipelineStatus.stopped; | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| class PipelineWatcher { | ||
| PipelineWatcher({ | ||
| @required this.dir, | ||
| @required this.pipeline, | ||
| }) : watcher = DirectoryWatcher(dir); | ||
|
|
||
| /// The path of the directory to watch for changes. | ||
| final String dir; | ||
|
|
||
| /// The pipeline to be executed when an event is fired by the watcher. | ||
| final Pipeline pipeline; | ||
|
|
||
| /// Used to watch a directory for any file system changes. | ||
| final DirectoryWatcher watcher; | ||
|
|
||
| void start() { | ||
| watcher.events.listen(_onEvent); | ||
| } | ||
|
|
||
| int _pipelineRunCount = 0; | ||
| Timer _scheduledPipeline; | ||
|
|
||
| void _onEvent(WatchEvent event) { | ||
| final String relativePath = path.relative(event.path, from: dir); | ||
| print('- [${event.type}] ${relativePath}'); | ||
|
|
||
| _pipelineRunCount++; | ||
| _scheduledPipeline?.cancel(); | ||
| _scheduledPipeline = Timer(const Duration(milliseconds: 100), () { | ||
| _scheduledPipeline = null; | ||
| _runPipeline(); | ||
| }); | ||
| } | ||
|
|
||
| void _runPipeline() { | ||
| int runCount; | ||
| switch (pipeline.status) { | ||
| case PipelineStatus.started: | ||
| pipeline.stop().then((_) { | ||
| runCount = _pipelineRunCount; | ||
| pipeline.start().then((_) => _pipelineDone(runCount)); | ||
| }); | ||
| break; | ||
|
|
||
| case PipelineStatus.stopping: | ||
| // We are already trying to stop the pipeline. No need to do anything. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What will cause the pipeline to restart?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The previous switch case does that. |
||
| break; | ||
|
|
||
| default: | ||
| runCount = _pipelineRunCount; | ||
| pipeline.start().then((_) => _pipelineDone(runCount)); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| void _pipelineDone(int pipelineRunCount) { | ||
| if (pipelineRunCount == _pipelineRunCount) { | ||
| print('*** Done! ***'); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: "a change is made" => "a file is saved"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"file is saved" isn't technically correct. A file could be removed and that would also trigger a rebuild.