Unverified Commit 3dde3e03 authored by Balvinder Singh Gambhir's avatar Balvinder Singh Gambhir Committed by GitHub

[flutter_tools] generates version.json for web using flutter tool (#64644)

Generates version.json in web directory of project. version.json can be used in the future version of package_info which will support web. version.json has the following keys : app_name, version and build_number. This file is generated every time a user runs or builds the project.
Co-authored-by: 's avatarJonah Williams <jonahwilliams@google.com>
parent e3c6979d
......@@ -30,6 +30,7 @@ import '../convert.dart';
import '../dart/package_map.dart';
import '../devfs.dart';
import '../globals.dart' as globals;
import '../project.dart';
import '../web/bootstrap.dart';
import '../web/chrome.dart';
......@@ -788,6 +789,7 @@ class WebDevFS implements DevFS {
webAssetServer.writeBytes('stack_trace_mapper.js', stackTraceMapper.readAsBytesSync());
webAssetServer.writeFile('manifest.json', '{"info":"manifest not generated in run mode."}');
webAssetServer.writeFile('flutter_service_worker.js', '// Service worker not loaded in run mode.');
webAssetServer.writeFile('version.json', FlutterProject.current().getVersionInfo());
webAssetServer.writeFile(
'main.dart.js',
generateBootstrapScript(
......
......@@ -14,6 +14,7 @@ import '../../base/io.dart';
import '../../build_info.dart';
import '../../dart/package_map.dart';
import '../../globals.dart' as globals;
import '../../project.dart';
import '../build_system.dart';
import '../depfile.dart';
import 'assets.dart';
......@@ -295,6 +296,11 @@ class WebReleaseBundle extends Target {
environment.outputDir.childFile(globals.fs.path.basename(outputFile.path)).path
);
}
final String versionInfo = FlutterProject.current().getVersionInfo();
environment.outputDir
.childFile('version.json')
.writeAsStringSync(versionInfo);
final Directory outputDirectory = environment.outputDir.childDirectory('assets');
outputDirectory.createSync(recursive: true);
final Depfile depfile = await copyAssets(
......
......@@ -8,6 +8,7 @@ import 'package:meta/meta.dart';
import 'package:xml/xml.dart';
import 'package:yaml/yaml.dart';
import '../src/convert.dart';
import 'android/gradle_utils.dart' as gradle;
import 'artifacts.dart';
import 'base/common.dart';
......@@ -263,6 +264,16 @@ class FlutterProject {
}
await injectPlugins(this, checkProjects: checkProjects);
}
/// Returns a json encoded string containing the [appName], [version], and [buildNumber] that is used to generate version.json
String getVersionInfo() {
final Map<String, String> versionFileJson = <String, String>{
'app_name': manifest.appName,
'version': manifest.buildName,
'build_number': manifest.buildNumber
};
return jsonEncode(versionFileJson);
}
}
/// Base class for projects per platform.
......
......@@ -92,6 +92,17 @@ void main() {
expect(generated, contains("import 'package:foo/main.dart' as entrypoint;"));
}));
test('version.json is created after release build', () => testbed.run(() async {
environment.defines[kBuildMode] = 'release';
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);
expect(environment.outputDir.childFile('version.json'), exists);
}));
test('WebReleaseBundle copies dart2js output and resource files to output directory', () => testbed.run(() async {
environment.defines[kBuildMode] = 'release';
final Directory webResources = environment.projectDir.childDirectory('web');
......
......@@ -11,6 +11,7 @@ import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/build_info.dart';
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/convert.dart';
import 'package:flutter_tools/src/features.dart';
import 'package:flutter_tools/src/flutter_manifest.dart';
import 'package:flutter_tools/src/ios/plist_parser.dart';
......@@ -241,6 +242,19 @@ void main() {
expectExists(pluginRegistrantClasses.childFile('GeneratedPluginRegistrant.h'));
expectExists(pluginRegistrantClasses.childFile('GeneratedPluginRegistrant.m'));
});
testUsingContext('Version.json info is correct', (){
final MemoryFileSystem fileSystem = MemoryFileSystem.test();
final FlutterManifest manifest = FlutterManifest.createFromString('''
name: test
version: 1.0.0+3
''', logger: BufferLogger.test());
final FlutterProject project = FlutterProject(fileSystem.systemTempDirectory,manifest,manifest);
final dynamic versionInfo = jsonDecode(project.getVersionInfo());
expect(versionInfo['app_name'],'test');
expect(versionInfo['version'],'1.0.0');
expect(versionInfo['build_number'],'3');
});
});
group('module status', () {
......
......@@ -520,6 +520,7 @@ void main() {
expect(webDevFS.webAssetServer.getFile('main.dart'), isNotNull);
expect(webDevFS.webAssetServer.getFile('manifest.json'), isNotNull);
expect(webDevFS.webAssetServer.getFile('flutter_service_worker.js'), isNotNull);
expect(webDevFS.webAssetServer.getFile('version.json'),isNotNull);
expect(await webDevFS.webAssetServer.dartSourceContents('dart_sdk.js'), 'HELLO');
expect(await webDevFS.webAssetServer.dartSourceContents('dart_sdk.js.map'), 'THERE');
......@@ -633,6 +634,7 @@ void main() {
expect(webDevFS.webAssetServer.getFile('main.dart'), isNotNull);
expect(webDevFS.webAssetServer.getFile('manifest.json'), isNotNull);
expect(webDevFS.webAssetServer.getFile('flutter_service_worker.js'), isNotNull);
expect(webDevFS.webAssetServer.getFile('version.json'), isNotNull);
expect(await webDevFS.webAssetServer.dartSourceContents('dart_sdk.js'), 'HELLO');
expect(await webDevFS.webAssetServer.dartSourceContents('dart_sdk.js.map'), 'THERE');
......
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