Skip to content

Commit

Permalink
feat: Added a setting for Permission for Podfile.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathrunet committed Dec 8, 2023
1 parent 46492d5 commit 8eaae3d
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 44 deletions.
14 changes: 11 additions & 3 deletions packages/katana_cli/lib/action/ads/ads.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class AdsCliAction extends CliCommand with CliActionMixin {
final ads = context.yaml.getAsMap("ads");
final androidAppId = ads.get("android_app_id", "");
final iosAppId = ads.get("ios_app_id", "");
final permission = ads.getAsMap("permission");
if (androidAppId.isEmpty && iosAppId.isEmpty) {
throw Exception(
"Specify the app ID for Android or iOS in [ads]->[android_app_id] or [ads]->[ios_app_id].",
Expand Down Expand Up @@ -81,7 +82,7 @@ class AdsCliAction extends CliCommand with CliActionMixin {
p1.value == "com.google.android.gms.ads.APPLICATION_ID",
),
);
if (applicationIdData != null) {
if (applicationIdData == null) {
application.first.children.add(
XmlElement(
XmlName("meta-data"),
Expand All @@ -99,7 +100,7 @@ class AdsCliAction extends CliCommand with CliActionMixin {
),
);
} else {
applicationIdData!.attributes
applicationIdData.attributes
..clear()
..addAll([
XmlAttribute(
Expand Down Expand Up @@ -143,11 +144,18 @@ class AdsCliAction extends CliCommand with CliActionMixin {
],
);
} else {
applicationIdData.nextSibling!.innerText = iosAppId;
applicationIdData.nextElementSibling!.innerText = iosAppId;
}
await plist.writeAsString(
document.toXmlString(pretty: true, indent: "\t", newLine: "\n"),
);
}
label("Addition of permission messages.");
await XCodePermissionType.userTrackingUsage.setMessageToXCode(
permission
.map((key, value) => MapEntry(key, value.toString()))
.where((key, value) => value.isNotEmpty),
);
await PodfilePermissionType.userTrackingUsage.enablePermissionToPodfile();
}
}
1 change: 1 addition & 0 deletions packages/katana_cli/lib/action/app/location.dart
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ class AppLocationCliAction extends CliCommand with CliActionMixin {
.map((key, value) => MapEntry(key, value.toString()))
.where((key, value) => value.isNotEmpty),
);
await PodfilePermissionType.locationUsage.enablePermissionToPodfile();
await command(
"Import packages.",
[
Expand Down
1 change: 1 addition & 0 deletions packages/katana_cli/lib/action/app/picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class AppPickerCliAction extends CliCommand with CliActionMixin {
.map((key, value) => MapEntry(key, value.toString()))
.where((key, value) => value.isNotEmpty),
);
await PodfilePermissionType.photoLibraryUsage.enablePermissionToPodfile();
await command(
"Import packages.",
[
Expand Down
2 changes: 2 additions & 0 deletions packages/katana_cli/lib/action/app/speech_to_text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class AppSpeechToTextCliAction extends CliCommand with CliActionMixin {
.map((key, value) => MapEntry(key, value.toString()))
.where((key, value) => value.isNotEmpty),
);
await PodfilePermissionType.microphoneUsage.enablePermissionToPodfile();
await PodfilePermissionType.speechRecognitionUsage.enablePermissionToPodfile();
label("Edit AndroidManifest.xml.");
final file = File("android/app/src/main/AndroidManifest.xml");
if (!file.existsSync()) {
Expand Down
58 changes: 29 additions & 29 deletions packages/katana_cli/lib/command/apply.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,36 +39,36 @@ import 'package:katana_cli/katana_cli.dart';
///
/// 実行する順番で並べてください。
const _actions = <CliActionMixin>[
AppInfoCliAction(),
AppSpreadSheetCliAction(),
AppCsrCliAction(),
AppP12CliAction(),
AppKeystoreCliAction(),
AppPickerCliAction(),
AppIconCliAction(),
AppDeeplinkCliAction(),
FirebaseInitCliAction(),
FirebaseAuthenticationCliAction(),
FirebaseSchedulerCliAction(),
FirebaseMessagingCliAction(),
FirebaseDynamicLinksCliAction(),
FirebaseTermsAndPrivacyCliAction(),
GitActionCliAction(),
GitPreCommitCliAction(),
AppOpenAICliAction(),
AppIntroductionCliAction(),
AppCalendarCliAction(),
AppTextToSpeechCliAction(),
AppSpeechToTextCliAction(),
AppLocationCliAction(),
AgoraCliAction(),
// AppInfoCliAction(),
// AppSpreadSheetCliAction(),
// AppCsrCliAction(),
// AppP12CliAction(),
// AppKeystoreCliAction(),
// AppPickerCliAction(),
// AppIconCliAction(),
// AppDeeplinkCliAction(),
// FirebaseInitCliAction(),
// FirebaseAuthenticationCliAction(),
// FirebaseSchedulerCliAction(),
// FirebaseMessagingCliAction(),
// FirebaseDynamicLinksCliAction(),
// FirebaseTermsAndPrivacyCliAction(),
// GitActionCliAction(),
// GitPreCommitCliAction(),
// AppOpenAICliAction(),
// AppIntroductionCliAction(),
// AppCalendarCliAction(),
// AppTextToSpeechCliAction(),
// AppSpeechToTextCliAction(),
// AppLocationCliAction(),
// AgoraCliAction(),
AdsCliAction(),
PurchaseCliAction(),
StripeCliAction(),
MailGmailCliAction(),
MailSendGridCliAction(),
AppGeocodingCliAction(),
EcosystemCliAction(),
// PurchaseCliAction(),
// StripeCliAction(),
// MailGmailCliAction(),
// MailSendGridCliAction(),
// AppGeocodingCliAction(),
// EcosystemCliAction(),
];

/// Reflect the settings in katana.yaml in the application project.
Expand Down
6 changes: 6 additions & 0 deletions packages/katana_cli/lib/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -457,14 +457,20 @@ ecosystem:
# Set the respective Ad App Id in [android_app_id] and [ios_app_id].
# If you want to use it on the web, please obtain app_ads.txt and place it under the web folder.
# https://admanager.google.com/home/
# Specify the permission message to use the library in IOS in [permission].
# Please include `en`, `ja`, etc. and write the message in that language there.
# 広告を出す場合の設定を行います。
# [android_app_id]と[ios_app_id]にそれぞれのAd App Idを設定してください。
# Webで利用する場合はapp_ads.txtを取得し、webフォルダ以下に配置してください。
# https://admanager.google.com/home/
# [permission]にIOSでライブラリを利用するための権限許可メッセージを指定します。
# `en`や`ja`などを記載しそこにその言語でのメッセージを記述してください。
ads:
enable: false
android_app_id:
ios_app_id:
permission:
en: If you [Allow], App will display ads optimized for you.
# Configure settings for store billing.
# ストア課金を行う場合の設定を行います。
Expand Down
1 change: 1 addition & 0 deletions packages/katana_cli/lib/katana_cli.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export 'command/pub/pub.dart';
export 'src/framework.dart';
export 'src/gradle.dart';
export 'src/xcode.dart';
export 'src/podfile.dart';
export 'src/functions.dart';
138 changes: 138 additions & 0 deletions packages/katana_cli/lib/src/podfile.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// Dart imports:
import 'dart:io';

// Package imports:
import 'package:katana/katana.dart';

/// Permission type for Podfile.
///
/// Podfile用のパーミッションタイプ。
enum PodfilePermissionType {
/// Permissions for camera use.
///
/// カメラ利用時のパーミッション。
cameraUsage("PERMISSION_CAMERA"),

/// Permissions for microphone use.
///
/// マイク利用時のパーミッション。
microphoneUsage("PERMISSION_MICROPHONE"),

/// Permission for constant use of location information.
///
/// 位置情報常時利用時のパーミッション。
locationUsage("PERMISSION_LOCATION"),

/// Permissions for motion sensor use.
///
/// モーションセンサー利用時のパーミッション。
motionUsageDescription("PERMISSION_SENSORS"),

/// Permissions when using media services.
///
/// メディアサービス利用時のパーミッション。
serviceMediaLibrary("PERMISSION_MEDIA_LIBRARY"),

/// Permissions for photo library use.
///
/// フォトライブラリ利用時のパーミッション。
photoLibraryUsage("PERMISSION_PHOTOS"),

/// Permissions when using the Add to Photo Library feature.
///
/// フォトライブラリへの追加機能利用時のパーミッション。
photoLibraryAddUsage("PERMISSION_PHOTOS"),

/// Permissions when using contacts.
///
/// 連絡先利用時のパーミッション。
contactsUsage("PERMISSION_CONTACTS"),

/// Permissions for calendar use.
///
/// カレンダー利用時のパーミッション。
calendarsUsage("PERMISSION_EVENTS"),

/// Permissions for reminder use.
///
/// リマインダー利用時のパーミッション。
remindersUsage("PERMISSION_REMINDERS"),

/// Permissions for Bluetooth use.
///
/// Bluetooth利用時のパーミッション。
bluetoothPeripheralUsage("PERMISSION_BLUETOOTH"),

/// Permissions when using voice recognition.
///
/// 音声認識利用時のパーミッション。
speechRecognitionUsage("PERMISSION_SPEECH_RECOGNIZER"),

/// Permissions when using user tracking.
///
/// ユーザートラッキング利用時のパーミッション。
userTrackingUsage("PERMISSION_APP_TRACKING_TRANSPARENCY");

/// Permission type for Podfile.
///
/// Podfile用のパーミッションタイプ。
const PodfilePermissionType(this.id);

/// Permission ID.
///
/// パーミッションのID。
final String id;

static final _permissionsRegExp = RegExp(
r"target.build_configurations.each do \|config\|[\s]+config.build_settings\['GCC_PREPROCESSOR_DEFINITIONS'\] \|\|= \[([\s\S]+)\][\s]+end",
);

/// Rewrite the podfile so that the specified permissions are available.
///
/// 指定されたパーミッションを利用できるようにPodfileを書き換えます。
Future<void> enablePermissionToPodfile() async {
final podfile = File("ios/Podfile");
if (!podfile.existsSync()) {
throw Exception(
"ios/Podfile does not exist. Please run `flutter create .` first.",
);
}
final rawData = await podfile.readAsString();
final match = _permissionsRegExp.firstMatch(rawData);
if (match != null) {
final permissionsText = match.group(1);
final permissions = permissionsText!
.split(",")
.map((e) => e.trim().trimString("'"))
.where((element) => element != r"$(inherited)")
.map((e) => e.replaceAll("=1", ""))
.where((element) => element.isNotEmpty)
.toList();
permissions.add(id);
final distincted = permissions.distinct();
final replaced = rawData.replaceAll(
_permissionsRegExp,
"""target.build_configurations.each do |config|
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
'\$(inherited)',
${distincted.map(
(e) => " '$e=1',",
).join("\n")}
]
end""",
);
await podfile.writeAsString(replaced);
} else {
final replaced = rawData.replaceAll(
"flutter_additional_ios_build_settings(target)",
"""flutter_additional_ios_build_settings(target)
target.build_configurations.each do |config|
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
'\$(inherited)',
'$id=1',
]
end""");
await podfile.writeAsString(replaced);
}
}
}
32 changes: 20 additions & 12 deletions packages/katana_cli/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ packages:
dependency: transitive
description:
name: http
sha256: "759d1a329847dd0f39226c688d3e06a6b8679668e350e2891a6474f8b4bb8525"
sha256: d4872660c46d929f6b8a9ef4e7a7eff7e49bbf0c4ec3f385ee32df5119175139
url: "https://pub.dev"
source: hosted
version: "1.1.0"
version: "1.1.2"
http_parser:
dependency: transitive
description:
Expand Down Expand Up @@ -141,10 +141,10 @@ packages:
dependency: transitive
description:
name: intl
sha256: "3bc132a9dbce73a7e4a21a17d06e1878839ffbf975568bc875c60537824b0c4d"
sha256: d6f56758b7d3014a48af9701c085700aac781a92a87a62b1333b46d8879661cf
url: "https://pub.dev"
source: hosted
version: "0.18.1"
version: "0.19.0"
js:
dependency: transitive
description:
Expand All @@ -159,7 +159,7 @@ packages:
path: "../katana"
relative: true
source: path
version: "2.7.1"
version: "2.9.1"
lints:
dependency: transitive
description:
Expand All @@ -180,18 +180,18 @@ packages:
dependency: transitive
description:
name: path
sha256: "8829d8a55c13fc0e37127c29fedf290c102f4e40ae94ada574091fe0ff96c917"
sha256: "087ce49c3f0dc39180befefc60fdb4acd8f8620e5682fe2476afd0b3688bb4af"
url: "https://pub.dev"
source: hosted
version: "1.8.3"
version: "1.9.0"
petitparser:
dependency: transitive
description:
name: petitparser
sha256: eeb2d1428ee7f4170e2bd498827296a18d4e7fc462b71727d111c0ac7707cfa6
sha256: c15605cd28af66339f8eb6fbe0e541bfe2d1b72d5825efc6598f3e0a31b9ad27
url: "https://pub.dev"
source: hosted
version: "6.0.1"
version: "6.0.2"
pointycastle:
dependency: transitive
description:
Expand Down Expand Up @@ -264,14 +264,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "4.2.1"
web:
dependency: transitive
description:
name: web
sha256: edc8a9573dd8c5a83a183dae1af2b6fd4131377404706ca4e5420474784906fa
url: "https://pub.dev"
source: hosted
version: "0.4.0"
xml:
dependency: "direct main"
description:
name: xml
sha256: af5e77e9b83f2f4adc5d3f0a4ece1c7f45a2467b695c2540381bac793e34e556
sha256: b015a8ad1c488f66851d762d3090a21c600e479dc75e68328c52774040cf9226
url: "https://pub.dev"
source: hosted
version: "6.4.2"
version: "6.5.0"
yaml:
dependency: "direct main"
description:
Expand All @@ -289,4 +297,4 @@ packages:
source: hosted
version: "1.0.3"
sdks:
dart: ">=3.0.0 <4.0.0"
dart: ">=3.2.0 <4.0.0"

0 comments on commit 8eaae3d

Please sign in to comment.