Skip to content

Commit

Permalink
fix: The default language of the AppStore is now configurable.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathrunet committed Jan 6, 2024
1 parent afbfcd7 commit 52f2ee9
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 3 deletions.
7 changes: 7 additions & 0 deletions packages/katana_cli/lib/src/app_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ class AppInfo {
if (defaultLocale.isNotEmpty) "": data[defaultLocale!]!,
...data,
});
final xcode = XCode();
await xcode.load();
xcode.pbxProject = xcode.pbxProject?.copyWith(
defaultLocale: defaultLocale,
locales: data.keys.toList(),
);
await xcode.save();
}

static Future<void> _createAndroidResValues(
Expand Down
128 changes: 128 additions & 0 deletions packages/katana_cli/lib/src/xcode.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class XCode {
String get rawData => _rawData;
late String _rawData;

/// PBXProject data.
///
/// PBXProjectのデータ。
PBXProject? pbxProject;

/// BuildFile data.
///
/// BuildFileのデータ。
Expand Down Expand Up @@ -78,6 +83,7 @@ class XCode {
_pbxVariantGroup = PBXVariantGroup._load(_rawData);
_pbxBuildConfiguration = PBXBuildConfiguration._load(_rawData);
_pbxFrameworksBuildPhase = PBXFrameworksBuildPhase._load(_rawData);
pbxProject = PBXProject._load(_rawData);
}

/// Data storage.
Expand All @@ -87,6 +93,7 @@ class XCode {
if (_rawData.isEmpty) {
throw Exception("No value. Please load data with [load].");
}
_rawData = PBXProject._save(_rawData, pbxProject);
_rawData = PBXVariantGroup._save(_rawData, _pbxVariantGroup);
_rawData = PBXResourcesBuildPhase._save(_rawData, _pbxResourcesBuildPhase);
_rawData = PBXGroup._save(_rawData, _pbxGroup);
Expand Down Expand Up @@ -1519,3 +1526,124 @@ class PBXFrameworksBuildPhaseFiles {
return "\t\t\t\t$id /* $fileName in $dirName */";
}
}

/// PBXProject data.
///
/// PBXProjectのデータ。
class PBXProject {
/// PBXProject data.
///
/// PBXProjectのデータ。
factory PBXProject({
required String code,
String defaultLocale = "en",
List<String> locales = const [],
}) {
return PBXProject._(
code: code,
defaultLocale: defaultLocale,
locales: locales,
);
}
PBXProject._({
required this.code,
this.defaultLocale = "en",
this.locales = const [],
});

static PBXProject? _load(String content) {
final region = RegExp(
r"/\* Begin PBXProject section \*/(?<code>[\s\S]+)/\* End PBXProject section \*/",
).firstMatch(content);
if (region == null) {
return null;
}
final code = region.namedGroup("code") ?? "";
if (code.isEmpty) {
return null;
}
final developmentRegion =
RegExp(r'developmentRegion = (?<defaultRegion>[a-zA-Z]+);')
.firstMatch(code)
?.namedGroup("defaultRegion") ??
"";
final regions = RegExp(r'knownRegions = \((?<regions>[^\)]+)\);')
.firstMatch(code)
?.namedGroup("regions") ??
"";
final locales = regions
.split(",")
.mapAndRemoveEmpty((e) {
return e.trim().replaceAll("\"", "");
})
.toList()
.removeEmpty();
return PBXProject(
code: code,
defaultLocale: developmentRegion,
locales: [
...locales,
if (!locales.contains("Base")) "Base",
],
);
}

static String _save(String content, PBXProject? project) {
if (project == null) {
return content;
}
return content.replaceAll(
RegExp(
r"/\* Begin PBXProject section \*/(?<code>[\s\S]+)/\* End PBXProject section \*/",
),
"/* Begin PBXProject section */${project.toString()}/* End PBXProject section */",
);
}

/// Value of `isa`.
///
/// `isa`の値。
final String isa = "PBXProject";

/// Default locale.
///
/// デフォルトロケール。
final String defaultLocale;

/// List of locales.
///
/// ロケールの一覧。
final List<String> locales;

/// Other Codes.
///
/// その他のコード。
final String code;

@override
String toString() {
return code
.replaceAll(RegExp(r'developmentRegion = (?<defaultRegion>[a-zA-Z]+);'),
"developmentRegion = $defaultLocale;")
.replaceAll(
RegExp(r'knownRegions = \((?<regions>[^\)]+)\);'),
"knownRegions = (\n${[
...locales,
if (!locales.contains("Base")) "Base",
].map((e) => ' $e').join(",\n")},\n );");
}

/// Copy with new data.
///
/// 新しいデータをコピーする。
PBXProject copyWith({
String? defaultLocale,
List<String>? locales,
}) {
return PBXProject(
code: code,
defaultLocale: defaultLocale ?? this.defaultLocale,
locales: locales ?? this.locales,
);
}
}
6 changes: 3 additions & 3 deletions packages/katana_cli/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ packages:
dependency: "direct main"
description:
name: archive
sha256: "7b875fd4a20b165a3084bd2d210439b22ebc653f21cea4842729c0c30c82596b"
sha256: "22600aa1e926be775fa5fe7e6894e7fb3df9efda8891c73f70fb3262399a432d"
url: "https://pub.dev"
source: hosted
version: "3.4.9"
version: "3.4.10"
args:
dependency: transitive
description:
Expand Down Expand Up @@ -159,7 +159,7 @@ packages:
path: "../katana"
relative: true
source: path
version: "2.10.0"
version: "2.10.1"
lints:
dependency: transitive
description:
Expand Down

0 comments on commit 52f2ee9

Please sign in to comment.