forked from firka/flutter
add version to pubspec.yaml (#16857)
Uses the `version` property from the `pubspec.yaml` file to set the corresponding fields in the `local.properties` file respectively in the `Generated.xcconfig` file. The `--build-name` and `--build-number` options have changed. Now they trump the `version` property from the `pubspec.yaml` file. If the `version` property is not set and the `--build-name` and `--build-number` options are not provided, the build command will not change the `local.properties` / `Generated.xcconfig` file.
This commit is contained in:
committed by
Todd Volkert
parent
f986048094
commit
c65e9d19a4
@@ -3,9 +3,6 @@
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
import '../android/android_sdk.dart';
|
||||
import '../artifacts.dart';
|
||||
@@ -17,7 +14,9 @@ import '../base/platform.dart';
|
||||
import '../base/process.dart';
|
||||
import '../base/utils.dart';
|
||||
import '../build_info.dart';
|
||||
import '../bundle.dart' as bundle;
|
||||
import '../cache.dart';
|
||||
import '../flutter_manifest.dart';
|
||||
import '../globals.dart';
|
||||
import 'android_sdk.dart';
|
||||
import 'android_studio.dart';
|
||||
@@ -95,7 +94,7 @@ Future<GradleProject> _gradleProject() async {
|
||||
// of calculating the app properties using Gradle. This may take minutes.
|
||||
Future<GradleProject> _readGradleProject() async {
|
||||
final String gradle = await _ensureGradle();
|
||||
updateLocalProperties();
|
||||
await updateLocalProperties();
|
||||
try {
|
||||
final Status status = logger.startProgress('Resolving dependencies...', expectSlowOperation: true);
|
||||
final RunResult runResult = await runCheckedAsync(
|
||||
@@ -198,10 +197,13 @@ distributionUrl=https\\://services.gradle.org/distributions/gradle-$gradleVersio
|
||||
}
|
||||
|
||||
/// Create android/local.properties if needed, and update Flutter settings.
|
||||
void updateLocalProperties({String projectPath, BuildInfo buildInfo}) {
|
||||
Future<void> updateLocalProperties({String projectPath, BuildInfo buildInfo}) async {
|
||||
final File localProperties = (projectPath == null)
|
||||
? fs.file(fs.path.join('android', 'local.properties'))
|
||||
: fs.file(fs.path.join(projectPath, 'android', 'local.properties'));
|
||||
final String flutterManifest = (projectPath == null)
|
||||
? fs.path.join(bundle.defaultManifestPath)
|
||||
: fs.path.join(projectPath, bundle.defaultManifestPath);
|
||||
bool changed = false;
|
||||
|
||||
SettingsFile settings;
|
||||
@@ -225,85 +227,39 @@ void updateLocalProperties({String projectPath, BuildInfo buildInfo}) {
|
||||
changed = true;
|
||||
}
|
||||
|
||||
FlutterManifest manifest;
|
||||
try {
|
||||
manifest = await FlutterManifest.createFromPath(flutterManifest);
|
||||
} catch (error) {
|
||||
throwToolExit('Failed to load pubspec.yaml: $error');
|
||||
}
|
||||
|
||||
final String buildName = buildInfo?.buildName ?? manifest.buildName;
|
||||
if (buildName != null) {
|
||||
settings.values['flutter.versionName'] = buildName;
|
||||
changed = true;
|
||||
}
|
||||
|
||||
final int buildNumber = buildInfo?.buildNumber ?? manifest.buildNumber;
|
||||
if (buildNumber != null) {
|
||||
settings.values['flutter.versionCode'] = '$buildNumber';
|
||||
changed = true;
|
||||
}
|
||||
|
||||
if (changed)
|
||||
settings.writeContents(localProperties);
|
||||
}
|
||||
|
||||
Future<Null> findAndReplaceVersionProperties({@required BuildInfo buildInfo}) {
|
||||
assert(buildInfo != null, 'buildInfo can\'t be null');
|
||||
final Completer<Null> completer = new Completer<Null>();
|
||||
|
||||
// early return, if nothing has to be changed
|
||||
if (buildInfo.buildNumber == null && buildInfo.buildName == null) {
|
||||
completer.complete();
|
||||
return completer.future;
|
||||
}
|
||||
|
||||
final File appGradle = fs.file(fs.path.join('android', 'app', 'build.gradle'));
|
||||
final File appGradleTmp = fs.file(fs.path.join('android', 'app', 'build.gradle.tmp'));
|
||||
appGradleTmp.createSync();
|
||||
|
||||
if (appGradle.existsSync() && appGradleTmp.existsSync()) {
|
||||
final Stream<List<int>> inputStream = appGradle.openRead();
|
||||
final IOSink sink = appGradleTmp.openWrite();
|
||||
|
||||
inputStream.transform(utf8.decoder)
|
||||
.transform(const LineSplitter())
|
||||
.map((String line) {
|
||||
|
||||
// find and replace build number
|
||||
if (buildInfo.buildNumber != null) {
|
||||
if (line.contains(new RegExp(r'^[ |\t]*(versionCode)[ =\t]*\d*'))) {
|
||||
return line.splitMapJoin(new RegExp(r'(versionCode)[ =\t]*\d*'), onMatch: (Match m) {
|
||||
return 'versionCode ${buildInfo.buildNumber}';
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// find and replace build name
|
||||
if (buildInfo.buildName != null) {
|
||||
if (line.contains(new RegExp(r'^[ |\t]*(versionName)[ =\t]*\"[0-9.]*"'))) {
|
||||
return line.splitMapJoin(new RegExp(r'(versionName)[ =\t]*\"[0-9.]*"'), onMatch: (Match m) {
|
||||
return 'versionName "${buildInfo.buildName}"';
|
||||
});
|
||||
}
|
||||
}
|
||||
return line;
|
||||
})
|
||||
.listen((String line) {
|
||||
sink.writeln(line);
|
||||
},
|
||||
onDone: () {
|
||||
sink.close();
|
||||
try {
|
||||
final File gradleOld = appGradle.renameSync(fs.path.join('android', 'app', 'build.gradle.old'));
|
||||
appGradleTmp.renameSync(fs.path.join('android', 'app', 'build.gradle'));
|
||||
gradleOld.deleteSync();
|
||||
completer.complete();
|
||||
} catch (error) {
|
||||
printError('Failed to change version properties. $error');
|
||||
completer.completeError('Failed to change version properties. $error');
|
||||
}
|
||||
},
|
||||
onError: (dynamic error, StackTrace stack) {
|
||||
printError('Failed to change version properties. ${error.toString()}');
|
||||
sink.close();
|
||||
appGradleTmp.deleteSync();
|
||||
completer.completeError('Failed to change version properties. ${error.toString()}', stack);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
return completer.future;
|
||||
}
|
||||
|
||||
Future<Null> buildGradleProject(BuildInfo buildInfo, String target) async {
|
||||
// Update the local.properties file with the build mode.
|
||||
// Update the local.properties file with the build mode, version name and code.
|
||||
// FlutterPlugin v1 reads local.properties to determine build mode. Plugin v2
|
||||
// uses the standard Android way to determine what to build, but we still
|
||||
// update local.properties, in case we want to use it in the future.
|
||||
updateLocalProperties(buildInfo: buildInfo);
|
||||
await findAndReplaceVersionProperties(buildInfo: buildInfo);
|
||||
// Version name and number are provided by the pubspec.yaml file
|
||||
// and can be overwritten with flutter build command.
|
||||
// The default Gradle script reads the version name and number
|
||||
// from the local.properties file.
|
||||
await updateLocalProperties(buildInfo: buildInfo);
|
||||
|
||||
final String gradle = await _ensureGradle();
|
||||
|
||||
|
||||
@@ -289,11 +289,11 @@ To edit platform code in an IDE see https://flutter.io/platform-plugins/#edit-co
|
||||
|
||||
if (argResults['pub']) {
|
||||
await pubGet(context: PubContext.create, directory: appPath, offline: argResults['offline']);
|
||||
new FlutterProject(fs.directory(appPath)).ensureReadyForPlatformSpecificTooling();
|
||||
await new FlutterProject(fs.directory(appPath)).ensureReadyForPlatformSpecificTooling();
|
||||
}
|
||||
|
||||
if (android_sdk.androidSdk != null)
|
||||
gradle.updateLocalProperties(projectPath: appPath);
|
||||
await gradle.updateLocalProperties(projectPath: appPath);
|
||||
|
||||
return generatedCount;
|
||||
}
|
||||
|
||||
@@ -82,13 +82,13 @@ class PackagesGetCommand extends FlutterCommand {
|
||||
|
||||
await _runPubGet(target);
|
||||
final FlutterProject rootProject = new FlutterProject(fs.directory(target));
|
||||
rootProject.ensureReadyForPlatformSpecificTooling();
|
||||
await rootProject.ensureReadyForPlatformSpecificTooling();
|
||||
|
||||
// Get/upgrade packages in example app as well
|
||||
if (rootProject.hasExampleApp) {
|
||||
final FlutterProject exampleProject = rootProject.example;
|
||||
await _runPubGet(exampleProject.directory.path);
|
||||
exampleProject.ensureReadyForPlatformSpecificTooling();
|
||||
await exampleProject.ensureReadyForPlatformSpecificTooling();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,8 @@ import 'base/file_system.dart';
|
||||
import 'cache.dart';
|
||||
import 'globals.dart';
|
||||
|
||||
final RegExp _versionPattern = new RegExp(r'^(\d+)(\.(\d+)(\.(\d+))?)?(\+(\d+))?$');
|
||||
|
||||
/// A wrapper around the `flutter` section in the `pubspec.yaml` file.
|
||||
class FlutterManifest {
|
||||
FlutterManifest._();
|
||||
@@ -51,6 +53,36 @@ class FlutterManifest {
|
||||
|
||||
String get appName => _descriptor['name'] ?? '';
|
||||
|
||||
/// The version String from the `pubspec.yaml` file.
|
||||
/// Can be null if it isn't set or has a wrong format.
|
||||
String get appVersion {
|
||||
final String version = _descriptor['version']?.toString();
|
||||
if (version != null && _versionPattern.hasMatch(version))
|
||||
return version;
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
/// The build version name from the `pubspec.yaml` file.
|
||||
/// Can be null if version isn't set or has a wrong format.
|
||||
String get buildName {
|
||||
if (appVersion != null && appVersion.contains('+'))
|
||||
return appVersion.split('+')?.elementAt(0);
|
||||
else
|
||||
return appVersion;
|
||||
}
|
||||
|
||||
/// The build version number from the `pubspec.yaml` file.
|
||||
/// Can be null if version isn't set or has a wrong format.
|
||||
int get buildNumber {
|
||||
if (appVersion != null && appVersion.contains('+')) {
|
||||
final String value = appVersion.split('+')?.elementAt(1);
|
||||
return value == null ? null : int.tryParse(value);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
bool get usesMaterialDesign {
|
||||
return _flutterDescriptor['uses-material-design'] ?? false;
|
||||
}
|
||||
|
||||
@@ -242,7 +242,7 @@ Future<XcodeBuildResult> buildXcodeProject({
|
||||
final Directory appDirectory = fs.directory(app.appDirectory);
|
||||
await _addServicesToBundle(appDirectory);
|
||||
|
||||
updateGeneratedXcodeProperties(
|
||||
await updateGeneratedXcodeProperties(
|
||||
projectPath: fs.currentDirectory.path,
|
||||
buildInfo: buildInfo,
|
||||
targetOverride: targetOverride,
|
||||
@@ -272,53 +272,6 @@ Future<XcodeBuildResult> buildXcodeProject({
|
||||
await fingerprinter.writeFingerprint();
|
||||
}
|
||||
|
||||
// If buildNumber is not specified, keep the project untouched.
|
||||
if (buildInfo.buildNumber != null) {
|
||||
final Status buildNumberStatus =
|
||||
logger.startProgress('Setting CFBundleVersion...', expectSlowOperation: true);
|
||||
try {
|
||||
final RunResult buildNumberResult = await runAsync(
|
||||
<String>[
|
||||
'/usr/bin/env',
|
||||
'xcrun',
|
||||
'agvtool',
|
||||
'new-version',
|
||||
'-all',
|
||||
buildInfo.buildNumber.toString(),
|
||||
],
|
||||
workingDirectory: app.appDirectory,
|
||||
);
|
||||
if (buildNumberResult.exitCode != 0) {
|
||||
throwToolExit('Xcode failed to set new version\n${buildNumberResult.stderr}');
|
||||
}
|
||||
} finally {
|
||||
buildNumberStatus.stop();
|
||||
}
|
||||
}
|
||||
|
||||
// If buildName is not specified, keep the project untouched.
|
||||
if (buildInfo.buildName != null) {
|
||||
final Status buildNameStatus =
|
||||
logger.startProgress('Setting CFBundleShortVersionString...', expectSlowOperation: true);
|
||||
try {
|
||||
final RunResult buildNameResult = await runAsync(
|
||||
<String>[
|
||||
'/usr/bin/env',
|
||||
'xcrun',
|
||||
'agvtool',
|
||||
'new-marketing-version',
|
||||
buildInfo.buildName,
|
||||
],
|
||||
workingDirectory: app.appDirectory,
|
||||
);
|
||||
if (buildNameResult.exitCode != 0) {
|
||||
throwToolExit('Xcode failed to set new marketing version\n${buildNameResult.stderr}');
|
||||
}
|
||||
} finally {
|
||||
buildNameStatus.stop();
|
||||
}
|
||||
}
|
||||
|
||||
final List<String> buildCommands = <String>[
|
||||
'/usr/bin/env',
|
||||
'xcrun',
|
||||
|
||||
@@ -2,9 +2,12 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
import '../artifacts.dart';
|
||||
import '../base/common.dart';
|
||||
import '../base/context.dart';
|
||||
import '../base/file_system.dart';
|
||||
import '../base/io.dart';
|
||||
@@ -15,6 +18,7 @@ import '../base/utils.dart';
|
||||
import '../build_info.dart';
|
||||
import '../bundle.dart' as bundle;
|
||||
import '../cache.dart';
|
||||
import '../flutter_manifest.dart';
|
||||
import '../globals.dart';
|
||||
|
||||
final RegExp _settingExpr = new RegExp(r'(\w+)\s*=\s*(.*)$');
|
||||
@@ -30,11 +34,11 @@ String _generatedXcodePropertiesPath(String projectPath) {
|
||||
|
||||
/// Writes default Xcode properties files in the Flutter project at [projectPath],
|
||||
/// if project is an iOS project and such files do not already exist.
|
||||
void generateXcodeProperties(String projectPath) {
|
||||
Future<void> generateXcodeProperties(String projectPath) async {
|
||||
if (fs.isDirectorySync(fs.path.join(projectPath, 'ios'))) {
|
||||
if (fs.file(_generatedXcodePropertiesPath(projectPath)).existsSync())
|
||||
return;
|
||||
updateGeneratedXcodeProperties(
|
||||
await updateGeneratedXcodeProperties(
|
||||
projectPath: projectPath,
|
||||
buildInfo: BuildInfo.debug,
|
||||
targetOverride: bundle.defaultMainPath,
|
||||
@@ -47,12 +51,12 @@ void generateXcodeProperties(String projectPath) {
|
||||
///
|
||||
/// targetOverride: Optional parameter, if null or unspecified the default value
|
||||
/// from xcode_backend.sh is used 'lib/main.dart'.
|
||||
void updateGeneratedXcodeProperties({
|
||||
Future<void> updateGeneratedXcodeProperties({
|
||||
@required String projectPath,
|
||||
@required BuildInfo buildInfo,
|
||||
String targetOverride,
|
||||
@required bool previewDart2,
|
||||
}) {
|
||||
}) async {
|
||||
final StringBuffer localsBuffer = new StringBuffer();
|
||||
|
||||
localsBuffer.writeln('// This is a generated file; do not edit or check into version control.');
|
||||
@@ -77,6 +81,24 @@ void updateGeneratedXcodeProperties({
|
||||
|
||||
localsBuffer.writeln('FLUTTER_FRAMEWORK_DIR=${flutterFrameworkDir(buildInfo.mode)}');
|
||||
|
||||
final String flutterManifest = fs.path.join(projectPath, bundle.defaultManifestPath);
|
||||
FlutterManifest manifest;
|
||||
try {
|
||||
manifest = await FlutterManifest.createFromPath(flutterManifest);
|
||||
} catch (error) {
|
||||
throwToolExit('Failed to load pubspec.yaml: $error');
|
||||
}
|
||||
|
||||
final String buildName = buildInfo?.buildName ?? manifest.buildName;
|
||||
if (buildName != null) {
|
||||
localsBuffer.writeln('FLUTTER_BUILD_NAME=$buildName');
|
||||
}
|
||||
|
||||
final int buildNumber = buildInfo?.buildNumber ?? manifest.buildNumber;
|
||||
if (buildNumber != null) {
|
||||
localsBuffer.writeln('FLUTTER_BUILD_NUMBER=$buildNumber');
|
||||
}
|
||||
|
||||
if (artifacts is LocalEngineArtifacts) {
|
||||
final LocalEngineArtifacts localEngineArtifacts = artifacts;
|
||||
localsBuffer.writeln('LOCAL_ENGINE=${localEngineArtifacts.engineOutPath}');
|
||||
|
||||
@@ -55,12 +55,12 @@ class FlutterProject {
|
||||
|
||||
/// Generates project files necessary to make Gradle builds work on Android
|
||||
/// and CocoaPods+Xcode work on iOS, for app projects only
|
||||
void ensureReadyForPlatformSpecificTooling() {
|
||||
Future<void> ensureReadyForPlatformSpecificTooling() async {
|
||||
if (!directory.existsSync() || hasExampleApp) {
|
||||
return;
|
||||
}
|
||||
injectPlugins(directory: directory.path);
|
||||
generateXcodeProperties(directory.path);
|
||||
await generateXcodeProperties(directory.path);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -329,7 +329,7 @@ abstract class FlutterCommand extends Command<Null> {
|
||||
|
||||
if (shouldRunPub) {
|
||||
await pubGet(context: PubContext.getVerifyContext(name));
|
||||
new FlutterProject(fs.currentDirectory).ensureReadyForPlatformSpecificTooling();
|
||||
await new FlutterProject(fs.currentDirectory).ensureReadyForPlatformSpecificTooling();
|
||||
}
|
||||
|
||||
setupApplicationPackages();
|
||||
|
||||
Reference in New Issue
Block a user