-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.hpp
169 lines (161 loc) · 5.03 KB
/
repository.hpp
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
//
// repository.hpp
// venv
//
// Created by Daher Alfawares on 11/30/17.
// Copyright © 2017 Daher Alfawares. All rights reserved.
//
#ifndef repository_hpp
#define repository_hpp
#include "static.hpp"
#include "x_table.hpp"
#include "venv.hpp"
#include <map>
#include <vector>
#include <fstream>
#include <string>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/map.hpp>
#include <boost/serialization/set.hpp>
namespace v {
namespace {
const std::string root_folder = ".venv";
}
class repository {
typedef std::string string;
typedef std::map<string,v::venv> map;
map venvs;
string current_venv;
public:
bool exists(const string& name) const {
if(venvs.find(name) == venvs.end()){
return false;
}
return true;
}
void assert_available(const string& name) const {
if(!this->exists(name)){
std::cerr << "can't find venv [" << name << "]" << std::endl;
throw -1;
}
}
void assert_available(const std::vector<string>& names) const {
for(auto name:names){
this->assert_available(name);
}
}
void create(const v::venv& venv){
if(this->exists(venv.name)){
std::cerr << "already exists" << std::endl;
throw -1;
}
venvs[venv.name] = venv;
current_venv = venv.name;
}
void rename(string old_name,string new_name){
this->assert_available(old_name);
if(this->exists(new_name)){
std::cerr << "already exists" << std::endl;
throw -1;
}
auto venv = this->venvs[old_name];
venv.name = new_name;
this->venvs.erase(old_name);
this->venvs[new_name] = venv;
if(current_venv == old_name){
current_venv = new_name;
}
}
void remove(string name){
if(this->exists(name)){
if(name == this->current().name){
std::cerr << "can't remove the active venv, select a another first." << std::endl;
throw -1;
}
std::cerr << "removing [" << name << "]" << std::endl;
auto venv = venvs[name];
for(auto item:venv.items){
venv.remove(item);
}
this->venvs.erase(name);
if(current_venv == name){
current_venv = "";
}
return;
} else {
std::cerr << "can't find [" << name << "]" << std::endl;
}
}
repository(){
std::ifstream ifs(v::root_folder+"/venv");
if(ifs.is_open()){
boost::archive::text_iarchive ia(ifs);
ia & venvs;
ia & current_venv;
}
}
~repository(){
std::ofstream ofs(v::root_folder+"/venv");
if(ofs.is_open()){
boost::archive::text_oarchive oa(ofs);
oa & venvs;
oa & current_venv;
}
}
void select(const std::string& name){
this->assert_available(name);
}
venv & operator [](const string& name){
this->assert_available(name);
return venvs[name];
}
const map& all_venvs() const {
return this->venvs;
}
venv& current(){
this->assert_available(this->current_venv);
return this->venvs[this->current_venv];
}
venv& current(string name){
this->assert_available(name);
this->current_venv = name;
return current();
}
};
inline
std::ostream& operator << (std::ostream& stream, repository& repo){
auto venvs = repo.all_venvs();
if(venvs.empty()){
stream << "repository is empty.";
return stream;
}
x::table out("list");
out("")("label")("path")("internal path")++;
for(auto venv:venvs){
auto v = venv.second;
auto selected = "☐";
if(v.name == repo.current().name){
selected = "☒";
}
for(auto i:v.items){
out(selected)(v.name)(v.external_path(i))(v.internal_path(i))++;
}
}
stream << out;
std::vector<std::string> empty_venvs;
for(auto venv:venvs){
if(venv.second.items.empty()){
empty_venvs.push_back(venv.second.name);
}
}
if(!empty_venvs.empty()){
stream << std::endl << "empty venvs:" << std::endl;
for(auto name:empty_venvs){
stream << "\t[" << name << "]" << std::endl;
}
}
return stream;
}
}
#endif /* repository_hpp */