mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00

The Flutter tool's build system executes targets that take inputs and
produces outputs. Previously, targets would hardcode paths if they
needed to use Flutter project files as inputs/outputs. For example:
```dart
class FooTarget extends Target {
@override
List<Source> get inputs => <Source>[
Source.pattern('{PROJECT_DIR}/foo/bar'),
];
}
```
This is problematic as the
[`FlutterProject`](05b5e79105/packages/flutter_tools/lib/src/project.dart (L89)
)
is the source of truth for where a Flutter project's files are located:
1. If we change a project file's location, we need to update
`FlutterProject` as well as any hardcoded target inputs/outputs.
2. Project files' location can be dynamic. For example, a Flutter app
puts iOS files in the `ios/` directory, but a Flutter module puts iOS
files in the `.ios/` directory. Targets need to duplicate
`FlutterProject`'s logic to determine the project file's location.
As a result, hardcoding project file paths in targets can be
error-prone.
This introduces a new `Source` factory that lets you use the
`FlutterProject` to create the source:
```dart
class FooTarget extends Target {
@override
List<Source> get inputs => <Source>[
Source.fromProject((FlutterProject project) => project.fooFile),
];
}
```
Part of https://github.com/flutter/flutter/issues/163874
Next pull request: https://github.com/flutter/flutter/pull/165916
## 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 and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.
If you need help, consider asking for advice on the #hackers-new channel
on [Discord].
<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
346 lines
10 KiB
Dart
346 lines
10 KiB
Dart
// Copyright 2014 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 'package:file_testing/file_testing.dart';
|
|
import 'package:flutter_tools/src/artifacts.dart';
|
|
import 'package:flutter_tools/src/base/file_system.dart';
|
|
import 'package:flutter_tools/src/base/platform.dart';
|
|
import 'package:flutter_tools/src/build_info.dart';
|
|
import 'package:flutter_tools/src/build_system/build_system.dart';
|
|
import 'package:flutter_tools/src/build_system/exceptions.dart';
|
|
import 'package:flutter_tools/src/globals.dart' as globals;
|
|
import 'package:flutter_tools/src/project.dart';
|
|
|
|
import '../../src/common.dart';
|
|
import '../../src/fake_process_manager.dart';
|
|
import '../../src/testbed.dart';
|
|
|
|
final Platform windowsPlatform = FakePlatform(operatingSystem: 'windows');
|
|
|
|
void main() {
|
|
late Testbed testbed;
|
|
late SourceVisitor visitor;
|
|
late Environment environment;
|
|
|
|
setUp(() {
|
|
testbed = Testbed(
|
|
setup: () {
|
|
globals.fs.directory('cache').createSync();
|
|
final Directory outputs = globals.fs.directory('outputs')..createSync();
|
|
environment = Environment.test(
|
|
globals.fs.currentDirectory,
|
|
outputDir: outputs,
|
|
artifacts: globals.artifacts!, // using real artifacts
|
|
processManager: FakeProcessManager.any(),
|
|
fileSystem: globals.fs,
|
|
// engineVersion being null simulates a local engine.
|
|
logger: globals.logger,
|
|
);
|
|
visitor = SourceVisitor(environment);
|
|
environment.buildDir.createSync(recursive: true);
|
|
},
|
|
);
|
|
});
|
|
|
|
test(
|
|
'configures implicit vs explicit correctly',
|
|
() => testbed.run(() {
|
|
expect(const Source.pattern('{PROJECT_DIR}/foo').implicit, false);
|
|
expect(const Source.pattern('{PROJECT_DIR}/*foo').implicit, true);
|
|
}),
|
|
);
|
|
|
|
test(
|
|
'can substitute {PROJECT_DIR}/foo',
|
|
() => testbed.run(() {
|
|
globals.fs.file('foo').createSync();
|
|
const Source fooSource = Source.pattern('{PROJECT_DIR}/foo');
|
|
fooSource.accept(visitor);
|
|
|
|
expect(visitor.sources.single.path, globals.fs.path.absolute('foo'));
|
|
}),
|
|
);
|
|
|
|
test(
|
|
'can substitute {OUTPUT_DIR}/foo',
|
|
() => testbed.run(() {
|
|
globals.fs.file('foo').createSync();
|
|
const Source fooSource = Source.pattern('{OUTPUT_DIR}/foo');
|
|
fooSource.accept(visitor);
|
|
|
|
expect(
|
|
visitor.sources.single.path,
|
|
globals.fs.path.absolute(globals.fs.path.join('outputs', 'foo')),
|
|
);
|
|
}),
|
|
);
|
|
|
|
test(
|
|
'can substitute {BUILD_DIR}/bar',
|
|
() => testbed.run(() {
|
|
final String path = globals.fs.path.join(environment.buildDir.path, 'bar');
|
|
globals.fs.file(path).createSync();
|
|
const Source barSource = Source.pattern('{BUILD_DIR}/bar');
|
|
barSource.accept(visitor);
|
|
|
|
expect(visitor.sources.single.path, globals.fs.path.absolute(path));
|
|
}),
|
|
);
|
|
|
|
test(
|
|
'can substitute {FLUTTER_ROOT}/foo',
|
|
() => testbed.run(() {
|
|
final String path = globals.fs.path.join(environment.flutterRootDir.path, 'foo');
|
|
globals.fs.file(path).createSync();
|
|
const Source barSource = Source.pattern('{FLUTTER_ROOT}/foo');
|
|
barSource.accept(visitor);
|
|
|
|
expect(visitor.sources.single.path, globals.fs.path.absolute(path));
|
|
}),
|
|
);
|
|
|
|
test(
|
|
'can substitute Artifact',
|
|
() => testbed.run(() {
|
|
final String path = globals.fs.path.join(
|
|
globals.cache.getArtifactDirectory('engine').path,
|
|
'windows-x64',
|
|
'foo',
|
|
);
|
|
globals.fs.file(path).createSync(recursive: true);
|
|
const Source fizzSource = Source.artifact(
|
|
Artifact.windowsDesktopPath,
|
|
platform: TargetPlatform.windows_x64,
|
|
);
|
|
fizzSource.accept(visitor);
|
|
|
|
expect(visitor.sources.single.resolveSymbolicLinksSync(), globals.fs.path.absolute(path));
|
|
}),
|
|
);
|
|
|
|
test(
|
|
'can substitute {PROJECT_DIR}/*.fizz',
|
|
() => testbed.run(() {
|
|
const Source fizzSource = Source.pattern('{PROJECT_DIR}/*.fizz');
|
|
fizzSource.accept(visitor);
|
|
|
|
expect(visitor.sources, isEmpty);
|
|
|
|
globals.fs.file('foo.fizz').createSync();
|
|
globals.fs.file('foofizz').createSync();
|
|
|
|
fizzSource.accept(visitor);
|
|
|
|
expect(visitor.sources.single.path, globals.fs.path.absolute('foo.fizz'));
|
|
}),
|
|
);
|
|
|
|
test(
|
|
'can substitute {PROJECT_DIR}/fizz.*',
|
|
() => testbed.run(() {
|
|
const Source fizzSource = Source.pattern('{PROJECT_DIR}/fizz.*');
|
|
fizzSource.accept(visitor);
|
|
|
|
expect(visitor.sources, isEmpty);
|
|
|
|
globals.fs.file('fizz.foo').createSync();
|
|
globals.fs.file('fizz').createSync();
|
|
|
|
fizzSource.accept(visitor);
|
|
|
|
expect(visitor.sources.single.path, globals.fs.path.absolute('fizz.foo'));
|
|
}),
|
|
);
|
|
|
|
test(
|
|
'can substitute {PROJECT_DIR}/a*bc',
|
|
() => testbed.run(() {
|
|
const Source fizzSource = Source.pattern('{PROJECT_DIR}/bc*bc');
|
|
fizzSource.accept(visitor);
|
|
|
|
expect(visitor.sources, isEmpty);
|
|
|
|
globals.fs.file('bcbc').createSync();
|
|
globals.fs.file('bc').createSync();
|
|
|
|
fizzSource.accept(visitor);
|
|
|
|
expect(visitor.sources.single.path, globals.fs.path.absolute('bcbc'));
|
|
}),
|
|
);
|
|
|
|
test(
|
|
'crashes on bad substitute of two **',
|
|
() => testbed.run(() {
|
|
const Source fizzSource = Source.pattern('{PROJECT_DIR}/*.*bar');
|
|
|
|
globals.fs.file('abcd.bar').createSync();
|
|
|
|
expect(() => fizzSource.accept(visitor), throwsA(isA<InvalidPatternException>()));
|
|
}),
|
|
);
|
|
|
|
test(
|
|
"can't substitute foo",
|
|
() => testbed.run(() {
|
|
const Source invalidBase = Source.pattern('foo');
|
|
|
|
expect(() => invalidBase.accept(visitor), throwsA(isA<InvalidPatternException>()));
|
|
}),
|
|
);
|
|
|
|
test(
|
|
'can substitute optional files',
|
|
() => testbed.run(() {
|
|
const Source missingSource = Source.pattern('{PROJECT_DIR}/foo', optional: true);
|
|
|
|
expect(globals.fs.file('foo').existsSync(), false);
|
|
missingSource.accept(visitor);
|
|
expect(visitor.sources, isEmpty);
|
|
}),
|
|
);
|
|
|
|
test(
|
|
'can resolve a missing depfile',
|
|
() => testbed.run(() {
|
|
visitor.visitDepfile('foo.d');
|
|
|
|
expect(visitor.sources, isEmpty);
|
|
expect(visitor.containsNewDepfile, true);
|
|
}),
|
|
);
|
|
|
|
test(
|
|
'can resolve a populated depfile',
|
|
() => testbed.run(() {
|
|
environment.buildDir.childFile('foo.d').writeAsStringSync('a.dart : c.dart');
|
|
|
|
visitor.visitDepfile('foo.d');
|
|
expect(visitor.sources.single.path, 'c.dart');
|
|
expect(visitor.containsNewDepfile, false);
|
|
|
|
final SourceVisitor outputVisitor = SourceVisitor(environment, false);
|
|
outputVisitor.visitDepfile('foo.d');
|
|
|
|
expect(outputVisitor.sources.single.path, 'a.dart');
|
|
expect(outputVisitor.containsNewDepfile, false);
|
|
}),
|
|
);
|
|
|
|
test(
|
|
'does not crash on completely invalid depfile',
|
|
() => testbed.run(() {
|
|
environment.buildDir.childFile('foo.d').writeAsStringSync('hello, world');
|
|
|
|
visitor.visitDepfile('foo.d');
|
|
expect(visitor.sources, isEmpty);
|
|
expect(visitor.containsNewDepfile, false);
|
|
}),
|
|
);
|
|
|
|
test(
|
|
'can parse depfile with windows paths',
|
|
() => testbed.run(() {
|
|
environment.buildDir.childFile('foo.d').writeAsStringSync(r'a.dart: C:\\foo\\bar.txt');
|
|
|
|
visitor.visitDepfile('foo.d');
|
|
expect(visitor.sources.single.path, r'C:\foo\bar.txt');
|
|
expect(visitor.containsNewDepfile, false);
|
|
}, overrides: <Type, Generator>{Platform: () => windowsPlatform}),
|
|
);
|
|
|
|
test(
|
|
'can parse depfile with spaces in paths',
|
|
() => testbed.run(() {
|
|
environment.buildDir.childFile('foo.d').writeAsStringSync(r'a.dart: foo\ bar.txt');
|
|
|
|
visitor.visitDepfile('foo.d');
|
|
expect(visitor.sources.single.path, r'foo bar.txt');
|
|
expect(visitor.containsNewDepfile, false);
|
|
}),
|
|
);
|
|
|
|
test(
|
|
'Non-local engine builds use the engine.stamp file as an Artifact dependency',
|
|
() => testbed.run(() {
|
|
final Artifacts artifacts = Artifacts.test();
|
|
final Environment environment = Environment.test(
|
|
globals.fs.currentDirectory,
|
|
artifacts: artifacts,
|
|
processManager: FakeProcessManager.any(),
|
|
fileSystem: globals.fs,
|
|
logger: globals.logger,
|
|
engineVersion: 'abcdefghijklmon', // Use a versioned engine.
|
|
);
|
|
visitor = SourceVisitor(environment);
|
|
|
|
const Source fizzSource = Source.artifact(
|
|
Artifact.windowsDesktopPath,
|
|
platform: TargetPlatform.windows_x64,
|
|
);
|
|
fizzSource.accept(visitor);
|
|
|
|
expect(visitor.sources.single.path, contains('engine.stamp'));
|
|
}),
|
|
);
|
|
|
|
test(
|
|
'can substitute project file',
|
|
() => testbed.run(() {
|
|
final String path = globals.fs.file('.flutter-plugins-dependencies').absolute.path;
|
|
globals.fs.file(path).createSync(recursive: true);
|
|
final Source pluginsSource = Source.fromProject(
|
|
(FlutterProject project) => project.flutterPluginsDependenciesFile,
|
|
);
|
|
pluginsSource.accept(visitor);
|
|
|
|
expect(visitor.sources.single.absolute.path, path);
|
|
expect(visitor.sources.single, exists);
|
|
}),
|
|
);
|
|
|
|
test(
|
|
'can substitute nonexistent project file',
|
|
() => testbed.run(() {
|
|
final String path = globals.fs.file('.flutter-plugins-dependencies').absolute.path;
|
|
final Source pluginsSource = Source.fromProject(
|
|
(FlutterProject project) => project.flutterPluginsDependenciesFile,
|
|
);
|
|
pluginsSource.accept(visitor);
|
|
|
|
expect(visitor.sources.single.absolute.path, path);
|
|
expect(visitor.sources.single, isNot(exists));
|
|
}),
|
|
);
|
|
|
|
test(
|
|
'can substitute optional project file',
|
|
() => testbed.run(() {
|
|
final String path = globals.fs.file('.flutter-plugins-dependencies').absolute.path;
|
|
globals.fs.file(path).createSync(recursive: true);
|
|
final Source pluginsSource = Source.fromProject(
|
|
(FlutterProject project) => project.flutterPluginsDependenciesFile,
|
|
optional: true,
|
|
);
|
|
pluginsSource.accept(visitor);
|
|
|
|
expect(visitor.sources.single.absolute.path, path);
|
|
expect(visitor.sources.single, exists);
|
|
}),
|
|
);
|
|
|
|
test(
|
|
'skips nonexistent optional project file',
|
|
() => testbed.run(() {
|
|
final Source pluginsSource = Source.fromProject(
|
|
(FlutterProject project) => project.flutterPluginsDependenciesFile,
|
|
optional: true,
|
|
);
|
|
pluginsSource.accept(visitor);
|
|
|
|
expect(visitor.sources.isEmpty, isTrue);
|
|
}),
|
|
);
|
|
}
|