Skip to content

Commit 17ec3b1

Browse files
authored
[flutter_tools] Introducing arg option for specifying the output directory for web (#113076)
1 parent 61deaef commit 17ec3b1

File tree

5 files changed

+70
-9
lines changed

5 files changed

+70
-9
lines changed

packages/flutter_tools/lib/src/commands/build_aar.dart

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class BuildAarCommand extends BuildSubCommand {
3737
addTreeShakeIconsFlag();
3838
usesFlavorOption();
3939
usesBuildNumberOption();
40+
usesOutputDir();
4041
usesPubOption();
4142
addSplitDebugInfoOption();
4243
addDartObfuscationOption();
@@ -47,16 +48,11 @@ class BuildAarCommand extends BuildSubCommand {
4748
addEnableExperimentation(hide: !verboseHelp);
4849
addAndroidSpecificBuildOptions(hide: !verboseHelp);
4950
argParser
50-
..addMultiOption(
51+
.addMultiOption(
5152
'target-platform',
5253
defaultsTo: <String>['android-arm', 'android-arm64', 'android-x64'],
5354
allowed: <String>['android-arm', 'android-arm64', 'android-x86', 'android-x64'],
5455
help: 'The target platform for which the project is compiled.',
55-
)
56-
..addOption(
57-
'output-dir',
58-
help: 'The absolute path to the directory where the repository is generated. '
59-
'By default, this is "<current-directory>android/build".',
6056
);
6157
}
6258

@@ -142,7 +138,7 @@ class BuildAarCommand extends BuildSubCommand {
142138
project: _getProject(),
143139
target: targetFile.path,
144140
androidBuildInfo: androidBuildInfo,
145-
outputDirectoryPath: stringArgDeprecated('output-dir'),
141+
outputDirectoryPath: stringArg('output'),
146142
buildNumber: buildNumber,
147143
);
148144
return FlutterCommandResult.success();

packages/flutter_tools/lib/src/commands/build_web.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class BuildWebCommand extends BuildSubCommand {
1919
}) : super(verboseHelp: verboseHelp) {
2020
addTreeShakeIconsFlag(enabledByDefault: false);
2121
usesTargetOption();
22+
usesOutputDir();
2223
usesPubOption();
2324
usesBuildNumberOption();
2425
usesBuildNameOption();
@@ -113,6 +114,11 @@ class BuildWebCommand extends BuildSubCommand {
113114
r'Please add `<base href="$FLUTTER_BASE_HREF">` to web/index.html'
114115
);
115116
}
117+
118+
// Currently supporting options [output-dir] and [output] as
119+
// valid approaches for setting output directory of build artifacts
120+
final String? outputDirectoryPath = stringArg('output');
121+
116122
displayNullSafetyMode(buildInfo);
117123
await buildWeb(
118124
flutterProject,
@@ -124,6 +130,7 @@ class BuildWebCommand extends BuildSubCommand {
124130
boolArgDeprecated('native-null-assertions'),
125131
baseHref,
126132
stringArgDeprecated('dart2js-optimization'),
133+
outputDirectoryPath: outputDirectoryPath,
127134
);
128135
return FlutterCommandResult.success();
129136
}

packages/flutter_tools/lib/src/runner/flutter_command.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,19 @@ abstract class FlutterCommand extends Command<void> {
392392
_usesPortOption = true;
393393
}
394394

395+
/// Add option values for output directory of artifacts
396+
void usesOutputDir() {
397+
// TODO(eliasyishak): this feature has been added to [BuildWebCommand] and
398+
// [BuildAarCommand]
399+
argParser.addOption('output',
400+
abbr: 'o',
401+
aliases: <String>['output-dir'],
402+
help:
403+
'The absolute path to the directory where the repository is generated. '
404+
'By default, this is <current-directory>/build/<target-platform>.\n'
405+
'Currently supported for subcommands: aar, web.');
406+
}
407+
395408
void addDevToolsOptions({required bool verboseHelp}) {
396409
argParser.addFlag(
397410
kEnableDevTools,

packages/flutter_tools/lib/src/web/compile.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,13 @@ Future<void> buildWeb(
2828
bool nativeNullAssertions,
2929
String? baseHref,
3030
String? dart2jsOptimization,
31+
{String? outputDirectoryPath}
3132
) async {
3233
final bool hasWebPlugins = (await findPlugins(flutterProject))
3334
.any((Plugin p) => p.platforms.containsKey(WebPlugin.kConfigKey));
34-
final Directory outputDirectory = globals.fs.directory(getWebBuildDirectory());
35+
final Directory outputDirectory = outputDirectoryPath == null
36+
? globals.fs.directory(getWebBuildDirectory())
37+
: globals.fs.directory(outputDirectoryPath);
3538
outputDirectory.createSync(recursive: true);
3639

3740
// The migrators to apply to a Web project.

packages/flutter_tools/test/commands.shard/hermetic/build_web_test.dart

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void main() {
8484
ProcessManager: () => FakeProcessManager.any(),
8585
});
8686

87-
testUsingContext('Builds a web bundle - end to end', () async {
87+
testUsingContext('Setup for a web build with default output directory', () async {
8888
final BuildCommand buildCommand = BuildCommand();
8989
final CommandRunner<void> runner = createTestCommandRunner(buildCommand);
9090
setupFileSystemForEndToEndTest(fileSystem);
@@ -116,6 +116,48 @@ void main() {
116116
}),
117117
});
118118

119+
testUsingContext('Setup for a web build with a user specified output directory',
120+
() async {
121+
final BuildCommand buildCommand = BuildCommand();
122+
final CommandRunner<void> runner = createTestCommandRunner(buildCommand);
123+
124+
setupFileSystemForEndToEndTest(fileSystem);
125+
126+
const String newBuildDir = 'new_dir';
127+
final Directory buildDir = fileSystem.directory(fileSystem.path.join(newBuildDir));
128+
129+
expect(buildDir.existsSync(), false);
130+
131+
await runner.run(<String>[
132+
'build',
133+
'web',
134+
'--no-pub',
135+
'--output=$newBuildDir'
136+
]);
137+
138+
expect(buildDir.existsSync(), true);
139+
}, overrides: <Type, Generator>{
140+
Platform: () => fakePlatform,
141+
FileSystem: () => fileSystem,
142+
FeatureFlags: () => TestFeatureFlags(isWebEnabled: true),
143+
ProcessManager: () => FakeProcessManager.any(),
144+
BuildSystem: () => TestBuildSystem.all(BuildResult(success: true), (Target target, Environment environment) {
145+
expect(environment.defines, <String, String>{
146+
'TargetFile': 'lib/main.dart',
147+
'HasWebPlugins': 'true',
148+
'cspMode': 'false',
149+
'SourceMaps': 'false',
150+
'NativeNullAssertions': 'true',
151+
'ServiceWorkerStrategy': 'offline-first',
152+
'BuildMode': 'release',
153+
'DartDefines': 'RkxVVFRFUl9XRUJfQVVUT19ERVRFQ1Q9dHJ1ZQ==',
154+
'DartObfuscation': 'false',
155+
'TrackWidgetCreation': 'false',
156+
'TreeShakeIcons': 'false',
157+
});
158+
}),
159+
});
160+
119161
testUsingContext('hidden if feature flag is not enabled', () async {
120162
expect(BuildWebCommand(verboseHelp: false).hidden, true);
121163
}, overrides: <Type, Generator>{

0 commit comments

Comments
 (0)