-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathcore.cpp
More file actions
122 lines (101 loc) · 4.9 KB
/
core.cpp
File metadata and controls
122 lines (101 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// system headers
#include <algorithm>
#include <assert.h>
#include <filesystem>
#include <iostream>
// local headers
#include <linuxdeploy/core/appdir.h>
#include <linuxdeploy/log/log.h>
#include "core.h"
using namespace linuxdeploy::core;
using namespace linuxdeploy::log;
using namespace linuxdeploy::desktopfile;
namespace fs = std::filesystem;
namespace linuxdeploy {
class DeployError : public std::runtime_error {
public:
explicit DeployError(const std::string& what) : std::runtime_error(what) {};
};
/**
* Resolve the 'MAIN' desktop file from all the available.
*
* @param desktopFilePaths
* @param deployedDesktopFiles
* @return the MAIN DesktopFile
* @throw DeployError in case of 'deployed desktop file not found'
*/
desktopfile::DesktopFile getMainDesktopFile(const std::vector<std::string>& desktopFilePaths,
const std::vector<desktopfile::DesktopFile>& deployedDesktopFiles) {
if (desktopFilePaths.empty()) {
ldLog() << LD_WARNING << "No desktop file specified, using first desktop file found:"
<< deployedDesktopFiles[0].path() << std::endl;
return deployedDesktopFiles[0];
}
auto firstDeployedDesktopFileName = fs::path(desktopFilePaths.front()).filename().string();
auto desktopFileMatchingName = find_if(
deployedDesktopFiles.begin(),
deployedDesktopFiles.end(),
[&firstDeployedDesktopFileName](const desktopfile::DesktopFile& desktopFile) {
auto fileName = fs::path(desktopFile.path()).filename().string();
return fileName == firstDeployedDesktopFileName;
}
);
if (desktopFileMatchingName != deployedDesktopFiles.end()) {
return *desktopFileMatchingName;
} else {
ldLog() << LD_ERROR << "Could not find desktop file deployed earlier any more:"
<< firstDeployedDesktopFileName << std::endl;
throw DeployError("Old desktop file is not reachable.");
}
}
bool deployAppDirRootFiles(std::vector<std::string> desktopFilePaths,
std::string customAppRunPath, appdir::AppDir& appDir) {
ldLog() << std::endl << "-- Deploying files into AppDir root directory --" << std::endl;
if (!customAppRunPath.empty()) {
ldLog() << LD_INFO << "Deploying custom AppRun: " << customAppRunPath << std::endl;
const auto& appRunPathInAppDir = appDir.path() / "AppRun";
if (fs::exists(appRunPathInAppDir)) {
ldLog() << LD_WARNING << "File exists, replacing with custom AppRun" << std::endl;
fs::remove(appRunPathInAppDir);
}
appDir.deployFile(customAppRunPath, appDir.path() / "AppRun");
appDir.executeDeferredOperations();
}
auto deployedDesktopFiles = appDir.deployedDesktopFiles();
if (deployedDesktopFiles.empty()) {
ldLog() << LD_WARNING << "Could not find desktop file in AppDir, cannot create links for AppRun, "
"desktop file and icon in AppDir root" << std::endl;
return true;
}
try {
desktopfile::DesktopFile desktopFile = getMainDesktopFile(desktopFilePaths, deployedDesktopFiles);
ldLog() << "Deploying files to AppDir root using desktop file:" << desktopFile.path() << std::endl;
return appDir.setUpAppDirRoot(desktopFile, customAppRunPath);
} catch (const DeployError& er) {
return false;
}
}
bool addDefaultKeys(DesktopFile& desktopFile, const std::string& executableFileName) {
ldLog() << "Adding default values to desktop file:" << desktopFile.path() << std::endl;
auto rv = true;
auto setDefault = [&rv, &desktopFile](const std::string& section, const std::string& key, const std::string& value) {
if (desktopFile.entryExists(section, key)) {
DesktopFileEntry entry;
// this should never return false
auto entryExists = desktopFile.getEntry(section, key, entry);
assert(entryExists);
ldLog() << LD_WARNING << "Key exists, not modified:" << key << "(current value:" << entry.value() << LD_NO_SPACE << ")" << std::endl;
rv = false;
} else {
auto entryOverwritten = desktopFile.setEntry(section, DesktopFileEntry(key, value));
assert(!entryOverwritten);
}
};
setDefault("Desktop Entry", "Name", executableFileName);
setDefault("Desktop Entry", "Exec", executableFileName);
setDefault("Desktop Entry", "Icon", executableFileName);
setDefault("Desktop Entry", "Type", "Application");
setDefault("Desktop Entry", "Categories", "Utility;");
return rv;
}
}