Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "build android" command to Sharezone CLI #685

Merged
merged 6 commits into from
Jul 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions tools/sz_repo_cli/lib/src/commands/src/build_android_command.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Copyright (c) 2022 Sharezone UG (haftungsbeschränkt)
// Licensed under the EUPL-1.2-or-later.
//
// You may obtain a copy of the Licence at:
// https://joinup.ec.europa.eu/software/page/eupl
//
// SPDX-License-Identifier: EUPL-1.2

import 'dart:io';

import 'package:args/command_runner.dart';
import 'package:sz_repo_cli/src/common/common.dart';

final _androidStages = [
'stable',
'alpha',
];

/// The different flavors of the Android app.
final _androidFlavors = [
'prod',
'dev',
];

/// The different output types of the Android app.
final _androidOutputType = [
'appbundle',
'apk',
];

class BuildAndroidCommand extends Command {
final SharezoneRepo _repo;

BuildAndroidCommand(this._repo) {
argParser
..addOption(
releaseStageOptionName,
abbr: 's',
allowed: _androidStages,
defaultsTo: 'stable',
)
..addOption(
outputTypeName,
help: 'The type of output type, either "appbundle" or "apk".',
allowed: _androidOutputType,
defaultsTo: 'appbundle',
)
..addOption(
buildNumberOptionName,
help: '''An identifier used as an internal version number.
Each build must have a unique identifier to differentiate it from previous builds.
It is used to determine whether one build is more recent than
another, with higher numbers indicating more recent build.
When none is specified, the value from pubspec.yaml is used.''',
)
..addOption(
flavorOptionName,
allowed: _androidFlavors,
help:
'The flavor to build for. At the moment only "prod" is supported.',
defaultsTo: 'prod',
);
nilsreichardt marked this conversation as resolved.
Show resolved Hide resolved
}

static const releaseStageOptionName = 'stage';
static const flavorOptionName = 'flavor';
static const buildNumberOptionName = 'build-number';
static const outputTypeName = 'output-type';

@override
String get description =>
'Build the Sharezone Android app in release mode. Codemagic CLI tools must be installed.';

@override
String get name => 'android';

@override
Future<void> run() async {
// Its less work to just print everything right now instead of selectively
// print and add custom print statements for non-verboes output.
// One might add non-verbose output in the future but right now this is
// easier.
nilsreichardt marked this conversation as resolved.
Show resolved Hide resolved
isVerbose = true;
Jonas-Sander marked this conversation as resolved.
Show resolved Hide resolved

await _buildApp();
stdout.writeln('Build finished 🎉 ');
nilsreichardt marked this conversation as resolved.
Show resolved Hide resolved
}

Future<void> _buildApp() async {
try {
final flavor = argResults[flavorOptionName] as String;
final stage = argResults[releaseStageOptionName] as String;
final outputType = argResults[outputTypeName] as String;
final buildNumber = argResults[buildNumberOptionName] as String;
await runProcessSucessfullyOrThrow(
'fvm',
[
'flutter',
'build',
outputType,
'--target',
'lib/main_$flavor.dart',
'--flavor',
flavor,
'--release',
'--dart-define',
'DEVELOPMENT_STAGE=${stage.toUpperCase()}',
if (buildNumber != null) ...['--build-number', '$buildNumber'],
],
workingDirectory: _repo.sharezoneFlutterApp.location.path,
);
} catch (e) {
throw Exception('Failed to build Android app: $e');
}
}
}
18 changes: 18 additions & 0 deletions tools/sz_repo_cli/lib/src/commands/src/build_command.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) 2022 Sharezone UG (haftungsbeschränkt)
// Licensed under the EUPL-1.2-or-later.
//
// You may obtain a copy of the Licence at:
// https://joinup.ec.europa.eu/software/page/eupl
//
// SPDX-License-Identifier: EUPL-1.2

import 'package:args/command_runner.dart';

class BuildCommand extends Command {
@override
String get description =>
'Build Sharezone app for a specific platform (e.g. web app)';

@override
String get name => 'build';
}
5 changes: 4 additions & 1 deletion tools/sz_repo_cli/lib/src/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import 'dart:io';
import 'package:args/command_runner.dart';
import 'package:path/path.dart' as p;
import 'package:sz_repo_cli/src/commands/src/add_license_headers_command.dart';
import 'package:sz_repo_cli/src/commands/src/build_android_command.dart';
import 'package:sz_repo_cli/src/commands/src/build_command.dart';
import 'package:sz_repo_cli/src/commands/src/check_license_headers_command.dart';
import 'package:sz_repo_cli/src/commands/src/format_command.dart';
import 'package:sz_repo_cli/src/commands/src/license_headers_command.dart';
Expand Down Expand Up @@ -42,7 +44,8 @@ Future<void> main(List<String> args) async {
..addCommand(LicenseHeadersCommand()
..addSubcommand(CheckLicenseHeadersCommand(repo))
..addSubcommand(AddLicenseHeadersCommand(repo)))
..addCommand(DeployCommand()..addSubcommand(DeployWebAppCommand(repo)));
..addCommand(DeployCommand()..addSubcommand(DeployWebAppCommand(repo)))
..addCommand(BuildCommand()..addSubcommand(BuildAndroidCommand(repo)));

await commandRunner.run(args).catchError((Object e) {
final ToolExit toolExit = e;
Expand Down
Loading