summaryrefslogtreecommitdiff
path: root/tools/minicargo/repository.cpp
blob: c6fe284f393d12932793d30e5d06494c61222507 (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
/*
 */
#include "repository.h"
#include "debug.h"

void Repository::load_cache(const ::helpers::path& path)
{
    throw "";
}
void Repository::load_vendored(const ::helpers::path& path)
{
    // Enumerate folders in this folder, try to open Cargo.toml files
    // Extract package name and version from each manifest
}

::std::shared_ptr<PackageManifest> Repository::from_path(::helpers::path in_path)
{
    DEBUG("Repository::from_path(" << in_path << ")");
    // 1. Normalise path
    auto path = in_path.normalise();
    DEBUG("path = " << path);

    auto it = m_path_cache.find(path);
    if(it == m_path_cache.end())
    {
        ::std::shared_ptr<PackageManifest> rv ( new PackageManifest(PackageManifest::load_from_toml(path)) );

        m_path_cache.insert( ::std::make_pair(::std::move(path), rv) );

        return rv;
    }
    else
    {
        return it->second;
    }
}
::std::shared_ptr<PackageManifest> Repository::find(const ::std::string& name, const PackageVersionSpec& version)
{
    auto itp = m_cache.equal_range(name);

    Entry* best = nullptr;
    for(auto i = itp.first; i != itp.second; ++i)
    {
        if( version.accepts(i->second.version) )
        {
            if( !best || best->version < i->second.version )
            {
                best = &i->second;
            }
        }
    }

    if( best )
    {
        if( !best->loaded_manifest )
        {
            if( best->manifest_path == "" )
            {
                throw "TODO: Download package";
            }
            best->loaded_manifest = ::std::shared_ptr<PackageManifest>( new PackageManifest(PackageManifest::load_from_toml(best->manifest_path)) );
        }

        return best->loaded_manifest;
    }
    else
    {
        return {};
    }
}