Extensions: support adding system repositories via the command line

This commit is contained in:
Campbell Barton
2024-07-10 17:39:06 +10:00
parent 1fc2530179
commit 7ee6451a51
2 changed files with 46 additions and 16 deletions

View File

@@ -422,12 +422,19 @@ class subcmd_repo:
repo_id: str,
directory: str,
url: str,
source: str,
cache: bool,
clear_all: bool,
no_prefs: bool,
) -> bool:
from bpy import context
# This could be allowed the Python API doesn't prevent it.
# However this is not going to do what the user would expect so disallow it.
if url and (source == 'SYSTEM'):
sys.stderr.write("Cannot use \"--url\" and \"--source=SYSTEM\" together.\n")
return False
extension_repos = context.preferences.extensions.repos
if clear_all:
while extension_repos:
@@ -438,6 +445,7 @@ class subcmd_repo:
module=repo_id,
custom_directory=directory,
remote_url=url,
source=source,
)
repo.use_cache = cache
@@ -758,6 +766,18 @@ def cli_extension_args_repo_add(subparsers: "argparse._SubParsersAction[argparse
),
)
subparse.add_argument(
"--source",
dest="source",
choices=('USER', 'SYSTEM'),
default='USER',
metavar="SOURCE",
help=(
"The type of source in ('USER', 'SYSTEM').\n"
"System repositories are managed outside of Blender and are considered read-only."
),
)
subparse.add_argument(
"--cache",
dest="cache",
@@ -786,6 +806,7 @@ def cli_extension_args_repo_add(subparsers: "argparse._SubParsersAction[argparse
name=args.name,
directory=args.directory,
url=args.url,
source=args.source,
cache=args.cache,
clear_all=args.clear_all,
no_prefs=args.no_prefs,

View File

@@ -174,6 +174,20 @@ static const EnumPropertyItem rna_enum_preference_gpu_backend_items[] = {
{0, nullptr, 0, nullptr, nullptr},
};
static const EnumPropertyItem rna_enum_preferences_extension_repo_source_type_items[] = {
{USER_EXTENSION_REPO_SOURCE_USER,
"USER",
0,
"User",
"Repository managed by the user, stored in user directories"},
{USER_EXTENSION_REPO_SOURCE_SYSTEM,
"SYSTEM",
0,
"System",
"Read-only repository provided by the system"},
{0, nullptr, 0, nullptr, nullptr},
};
#ifdef RNA_RUNTIME
# include "BLI_math_vector.h"
@@ -604,7 +618,8 @@ static void rna_userdef_asset_library_remove(ReportList *reports, PointerRNA *pt
static bUserExtensionRepo *rna_userdef_extension_repo_new(const char *name,
const char *module,
const char *custom_directory,
const char *remote_url)
const char *remote_url,
const int source)
{
Main *bmain = G.main;
BKE_callback_exec_null(bmain, BKE_CB_EVT_EXTENSION_REPOS_UPDATE_PRE);
@@ -623,6 +638,8 @@ static bUserExtensionRepo *rna_userdef_extension_repo_new(const char *name,
repo->flag |= USER_EXTENSION_REPO_FLAG_USE_CUSTOM_DIRECTORY;
}
repo->source = source;
BKE_callback_exec_null(bmain, BKE_CB_EVT_EXTENSION_REPOS_UPDATE_POST);
USERDEF_TAG_DIRTY;
return repo;
@@ -6794,20 +6811,6 @@ static void rna_def_userdef_filepaths_extension_repo(BlenderRNA *brna)
StructRNA *srna;
PropertyRNA *prop;
static const EnumPropertyItem source_type_items[] = {
{USER_EXTENSION_REPO_SOURCE_USER,
"USER",
0,
"User",
"Repository managed by the user, stored in user directories"},
{USER_EXTENSION_REPO_SOURCE_SYSTEM,
"SYSTEM",
0,
"System",
"Read-only repository provided by the system"},
{0, nullptr, 0, nullptr, nullptr},
};
srna = RNA_def_struct(brna, "UserExtensionRepo", nullptr);
RNA_def_struct_sdna(srna, "bUserExtensionRepo");
RNA_def_struct_ui_text(
@@ -6861,7 +6864,7 @@ static void rna_def_userdef_filepaths_extension_repo(BlenderRNA *brna)
RNA_def_property_update(prop, 0, "rna_userdef_extension_sync_update");
prop = RNA_def_property(srna, "source", PROP_ENUM, PROP_NONE);
RNA_def_property_enum_items(prop, source_type_items);
RNA_def_property_enum_items(prop, rna_enum_preferences_extension_repo_source_type_items);
RNA_def_property_enum_funcs(prop, nullptr, "rna_userdef_extension_repo_source_set", nullptr);
RNA_def_property_ui_text(
prop,
@@ -7028,6 +7031,12 @@ static void rna_def_userdef_extension_repos_collection(BlenderRNA *brna, Propert
"");
RNA_def_string(
func, "remote_url", nullptr, sizeof(bUserExtensionRepo::remote_url), "Remote URL", "");
RNA_def_enum(func,
"source",
rna_enum_preferences_extension_repo_source_type_items,
USER_EXTENSION_REPO_SOURCE_USER,
"Source",
"How the repository is managed");
/* return type */
parm = RNA_def_pointer(func, "repo", "UserExtensionRepo", "", "Newly added repository");