Skip to content

Commit

Permalink
[flutter_tools] Introducing arg option for specifying the output dire…
Browse files Browse the repository at this point in the history
…ctory for web (flutter#113076)
  • Loading branch information
eliasyishak committed Nov 1, 2022
1 parent 61deaef commit 17ec3b1
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 9 deletions.
10 changes: 3 additions & 7 deletions packages/flutter_tools/lib/src/commands/build_aar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class BuildAarCommand extends BuildSubCommand {
addTreeShakeIconsFlag();
usesFlavorOption();
usesBuildNumberOption();
usesOutputDir();
usesPubOption();
addSplitDebugInfoOption();
addDartObfuscationOption();
Expand All @@ -47,16 +48,11 @@ class BuildAarCommand extends BuildSubCommand {
addEnableExperimentation(hide: !verboseHelp);
addAndroidSpecificBuildOptions(hide: !verboseHelp);
argParser
..addMultiOption(
.addMultiOption(
'target-platform',
defaultsTo: <String>['android-arm', 'android-arm64', 'android-x64'],
allowed: <String>['android-arm', 'android-arm64', 'android-x86', 'android-x64'],
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".',
);
}

Expand Down Expand Up @@ -142,7 +138,7 @@ class BuildAarCommand extends BuildSubCommand {
project: _getProject(),
target: targetFile.path,
androidBuildInfo: androidBuildInfo,
outputDirectoryPath: stringArgDeprecated('output-dir'),
outputDirectoryPath: stringArg('output'),
buildNumber: buildNumber,
);
return FlutterCommandResult.success();
Expand Down
7 changes: 7 additions & 0 deletions packages/flutter_tools/lib/src/commands/build_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class BuildWebCommand extends BuildSubCommand {
}) : super(verboseHelp: verboseHelp) {
addTreeShakeIconsFlag(enabledByDefault: false);
usesTargetOption();
usesOutputDir();
usesPubOption();
usesBuildNumberOption();
usesBuildNameOption();
Expand Down Expand Up @@ -113,6 +114,11 @@ class BuildWebCommand extends BuildSubCommand {
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);
await buildWeb(
flutterProject,
Expand All @@ -124,6 +130,7 @@ class BuildWebCommand extends BuildSubCommand {
boolArgDeprecated('native-null-assertions'),
baseHref,
stringArgDeprecated('dart2js-optimization'),
outputDirectoryPath: outputDirectoryPath,
);
return FlutterCommandResult.success();
}
Expand Down
13 changes: 13 additions & 0 deletions packages/flutter_tools/lib/src/runner/flutter_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,19 @@ abstract class FlutterCommand extends Command<void> {
_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}) {
argParser.addFlag(
kEnableDevTools,
Expand Down
5 changes: 4 additions & 1 deletion packages/flutter_tools/lib/src/web/compile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ Future<void> buildWeb(
bool nativeNullAssertions,
String? baseHref,
String? dart2jsOptimization,
{String? outputDirectoryPath}
) async {
final bool hasWebPlugins = (await findPlugins(flutterProject))
.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);

// The migrators to apply to a Web project.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void main() {
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 CommandRunner<void> runner = createTestCommandRunner(buildCommand);
setupFileSystemForEndToEndTest(fileSystem);
Expand Down Expand Up @@ -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 {
expect(BuildWebCommand(verboseHelp: false).hidden, true);
}, overrides: <Type, Generator>{
Expand Down

0 comments on commit 17ec3b1

Please sign in to comment.