-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitcher.cpp
More file actions
484 lines (425 loc) · 16.4 KB
/
Copy pathswitcher.cpp
File metadata and controls
484 lines (425 loc) · 16.4 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <iomanip>
#include <windows.h>
using namespace std;
void copyCharArray(char*a[],char*b[],int size){
for(int i=0;i<size;i++){
a[i] = b[i];
}
}
string toRawString(std::string const& in)
{
string ret = in;
auto p = ret.find('\t');
if ( p != ret.npos )
{
ret.replace(p, 1, "\\t");
}
return ret;
}
string join(vector<string> str,string separator){
string res = "";
for(int i=0;i<str.size();i++){
if(str[i].length() > 0){
res += str[i] + separator;
}
}
return res;
}
vector<string> split(string str,string delimiter){
vector<string> splitted;
int pos = 0;
string token;
while((pos = str.find(delimiter)) != string::npos){
token = str.substr(0,pos);
splitted.push_back(token);
str.erase(0,pos + delimiter.length());
}
splitted.push_back(str);
return splitted;
}
class PHP {
public:
string path;
string version;
static const string VERSION_KEY;
static const string PATH_KEY;
PHP (string version,string path){
this->version = version;
this->path = path;
}
};
const string PHP::VERSION_KEY = "--version";
const string PHP::PATH_KEY = "--path";
struct Color{
string name;
int code;
};
class Output{
public:
int defaultColor;
HANDLE hConsole;
Output(){
this->hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbiInfo);
this->defaultColor = csbiInfo.wAttributes;
this->setColors();
}
string changeColor(string str,string color){
Color c = this->getColor(color);
if(c.name != ""){
SetConsoleTextAttribute(this->hConsole, c.code);
// return c.code+str+this->getColor("reset").code;
cout << str;
// reset color to default terminal color
SetConsoleTextAttribute(this->hConsole, this->defaultColor);
}
return "";
}
private:
Color colors[4];
void setColors(){
Color c1,c2,c3,c4;
c1.name = "reset";
c1.code = this->defaultColor;
c2.name = "red";
c2.code = 4;
c3.name = "green";
c3.code = 2;
c4.name = "blue";
c4.code = 1;
colors[0] = c1;
colors[1] = c2;
colors[2] = c3;
colors[3] = c4;
}
Color getColor(string name){
for(int i=0;i<4;i++){
if(this->colors[i].name == name){
return this->colors[i];
}
}
Color empColor;
empColor.name = "default";
empColor.code = this->defaultColor;
return empColor;
}
};
class FileManager{
public:
vector<PHP>availableVersions;
FileManager(){
this->setupAvailableVersions();
}
void addLine(string filename,string line){
//create if file doesn't exists and append anything to its end
fstream file(filename,ios::ate | ios::app);
// file empty of not
if(file.tellg() == 0){
file << line;
}else{
file << "\n"+line;
}
file.close();
}
void removeVersion(string version){
// update the vector
for(int i=0;i<this->availableVersions.size();i++){
if(this->availableVersions[i].version == version){
this->availableVersions.erase(this->availableVersions.begin()+i);
break;
}
}
// update the db file
this->updateDatabase();
}
private:
// load all the saved php versions from database to the array/vector "availableVersions"
void setupAvailableVersions(){
fstream dbfile;
dbfile.open("database.txt",ios::in);
if(dbfile.is_open()){
string tp;
while(getline(dbfile,tp)){
tp = toRawString(tp);
if(tp.length() > 0){
string delimiter = "<===>";
string version = tp.substr(0,tp.find("<===>"));
string path = tp.substr(tp.find("<===>")+delimiter.length(),tp.length());
if(version.length() > 0 && path.length() > 0){
PHP p(version,path);
this->availableVersions.push_back(p);
}
}
}
dbfile.close();
}
}
void updateDatabase(){
fstream file("database.txt",fstream::out | fstream::trunc);
for(int i=0;i<this->availableVersions.size();i++){
file << this->availableVersions[i].version+"<===>"+this->availableVersions[i].path+"\n";
}
file.close();
}
};
class Registrar{
public:
vector<string> all_path_envs;
FileManager *fileManager;
Output *output;
Registrar(FileManager *fm,Output *op){
this->fileManager = fm;
this->output = op;
this->loadPathEnv();
}
//loads the user path env variable and saves it as vector in "all_path_envs"
void loadPathEnv(){
string path_env = this->registry_read(TEXT("Environment"),TEXT("Path"),REG_EXPAND_SZ);
vector<string> all_paths = split(path_env,";");
for(int i=0;i<all_paths.size();i++){
this->all_path_envs.push_back(all_paths[i]);
}
}
string registry_read (LPCTSTR subkey,LPCTSTR name,DWORD type)
{
HKEY key;
char value[1024];
DWORD value_length = 1024;
RegOpenKey(HKEY_CURRENT_USER,subkey,&key);
RegQueryValueEx(key,name, NULL, &type, (LPBYTE)value, &value_length);
RegCloseKey(key);
string str(value);
return str;
}
void registry_write (LPCTSTR subkey,LPCTSTR name,DWORD type,const char* value)
{
HKEY key;
RegOpenKey(HKEY_CURRENT_USER,subkey,&key);
RegSetValueEx(key, name, 0, type, (LPBYTE)value, strlen(value)*sizeof(char));
RegCloseKey(key);
}
void deactivate_all_versions(bool save = false){
for(int i=0;i < this->all_path_envs.size();i++){
for(int j=0;j< this->fileManager->availableVersions.size();j++){
if(this->all_path_envs[i] == this->fileManager->availableVersions[j].path){
this->all_path_envs.erase(this->all_path_envs.begin()+i);
cout << this->output->changeColor("INFO: ","blue") + "Version "+ this->fileManager->availableVersions[j].version + " has been deactivated" << endl;
}
}
}
if(save){
this->save_new_path();
}
}
PHP* current_activated_version(){
for(int i=0;i<this->all_path_envs.size();i++){
for(int j=0;j< this->fileManager->availableVersions.size();j++){
if(this->all_path_envs[i] == this->fileManager->availableVersions[j].path){
return new PHP(this->fileManager->availableVersions[j].version,this->fileManager->availableVersions[j].path);
}
}
}
return new PHP("","");
}
void save_new_path(){
string new_path = join(this->all_path_envs,";");
this->registry_write(TEXT("Environment"),TEXT("Path"),REG_EXPAND_SZ,new_path.c_str());
}
void activate_version(PHP php){
this->deactivate_all_versions();
this->all_path_envs.push_back(php.path);
this->save_new_path();
cout << this->output->changeColor("SUCCESS: ","green") + "Activated Version " + php.version;
cout << endl << "(Please Open A New Terminal To See Changes)";
}
};
class Manager;
class Validation{
public:
static PHP validatePHPVersion(Manager *manager,string version);
static bool alreadyExistCheck(Manager *manager,string version,bool shouldExistOrNot);
static void isNotActivatedCheck(Manager *manager,string version);
};
class Manager{
public:
Output *output;
int sizeOfArgs;
Manager(char* allArgs[],int argsSize){
copyCharArray(this->args,allArgs,argsSize);
this->sizeOfArgs = argsSize;
}
void setup(){
this->output = new Output;
this->fileManager = new FileManager;
this->registrar = new Registrar(this->fileManager,this->output);
}
void addPHP(){
string version = this->getArgumentValue(this->getArgument(PHP::VERSION_KEY),10);
string path = this->getArgumentValue(this->getArgument(PHP::PATH_KEY));
Validation::alreadyExistCheck(this,version,false);
this->php = new PHP(version,path);
this->fileManager->addLine("database.txt",this->php->version+"<===>"+this->php->path);
cout << this->output->changeColor("Success: ","green")+"Added PHP "+this->php->version + " in Database";
}
void setPHP(){
string version = this->getArgumentValue(this->getArgument(PHP::VERSION_KEY),10);
PHP matched_version = Validation::validatePHPVersion(this,version);
this->registrar->activate_version(matched_version);
}
void listPHPs(){
if(this->fileManager->availableVersions.size() > 0){
for(int i=0;i<this->fileManager->availableVersions.size();i++){
cout << setw(30) << left << (this->output->changeColor("("+to_string(i+1)+") ","blue") + this->fileManager->availableVersions[i].version) << this->fileManager->availableVersions[i].path << endl;
}
}else{
cout << this->output->changeColor("INFO: ","blue") + "No Version Available in local database";
}
}
void unsetPHP(){
this->registrar->deactivate_all_versions(true);
}
void removePHP(){
string version = this->getArgumentValue(this->getArgument(PHP::VERSION_KEY),10);
Validation::alreadyExistCheck(this,version,true);
Validation::isNotActivatedCheck(this,version);
this->fileManager->removeVersion(version);
cout << this->output->changeColor("SUCCESS: ","green") + "Removed version " + version + " from Database";
}
void help(){
cout << this->output->changeColor("Syntax: ","blue") << endl << " " << "switcher action [--arguments]" << endl;
cout << this->output->changeColor("Actions: ","blue") << endl;
cout << " " << setw(25) << left << this->output->changeColor("add ","green") << "Add a new version data to Local Database using given arguments." << endl;
cout << " " << setw(25) << left << this->output->changeColor("remove ","green") << "Remove a version data to Local Database using given arguments." << endl;
cout << " " << setw(25) << left << this->output->changeColor("set ","green") << "Activate (switch to) a version from database." << endl;
cout << " " << setw(25) << left << this->output->changeColor("unset ","green") << "Deactivate any activated version" << endl;
cout << " " << setw(25) << left << this->output->changeColor("list ","green") << "List all available versions" << endl;
cout << this->output->changeColor("Arguments: ","blue") << endl;
cout << " " << setw(25) << left << this->output->changeColor("--version ","green") << "Specify a version" << endl;
cout << " " << setw(25) << left << this->output->changeColor("--path ","green") << "Specify a path for version" << endl;
}
void refreshEnv(){
LPCTSTR keyPath = TEXT("Environment");
SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)keyPath, SMTO_BLOCK, 100, NULL);
}
FileManager *fileManager;
PHP *php;
Registrar *registrar;
private:
char *args[4];
string getArgument(string argKey){
try{
bool found = false;
string current;
for(int i=0;i<this->sizeOfArgs;i++){
if(!found){
current = this->args[i];
if(current.find(argKey) != string::npos){
found = true;
break;
}
}else{
break;
}
}
if(found){
return current;
}
throw("Argument '"+argKey+"' is missing");
}catch(string e){
cout << this->output->changeColor("ERROR: ","red") + e;
exit(1);
}
}
string getArgumentValue(string arg,int maxLength = 50){
string this_arg = arg;
string res;
try{
res = arg.replace(0,arg.find("=")+1,"");
if(res == ""){
throw("Invalid Argument Value For '"+this_arg+"'");
}else if(res.length() > maxLength){
throw((string)"Argument value is too long (Max length for that argument is: "+to_string(maxLength)+")");
}
}catch(string e){
cout << this->output->changeColor("ERROR: ","red") + e;
exit(1);
}
return res;
}
};
PHP Validation::validatePHPVersion(Manager *manager,string version = ""){
try{
for(int i=0;i<manager->fileManager->availableVersions.size();i++){
if(manager->fileManager->availableVersions[i].version == version){
return manager->fileManager->availableVersions[i];
}
}
throw("Version "+version+" does not exists in Database, please add it first");
}catch(string e){
cout << manager->output->changeColor("ERROR: ","red") + e;
exit(1);
}
}
bool Validation::alreadyExistCheck(Manager *manager,string version,bool shouldExistOrNot = false){
try{
for(int i=0;i<manager->fileManager->availableVersions.size();i++){
if(manager->fileManager->availableVersions[i].version == version){
if(shouldExistOrNot){
return true;
}else{
throw("The version "+version+" already exists in Database");
}
}
}
if(shouldExistOrNot){
throw("The version "+version+" is already removed from database");
}else{
return true;
}
}catch(string e){
cout << manager->output->changeColor("ERROR: ","red") + e;
exit(1);
}
}
void Validation::isNotActivatedCheck(Manager *manager,string version){
PHP *current_activated = manager->registrar->current_activated_version();
try{
if(version == current_activated->version){
throw((string)"Currently activated versions can't be removed.");
}
}catch(string e){
cout << manager->output->changeColor("ERROR: ","red") + e;
exit(1);
}
}
int main(int argc,char* argv[]){
Manager *manager = new Manager(argv,argc);
manager->setup();
if(argc > 1){
string action = argv[1];
if(action == "add"){
manager->addPHP();
}else if(action == "remove"){
manager->removePHP();
}else if(action == "set"){
manager->setPHP();
manager->refreshEnv();
}else if(action == "list"){
manager->listPHPs();
}else if(action == "unset"){
manager->unsetPHP();
manager->refreshEnv();
}else if(action == "--help"){
manager->help();
}
}else{
cout << manager->output->changeColor("ERROR: ","red") + "Some Arguments Are Missing";
exit(1);
}
return 0;
}