Unverified Commit 78ae72b0 authored by Janko Djuric's avatar Janko Djuric Committed by GitHub

[flutter_tools] Add --build-number and --build-name support to web and linux (#100377)

parent 391a39a6
...@@ -237,6 +237,10 @@ class BuildInfo { ...@@ -237,6 +237,10 @@ class BuildInfo {
kFileSystemRoots: fileSystemRoots.join(','), kFileSystemRoots: fileSystemRoots.join(','),
if (fileSystemScheme != null) if (fileSystemScheme != null)
kFileSystemScheme: fileSystemScheme!, kFileSystemScheme: fileSystemScheme!,
if (buildName != null)
kBuildName: buildName!,
if (buildNumber != null)
kBuildNumber: buildNumber!,
}; };
} }
...@@ -952,6 +956,12 @@ const String kIconTreeShakerFlag = 'TreeShakeIcons'; ...@@ -952,6 +956,12 @@ const String kIconTreeShakerFlag = 'TreeShakeIcons';
/// The input key for an SkSL bundle path. /// The input key for an SkSL bundle path.
const String kBundleSkSLPath = 'BundleSkSLPath'; const String kBundleSkSLPath = 'BundleSkSLPath';
/// The define to pass build name
const String kBuildName = 'BuildName';
/// The define to pass build number
const String kBuildNumber = 'BuildNumber';
final Converter<String, String> _defineEncoder = utf8.encoder.fuse(base64.encoder); final Converter<String, String> _defineEncoder = utf8.encoder.fuse(base64.encoder);
final Converter<String, String> _defineDecoder = base64.decoder.fuse(utf8.decoder); final Converter<String, String> _defineDecoder = base64.decoder.fuse(utf8.decoder);
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
import '../../artifacts.dart'; import '../../artifacts.dart';
import '../../base/file_system.dart'; import '../../base/file_system.dart';
import '../../build_info.dart'; import '../../build_info.dart';
import '../../convert.dart';
import '../../devfs.dart'; import '../../devfs.dart';
import '../../project.dart'; import '../../project.dart';
import '../build_system.dart'; import '../build_system.dart';
...@@ -135,7 +136,7 @@ abstract class BundleLinuxAssets extends Target { ...@@ -135,7 +136,7 @@ abstract class BundleLinuxAssets extends Target {
environment.buildDir.childFile('app.dill') environment.buildDir.childFile('app.dill')
.copySync(outputDirectory.childFile('kernel_blob.bin').path); .copySync(outputDirectory.childFile('kernel_blob.bin').path);
} }
final String versionInfo = FlutterProject.current().getVersionInfo(); final String versionInfo = getVersionInfo(environment.defines);
final Depfile depfile = await copyAssets( final Depfile depfile = await copyAssets(
environment, environment,
outputDirectory, outputDirectory,
...@@ -153,6 +154,23 @@ abstract class BundleLinuxAssets extends Target { ...@@ -153,6 +154,23 @@ abstract class BundleLinuxAssets extends Target {
environment.buildDir.childFile('flutter_assets.d'), environment.buildDir.childFile('flutter_assets.d'),
); );
} }
/// Return json encoded string that contains data about version for package_info
String getVersionInfo(Map<String, String> defines) {
final Map<String, dynamic> versionInfo =
jsonDecode(FlutterProject.current().getVersionInfo())
as Map<String, dynamic>;
if (defines.containsKey(kBuildNumber)) {
versionInfo['build_number'] = defines[kBuildNumber];
}
if (defines.containsKey(kBuildName)) {
versionInfo['version'] = defines[kBuildName];
}
return jsonEncode(versionInfo);
}
} }
/// A wrapper for AOT compilation that copies app.so into the output directory. /// A wrapper for AOT compilation that copies app.so into the output directory.
......
...@@ -341,10 +341,7 @@ class WebReleaseBundle extends Target { ...@@ -341,10 +341,7 @@ class WebReleaseBundle extends Target {
); );
} }
final String versionInfo = FlutterProject.current().getVersionInfo(); createVersionFile(environment, environment.defines);
environment.outputDir
.childFile('version.json')
.writeAsStringSync(versionInfo);
final Directory outputDirectory = environment.outputDir.childDirectory('assets'); final Directory outputDirectory = environment.outputDir.childDirectory('assets');
outputDirectory.createSync(recursive: true); outputDirectory.createSync(recursive: true);
final Depfile depfile = await copyAssets( final Depfile depfile = await copyAssets(
...@@ -411,6 +408,25 @@ class WebReleaseBundle extends Target { ...@@ -411,6 +408,25 @@ class WebReleaseBundle extends Target {
environment.buildDir.childFile('web_resources.d'), environment.buildDir.childFile('web_resources.d'),
); );
} }
/// Create version.json file that contains data about version for package_info
void createVersionFile(Environment environment, Map<String, String> defines) {
final Map<String, dynamic> versionInfo =
jsonDecode(FlutterProject.current().getVersionInfo())
as Map<String, dynamic>;
if (defines.containsKey(kBuildNumber)) {
versionInfo['build_number'] = defines[kBuildNumber];
}
if (defines.containsKey(kBuildName)) {
versionInfo['version'] = defines[kBuildName];
}
environment.outputDir
.childFile('version.json')
.writeAsStringSync(jsonEncode(versionInfo));
}
} }
/// Static assets provided by the Flutter SDK that do not change, such as /// Static assets provided by the Flutter SDK that do not change, such as
......
...@@ -22,6 +22,8 @@ class BuildLinuxCommand extends BuildSubCommand { ...@@ -22,6 +22,8 @@ class BuildLinuxCommand extends BuildSubCommand {
}) : _operatingSystemUtils = operatingSystemUtils, }) : _operatingSystemUtils = operatingSystemUtils,
super(verboseHelp: verboseHelp) { super(verboseHelp: verboseHelp) {
addCommonDesktopBuildOptions(verboseHelp: verboseHelp); addCommonDesktopBuildOptions(verboseHelp: verboseHelp);
usesBuildNumberOption();
usesBuildNameOption();
final String defaultTargetPlatform = final String defaultTargetPlatform =
(_operatingSystemUtils.hostPlatform == HostPlatform.linux_arm64) ? (_operatingSystemUtils.hostPlatform == HostPlatform.linux_arm64) ?
'linux-arm64' : 'linux-x64'; 'linux-arm64' : 'linux-x64';
......
...@@ -20,6 +20,8 @@ class BuildWebCommand extends BuildSubCommand { ...@@ -20,6 +20,8 @@ class BuildWebCommand extends BuildSubCommand {
addTreeShakeIconsFlag(enabledByDefault: false); addTreeShakeIconsFlag(enabledByDefault: false);
usesTargetOption(); usesTargetOption();
usesPubOption(); usesPubOption();
usesBuildNumberOption();
usesBuildNameOption();
addBuildModeFlags(verboseHelp: verboseHelp, excludeDebug: true); addBuildModeFlags(verboseHelp: verboseHelp, excludeDebug: true);
usesDartDefineOption(); usesDartDefineOption();
usesWebRendererOption(); usesWebRendererOption();
......
...@@ -148,6 +148,31 @@ void main() { ...@@ -148,6 +148,31 @@ void main() {
ProcessManager: () => FakeProcessManager.any(), ProcessManager: () => FakeProcessManager.any(),
BuildSystem: () => TestBuildSystem.all(BuildResult(success: true)), BuildSystem: () => TestBuildSystem.all(BuildResult(success: true)),
}); });
testUsingContext('Web build supports build-name and build-number', () async {
final TestWebBuildCommand buildCommand = TestWebBuildCommand();
final CommandRunner<void> runner = createTestCommandRunner(buildCommand);
setupFileSystemForEndToEndTest(fileSystem);
await runner.run(<String>[
'build',
'web',
'--no-pub',
'--build-name=1.2.3',
'--build-number=42',
]);
final BuildInfo buildInfo = await buildCommand.webCommand
.getBuildInfo(forcedBuildMode: BuildMode.debug);
expect(buildInfo.buildNumber, '42');
expect(buildInfo.buildName, '1.2.3');
}, overrides: <Type, Generator>{
Platform: () => fakePlatform,
FileSystem: () => fileSystem,
FeatureFlags: () => TestFeatureFlags(isWebEnabled: true),
ProcessManager: () => FakeProcessManager.any(),
BuildSystem: () => TestBuildSystem.all(BuildResult(success: true)),
});
} }
void setupFileSystemForEndToEndTest(FileSystem fileSystem) { void setupFileSystemForEndToEndTest(FileSystem fileSystem) {
......
...@@ -121,6 +121,8 @@ void main() { ...@@ -121,6 +121,8 @@ void main() {
codeSizeDirectory: 'foo/code-size', codeSizeDirectory: 'foo/code-size',
fileSystemRoots: <String>['test5', 'test6'], fileSystemRoots: <String>['test5', 'test6'],
fileSystemScheme: 'scheme', fileSystemScheme: 'scheme',
buildName: '122',
buildNumber: '22'
); );
expect(buildInfo.toBuildSystemEnvironment(), <String, String>{ expect(buildInfo.toBuildSystemEnvironment(), <String, String>{
...@@ -136,6 +138,8 @@ void main() { ...@@ -136,6 +138,8 @@ void main() {
'CodeSizeDirectory': 'foo/code-size', 'CodeSizeDirectory': 'foo/code-size',
'FileSystemRoots': 'test5,test6', 'FileSystemRoots': 'test5,test6',
'FileSystemScheme': 'scheme', 'FileSystemScheme': 'scheme',
'BuildName': '122',
'BuildNumber': '22',
}); });
}); });
......
...@@ -100,6 +100,8 @@ void main() { ...@@ -100,6 +100,8 @@ void main() {
fileSystem.currentDirectory, fileSystem.currentDirectory,
defines: <String, String>{ defines: <String, String>{
kBuildMode: 'debug', kBuildMode: 'debug',
kBuildName: '2.0.0',
kBuildNumber: '22',
}, },
inputs: <String, String>{ inputs: <String, String>{
kBundleSkSLPath: 'bundle.sksl', kBundleSkSLPath: 'bundle.sksl',
...@@ -133,6 +135,9 @@ void main() { ...@@ -133,6 +135,9 @@ void main() {
expect(output.childFile('kernel_blob.bin'), exists); expect(output.childFile('kernel_blob.bin'), exists);
expect(output.childFile('AssetManifest.json'), exists); expect(output.childFile('AssetManifest.json'), exists);
expect(output.childFile('version.json'), exists); expect(output.childFile('version.json'), exists);
final String versionFile = output.childFile('version.json').readAsStringSync();
expect(versionFile, contains('"version":"2.0.0"'));
expect(versionFile, contains('"build_number":"22"'));
// SkSL // SkSL
expect(output.childFile('io.flutter.shaders.json'), exists); expect(output.childFile('io.flutter.shaders.json'), exists);
expect(output.childFile('io.flutter.shaders.json').readAsStringSync(), '{"data":{"A":"B"}}'); expect(output.childFile('io.flutter.shaders.json').readAsStringSync(), '{"data":{"A":"B"}}');
......
...@@ -102,6 +102,22 @@ void main() { ...@@ -102,6 +102,22 @@ void main() {
expect(environment.outputDir.childFile('version.json'), exists); expect(environment.outputDir.childFile('version.json'), exists);
})); }));
test('override version values', () => testbed.run(() async {
environment.defines[kBuildMode] = 'release';
environment.defines[kBuildName] = '2.0.0';
environment.defines[kBuildNumber] = '22';
final Directory webResources = environment.projectDir.childDirectory('web');
webResources.childFile('index.html').createSync(recursive: true);
environment.buildDir.childFile('main.dart.js').createSync();
await const WebReleaseBundle().build(environment);
final String versionFile = environment.outputDir
.childFile('version.json')
.readAsStringSync();
expect(versionFile, contains('"version":"2.0.0"'));
expect(versionFile, contains('"build_number":"22"'));
}));
test('Base href is created in index.html with given base-href after release build', () => testbed.run(() async { test('Base href is created in index.html with given base-href after release build', () => testbed.run(() async {
environment.defines[kBuildMode] = 'release'; environment.defines[kBuildMode] = 'release';
environment.defines[kBaseHref] = '/basehreftest/'; environment.defines[kBaseHref] = '/basehreftest/';
......
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