Skip to content

Commit

Permalink
feat: auto install some modules
Browse files Browse the repository at this point in the history
  • Loading branch information
black-desk committed Dec 25, 2024
1 parent e1c9652 commit 49d90e9
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 21 deletions.
23 changes: 22 additions & 1 deletion api/schema/v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,28 @@
],
"properties": {
"package": {
"$ref": "#/$defs/PackageManager1Package"
"title": "PackageManager1InstallParametersPacakge",
"type": "object",
"required": [
"id"
],
"properties": {
"channel": {
"type": "string"
},
"id": {
"type": "string"
},
"version": {
"type": "string"
},
"modules": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
"options": {
"$ref": "#/$defs/CommonOptions"
Expand Down
16 changes: 15 additions & 1 deletion api/schema/v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,21 @@ $defs:
- options
properties:
package:
$ref: '#/$defs/PackageManager1Package'
title: PackageManager1InstallParametersPacakge
type: object
required:
- id
properties:
channel:
type: string
id:
type: string
version:
type: string
modules:
type: array
items:
type: string
options:
$ref: '#/$defs/CommonOptions'
PackageManager1PackageTaskResult:
Expand Down
43 changes: 34 additions & 9 deletions libs/api/src/linglong/api/types/v1/Generators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@
#include "linglong/api/types/v1/PackageManager1SearchParameters.hpp"
#include "linglong/api/types/v1/PackageManager1RequestInteractionAdditionalMessage.hpp"
#include "linglong/api/types/v1/PackageManager1PackageTaskResult.hpp"
#include "linglong/api/types/v1/PackageManager1Package.hpp"
#include "linglong/api/types/v1/PackageManager1ModifyRepoParameters.hpp"
#include "linglong/api/types/v1/PackageManager1JobInfo.hpp"
#include "linglong/api/types/v1/PackageManager1InstallParameters.hpp"
#include "linglong/api/types/v1/PackageManager1Package.hpp"
#include "linglong/api/types/v1/PackageManager1InstallParametersPacakge.hpp"
#include "linglong/api/types/v1/PackageManager1GetRepoInfoResult.hpp"
#include "linglong/api/types/v1/PackageManager1GetRepoInfoResultRepoInfo.hpp"
#include "linglong/api/types/v1/PackageInfoV2.hpp"
Expand Down Expand Up @@ -145,8 +146,8 @@ void to_json(json & j, const PackageManager1GetRepoInfoResultRepoInfo & x);
void from_json(const json & j, PackageManager1GetRepoInfoResult & x);
void to_json(json & j, const PackageManager1GetRepoInfoResult & x);

void from_json(const json & j, PackageManager1Package & x);
void to_json(json & j, const PackageManager1Package & x);
void from_json(const json & j, PackageManager1InstallParametersPacakge & x);
void to_json(json & j, const PackageManager1InstallParametersPacakge & x);

void from_json(const json & j, PackageManager1InstallParameters & x);
void to_json(json & j, const PackageManager1InstallParameters & x);
Expand All @@ -157,6 +158,9 @@ void to_json(json & j, const PackageManager1JobInfo & x);
void from_json(const json & j, PackageManager1ModifyRepoParameters & x);
void to_json(json & j, const PackageManager1ModifyRepoParameters & x);

void from_json(const json & j, PackageManager1Package & x);
void to_json(json & j, const PackageManager1Package & x);

void from_json(const json & j, PackageManager1PackageTaskResult & x);
void to_json(json & j, const PackageManager1PackageTaskResult & x);

Expand Down Expand Up @@ -673,21 +677,21 @@ j["message"] = x.message;
j["type"] = x.type;
}

inline void from_json(const json & j, PackageManager1Package& x) {
inline void from_json(const json & j, PackageManager1InstallParametersPacakge& x) {
x.channel = get_stack_optional<std::string>(j, "channel");
x.id = j.at("id").get<std::string>();
x.packageManager1PackageModule = get_stack_optional<std::string>(j, "module");
x.modules = get_stack_optional<std::vector<std::string>>(j, "modules");
x.version = get_stack_optional<std::string>(j, "version");
}

inline void to_json(json & j, const PackageManager1Package & x) {
inline void to_json(json & j, const PackageManager1InstallParametersPacakge & x) {
j = json::object();
if (x.channel) {
j["channel"] = x.channel;
}
j["id"] = x.id;
if (x.packageManager1PackageModule) {
j["module"] = x.packageManager1PackageModule;
if (x.modules) {
j["modules"] = x.modules;
}
if (x.version) {
j["version"] = x.version;
Expand All @@ -696,7 +700,7 @@ j["version"] = x.version;

inline void from_json(const json & j, PackageManager1InstallParameters& x) {
x.options = j.at("options").get<CommonOptions>();
x.package = j.at("package").get<PackageManager1Package>();
x.package = j.at("package").get<PackageManager1InstallParametersPacakge>();
}

inline void to_json(json & j, const PackageManager1InstallParameters & x) {
Expand Down Expand Up @@ -733,6 +737,27 @@ j["defaultRepo"] = x.defaultRepo;
j["repos"] = x.repos;
}

inline void from_json(const json & j, PackageManager1Package& x) {
x.channel = get_stack_optional<std::string>(j, "channel");
x.id = j.at("id").get<std::string>();
x.packageManager1PackageModule = get_stack_optional<std::string>(j, "module");
x.version = get_stack_optional<std::string>(j, "version");
}

inline void to_json(json & j, const PackageManager1Package & x) {
j = json::object();
if (x.channel) {
j["channel"] = x.channel;
}
j["id"] = x.id;
if (x.packageManager1PackageModule) {
j["module"] = x.packageManager1PackageModule;
}
if (x.version) {
j["version"] = x.version;
}
}

inline void from_json(const json & j, PackageManager1PackageTaskResult& x) {
x.taskObjectPath = get_stack_optional<std::string>(j, "taskObjectPath");
x.code = j.at("code").get<int64_t>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "linglong/api/types/v1/helper.hpp"

#include "linglong/api/types/v1/CommonOptions.hpp"
#include "linglong/api/types/v1/PackageManager1Package.hpp"
#include "linglong/api/types/v1/PackageManager1InstallParametersPacakge.hpp"

namespace linglong {
namespace api {
Expand All @@ -35,7 +35,7 @@ using nlohmann::json;
*/
struct PackageManager1InstallParameters {
CommonOptions options;
PackageManager1Package package;
PackageManager1InstallParametersPacakge package;
};
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// This file is generated by tools/codegen.sh
// DO NOT EDIT IT.

// clang-format off

// To parse this JSON data, first install
//
// json.hpp https://github.com/nlohmann/json
//
// Then include this file, and then do
//
// PackageManager1InstallParametersPacakge.hpp data = nlohmann::json::parse(jsonString);

#pragma once

#include <optional>
#include <nlohmann/json.hpp>
#include "linglong/api/types/v1/helper.hpp"

namespace linglong {
namespace api {
namespace types {
namespace v1 {
using nlohmann::json;

struct PackageManager1InstallParametersPacakge {
std::optional<std::string> channel;
std::string id;
std::optional<std::vector<std::string>> modules;
std::optional<std::string> version;
};
}
}
}
}

// clang-format on
88 changes: 87 additions & 1 deletion libs/linglong/src/linglong/cli/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <QEventLoop>

Check warning on line 37 in libs/linglong/src/linglong/cli/cli.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QEventLoop> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QFileInfo>

Check warning on line 38 in libs/linglong/src/linglong/cli/cli.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QFileInfo> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <cstdlib>

Check warning on line 40 in libs/linglong/src/linglong/cli/cli.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <cstdlib> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <filesystem>

Check warning on line 41 in libs/linglong/src/linglong/cli/cli.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <filesystem> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <iostream>

Check warning on line 42 in libs/linglong/src/linglong/cli/cli.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <iostream> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <system_error>

Check warning on line 43 in libs/linglong/src/linglong/cli/cli.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <system_error> not found. Please note: Cppcheck does not need standard library headers to get proper results.
Expand All @@ -47,6 +48,84 @@

using namespace linglong::utils::error;

namespace {

static std::vector<std::string> getAutoModuleList() noexcept
{
auto getModuleFromLanguageEnv = [](const std::string &lang) -> std::vector<std::string> {
if (lang.length() < 2) {
return {};
}

if (!std::all_of(lang.begin(), lang.begin() + 2, [](char c) {
return 'a' <= c && c <= 'z';
})) {
return {};
}

std::vector<std::string> modules;
modules.push_back("lang_" + lang.substr(0, 2));

if (lang.length() == 2) {
return modules;
}

if (lang[2] == '.') {
return modules;
}

if (lang[2] == '@') {
return modules;
}

if (lang[2] != '_') {
return {};
}

if (lang.length() < 5) {
return {};
}

modules.push_back("lang_" + lang.substr(0, 5));

if (lang.length() == 5) {
return modules;
}

if (lang[5] == '.') {
return modules;
}

if (lang[5] == '@') {
return modules;
}

return {};
};

auto envs = {
"LANG", "LC_ADDRESS", "LC_ALL", "LC_IDENTIFICATION",
"LC_MEASUREMENT", "LC_MESSAGES", "LC_MONETARY", "LC_NAME",
"LC_NUMERIC", "LC_PAPER", "LC_TELEPHONE", "LC_TIME",
};

std::vector<std::string> result = { "binary" };

for (const auto &env : envs) {
auto lang = getenv(env);
if (lang == nullptr) {
continue;
}
auto modules = getModuleFromLanguageEnv(lang);
result.insert(result.end(), modules.begin(), modules.end());
}

std::sort(result.begin(), result.end());
return { result.begin(), std::unique(result.begin(), result.end()) };
}

} // namespace

namespace linglong::cli {

void Cli::onTaskPropertiesChanged(QString interface, // NOLINT
Expand Down Expand Up @@ -947,7 +1026,14 @@ int Cli::install()
}

if (!options.module.empty()) {
params.package.packageManager1PackageModule = options.module;
params.package.modules = { options.module };
} else {
params.package.modules = getAutoModuleList();
}

qDebug() << "Install modules";
for (const auto &module : *params.package.modules) {
qDebug() << module.c_str();
}

auto pendingReply = this->pkgMan.Install(utils::serialize::toQVariantMap(params));
Expand Down
29 changes: 22 additions & 7 deletions libs/linglong/src/linglong/package_manager/package_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1092,12 +1092,26 @@ auto PackageManager::Install(const QVariantMap &parameters) noexcept -> QVariant
if (!paras) {
return toDBusReply(paras);
}

api::types::v1::PackageManager1Package package;
package.id = paras->package.id;
package.channel = paras->package.channel;
package.version = paras->package.version;

// 解析用户输入
auto fuzzyRef = fuzzyReferenceFromPackage(paras->package);
auto fuzzyRef = fuzzyReferenceFromPackage(package);
if (!fuzzyRef) {
return toDBusReply(fuzzyRef);
}
auto curModule = paras->package.packageManager1PackageModule.value_or("binary");

std::string curModule = "binary";

if (paras->package.modules && paras->package.modules->size() == 1) {
// Manually install single module
curModule = paras->package.modules->front();
}

auto modules = paras->package.modules.value_or(std::vector<std::string>{ curModule });

// 安装module
if (curModule != "binary") {
Expand Down Expand Up @@ -1207,6 +1221,7 @@ auto PackageManager::Install(const QVariantMap &parameters) noexcept -> QVariant
? std::make_optional(std::move(localRef).value())
: std::nullopt,
curModule,
modules,
skipInteraction = paras->options.skipInteraction,
msgType,
additionalMessage](PackageTask &taskRef) {
Expand Down Expand Up @@ -1236,11 +1251,11 @@ auto PackageManager::Install(const QVariantMap &parameters) noexcept -> QVariant
if (isTaskDone(taskRef.subState())) {
return;
}
auto modules = std::vector{ curModule };
if (localRef.has_value()) {
modules = this->repo.getModuleList(*localRef);
}
this->Install(taskRef, remoteRef, localRef, modules);

this->Install(taskRef,
remoteRef,
localRef,
localRef.has_value() ? this->repo.getModuleList(*localRef) : modules);
};

auto taskRet = tasks.addNewTask({ refSpec }, std::move(installer), connection());
Expand Down

0 comments on commit 49d90e9

Please sign in to comment.