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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
#include "../include/Package.hpp"
#include "../include/curl.hpp"
#include "../external/rapidjson/error/en.h"
using namespace std;
using namespace rapidjson;
static Document *packageList = nullptr;
namespace sibs
{
// TODO: Use containsPlatform in Conf.hpp instead? dont need a vector of string when we can use vector of enum
bool containsPlatform(const vector<string> &supportedPlatforms, const char *platform)
{
for(const string &supportedPlatform : supportedPlatforms)
{
if(strcmp(supportedPlatform.c_str(), platform) == 0 || strcmp(supportedPlatform.c_str(), "any") == 0)
return true;
}
return false;
}
Result<PackageMetadata> getPackageMetadata(Value::ConstObject jsonObj)
{
const auto &description = jsonObj["description"];
if(!description.IsString()) return Result<PackageMetadata>::Err("Expected description to be a string");
const auto &version = jsonObj["version"];
if(!version.IsString()) return Result<PackageMetadata>::Err("Expected version to be a string");
const auto &platforms = jsonObj["platforms"];
if(!platforms.IsArray() || platforms.GetArray().Empty()) return Result<PackageMetadata>::Err("Expected platforms to be an array of strings");
const auto &urls = jsonObj["urls"];
if(!urls.IsArray() || urls.GetArray().Empty()) return Result<PackageMetadata>::Err("Expected urls to be an array of string");
PackageMetadata packageMetadata;
packageMetadata.description.assign(description.GetString(), description.GetStringLength());
packageMetadata.version.assign(version.GetString(), version.GetStringLength());
const auto &platformsArray = platforms.GetArray();
packageMetadata.platforms.reserve(platformsArray.Size());
for(int i = 0; i < platformsArray.Size(); ++i)
{
const auto &platformElement = platformsArray[i];
if(!platformElement.IsString())
return Result<PackageMetadata>::Err("Expected platforms to only contain strings");
packageMetadata.platforms.push_back(platformElement.GetString());
}
const auto &urlsArray = urls.GetArray();
packageMetadata.urls.reserve(urlsArray.Size());
for(int i = 0; i < urlsArray.Size(); ++i)
{
const auto &urlElement = urlsArray[i];
if(!urlElement.IsString())
return Result<PackageMetadata>::Err("Expected urls to only contain strings");
packageMetadata.urls.push_back(urlElement.GetString());
}
return Result<PackageMetadata>::Ok(packageMetadata);
}
Result<string> getPackageUrl(const PackageMetadata &packageMetadata, const char *packageName, const char *packageVersion, const char *platform)
{
if(strcmp(packageMetadata.version.c_str(), packageVersion) != 0)
{
string errMsg = "Package \"";
errMsg += packageName;
errMsg += "\" does not exist for version \"";
errMsg += packageVersion;
errMsg += "\"";
return Result<string>::Err(errMsg);
}
if(!containsPlatform(packageMetadata.platforms, platform))
{
string errMsg = "Package \"";
errMsg += packageName;
errMsg += "\" with version \"";
errMsg += packageVersion;
errMsg += "\" does not support platform \"";
errMsg += platform;
errMsg += "\"";
return Result<string>::Err(errMsg);
}
return Result<string>::Ok(packageMetadata.urls[0]);
}
// TODO: Always downloading is fine right now because the package list is small. This should later be modified to use local cache.
// The package file should be stored locally (at ~/.cache/sibs/packages.json) and the version file should also be stored.
// First we check if the version is incorrect and if it, we download the new packages.json file.
// We should only check if the package list is up to date once every 10 minute or so (make it configurable in a config file?)
// to improve build performance and reduce server load.
// Or maybe we dont have to do that at all....
Result<Document*> Package::getPackageList(const char *url)
{
if(packageList)
return Result<Document*>::Ok(packageList);
HttpResult httpResult = curl::get(url);
if(!httpResult.success)
return Result<Document*>::Err(httpResult.str, httpResult.httpCode);
Document *doc = new Document();
ParseResult parseResult = doc->Parse(httpResult.str.c_str());
if(!parseResult)
{
string errMsg = "Failed to parse package list json file: ";
errMsg += GetParseError_En(parseResult.Code());
errMsg += " (";
errMsg += to_string(parseResult.Offset());
errMsg += ")";
return Result<Document*>::Err(errMsg);
}
packageList = doc;
return Result<Document*>::Ok(packageList);
}
Result<string> Package::getPackageUrl(const char *packageName, const char *packageVersion, const char *platform)
{
Result<Document*> packageList = Package::getPackageList("https://raw.githubusercontent.com/DEC05EBA/sibs_packages/master/packages.json");
if(!packageList)
return Result<string>::Err(packageList);
const Document &packageDoc = *packageList.unwrap();
const Value &packageMetaDataJson = packageDoc[packageName];
if(packageMetaDataJson.IsObject())
{
Result<PackageMetadata> packageMetadataResult = getPackageMetadata(packageMetaDataJson.GetObject());
if(!packageMetadataResult) return Result<string>::Err(packageMetadataResult);
return ::sibs::getPackageUrl(packageMetadataResult.unwrap(), packageName, packageVersion, platform);
}
else if(packageMetaDataJson.IsArray())
{
int i = 0;
for(const auto &packageData : packageMetaDataJson.GetArray())
{
if(!packageData.IsObject())
{
string errMsg = "Package file is corrupt. ";
errMsg += packageName;
errMsg += "[";
errMsg += to_string(i);
errMsg += "] is not an object";
return Result<string>::Err(errMsg);
}
Result<PackageMetadata> packageMetadataResult = getPackageMetadata(packageMetaDataJson.GetObject());
if(packageMetadataResult)
{
Result<string> packageUrlResult = ::sibs::getPackageUrl(packageMetadataResult.unwrap(), packageName, packageVersion, platform);
if(packageUrlResult)
return packageUrlResult;
}
++i;
}
string errMsg = "Package \"";
errMsg += packageName;
errMsg += "\" with version \"";
errMsg += packageVersion;
errMsg += "\" does not exist or does not exist for platform \"";
errMsg += platform;
errMsg += "\"";
return Result<string>::Err(errMsg);
}
else
{
string errMsg = "No package with the name \"";
errMsg += packageName;
errMsg += "\" was found";
return Result<string>::Err(errMsg);
}
}
}
|