aboutsummaryrefslogtreecommitdiff
path: root/src/Package.cpp
blob: 925de82360c0c017d074ce1f6a0c59ff7d816690 (plain)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "../include/Package.hpp"
#include "../include/curl.hpp"
#include "../include/VersionParser.hpp"
#include "../external/rapidjson/error/en.h"

#ifdef GetObject
#undef GetObject
#endif

using namespace std;
using namespace rapidjson;

static Document *packageList = nullptr;

namespace sibs
{
    static vector<Platform> getPlatformsByNames(const vector<string> &platformNames)
    {
        vector<Platform> result;
        result.reserve(platformNames.size());
        for(const string &platformName : platformNames)
        {
            result.push_back(getPlatformByName(StringView(platformName.data(), platformName.size())));
        }
        return result;
    }

    static Result<PackageMetadata> getPackageMetadata(Value::ConstObject jsonObj)
    {
        const auto &description = jsonObj.FindMember("description");
        if(description == jsonObj.MemberEnd() || !description->value.IsString()) return Result<PackageMetadata>::Err("Expected description to be a string");
        const auto &version = jsonObj.FindMember("version");
        if(version == jsonObj.MemberEnd() || !version->value.IsString()) return Result<PackageMetadata>::Err("Expected version to be a string");
        const auto &platforms = jsonObj.FindMember("platforms");
        if(platforms == jsonObj.MemberEnd() || !platforms->value.IsArray() || platforms->value.GetArray().Empty()) return Result<PackageMetadata>::Err("Expected platforms to be an array of strings");
        const auto &urls = jsonObj.FindMember("urls");
        if(urls == jsonObj.MemberEnd() || !urls->value.IsArray() || urls->value.GetArray().Empty()) return Result<PackageMetadata>::Err("Expected urls to be an array of string");
        
        PackageMetadata packageMetadata;
        packageMetadata.description.assign(description->value.GetString(), description->value.GetStringLength());

        int versionStrSize = 0;
        Result<PackageVersion> versionResult = parsePackageVersion({ version->value.GetString(), version->value.GetStringLength() }, &versionStrSize);
        if(!versionResult)
            return Result<PackageMetadata>::Err("package version is in wrong fromat, error: " + versionResult.getErrMsg());
        if(versionStrSize != (int)version->value.GetStringLength())
            return Result<PackageMetadata>::Err("package version is in wrong format, expected only numbers and dots (version: " + string(version->value.GetString(), version->value.GetStringLength()) + ")");

        packageMetadata.version = versionResult.unwrap();
        
        const auto &platformsArray = platforms->value.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->value.GetArray();
        if(urlsArray.Empty())
            return Result<PackageMetadata>::Err("Expected url list to not be empty");

        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);
    }
    
    static Result<bool> isPackageUsableForPlatform(const PackageMetadata &packageMetadata, const char *packageName, const PackageVersionRange &versionRange, Platform platform)
    {
        if(!versionRange.isInRange(packageMetadata.version))
        {
            string errMsg = "Package \"";
            errMsg += packageName;
            errMsg += "\" does not exist for version range \"";
            errMsg += versionRange.toString();
            errMsg += "\"";
            return Result<bool>::Err(errMsg);
        }
        
        if(!containsPlatform(getPlatformsByNames(packageMetadata.platforms), platform))
        {
            string errMsg = "Package \"";
            errMsg += packageName;
            errMsg += "\" with version \"";
            errMsg += packageMetadata.version.toString();
            errMsg += "\" does not support platform \"";
            errMsg += platform;
            errMsg += "\"";
            return Result<bool>::Err(errMsg);
        }

        return Result<bool>::Ok(true);
    }
    
    // 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<PackageMetadata> Package::getPackage(const char *packageName, const PackageVersionRange &versionRange, Platform platform)
    {
        Result<Document*> packageList = Package::getPackageList("https://gitlab.com/DEC05EBA/sibs_packages/raw/master/packages.json");
        if(!packageList)
            return Result<PackageMetadata>::Err(packageList);
        
        const Document &packageDoc = *packageList.unwrap();
        
		auto packageMetaDataJsonIt = packageDoc.FindMember(packageName);
		if (packageMetaDataJsonIt == packageDoc.MemberEnd())
		{
			string errMsg = "No package with the name \"";
			errMsg += packageName;
			errMsg += "\" was found";
			return Result<PackageMetadata>::Err(errMsg);
		}

        if(packageMetaDataJsonIt->value.IsObject())
        {
            Result<PackageMetadata> packageMetadataResult = getPackageMetadata(packageMetaDataJsonIt->value.GetObject());
            if(!packageMetadataResult) return packageMetadataResult;
            Result<bool> packageUsableResult = isPackageUsableForPlatform(packageMetadataResult.unwrap(), packageName, versionRange, platform);
            if(packageUsableResult)
                return Result<PackageMetadata>::Ok(packageMetadataResult.unwrap());
            else
                return Result<PackageMetadata>::Err(packageUsableResult);
        }
        else if(packageMetaDataJsonIt->value.IsArray())
        {
            int i = 0;
            for(const auto &packageData : packageMetaDataJsonIt->value.GetArray())
            {
                if(!packageData.IsObject())
                {
                    string errMsg = "Package file is corrupt. ";
                    errMsg += packageName;
                    errMsg += "[";
                    errMsg += to_string(i);
                    errMsg += "] is not an object";
                    return Result<PackageMetadata>::Err(errMsg);
                }
                
                Result<PackageMetadata> packageMetadataResult = getPackageMetadata(packageData.GetObject());
                if(!packageMetadataResult) return packageMetadataResult;
                if(isPackageUsableForPlatform(packageMetadataResult.unwrap(), packageName, versionRange, platform))
                    return Result<PackageMetadata>::Ok(packageMetadataResult.unwrap());
            
                ++i;
            }
            
            string errMsg = "Package \"";
            errMsg += packageName;
            errMsg += "\" in version range \"";
            errMsg += versionRange.toString();
            errMsg += "\" does not exist or does not exist for platform \"";
            errMsg += asString(platform);
            errMsg += "\"";
            return Result<PackageMetadata>::Err(errMsg);
        }
        else
        {
            string errMsg = "No package with the name \"";
            errMsg += packageName;
            errMsg += "\" was found";
            return Result<PackageMetadata>::Err(errMsg);
        }
    }
}