mirror of
https://github.com/fankes/termux-app.git
synced 2025-09-06 02:35:19 +08:00
Added: Add support for TERMUX_APP_PACKAGE_MANAGER
and TERMUX_APP_PACKAGE_VARIANT
to build APKs with different package manager configurations
The `TermuxBootstrap` class has been added that defines the `PackageManager` and `PackageVariant` classes for the supported package manager configurations for the app. The variant is defined by the `project.ext.packageVariant` value in the `app/build.gradle` and its value is used by the `build.gradle` to pack its respective bootstrap zips in the app APK at build time and the value is used to set `TermuxBootstrap.TERMUX_APP_PACKAGE_MANAGER` and `TermuxBootstrap.TERMUX_APP_PACKAGE_VARIANT` static values that are used at runtime by the app to run variant specific code. The manager is automatically extracted from the variant as the substring before first dash `-`. The default variant is `apt-android-7` and it can either be replaced in `app/build.gradle` manually or the `TERMUX_PACKAGE_VARIANT` env variable can be exported in which the build command is run. The `TERMUX_APP_PACKAGE_MANAGER` and `TERMUX_APP_PACKAGE_VARIANT` environmental variables will be exported by the app and they will also be added in Termux app info in about page and reports, allowing users and devs to know which variant is currently installed. Bootstrap of a different variant must not be manually installed by the user after app installation by replacing `$PREFIX` since app code is dependant on the variant used to build the APK. Currently, `apt-android-7` and `apt-android-5` variants will be built for by the workflows but they will fail for `apt-android-5` since `build.gradle` support is currently not enabled and will be enabled by a pull request that adds support for Android 5. The workflow needs to try to build the `apt-android-5` variant so that pull request builds are generated.
This commit is contained in:
@@ -0,0 +1,174 @@
|
||||
package com.termux.shared.termux;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.termux.shared.logger.Logger;
|
||||
|
||||
public class TermuxBootstrap {
|
||||
|
||||
private static final String LOG_TAG = "TermuxBootstrap";
|
||||
|
||||
/** The {@link PackageManager} for the bootstrap in the app APK added in app/build.gradle. */
|
||||
public static PackageManager TERMUX_APP_PACKAGE_MANAGER;
|
||||
|
||||
/** The {@link PackageVariant} for the bootstrap in the app APK added in app/build.gradle. */
|
||||
public static PackageVariant TERMUX_APP_PACKAGE_VARIANT;
|
||||
|
||||
/** Set name as app wide night mode value. */
|
||||
public static void setTermuxPackageManagerAndVariant(@Nullable String packageVariantName) {
|
||||
TERMUX_APP_PACKAGE_VARIANT = PackageVariant.variantOf(packageVariantName);
|
||||
if (TERMUX_APP_PACKAGE_VARIANT == null) {
|
||||
throw new RuntimeException("Unsupported TERMUX_APP_PACKAGE_VARIANT \"" + packageVariantName + "\"");
|
||||
}
|
||||
|
||||
Logger.logVerbose(LOG_TAG, "Set TERMUX_APP_PACKAGE_VARIANT to \"" + TERMUX_APP_PACKAGE_VARIANT + "\"");
|
||||
|
||||
// Set packageManagerName to substring before first dash "-" in packageVariantName
|
||||
int index = packageVariantName.indexOf('-');
|
||||
String packageManagerName = (index == -1) ? null : packageVariantName.substring(0, index);
|
||||
TERMUX_APP_PACKAGE_MANAGER = PackageManager.managerOf(packageManagerName);
|
||||
if (TERMUX_APP_PACKAGE_MANAGER == null) {
|
||||
throw new RuntimeException("Unsupported TERMUX_APP_PACKAGE_MANAGER \"" + packageManagerName + "\" with variant \"" + packageVariantName + "\"");
|
||||
}
|
||||
|
||||
Logger.logVerbose(LOG_TAG, "Set TERMUX_APP_PACKAGE_MANAGER to \"" + TERMUX_APP_PACKAGE_MANAGER + "\"");
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** Is {@link PackageManager#APT} set as {@link #TERMUX_APP_PACKAGE_MANAGER}. */
|
||||
public static boolean isAppPackageManagerAPT() {
|
||||
return PackageManager.APT.equals(TERMUX_APP_PACKAGE_MANAGER);
|
||||
}
|
||||
|
||||
///** Is {@link PackageManager#TAPM} set as {@link #TERMUX_APP_PACKAGE_MANAGER}. */
|
||||
//public static boolean isAppPackageManagerTAPM() {
|
||||
// return PackageManager.TAPM.equals(TERMUX_APP_PACKAGE_MANAGER);
|
||||
//}
|
||||
|
||||
///** Is {@link PackageManager#PACMAN} set as {@link #TERMUX_APP_PACKAGE_MANAGER}. */
|
||||
//public static boolean isAppPackageManagerPACMAN() {
|
||||
// return PackageManager.PACMAN.equals(TERMUX_APP_PACKAGE_MANAGER);
|
||||
//}
|
||||
|
||||
|
||||
|
||||
/** Is {@link PackageVariant#APT_ANDROID_7} set as {@link #TERMUX_APP_PACKAGE_VARIANT}. */
|
||||
public static boolean isAppPackageVariantAPTAndroid7() {
|
||||
return PackageVariant.APT_ANDROID_7.equals(TERMUX_APP_PACKAGE_VARIANT);
|
||||
}
|
||||
|
||||
///** Is {@link PackageVariant#APT_ANDROID_5} set as {@link #TERMUX_APP_PACKAGE_VARIANT}. */
|
||||
//public static boolean isAppPackageVariantAPTAndroid5() {
|
||||
// return PackageVariant.APT_ANDROID_5.equals(TERMUX_APP_PACKAGE_VARIANT);
|
||||
//}
|
||||
|
||||
///** Is {@link PackageVariant#TAPM_ANDROID_7} set as {@link #TERMUX_APP_PACKAGE_VARIANT}. */
|
||||
//public static boolean isAppPackageVariantTAPMAndroid7() {
|
||||
// return PackageVariant.TAPM_ANDROID_7.equals(TERMUX_APP_PACKAGE_VARIANT);
|
||||
//}
|
||||
|
||||
///** Is {@link PackageVariant#PACMAN_ANDROID_7} set as {@link #TERMUX_APP_PACKAGE_VARIANT}. */
|
||||
//public static boolean isAppPackageVariantTPACMANAndroid7() {
|
||||
// return PackageVariant.PACMAN_ANDROID_7.equals(TERMUX_APP_PACKAGE_VARIANT);
|
||||
//}
|
||||
|
||||
|
||||
|
||||
/** Termux package manager. */
|
||||
public enum PackageManager {
|
||||
|
||||
/**
|
||||
* Advanced Package Tool (APT) for managing debian deb package files.
|
||||
* https://wiki.debian.org/Apt
|
||||
* https://wiki.debian.org/deb
|
||||
*/
|
||||
APT("apt");
|
||||
|
||||
///**
|
||||
// * Termux Android Package Manager (TAPM) for managing termux apk package files.
|
||||
// * https://en.wikipedia.org/wiki/Apk_(file_format)
|
||||
// */
|
||||
//TAPM("tapm");
|
||||
|
||||
///**
|
||||
// * Package Manager (PACMAN) for managing arch linux pkg.tar package files.
|
||||
// * https://wiki.archlinux.org/title/pacman
|
||||
// * https://en.wikipedia.org/wiki/Arch_Linux#Pacman
|
||||
// */
|
||||
//PACMAN("pacman");
|
||||
|
||||
private final String name;
|
||||
|
||||
PackageManager(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public boolean equalsManager(String manager) {
|
||||
return manager != null && manager.equals(this.name);
|
||||
}
|
||||
|
||||
/** Get {@link PackageManager} for {@code name} if found, otherwise {@code null}. */
|
||||
@Nullable
|
||||
public static PackageManager managerOf(String name) {
|
||||
if (name == null || name.isEmpty()) return null;
|
||||
for (PackageManager v : PackageManager.values()) {
|
||||
if (v.name.equals(name)) {
|
||||
return v;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** Termux package variant. The substring before first dash "-" must match one of the {@link PackageManager}. */
|
||||
public enum PackageVariant {
|
||||
|
||||
/** {@link PackageManager#APT} variant for Android 7+. */
|
||||
APT_ANDROID_7("apt-android-7");
|
||||
|
||||
///** {@link PackageManager#APT} variant for Android 5+. */
|
||||
//APT_ANDROID_5("apt-android-5");
|
||||
|
||||
///** {@link PackageManager#TAPM} variant for Android 7+. */
|
||||
//TAPM_ANDROID_7("tapm-android-7");
|
||||
|
||||
///** {@link PackageManager#PACMAN} variant for Android 7+. */
|
||||
//PACMAN_ANDROID_7("pacman-android-7");
|
||||
|
||||
private final String name;
|
||||
|
||||
PackageVariant(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public boolean equalsVariant(String variant) {
|
||||
return variant != null && variant.equals(this.name);
|
||||
}
|
||||
|
||||
/** Get {@link PackageVariant} for {@code name} if found, otherwise {@code null}. */
|
||||
@Nullable
|
||||
public static PackageVariant variantOf(String name) {
|
||||
if (name == null || name.isEmpty()) return null;
|
||||
for (PackageVariant v : PackageVariant.values()) {
|
||||
if (v.name.equals(name)) {
|
||||
return v;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -397,6 +397,11 @@ public class TermuxUtils {
|
||||
|
||||
markdownString.append((AndroidUtils.getAppInfoMarkdownString(context)));
|
||||
|
||||
if (context.getPackageName().equals(TermuxConstants.TERMUX_PACKAGE_NAME)) {
|
||||
AndroidUtils.appendPropertyToMarkdown(markdownString, "TERMUX_APP_PACKAGE_MANAGER", TermuxBootstrap.TERMUX_APP_PACKAGE_MANAGER);
|
||||
AndroidUtils.appendPropertyToMarkdown(markdownString, "TERMUX_APP_PACKAGE_VARIANT", TermuxBootstrap.TERMUX_APP_PACKAGE_VARIANT);
|
||||
}
|
||||
|
||||
Error error;
|
||||
error = TermuxFileUtils.isTermuxFilesDirectoryAccessible(context, true, true);
|
||||
if (error != null) {
|
||||
|
@@ -6,6 +6,7 @@ import androidx.annotation.NonNull;
|
||||
|
||||
import com.termux.shared.errors.Error;
|
||||
import com.termux.shared.file.filesystem.FileTypes;
|
||||
import com.termux.shared.termux.TermuxBootstrap;
|
||||
import com.termux.shared.termux.TermuxConstants;
|
||||
import com.termux.shared.file.FileUtils;
|
||||
import com.termux.shared.logger.Logger;
|
||||
@@ -61,6 +62,10 @@ public class TermuxShellUtils {
|
||||
environment.add("TERMUX_APP_PID=" + TERMUX_APP_PID);
|
||||
if (TERMUX_APK_RELEASE != null)
|
||||
environment.add("TERMUX_APK_RELEASE=" + TERMUX_APK_RELEASE);
|
||||
if (TermuxBootstrap.TERMUX_APP_PACKAGE_MANAGER != null)
|
||||
environment.add("TERMUX_APP_PACKAGE_MANAGER=" + TermuxBootstrap.TERMUX_APP_PACKAGE_MANAGER.getName());
|
||||
if (TermuxBootstrap.TERMUX_APP_PACKAGE_VARIANT != null)
|
||||
environment.add("TERMUX_APP_PACKAGE_VARIANT=" + TermuxBootstrap.TERMUX_APP_PACKAGE_VARIANT.getName());
|
||||
if (TERMUX_APP_AM_SOCKET_SERVER_ENABLED != null)
|
||||
environment.add("TERMUX_APP_AM_SOCKET_SERVER_ENABLED=" + TERMUX_APP_AM_SOCKET_SERVER_ENABLED);
|
||||
|
||||
|
Reference in New Issue
Block a user