-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwsjcpp_package_manager.cpp
More file actions
2931 lines (2527 loc) · 108 KB
/
Copy pathwsjcpp_package_manager.cpp
File metadata and controls
2931 lines (2527 loc) · 108 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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "wsjcpp_package_manager.h"
#include <iostream>
#include <wsjcpp_core.h>
#include <fstream>
#include <iomanip>
#include "wsjcpp_helpers.h"
#include <wsjcpp_core.h>
#include <stdio.h>
#include <ctype.h>
#include <sys/stat.h>
#include <regex>
#include <wsjcpp_safe_scripting.h>
// ---------------------------------------------------------------------
// WsjcppPackageManagerOrigin - server info class
WsjcppPackageManagerOrigin::WsjcppPackageManagerOrigin() {
TAG = "WsjcppPackageManagerOrigin";
m_pYamlOrigin = nullptr;
}
WsjcppYamlNode *WsjcppPackageManagerOrigin::toYAML() {
m_pYamlOrigin->setValue(m_sAddress, WSJCPP_YAML_QUOTES_DOUBLE);
return m_pYamlOrigin;
}
bool WsjcppPackageManagerOrigin::fromYAML(WsjcppYamlNode *pYaml) {
m_pYamlOrigin = pYaml;
if (!m_pYamlOrigin->hasElement("address")) {
WsjcppLog::err(TAG, "Missing required field 'address' in " + m_pYamlOrigin->getForLogFormat());
return false;
} else {
m_sAddress = m_pYamlOrigin->getElement("address")->getValue();
}
if (!m_pYamlOrigin->hasElement("type")) {
WsjcppLog::err(TAG, "Missing required field 'type' in " + m_pYamlOrigin->getForLogFormat());
return false;
} else {
m_sType = m_pYamlOrigin->getElement("type")->getValue();
}
return true;
}
// ---------------------------------------------------------------------
std::string WsjcppPackageManagerOrigin::getAddress() {
return m_sAddress;
}
// ---------------------------------------------------------------------
std::string WsjcppPackageManagerOrigin::getType() {
return m_sType;
}
// ---------------------------------------------------------------------
void WsjcppPackageManagerOrigin::setAddress(const std::string &sAddress) {
m_sAddress = sAddress;
}
// ---------------------------------------------------------------------
void WsjcppPackageManagerOrigin::setType(const std::string &sType) {
m_sType = sType;
}
// ---------------------------------------------------------------------
// WsjcppPackageManagerRepository - repository struct
WsjcppPackageManagerRepository::WsjcppPackageManagerRepository() {
TAG = "WsjcppPackageManagerRepository";
}
// ---------------------------------------------------------------------
WsjcppYamlNode *WsjcppPackageManagerRepository::toYAML() {
m_pYamlRepository->getElement("url")->setValue(m_sUrl, WSJCPP_YAML_QUOTES_DOUBLE);
m_pYamlRepository->getElement("type")->setValue(m_sType, WSJCPP_YAML_QUOTES_DOUBLE);
return m_pYamlRepository;
}
// ---------------------------------------------------------------------
bool WsjcppPackageManagerRepository::fromYAML(WsjcppYamlNode *pYaml) {
m_pYamlRepository = pYaml;
if (!m_pYamlRepository->hasElement("type")) {
WsjcppLog::err(TAG, "Missing required field 'type' in " + m_pYamlRepository->getForLogFormat());
return false;
} else {
m_sType = m_pYamlRepository->getElement("type")->getValue();
}
if (!m_pYamlRepository->hasElement("url")) {
// TODO prepare in yaml getOriginalLineForError()
WsjcppLog::err(TAG, "Missing required field 'url' in " + m_pYamlRepository->getForLogFormat());
return false;
} else {
m_sUrl = m_pYamlRepository->getElement("url")->getValue();
}
return true;
}
// ---------------------------------------------------------------------
std::string WsjcppPackageManagerRepository::getType() {
return m_sType;
}
// ---------------------------------------------------------------------
std::string WsjcppPackageManagerRepository::getUrl() {
return m_sUrl;
}
// ---------------------------------------------------------------------
// WsjcppPackageManagerUnitTest - main class
WsjcppPackageManagerUnitTest::WsjcppPackageManagerUnitTest() {
TAG = "WsjcppPackageManagerUnitTest";
m_bEnabled = true; // default enbaled
}
// ---------------------------------------------------------------------
WsjcppYamlNode *WsjcppPackageManagerUnitTest::toYAML() {
m_pYamlUnitTest->getElement("url")->setValue(m_sName, WSJCPP_YAML_QUOTES_DOUBLE);
m_pYamlUnitTest->getElement("type")->setValue(m_sDescription, WSJCPP_YAML_QUOTES_DOUBLE);
m_pYamlUnitTest->getElement("enabled")->setValue(m_bEnabled ? "yes" : "no");
return m_pYamlUnitTest;
}
// ---------------------------------------------------------------------
bool WsjcppPackageManagerUnitTest::fromYAML(WsjcppYamlNode *pYaml) {
m_pYamlUnitTest = pYaml;
std::vector<std::string> vKeys = m_pYamlUnitTest->getKeys();
for (int i = 0; i < vKeys.size(); i++) {
std::string sKey = vKeys[i];
if (sKey == "name") {
m_sName = m_pYamlUnitTest->getElement("name")->getValue();
} else if (sKey == "description") {
m_sDescription = m_pYamlUnitTest->getElement("description")->getValue();
} else if (sKey == "enabled") {
m_bEnabled = m_pYamlUnitTest->getElement("enabled")->getValue() == "yes";
} else {
WsjcppLog::warn(TAG, "Excess field '" + sKey + "' in " + m_pYamlUnitTest->getForLogFormat());
}
}
// default values
if (!m_pYamlUnitTest->hasElement("enabled")) {
m_bEnabled = true;
}
// TODO move to yaml some validate function
std::vector<std::string> vRequiredKeys;
vRequiredKeys.push_back("name");
vRequiredKeys.push_back("description");
bool bHasError = false;
for (int i = 0; i < vRequiredKeys.size(); i++) {
std::string sKey = vRequiredKeys[i];
if (!m_pYamlUnitTest->hasElement(sKey)) {
WsjcppLog::err(TAG, "Missing required field '" + sKey + "' in " + m_pYamlUnitTest->getForLogFormat());
bHasError = true;
}
}
return !bHasError;
}
// ---------------------------------------------------------------------
std::string WsjcppPackageManagerUnitTest::getName() {
return m_sName;
}
// ---------------------------------------------------------------------
std::string WsjcppPackageManagerUnitTest::getDescription() {
return m_sDescription;
}
// ---------------------------------------------------------------------
bool WsjcppPackageManagerUnitTest::isEnabled() {
return m_bEnabled;
}
// ---------------------------------------------------------------------
void WsjcppPackageManagerUnitTest::setName(const std::string &sName) {
m_sName = sName;
}
// ---------------------------------------------------------------------
void WsjcppPackageManagerUnitTest::setDescription(const std::string &sDescription) {
m_sDescription = sDescription;
}
// ---------------------------------------------------------------------
void WsjcppPackageManagerUnitTest::setEnabled(bool bEnabled) {
m_bEnabled = bEnabled;
}
// ---------------------------------------------------------------------
// WsjcppPackageManager - main class
WsjcppPackageManager::WsjcppPackageManager(const std::string &sDir) {
TAG = "WsjcppPackageManager";
if (sDir.length() > 0 && sDir[0] != '/') { // if not absolute path
m_sRootDir = WsjcppCore::getCurrentDirectory() + "/" + sDir;
} else {
m_sRootDir = sDir;
}
m_sRootDir = WsjcppCore::doNormalizePath(m_sRootDir);
m_sDirnameResources = "src-resources.wsjcpp";
m_sDirResources = WsjcppCore::doNormalizePath(m_sRootDir + "/" + m_sDirnameResources);
m_sDirWithSources = WsjcppCore::doNormalizePath(m_sRootDir + "/src.wsjcpp");
// TODO m_sGithubPrefix = "git@"; // try clone project to cache directory
m_sYamlFilename = "wsjcpp.yml";
m_sWsjcppCurrentVersion = std::string(WSJCPP_APP_VERSION);
m_sWsjcppVersion = m_sWsjcppCurrentVersion;
m_bHolded = false;
m_sIssues = "none";
m_pDownloaders = new WsjcppPackageDownloaders();
// version file
m_bVersionFile = false;
m_sVersionFile_Path = "./";
m_sVersionFile_Filename = "VERSION";
m_sVersionFile_PrintedFormat = "v{MAJOR}.{MINOR}.{BUILD}";
}
// ---------------------------------------------------------------------
WsjcppPackageManager::WsjcppPackageManager(const std::string &sDir, const std::string &sParentDir, bool bHolded)
: WsjcppPackageManager(sDir) {
m_sDirWithSources = WsjcppCore::doNormalizePath(m_sRootDir + "/src.wsjcpp");
m_sYamlFilename = "wsjcpp.hold.yml";
m_bHolded = true; // must be detect by wsjcpp.hold.yml
m_bHasDocker = false;
m_sParentDir = sParentDir; // ???
}
// ---------------------------------------------------------------------
WsjcppPackageManager::~WsjcppPackageManager() {
delete m_pDownloaders;
}
// ---------------------------------------------------------------------
std::string WsjcppPackageManager::getDir() const {
return m_sRootDir;
}
// ---------------------------------------------------------------------
bool WsjcppPackageManager::isHolded() const {
return m_bHolded;
}
// ---------------------------------------------------------------------
bool WsjcppPackageManager::init() {
std::string sCurrentDirectory = WsjcppCore::getCurrentDirectory() + "/" + m_sRootDir;
sCurrentDirectory = WsjcppCore::doNormalizePath(sCurrentDirectory);
if (sCurrentDirectory[sCurrentDirectory.length()-1] == '/') {
sCurrentDirectory = sCurrentDirectory.substr(0, sCurrentDirectory.size()-1);
}
m_sYamlFullpath = "wsjcpp.yml";
// TODO check wsjcpp.yml
m_sName = WsjcppCore::extractFilename(sCurrentDirectory);
m_yamlPackageInfo.getRoot()->setElementValue("name", m_sName, WSJCPP_YAML_QUOTES_NONE, WSJCPP_YAML_QUOTES_DOUBLE);
m_sVersion = "v0.0.1";
m_yamlPackageInfo.getRoot()->setElementValue("version", m_sVersion, WSJCPP_YAML_QUOTES_NONE, WSJCPP_YAML_QUOTES_DOUBLE);
m_sCMakeMinimumRequired = "3.0";
m_yamlPackageInfo.getRoot()->setElementValue("cmake_minimum_required", m_sCMakeMinimumRequired, WSJCPP_YAML_QUOTES_NONE, WSJCPP_YAML_QUOTES_DOUBLE);
m_sCMakeCxxStandard = "17";
m_yamlPackageInfo.getRoot()->setElementValue("cmake_cxx_standard", m_sCMakeCxxStandard, WSJCPP_YAML_QUOTES_NONE, WSJCPP_YAML_QUOTES_DOUBLE);
std::cout << "Source Package Name: " << m_sName << std::endl;
std::cout << "Version: " << m_sVersion << " (Hint: in future you can easy change this version by use a command line, for example: 'wsjcpp ch ver v0.0.2')" << std::endl;
std::cout << "Description: ";
std::getline(std::cin, m_sDescription);
m_yamlPackageInfo.getRoot()->setElementValue("description", m_sDescription, WSJCPP_YAML_QUOTES_NONE, WSJCPP_YAML_QUOTES_DOUBLE);
std::string sAuthorName = "";
std::cout << "Author's Name: ";
std::getline(std::cin, sAuthorName);
std::string sAuthorEmail = "";
std::cout << "Author's Email: ";
std::getline(std::cin, sAuthorEmail);
addAuthor(sAuthorName, sAuthorEmail);
addOrigin("https://sea5kg.ru/wsjcpp-package-registry/");
m_yamlPackageInfo.getRoot()->createElementArray("keywords");
m_yamlPackageInfo.getRoot()->getElement("keywords")->appendElementValue("c++", WSJCPP_YAML_QUOTES_DOUBLE);
// addKeyword("wsjcpp");
return true;
}
// ---------------------------------------------------------------------
bool WsjcppPackageManager::save(/*std::string &sError*/) { // TODO uncomment sError
if (m_bHolded) {
WsjcppLog::throw_err(TAG, "wsjcpp is holded");
return false;
}
if (m_vDependencies.size() > 0) {
if (!WsjcppCore::dirExists(m_sDirWithSources)) {
WsjcppCore::makeDir(m_sDirWithSources);
}
}
std::string sError;
return m_yamlPackageInfo.saveToFile(m_sYamlFullpath, sError);
}
// ---------------------------------------------------------------------
bool WsjcppPackageManager::load(/*std::string &sError*/) { // TODO uncomment sError
m_sYamlFullpath = WsjcppCore::doNormalizePath(m_sRootDir + "/" + m_sYamlFilename);
if (!WsjcppCore::fileExists(m_sYamlFullpath)) {
// sError = "File '" + m_sYamlFullpath + "' did not found";
std::cout << "ERROR(load): '" << m_sYamlFullpath << "' did not found" << std::endl;
return false;
}
std::string sError;
if (!m_yamlPackageInfo.loadFromFile(m_sYamlFullpath, sError)) {
return false;
}
std::vector<std::string> vKeys = m_yamlPackageInfo.getCursor().keys();
for (int i = 0; i < vKeys.size(); i++) {
std::string sKey = vKeys[i];
// WsjcppLog::info(TAG, "Process option '" + sKey + "'");
if (sKey == "version") {
if (!readFieldVersion()) {
return false;
}
} else if (sKey == "cmake_minimum_required") {
if (!readFieldCMakeMinimumRequired()) {
return false;
}
} else if (sKey == "cmake_cxx_standard") {
if (!readFieldCMakeCxxStandard()) {
return false;
}
} else if (sKey == "name") {
if (!readFieldName()) {
return false;
}
} else if (sKey == "description") {
if (!readFieldDescription()) {
return false;
}
} else if (sKey == "wsjcpp_version") {
if (!readFieldWsjcppVersion()) {
return false;
}
} else if (sKey == "auto-generated-files") {
if (!readFieldAutoGeneratedFiles()) {
return false;
}
} else if (sKey == "issues") {
if (!readFieldIssues()) {
return false;
}
} else if (sKey == "keywords") {
if (!readFieldKeywords()) {
return false;
}
} else if (sKey == "authors") {
if (!readFieldAuthors()) {
return false;
}
} else if (sKey == "distribution") {
if (!readFieldDistribution()) {
return false;
}
} else if (sKey == "origins") {
if (!readFieldOrigins()) {
return false;
}
} else if (sKey == "dependencies") {
if (!readFieldDependencies()) {
return false;
}
} else if (sKey == "repositories") {
if (!readFieldRepositories()) {
return false;
}
} else if (sKey == "resources") {
if (!readFieldResources()) {
return false;
}
} else if (sKey == "unit-tests") {
if (!readFieldUnitTests()) {
return false;
}
} else if (sKey == "required-libraries") {
if (!readFieldRequiredLibraries()) {
return false;
}
} else if (sKey == "required-pkg-config") {
if (!readFieldRequiredPkgConfig()) {
return false;
}
} else if (sKey == "docker") {
m_bHasDocker = true;
WsjcppLog::warn(TAG, "TODO read docker section");
// if (!readFieldRequiredPkgConfig()) {
// return false;
// }
} else {
WsjcppLog::warn(TAG, "Ignored option '" + sKey + "' in " + m_yamlPackageInfo.getRoot()->getForLogFormat());
}
}
// WsjcppLog::warn(TAG, "Loaded");
// TODO required-libraries
// TODO required-pkg-config
// TODO replace-dependencies
// TODO check requered fields
return true;
}
// ---------------------------------------------------------------------
bool WsjcppPackageManager::addSourceFile(const std::string &sSourceFile, const std::string &sTargetFile, const std::string &sType) {
if (m_bHolded) {
WsjcppLog::err(TAG, "wsjcpp is holded");
return false;
}
if (!WsjcppCore::fileExists(sSourceFile)) {
WsjcppLog::err(TAG, "'" + sSourceFile + "' file does not exists");
return false;
}
std::vector<WsjcppPackageManagerDistributionFile>::iterator it;
for (it = m_vDistributionFiles.begin(); it != m_vDistributionFiles.end(); ++it) {
if (it->getSourceFile() == sSourceFile) {
WsjcppLog::err(TAG, "This package already contained file '" + sSourceFile + "'");
return false;
}
}
std::string sContent = "";
if (!WsjcppCore::readTextFile(sSourceFile, sContent)) {
return false;
}
WsjcppPackageManagerDistributionFile file;
file.setSourceFile(sSourceFile);
file.setTargetFile(sTargetFile);
file.setType(sType);
m_vDistributionFiles.push_back(file);
WsjcppYamlNode *pRoot = m_yamlPackageInfo.getRoot();
if (!pRoot->hasElement("distribution")) {
pRoot->createElementArray("distribution");
}
WsjcppYamlNode *pDist = m_yamlPackageInfo.getRoot()->getElement("distribution");
WsjcppYamlPlaceInFile pl;
WsjcppYamlNode *pItem = new WsjcppYamlNode(pDist, &m_yamlPackageInfo, pl, WSJCPP_YAML_NODE_MAP);
pItem->setElementValue("source-file", sSourceFile, WSJCPP_YAML_QUOTES_NONE, WSJCPP_YAML_QUOTES_DOUBLE);
pItem->setElementValue("target-file", sTargetFile, WSJCPP_YAML_QUOTES_NONE, WSJCPP_YAML_QUOTES_DOUBLE);
pItem->setElementValue("type", sType, WSJCPP_YAML_QUOTES_NONE, WSJCPP_YAML_QUOTES_DOUBLE);
pDist->appendElement(pItem);
updateAutogeneratedFiles();
return true;
}
// ---------------------------------------------------------------------
bool WsjcppPackageManager::removeSourceFile(const std::string &sSourceFile) {
if (m_bHolded) {
WsjcppLog::err(TAG, "wsjcpp is holded");
return false;
}
bool bResult = false;
for (auto it = m_vDistributionFiles.begin(); it != m_vDistributionFiles.end(); ++it) {
if (it->getSourceFile() == sSourceFile) {
m_vDistributionFiles.erase(it);
bResult = true;
break;
}
}
if (bResult) {
bResult = false;
WsjcppYamlNode *pDist = m_yamlPackageInfo.getRoot()->getElement("distribution");
int nLen = pDist->getLength();
for (int i = nLen-1; i >= 0; i--) {
WsjcppYamlNode *pItem = pDist->getElement(i);
if (pItem->getElement("source-file")->getValue() == sSourceFile) {
pDist->removeElement(i);
bResult = true;
}
}
}
if (!bResult) {
WsjcppLog::err(TAG, "Distribution file '" + sSourceFile + "' cound not found in this package");
} else {
updateAutogeneratedFiles();
}
return bResult;
}
// ---------------------------------------------------------------------
bool WsjcppPackageManager::updateSourceFile(const std::string &sSourceFile, bool bAutoUpdate = true) {
if (m_bHolded) {
WsjcppLog::err(TAG, "wsjcpp is holded");
return false;
}
std::string sFilePath = m_sRootDir + "/" + sSourceFile;
if (!WsjcppCore::fileExists(sFilePath)) {
WsjcppLog::err(TAG, "'" + sSourceFile + "' file does not exists in " + m_sRootDir);
return false;
}
std::string sContent = "";
if (!WsjcppCore::readTextFile(sFilePath, sContent)) {
return false;
}
std::string sSha1 = WsjcppHelpers::sha1_calc_hex(sContent);
bool bFound = false;
std::vector<WsjcppPackageManagerDistributionFile>::iterator it;
for (it = m_vDistributionFiles.begin(); it != m_vDistributionFiles.end(); ++it) {
if (it->getSourceFile() == sSourceFile) {
it->setSha1(sSha1);
bFound = true;
}
}
if (!bFound) {
WsjcppLog::err(TAG, "'" + sSourceFile + "' file not found in list. Please add this before");
return false;
}
if (bFound) {
bFound = false;
WsjcppYamlNode *pDist = m_yamlPackageInfo.getRoot()->getElement("distribution");
int nLen = pDist->getLength();
for (int i = nLen-1; i >= 0; i--) {
WsjcppYamlNode *pItem = pDist->getElement(i);
if (pItem->getElement("source-file")->getValue() == sSourceFile) {
if (!pItem->hasElement("sha1")) {
pItem->setElementValue("sha1", sSha1, WSJCPP_YAML_QUOTES_NONE, WSJCPP_YAML_QUOTES_DOUBLE);
} else {
pItem->getElement("sha1")->setValue(sSha1, WSJCPP_YAML_QUOTES_DOUBLE);
}
bFound = true;
}
}
}
if (bAutoUpdate) {
updateAutogeneratedFiles();
}
return bFound;
}
// ---------------------------------------------------------------------
bool WsjcppPackageManager::createUnitTest(const std::string &sUnitTestName, const std::string &sUnitTestDescription) {
if (m_bHolded) {
WsjcppLog::err(TAG, "wsjcpp is holded");
return false;
}
std::string sName = normalizeUnitTestName(sUnitTestName, false);
if (sName != sUnitTestName) {
WsjcppLog::warn(TAG, "UnitTest name was normalized '" + sUnitTestName + "' -> '" + sName + "'");
}
for (int i = 0; i < m_vUnitTests.size(); i++) {
if (m_vUnitTests[i].getName() == sName) {
WsjcppLog::err(TAG, "Unit test with name '" + sName + "' already exists");
return false;
}
}
// first find generate file by generate.WsjcppUnitTest.wsjcpp-script
std::string sWsjcppUnitTestScriptFilePath = "";
std::vector<WsjcppPackageManagerSafeScriptingGenerate> vScripts = getListOfSafeScriptingGenerate();
for (int i = 0; i < vScripts.size(); i++) {
if (vScripts[i].getName() == "WsjcppUnitTest") {
sWsjcppUnitTestScriptFilePath = vScripts[i].getFullPath();
}
}
if (sWsjcppUnitTestScriptFilePath == "") {
WsjcppLog::err(TAG, "Not found 'generate.WsjcppUnitTest.wsjcpp-script', porabble try install 'wsjcpp-core' package.");
return false;
}
// TODO redesign WsjcppCore::makeDirPath
std::string sUnitTestsFolder = WsjcppCore::doNormalizePath(m_sRootDir + "/unit-tests.wsjcpp");
if (!WsjcppCore::dirExists(sUnitTestsFolder)) {
if (!WsjcppCore::makeDir(sUnitTestsFolder)) {
WsjcppLog::err(TAG, "Could not create '" + sUnitTestsFolder + "'");
return false;
}
}
sUnitTestsFolder = sUnitTestsFolder + "/src";
if (!WsjcppCore::dirExists(sUnitTestsFolder)) {
if (!WsjcppCore::makeDir(sUnitTestsFolder)) {
WsjcppLog::err(TAG, "Could not create '" + sUnitTestsFolder + "'");
return false;
}
}
if (!WsjcppCore::dirExists("./unit-tests.wsjcpp/src/")) {
WsjcppLog::err(TAG, "Directory does not exists './unit-tests.wsjcpp/src/'");
return false;
}
std::string sUnitTestFilepathSources = sUnitTestsFolder + "/" + this->generateFilenameForUnitTest(sUnitTestName) + ".cpp";
WsjcppPackageManagerUnitTest unitTest;
unitTest.setName(sName);
unitTest.setDescription(sUnitTestDescription);
m_vUnitTests.push_back(unitTest);
m_yamlPackageInfo.getRoot()->createElementMap("unit-tests", WSJCPP_YAML_QUOTES_NONE);
WsjcppYamlNode *pUnitTests = m_yamlPackageInfo.getRoot()->getElement("unit-tests");
pUnitTests->createElementArray("cases", WSJCPP_YAML_QUOTES_NONE);
WsjcppYamlNode *pCases = pUnitTests->getElement("cases");
WsjcppYamlNode *pNewItem = pCases->createElementMap();
pNewItem->setElementValue("name", sName, WSJCPP_YAML_QUOTES_NONE, WSJCPP_YAML_QUOTES_DOUBLE);
pNewItem->setElementValue("description", sUnitTestDescription, WSJCPP_YAML_QUOTES_NONE, WSJCPP_YAML_QUOTES_DOUBLE);
WsjcppSafeScriptingContext scriptContext;
std::vector<std::string> vScriptArgs;
vScriptArgs.push_back("UnitTest" + sName);
vScriptArgs.push_back(sUnitTestFilepathSources);
std::string sScriptContent;
if (!WsjcppCore::readTextFile(sWsjcppUnitTestScriptFilePath, sScriptContent)) {
WsjcppLog::err(TAG, "Could not read file: '" + sWsjcppUnitTestScriptFilePath + "");
return false;
}
int nResult = scriptContext.exec(
m_sRootDir,
sWsjcppUnitTestScriptFilePath,
sScriptContent,
vScriptArgs
);
if (nResult != 0) {
return false;
}
updateAutogeneratedFiles();
return true;
}
// ---------------------------------------------------------------------
bool WsjcppPackageManager::deleteUnitTest(const std::string &sUnitTestName) {
if (m_bHolded) {
WsjcppLog::err(TAG, "Package is holded");
return false;
}
std::string sName = normalizeUnitTestName(sUnitTestName, false);
if (sName != sUnitTestName) {
WsjcppLog::warn(TAG, "UnitTest name was normalized '" + sUnitTestName + "' -> '" + sName + "'");
}
bool bFound = false;
std::vector<WsjcppPackageManagerUnitTest>::iterator it;
for (it = m_vUnitTests.begin(); it < m_vUnitTests.end(); ++it) {
if (it->getName() == sName) {
bFound = true;
m_vUnitTests.erase(it);
break;
}
}
if (!bFound) {
WsjcppLog::err(TAG, "Not found unit-test with name '" + sName + "'");
return false;
}
WsjcppYamlCursor cur = m_yamlPackageInfo["unit-tests"]["cases"];
WsjcppYamlNode *pItem = m_yamlPackageInfo.getRoot()->getElement("unit-tests")->getElement("cases");
int nLength = cur.size();
for (int i = 0; i < nLength; i++) {
if (pItem->getElement(i)->getElement("name")->getValue() == sName) {
std::string sBaseName = this->generateFilenameForUnitTest(sUnitTestName);
std::string sFileSource = WsjcppCore::doNormalizePath(m_sRootDir + "/unit-tests.wsjcpp/src/" + sBaseName + ".cpp");
if (WsjcppCore::fileExists(sFileSource)) {
WsjcppCore::removeFile(sFileSource);
}
/*if (nLength == 1) {
return m_yamlPackageInfo.getRoot()->removeElement("unit-tests");
}*/
if (pItem->removeElement(i)) {
updateAutogeneratedFiles();
return true;
}
}
}
return false;
}
// ---------------------------------------------------------------------
bool WsjcppPackageManager::enableUnitTest(const std::string &sUnitTestName, bool bEnable) {
if (m_bHolded) {
WsjcppLog::err(TAG, "Package is holded");
return false;
}
bool bFound = false;
std::vector<WsjcppPackageManagerUnitTest>::iterator it;
for (it = m_vUnitTests.begin(); it < m_vUnitTests.end(); ++it) {
if (it->getName() == sUnitTestName) {
it->setEnabled(bEnable);
bFound = true;
break;
}
}
if (!bFound) {
WsjcppLog::err(TAG, "Not found unit-test with name '" + sUnitTestName + "'");
return false;
}
WsjcppYamlCursor cur = m_yamlPackageInfo["unit-tests"]["cases"]; // .getElement("cases");
WsjcppYamlNode *pNode = m_yamlPackageInfo.getRoot()->getElement("unit-tests")->getElement("cases");
int nLength = cur.size();
for (int i = 0; i < nLength; i++) {
if (pNode->getElement(i)->getElement("name")->getValue() == sUnitTestName) {
pNode->getElement(i)->setElementValue("enabled", bEnable ? "yes" : "no");
return true;
}
}
return false;
}
// ---------------------------------------------------------------------
bool WsjcppPackageManager::addOrigin(const std::string &sAddress) {
if (m_bHolded) {
WsjcppLog::err(TAG, "Package is holded");
return false;
}
// TODO check address format
// TODO request online for type
for (auto it = m_vOrigins.begin(); it != m_vOrigins.end(); ++it) {
if (it->getAddress() == sAddress) {
WsjcppLog::err(TAG, "Error: Origin '" + sAddress + "' already defined.");
return false;
}
}
std::string sOriginType = "package-registry";
WsjcppPackageManagerOrigin origin;
origin.setAddress(sAddress);
origin.setType(sOriginType);
m_vOrigins.push_back(origin);
if (!m_yamlPackageInfo.getRoot()->hasElement("origins")) {
m_yamlPackageInfo.getRoot()->createElementArray("origins");
}
WsjcppYamlNode *pOrigins = m_yamlPackageInfo.getRoot()->getElement("origins");
WsjcppYamlPlaceInFile pl;
WsjcppYamlNode *pNewItemMap = new WsjcppYamlNode(pOrigins, &m_yamlPackageInfo, pl, WSJCPP_YAML_NODE_MAP);
pNewItemMap->setElementValue("address", sAddress, WSJCPP_YAML_QUOTES_NONE, WSJCPP_YAML_QUOTES_DOUBLE);
pNewItemMap->setElementValue("type", sOriginType, WSJCPP_YAML_QUOTES_NONE, WSJCPP_YAML_QUOTES_DOUBLE);
pOrigins->appendElement(pNewItemMap);
return true;
}
// ---------------------------------------------------------------------
bool WsjcppPackageManager::deleteOrigin(const std::string &sAddress) {
if (m_bHolded) {
WsjcppLog::err(TAG, "wsjcpp is holded");
return false;
}
std::vector<WsjcppPackageManagerOrigin>::iterator it;
for (it = m_vOrigins.begin(); it != m_vOrigins.end(); ++it) {
if (it->getAddress() == sAddress) {
m_vOrigins.erase(it);
return true;
}
}
WsjcppLog::err(TAG, "Origin '" + sAddress + "' did not found." );
return false;
}
// ---------------------------------------------------------------------
bool WsjcppPackageManager::updateDependencies() {
if (m_bHolded) {
std::cout << "ERROR: wsjcpp is holded" << std::endl;
return false;
}
return true;
}
// ---------------------------------------------------------------------
bool WsjcppPackageManager::verify() {
std::vector<std::string> m_vVerified;
// HERE verify current package
// TODO find root dir of roto package with wsjcpp.yaml
bool ret = true;
for (auto it = m_vDependencies.begin(); it != m_vDependencies.end(); ++it) {
std::string sPackageName = it->getName();
std::string sPackageInstallationDir = it->getInstallationDir();
if (m_bHolded) {
std::string sPath = WsjcppCore::doNormalizePath(m_sRootDir + "/../../" + sPackageInstallationDir);
// std::cout << "Try load (holded) " << sPath << std::endl;
WsjcppPackageManager m(sPath, m_sParentDir, true);
if (m.load()) {
if (!m.verify()) {
std::cerr << "ERROR: Unverified package (holded): " << sPath << std::endl;
ret = false;
continue;
}
for (const auto& df : m.getListOfDistributionFiles()) {
std::string sFilename = WsjcppCore::doNormalizePath(sPath + "/" + df.getTargetFile());
if (!WsjcppCore::fileExists(sFilename)) {
std::cerr << "ERROR: Could not find file: " << sFilename << std::endl;
// sFilesHasChanges += " - missed: " + sFilename + " (with sha1:'" + src.getSha1() + "')\n";
ret = false;
continue;
}
// TODO redesign to WsjcppHelpers::getSha1ByFile(sFilename);
std::string sContent = "";
if (!WsjcppCore::readTextFile(sFilename, sContent)) {
std::cerr << "ERROR: Could not read file '" + sFilename + "'";
ret = false;
continue;
}
std::string sSha1 = WsjcppHelpers::sha1_calc_hex(sContent);
if (sSha1 != df.getSha1()) {
std::cerr << "ERROR: wrong sha1 for '" + sFilename + "' (expected sha1 '" + sSha1 + "', but got '" + df.getSha1() + "')\n";
ret = false;
continue;
}
}
std::cout << "Holded package verified: " << sPackageName << std::endl;
m_vVerified.push_back(sPackageName);
} else {
std::cerr << "ERROR: Could not load package (holded): " << sPath << std::endl;
ret = false;
}
} else {
std::string sPath = WsjcppCore::doNormalizePath(m_sRootDir + "/" + sPackageInstallationDir);
WsjcppPackageManager m(sPath, m_sDirWithSources, true);
if (m.load()) {
for (const auto& df : m.getListOfDistributionFiles()) {
std::string sFilename = WsjcppCore::doNormalizePath(sPath + "/" + df.getTargetFile());
if (!WsjcppCore::fileExists(sFilename)) {
std::cerr << "ERROR: Could not find file: " << sFilename << std::endl;
// sFilesHasChanges += " - missed: " + sFilename + " (with sha1:'" + src.getSha1() + "')\n";
ret = false;
continue;
}
// TODO redesign to WsjcppHelpers::getSha1ByFile(sFilename);
std::string sContent = "";
if (!WsjcppCore::readTextFile(sFilename, sContent)) {
std::cerr << "ERROR: Could not read file '" + sFilename + "'";
ret = false;
continue;
}
std::string sSha1 = WsjcppHelpers::sha1_calc_hex(sContent);
if (sSha1 != df.getSha1()) {
std::cerr << "ERROR: wrong sha1 for '" + sFilename + "' (expected sha1 '" + sSha1 + "', but got '" + df.getSha1() + "')\n";
ret = false;
continue;
}
}
if (!m.verify()) {
std::cerr << "ERROR: Could not verify package: " << sPath << std::endl;
ret = false;
} else {
std::cout << "Package verified: " << sPackageName << std::endl;
m_vVerified.push_back(sPackageName);
}
} else {
std::cerr << "ERROR: Could not load package: " << sPath << std::endl;
ret = false;
}
}
}
return ret;
}
// ---------------------------------------------------------------------
bool WsjcppPackageManager::install(const std::string &sPackage, std::string &sError) {
if (m_bHolded) {
WsjcppLog::err(TAG, "Could not install package when holded");
return false;
}
if (isInstalled(sPackage)) {
sError = "Already installed.";
return false;
}
std::cout << "Search package from " << sPackage << " ..." << std::endl;
std::string sWsjcppBaseUrl = sPackage;
WsjcppPackageManagerDependence dep;
if (!m_pDownloaders->downloadToCache(sPackage, m_sRootDir, dep, sError)) {
return false;
}
if (isInstalled(dep.getName())) {
sError = "Package '" + dep.getName() + "' already installed.";
return false;
}
addDependency(dep);
if (!installFromCache(sPackage, dep, sError)) {
// TODO if could not install package cleanup target folder
removeDependency(dep);
return false;
}
return true;
}
// ---------------------------------------------------------------------
bool WsjcppPackageManager::checkInstalledPackage(
const std::string &sPackage,
std::vector<std::string> &vFilesInstalled,
std::string &sPackageUrl,
std::string &sError
) {
// find installed package
std::string sPackageInstallationDir = "";
std::vector<WsjcppPackageManagerDependence> deps = getListOfDependencies();
for (int i = 0; i < deps.size(); i++) {
WsjcppPackageManagerDependence dep = deps[i];
if (sPackage == dep.getUrl() || sPackage == dep.getName()) {
sPackageUrl = dep.getUrl();
sPackageInstallationDir = WsjcppCore::doNormalizePath(m_sRootDir + "/" + dep.getInstallationDir());
}
}
if (sPackageUrl == "" || sPackageInstallationDir == "") {
sError = "Not found installed package '" + sPackage + "'";
return false;
}
// prepare old file list for remove before install
// std::cout << "sPackageInstallationDir = " << sPackageInstallationDir << std::endl;
WsjcppPackageManager installedPkg(sPackageInstallationDir, m_sRootDir, true);
if (!installedPkg.load()) {
sError = "Could not load " + sPackageInstallationDir;
return false;
}
std::vector<WsjcppPackageManagerDistributionFile> vSources = installedPkg.getListOfDistributionFiles();
bool bHasChanges = false;
std::string sFilesHasChanges = "";
vFilesInstalled.clear();
vFilesInstalled.push_back(sPackageInstallationDir + "/wsjcpp.hold.yml");