Unverified Commit 17ec3b1d authored by Elias Yishak's avatar Elias Yishak Committed by GitHub

[flutter_tools] Introducing arg option for specifying the output directory for web (#113076)

parent 61deaef5
...@@ -37,6 +37,7 @@ class BuildAarCommand extends BuildSubCommand { ...@@ -37,6 +37,7 @@ class BuildAarCommand extends BuildSubCommand {
addTreeShakeIconsFlag(); addTreeShakeIconsFlag();
usesFlavorOption(); usesFlavorOption();
usesBuildNumberOption(); usesBuildNumberOption();
usesOutputDir();
usesPubOption(); usesPubOption();
addSplitDebugInfoOption(); addSplitDebugInfoOption();
addDartObfuscationOption(); addDartObfuscationOption();
...@@ -47,16 +48,11 @@ class BuildAarCommand extends BuildSubCommand { ...@@ -47,16 +48,11 @@ class BuildAarCommand extends BuildSubCommand {
addEnableExperimentation(hide: !verboseHelp); addEnableExperimentation(hide: !verboseHelp);
addAndroidSpecificBuildOptions(hide: !verboseHelp); addAndroidSpecificBuildOptions(hide: !verboseHelp);
argParser argParser
..addMultiOption( .addMultiOption(
'target-platform', 'target-platform',
defaultsTo: <String>['android-arm', 'android-arm64', 'android-x64'], defaultsTo: <String>['android-arm', 'android-arm64', 'android-x64'],
allowed: <String>['android-arm', 'android-arm64', 'android-x86', 'android-x64'], allowed: <String>['android-arm', 'android-arm64', 'android-x86', 'android-x64'],
help: 'The target platform for which the project is compiled.', help: 'The target platform for which the project is compiled.',
)
..addOption(
'output-dir',
help: 'The absolute path to the directory where the repository is generated. '
'By default, this is "<current-directory>android/build".',
); );
} }
...@@ -142,7 +138,7 @@ class BuildAarCommand extends BuildSubCommand { ...@@ -142,7 +138,7 @@ class BuildAarCommand extends BuildSubCommand {
project: _getProject(), project: _getProject(),
target: targetFile.path, target: targetFile.path,
androidBuildInfo: androidBuildInfo, androidBuildInfo: androidBuildInfo,
outputDirectoryPath: stringArgDeprecated('output-dir'), outputDirectoryPath: stringArg('output'),
buildNumber: buildNumber, buildNumber: buildNumber,
); );
return FlutterCommandResult.success(); return FlutterCommandResult.success();
......
...@@ -19,6 +19,7 @@ class BuildWebCommand extends BuildSubCommand { ...@@ -19,6 +19,7 @@ class BuildWebCommand extends BuildSubCommand {
}) : super(verboseHelp: verboseHelp) { }) : super(verboseHelp: verboseHelp) {
addTreeShakeIconsFlag(enabledByDefault: false); addTreeShakeIconsFlag(enabledByDefault: false);
usesTargetOption(); usesTargetOption();
usesOutputDir();
usesPubOption(); usesPubOption();
usesBuildNumberOption(); usesBuildNumberOption();
usesBuildNameOption(); usesBuildNameOption();
...@@ -113,6 +114,11 @@ class BuildWebCommand extends BuildSubCommand { ...@@ -113,6 +114,11 @@ class BuildWebCommand extends BuildSubCommand {
r'Please add `<base href="$FLUTTER_BASE_HREF">` to web/index.html' r'Please add `<base href="$FLUTTER_BASE_HREF">` to web/index.html'
); );
} }
// Currently supporting options [output-dir] and [output] as
// valid approaches for setting output directory of build artifacts
final String? outputDirectoryPath = stringArg('output');
displayNullSafetyMode(buildInfo); displayNullSafetyMode(buildInfo);
await buildWeb( await buildWeb(
flutterProject, flutterProject,
...@@ -124,6 +130,7 @@ class BuildWebCommand extends BuildSubCommand { ...@@ -124,6 +130,7 @@ class BuildWebCommand extends BuildSubCommand {
boolArgDeprecated('native-null-assertions'), boolArgDeprecated('native-null-assertions'),
baseHref, baseHref,
stringArgDeprecated('dart2js-optimization'), stringArgDeprecated('dart2js-optimization'),
outputDirectoryPath: outputDirectoryPath,
); );
return FlutterCommandResult.success(); return FlutterCommandResult.success();
} }
......
...@@ -392,6 +392,19 @@ abstract class FlutterCommand extends Command<void> { ...@@ -392,6 +392,19 @@ abstract class FlutterCommand extends Command<void> {
_usesPortOption = true; _usesPortOption = true;
} }
/// Add option values for output directory of artifacts
void usesOutputDir() {
// TODO(eliasyishak): this feature has been added to [BuildWebCommand] and
// [BuildAarCommand]
argParser.addOption('output',
abbr: 'o',
aliases: <String>['output-dir'],
help:
'The absolute path to the directory where the repository is generated. '
'By default, this is <current-directory>/build/<target-platform>.\n'
'Currently supported for subcommands: aar, web.');
}
void addDevToolsOptions({required bool verboseHelp}) { void addDevToolsOptions({required bool verboseHelp}) {
argParser.addFlag( argParser.addFlag(
kEnableDevTools, kEnableDevTools,
......
...@@ -28,10 +28,13 @@ Future<void> buildWeb( ...@@ -28,10 +28,13 @@ Future<void> buildWeb(
bool nativeNullAssertions, bool nativeNullAssertions,
String? baseHref, String? baseHref,
String? dart2jsOptimization, String? dart2jsOptimization,
{String? outputDirectoryPath}
) async { ) async {
final bool hasWebPlugins = (await findPlugins(flutterProject)) final bool hasWebPlugins = (await findPlugins(flutterProject))
.any((Plugin p) => p.platforms.containsKey(WebPlugin.kConfigKey)); .any((Plugin p) => p.platforms.containsKey(WebPlugin.kConfigKey));
final Directory outputDirectory = globals.fs.directory(getWebBuildDirectory()); final Directory outputDirectory = outputDirectoryPath == null
? globals.fs.directory(getWebBuildDirectory())
: globals.fs.directory(outputDirectoryPath);
outputDirectory.createSync(recursive: true); outputDirectory.createSync(recursive: true);
// The migrators to apply to a Web project. // The migrators to apply to a Web project.
......
...@@ -84,7 +84,7 @@ void main() { ...@@ -84,7 +84,7 @@ void main() {
ProcessManager: () => FakeProcessManager.any(), ProcessManager: () => FakeProcessManager.any(),
}); });
testUsingContext('Builds a web bundle - end to end', () async { testUsingContext('Setup for a web build with default output directory', () async {
final BuildCommand buildCommand = BuildCommand(); final BuildCommand buildCommand = BuildCommand();
final CommandRunner<void> runner = createTestCommandRunner(buildCommand); final CommandRunner<void> runner = createTestCommandRunner(buildCommand);
setupFileSystemForEndToEndTest(fileSystem); setupFileSystemForEndToEndTest(fileSystem);
...@@ -116,6 +116,48 @@ void main() { ...@@ -116,6 +116,48 @@ void main() {
}), }),
}); });
testUsingContext('Setup for a web build with a user specified output directory',
() async {
final BuildCommand buildCommand = BuildCommand();
final CommandRunner<void> runner = createTestCommandRunner(buildCommand);
setupFileSystemForEndToEndTest(fileSystem);
const String newBuildDir = 'new_dir';
final Directory buildDir = fileSystem.directory(fileSystem.path.join(newBuildDir));
expect(buildDir.existsSync(), false);
await runner.run(<String>[
'build',
'web',
'--no-pub',
'--output=$newBuildDir'
]);
expect(buildDir.existsSync(), true);
}, overrides: <Type, Generator>{
Platform: () => fakePlatform,
FileSystem: () => fileSystem,
FeatureFlags: () => TestFeatureFlags(isWebEnabled: true),
ProcessManager: () => FakeProcessManager.any(),
BuildSystem: () => TestBuildSystem.all(BuildResult(success: true), (Target target, Environment environment) {
expect(environment.defines, <String, String>{
'TargetFile': 'lib/main.dart',
'HasWebPlugins': 'true',
'cspMode': 'false',
'SourceMaps': 'false',
'NativeNullAssertions': 'true',
'ServiceWorkerStrategy': 'offline-first',
'BuildMode': 'release',
'DartDefines': 'RkxVVFRFUl9XRUJfQVVUT19ERVRFQ1Q9dHJ1ZQ==',
'DartObfuscation': 'false',
'TrackWidgetCreation': 'false',
'TreeShakeIcons': 'false',
});
}),
});
testUsingContext('hidden if feature flag is not enabled', () async { testUsingContext('hidden if feature flag is not enabled', () async {
expect(BuildWebCommand(verboseHelp: false).hidden, true); expect(BuildWebCommand(verboseHelp: false).hidden, true);
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment