-
Notifications
You must be signed in to change notification settings - Fork 20
/
ProjectInfo.cpp
4629 lines (3933 loc) · 132 KB
/
ProjectInfo.cpp
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 "ProjectInfo.h"
#if defined __APPLE__ || defined __linux__
const std::string promptStr = "⥤ ";
#else
const std::string promptStr = "=> ";
#endif
int create_default_html_template(const Path& templatePath)
{
templatePath.ensureDirExists();
std::ofstream ofs(templatePath.str());
ofs << "<html>" << std::endl;
ofs << "\t<head>" << std::endl;
ofs << "\t\t@input(\"" << templatePath.dir << "head.content\")" << std::endl;
ofs << "\t</head>" << std::endl << std::endl;
ofs << "\t<body>" << std::endl;
ofs << "\t\t@content" << std::endl;
ofs << "\t</body>" << std::endl;
ofs << "</html>" << std::endl << std::endl;
ofs.close();
ofs.open(Path(templatePath.dir, "head.content").str());
ofs << "<title>empty site - $[title]</title>" << std::endl;
ofs.close();
return 0;
}
int create_blank_template(const Path& templatePath)
{
templatePath.ensureDirExists();
std::ofstream ofs(templatePath.str());
ofs << "@content" << std::endl;
ofs.close();
return 0;
}
int create_config_file(const Path& configPath, const std::string& outputExt, bool global)
{
configPath.ensureDirExists();
ProjectInfo project;
if(!global)
{
project.contentDir = "content/";
project.contentExt = ".content";
project.outputDir = "output/";
project.outputExt = outputExt;
project.scriptExt = ".f";
}
if(global)
{
project.defaultTemplate = Path(app_dir() + "/.nift/template/", "global.template");
create_default_html_template(project.defaultTemplate);
}
else if(outputExt == ".html" || outputExt == ".php")
project.defaultTemplate = Path("template/", "page.template");
else
project.defaultTemplate = Path("template/", "project.template");
project.backupScripts = 1;
project.lolcatDefault = 0;
project.lolcatCmd = "nift lolcat-cc -f";
project.buildThreads = -1;
project.paginateThreads = -1;
project.incrMode = INCR_MOD;
project.terminal = "normal";
project.unixTextEditor = "gedit";
project.winTextEditor = "notepad";
if(!global)
{
if(dir_exists(".git/")) //should really be checking if outputDir exists and has .git directory
{
if(get_pb(project.outputBranch))
{
start_err(std::cout) << "failed to get present branch" << std::endl;
return 1;
}
project.rootBranch = project.outputBranch;
}
else
project.rootBranch = project.outputBranch = "##unset##";
}
project.save_config(configPath.str(), global);
return 0;
}
bool upgradeProject()
{
start_warn(std::cout) << "attempting to upgrade project for newer version of Nift" << std::endl;
if(rename(".siteinfo/", ".nsm/"))
{
start_err(std::cout) << "upgrade: failed to move config directory '.siteinfo/' to '.nsm/'" << std::endl;
return 1;
}
if(rename(".nsm/nsm.config", ".nsm/nift.config"))
{
start_err(std::cout) << "upgrade: failed to move config file '.nsm/nsm.config' to '.nsm/nift.config'" << std::endl;
return 1;
}
if(rename(".nsm/pages.list", ".nsm/tracking.list"))
{
start_err(std::cout) << "upgrade: failed to move pages list file '.nsm/pages.list' to tracking list file '.nsm/tracking.list'" << std::endl;
return 1;
}
ProjectInfo project;
if(project.open_local_config(1))
{
start_err(std::cout) << "upgrade: failed, was unable to open configuration file" << std::endl;
return 1;
}
else if(project.open_tracking(1)) //this needs to change
{
start_err(std::cout) << "upgrade: failed, was unable to open tracking list" << std::endl;
return 1;
}
//need to update output extension files
Path oldExtPath, newExtPath;
for(auto tInfo=project.trackedAll.begin(); tInfo!=project.trackedAll.end(); tInfo++)
{
oldExtPath = newExtPath = tInfo->outputPath.getInfoPath();
oldExtPath.file = oldExtPath.file.substr(0, oldExtPath.file.find_first_of('.')) + ".pageExt";
newExtPath.file = newExtPath.file.substr(0, newExtPath.file.find_first_of('.')) + ".outputExt";
if(file_exists(oldExtPath.str()))
{
if(rename(oldExtPath.str().c_str(), newExtPath.str().c_str()))
{
start_err(std::cout) << "upgrade: failed to move output extension file " << quote(oldExtPath.str()) << " to new extension file " << quote(newExtPath.str()) << std::endl;
return 1;
}
}
}
std::cout << "Successfully upgraded project configuration for newer version of Nift" << std::endl;
std::cout << c_light_blue << "note" << c_white << ": the syntax to input page and project/site information has also changed, please check the content files page of the docs" << std::endl;
return 0;
}
int ProjectInfo::open(const bool& addMsg)
{
if(open_local_config(addMsg))
return 1;
if(open_tracking(addMsg))
return 1;
return 0;
}
int ProjectInfo::open_config(const Path& configPath, const bool& global, const bool& addMsg)
{
if(!file_exists(configPath.str()))
{
if(global)
create_config_file(configPath, ".html", 1);
else
{
start_err(std::cout) << "open_config(): could not load Nift project config file as '" << get_pwd() << "/.nsm/nift.config' does not exist" << std::endl;
return 1;
}
}
if(addMsg)
{
std::cout << "loading ";
if(global)
std::cout << "global ";
else
std::cout << "project ";
std::cout << "config file: " << configPath << std::endl;
//std::cout << std::flush;
std::fflush(stdout);
}
contentDir = outputDir = "";
backupScripts = 1;
buildThreads = paginateThreads = 0;
incrMode = INCR_MOD;
contentExt = outputExt = scriptExt = unixTextEditor = winTextEditor = rootBranch = outputBranch = "";
defaultTemplate = Path("", "");
//reads Nift config file
std::ifstream ifs(configPath.str());
bool configChanged = 0;
std::string inLine, inType, str = "";
int lineNo = 0;
while(getline(ifs, inLine))
{
lineNo++;
if(!is_whitespace(inLine) && inLine.size() && inLine[0] != '#')
{
std::istringstream iss(inLine);
iss >> inType;
if(inType == "contentDir")
{
read_quoted(iss, contentDir);
contentDir = comparable(contentDir);
}
else if(inType == "contentExt")
read_quoted(iss, contentExt);
else if(inType == "outputDir")
{
read_quoted(iss, outputDir);
outputDir = comparable(outputDir);
}
else if(inType == "siteDir") //can delete this later
{
read_quoted(iss, outputDir);
outputDir = comparable(outputDir);
configChanged = 1;
//start_warn(std::cout, Path(".nsm/", "nift.config"), lineNo) << "updated siteDir to outputDir" << std::endl;
}
else if(inType == "outputExt")
read_quoted(iss, outputExt);
else if(inType == "pageExt") //can delete this later
{
read_quoted(iss, outputExt);
configChanged = 1;
//start_warn(std::cout, Path(".nsm/", "nift.config"), lineNo) << "updated pageExt to outputExt" << std::endl;
}
else if(inType == "scriptExt")
read_quoted(iss, scriptExt);
else if(inType == "defaultTemplate")
defaultTemplate.read_file_path_from(iss);
else if(inType == "backupScripts")
iss >> backupScripts;
else if(inType == "lolcatDefault" || inType == "lolcat") //carry over from poor naming choices
iss >> lolcatDefault;
else if(inType == "lolcatCmd")
read_quoted(iss, lolcatCmd);
else if(inType == "buildThreads")
iss >> buildThreads;
else if(inType == "paginateThreads")
iss >> paginateThreads;
else if(inType == "incrementalMode")
iss >> incrMode;
else if(inType == "terminal")
{
read_quoted(iss, terminal);
#if defined _WIN32 || defined _WIN64
if(terminal == "ps" || terminal == "powershell")
use_powershell_colours();
#endif
}
else if(inType == "unixTextEditor")
read_quoted(iss, unixTextEditor);
else if(inType == "winTextEditor")
read_quoted(iss, winTextEditor);
else if(inType == "rootBranch")
read_quoted(iss, rootBranch);
else if(inType == "outputBranch")
read_quoted(iss, outputBranch);
else if(inType == "siteBranch") // can delete this later
{
read_quoted(iss, outputBranch);
configChanged = 1;
std::cout << std::endl;
start_warn(std::cout, configPath, lineNo) << "updated siteBranch to outputBranch" << std::endl;
}
else
{
continue;
//if we throw error here can't compile projects for newer versions with older versions of Nift
//std::cout << std::endl;
//start_err(std::cout, configPath, lineNo) << "do not recognise confirguration parameter " << inType << std::endl;
//return 1;
}
iss >> str;
if(str != "" && str[0] != '#')
{
start_err(std::cout, configPath, lineNo) << "was not expecting anything on this line from " << c_light_blue << quote(str) << c_white << " onwards" << std::endl;
std::cout << c_aqua << "note: " << c_white << "you can comment out the remainder of a line with #" << std::endl;
ifs.close();
return 1;
}
}
}
ifs.close();
add_colour();
if(!global)
{
if(contentDir == "")
{
start_err(std::cout, configPath) << "content directory not specified" << std::endl;
return 1;
}
if(contentExt == "" || contentExt[0] != '.')
{
start_err(std::cout, configPath) << "content extension must start with a fullstop" << std::endl;
return 1;
}
if(outputDir == "")
{
start_err(std::cout, configPath) << "output directory not specified" << std::endl;
return 1;
}
if(outputExt == "" || outputExt[0] != '.')
{
start_err(std::cout, configPath) << "output extension must start with a fullstop" << std::endl;
return 1;
}
if(scriptExt != "" && scriptExt [0] != '.')
{
start_err(std::cout, configPath) << "script extension must start with a fullstop" << std::endl;
return 1;
}
else if(scriptExt == "")
{
start_warn(std::cout, configPath) << "script extension not detected, set to default of '.f'" << std::endl;
scriptExt = ".f";
configChanged = 1;
}
}
if(buildThreads == 0)
{
start_warn(std::cout, configPath) << "number of build threads not detected or invalid, set to default of -1 (number of cores)" << std::endl;
buildThreads = -1;
configChanged = 1;
}
if(paginateThreads == 0)
{
start_warn(std::cout, configPath) << "number of paginate threads not detected or invalid, set to default of -1 (number of cores)" << std::endl;
paginateThreads = -1;
configChanged = 1;
}
if(lolcatCmd.size() == 0)
{
start_warn(std::cout, configPath) << "no config detected for whether to use lolcat by default for REPL sessions" << std::endl;
lolcatDefault = 0;
lolcatCmd = "nift lolcat-cc -f";
configChanged = 1;
}
//code to set unixTextEditor if not present
if(unixTextEditor == "")
{
start_warn(std::cout, configPath) << "unix text editor not detected, set to default of 'nano'" << std::endl;
unixTextEditor = "nano";
configChanged = 1;
}
//code to set winTextEditor if not present
if(winTextEditor == "")
{
start_warn(std::cout, configPath) << "windows text editor not detected, set to default of 'notepad'" << std::endl;
winTextEditor = "notepad";
configChanged = 1;
}
if(!global)
{
//code to figure out rootBranch and outputBranch if not present
if(rootBranch == "" || outputBranch == "")
{
start_warn(std::cout, configPath) << "root or output branch not detected, have attempted to determine them, ensure values in config file are correct" << std::endl;
if(dir_exists(".git"))
{
std::set<std::string> branches;
if(get_git_branches(branches))
{
start_err(std::cout, configPath) << "open_config: failed to retrieve git branches" << std::endl;
return 1;
}
if(branches.count("stage"))
{
rootBranch = "stage";
outputBranch = "master";
}
else
rootBranch = outputBranch = "master";
}
else
rootBranch = outputBranch = "##unset##";
configChanged = 1;
}
}
//clear_console_line();
if(configChanged)
save_config(configPath.str(), global);
return 0;
}
int ProjectInfo::open_global_config(const bool& addMsg)
{
return open_config(Path(app_dir() + "/.nift/", "nift.config"), 1, addMsg);
}
int ProjectInfo::open_local_config(const bool& addMsg)
{
return open_config(Path(".nsm/", "nift.config"), 0, addMsg);
}
int ProjectInfo::open_tracking(const bool& addMsg)
{
if(addMsg)
std::cout << "loading project tracking file: " << Path(".nsm/", "tracking.list") << std::endl;
//std::cout << std::flush;
std::fflush(stdout);
trackedAll.clear();
if(!file_exists(".nsm/tracking.list"))
{
start_err(std::cout) << "open_tracking(): could not load tracking information as '" << get_pwd() << "/.nsm/tracking.list' does not exist" << std::endl;
return 1;
}
//reads tracking list file
std::ifstream ifs(".nsm/tracking.list"),
ifsx;
Name inName;
Title inTitle;
Path inTemplatePath;
std::string inExt;
while(read_quoted(ifs, inName))
{
inTitle.read_quoted_from(ifs);
inTemplatePath.read_file_path_from(ifs);
TrackedInfo inInfo = make_info(inName, inTitle, inTemplatePath);
//checks for non-default content extension
Path extPath = inInfo.outputPath.getInfoPath();
extPath.file = extPath.file.substr(0, extPath.file.find_first_of('.')) + ".contExt";
if(file_exists(extPath.str()))
{
ifsx.open(extPath.str());
ifsx >> inExt;
inInfo.contentPath.file = inInfo.contentPath.file.substr(0, inInfo.contentPath.file.find_first_of('.')) + inExt;
ifsx.close();
}
//checks for non-default output extension
extPath = inInfo.outputPath.getInfoPath();
extPath.file = extPath.file.substr(0, extPath.file.find_first_of('.')) + ".outputExt";
if(file_exists(extPath.str()))
{
ifsx.open(extPath.str());
ifsx >> inExt;
inInfo.outputPath.file = inInfo.outputPath.file.substr(0, inInfo.outputPath.file.find_first_of('.')) + inExt;
ifsx.close();
}
//checks that content and template files aren't the same
if(inInfo.contentPath == inInfo.templatePath)
{
start_err(std::cout) << "failed to open .nsm/tracking.list" << std::endl;
std::cout << c_light_blue << "reason: " << c_white << quote(inInfo.name) << " has same content and template path " << inInfo.templatePath << std::endl;
ifs.close();
return 1;
}
//makes sure there's no duplicate entries in tracking.list
if(trackedAll.count(inInfo) > 0)
{
TrackedInfo cInfo = *(trackedAll.find(inInfo));
start_err(std::cout) << "failed to load " << Path(".nsm/", "tracking.list") << std::endl;
std::cout << c_light_blue << "reason: " << c_white << "duplicate entry for " << inInfo.name << std::endl;
std::cout << c_light_blue << promptStr << c_white << "first entry:" << std::endl;
std::cout << " title: " << cInfo.title << std::endl;
std::cout << " output path: " << cInfo.outputPath << std::endl;
std::cout << " content path: " << cInfo.contentPath << std::endl;
std::cout << "template path: " << cInfo.templatePath << std::endl;
std::cout << c_light_blue << promptStr << c_white << "second entry:" << std::endl;
std::cout << " title: " << inInfo.title << std::endl;
std::cout << " output path: " << inInfo.outputPath << std::endl;
std::cout << " content path: " << inInfo.contentPath << std::endl;
std::cout << "template path: " << inInfo.templatePath << std::endl;
ifs.close();
return 1;
}
trackedAll.insert(inInfo);
}
ifs.close();
//clear_console_line();
return 0;
}
Path ProjectInfo::execrc_path(const std::string& exec, const std::string& execrc_ext)
{
return Path(app_dir() + "/.nift/", exec + "rc." + execrc_ext);
}
Path ProjectInfo::execrc_path(const std::string& exec, const char& execrc_ext)
{
return Path(app_dir() + "/.nift/", exec + "rc." + execrc_ext);
}
int ProjectInfo::save_config(const std::string& configPathStr, const bool& global)
{
std::ofstream ofs(configPathStr);
if(!global)
{
ofs << "contentDir " << quote(contentDir) << "\n";
ofs << "contentExt " << quote(contentExt) << "\n";
ofs << "outputDir " << quote(outputDir) << "\n";
ofs << "outputExt " << quote(outputExt) << "\n";
ofs << "scriptExt " << quote(scriptExt) << "\n";
}
ofs << "defaultTemplate " << quote(defaultTemplate.str()) << "\n\n";
ofs << "backupScripts " << backupScripts << "\n\n";
ofs << "lolcatDefault " << lolcatDefault << "\n\n";
ofs << "lolcatCmd " << quote(lolcatCmd) << "\n\n";
ofs << "buildThreads " << buildThreads << "\n\n";
ofs << "paginateThreads " << paginateThreads << "\n\n";
ofs << "incrementalMode " << incrMode << "\n\n";
ofs << "terminal " << quote(terminal) << "\n\n";
ofs << "unixTextEditor " << quote(unixTextEditor) << "\n";
ofs << "winTextEditor " << quote(winTextEditor) << "\n\n";
if(!global)
{
ofs << "rootBranch " << quote(rootBranch) << "\n";
ofs << "outputBranch " << quote(outputBranch) << "\n";
}
ofs.close();
return 0;
}
int ProjectInfo::save_global_config()
{
return save_config(app_dir() + "/.nift/nift.config", 1);
}
int ProjectInfo::save_local_config()
{
return save_config(".nsm/nift.config", 0);
}
int ProjectInfo::save_tracking()
{
std::ofstream ofs(".nsm/tracking.list");
for(auto tInfo=trackedAll.begin(); tInfo!=trackedAll.end(); tInfo++)
ofs << *tInfo << "\n\n";
ofs.close();
return 0;
}
TrackedInfo ProjectInfo::make_info(const Name& name)
{
TrackedInfo trackedInfo;
trackedInfo.name = name;
Path nameAsPath;
nameAsPath.set_file_path_from(unquote(name));
trackedInfo.contentPath = Path(contentDir + nameAsPath.dir, nameAsPath.file + contentExt);
trackedInfo.outputPath = Path(outputDir + nameAsPath.dir, nameAsPath.file + outputExt);
trackedInfo.title = name;
trackedInfo.templatePath = defaultTemplate;
//checks for non-default extensions
std::string inExt;
Path extPath = trackedInfo.outputPath.getInfoPath();
extPath.file = extPath.file.substr(0, extPath.file.find_first_of('.')) + ".contExt";
if(file_exists(extPath.str()))
{
std::ifstream ifs(extPath.str());
getline(ifs, inExt);
ifs.close();
trackedInfo.contentPath.file = trackedInfo.contentPath.file.substr(0, trackedInfo.contentPath.file.find_first_of('.')) + inExt;
}
extPath.file = extPath.file.substr(0, extPath.file.find_first_of('.')) + ".outputExt";
if(file_exists(extPath.str()))
{
std::ifstream ifs(extPath.str());
getline(ifs, inExt);
ifs.close();
trackedInfo.outputPath.file = trackedInfo.outputPath.file.substr(0, trackedInfo.outputPath.file.find_first_of('.')) + inExt;
}
return trackedInfo;
}
TrackedInfo ProjectInfo::make_info(const Name& name, const Title& title)
{
TrackedInfo trackedInfo;
trackedInfo.name = name;
Path nameAsPath;
nameAsPath.set_file_path_from(unquote(name));
trackedInfo.contentPath = Path(contentDir + nameAsPath.dir, nameAsPath.file + contentExt);
trackedInfo.outputPath = Path(outputDir + nameAsPath.dir, nameAsPath.file + outputExt);
trackedInfo.title = title;
trackedInfo.templatePath = defaultTemplate;
//checks for non-default extensions
std::string inExt;
Path extPath = trackedInfo.outputPath.getInfoPath();
extPath.file = extPath.file.substr(0, extPath.file.find_first_of('.')) + ".contExt";
if(file_exists(extPath.str()))
{
std::ifstream ifs(extPath.str());
getline(ifs, inExt);
ifs.close();
trackedInfo.contentPath.file = trackedInfo.contentPath.file.substr(0, trackedInfo.contentPath.file.find_first_of('.')) + inExt;
}
extPath.file = extPath.file.substr(0, extPath.file.find_first_of('.')) + ".outputExt";
if(file_exists(extPath.str()))
{
std::ifstream ifs(extPath.str());
getline(ifs, inExt);
ifs.close();
trackedInfo.outputPath.file = trackedInfo.outputPath.file.substr(0, trackedInfo.outputPath.file.find_first_of('.')) + inExt;
}
return trackedInfo;
}
TrackedInfo ProjectInfo::make_info(const Name& name, const Title& title, const Path& templatePath)
{
TrackedInfo trackedInfo;
trackedInfo.name = name;
Path nameAsPath;
nameAsPath.set_file_path_from(unquote(name));
trackedInfo.contentPath = Path(contentDir + nameAsPath.dir, nameAsPath.file + contentExt);
trackedInfo.outputPath = Path(outputDir + nameAsPath.dir, nameAsPath.file + outputExt);
trackedInfo.title = title;
trackedInfo.templatePath = templatePath;
//checks for non-default extensions
std::string inExt;
Path extPath = trackedInfo.outputPath.getInfoPath();
extPath.file = extPath.file.substr(0, extPath.file.find_first_of('.')) + ".contExt";
if(file_exists(extPath.str()))
{
std::ifstream ifs(extPath.str());
getline(ifs, inExt);
ifs.close();
trackedInfo.contentPath.file = trackedInfo.contentPath.file.substr(0, trackedInfo.contentPath.file.find_first_of('.')) + inExt;
}
extPath.file = extPath.file.substr(0, extPath.file.find_first_of('.')) + ".outputExt";
if(file_exists(extPath.str()))
{
std::ifstream ifs(extPath.str());
getline(ifs, inExt);
ifs.close();
trackedInfo.outputPath.file = trackedInfo.outputPath.file.substr(0, trackedInfo.outputPath.file.find_first_of('.')) + inExt;
}
return trackedInfo;
}
TrackedInfo make_info(const Name& name,
const Title& title,
const Path& templatePath,
const Directory& contentDir,
const Directory& outputDir,
const std::string& contentExt,
const std::string& outputExt)
{
TrackedInfo trackedInfo;
trackedInfo.name = name;
Path nameAsPath;
nameAsPath.set_file_path_from(unquote(name));
trackedInfo.contentPath = Path(contentDir + nameAsPath.dir, nameAsPath.file + contentExt);
trackedInfo.outputPath = Path(outputDir + nameAsPath.dir, nameAsPath.file + outputExt);
trackedInfo.title = title;
trackedInfo.templatePath = templatePath;
return trackedInfo;
}
TrackedInfo ProjectInfo::get_info(const Name& name)
{
TrackedInfo trackedInfo;
trackedInfo.name = name;
auto result = trackedAll.find(trackedInfo);
if(result != trackedAll.end())
return *result;
trackedInfo.name = "##not-found##";
return trackedInfo;
}
int ProjectInfo::info(const std::vector<Name>& names)
{
std::cout << c_light_blue << promptStr << c_white << "info on specified names:" << std::endl;
for(auto name=names.begin(); name!=names.end(); name++)
{
if(name != names.begin())
std::cout << std::endl;
TrackedInfo cInfo;
cInfo.name = *name;
if(trackedAll.count(cInfo))
{
cInfo = *(trackedAll.find(cInfo));
std::cout << " name: " << c_green << quote(cInfo.name) << c_white << std::endl;
std::cout << " title: " << cInfo.title << std::endl;
std::cout << " output path: " << cInfo.outputPath << std::endl;
std::cout << " output ext: " << quote(get_output_ext(cInfo)) << std::endl;
std::cout << " content path: " << cInfo.contentPath << std::endl;
std::cout << " content ext: " << quote(get_cont_ext(cInfo)) << std::endl;
std::cout << " script ext: " << quote(get_script_ext(cInfo)) << std::endl;
std::cout << "template path: " << cInfo.templatePath << std::endl;
}
else
std::cout << "Nift is not tracking " << *name << std::endl;
}
return 0;
}
int ProjectInfo::info_all()
{
if(file_exists(".nsm/.watchinfo/watching.list"))
{
WatchList wl;
if(wl.open())
{
start_err(std::cout) << "info-all: failed to open watch list" << std::endl;
return 1;
}
std::cout << c_light_blue << promptStr << c_white << "all watched directories:" << std::endl;
for(auto wd=wl.dirs.begin(); wd!=wl.dirs.end(); wd++)
{
if(wd != wl.dirs.begin())
std::cout << std::endl;
std::cout << " watch directory: " << c_green << quote(wd->watchDir) << c_white << std::endl;
for(auto ext=wd->contExts.begin(); ext!=wd->contExts.end(); ext++)
{
std::cout << "content extension: " << quote(*ext) << std::endl;
std::cout << " template path: " << wd->templatePaths.at(*ext) << std::endl;
std::cout << " output extension: " << wd->outputExts.at(*ext) << std::endl;
}
}
}
std::cout << c_light_blue << promptStr << c_white << "all tracked names:" << std::endl;
for(auto trackedInfo=trackedAll.begin(); trackedInfo!=trackedAll.end(); trackedInfo++)
{
if(trackedInfo != trackedAll.begin())
std::cout << std::endl;
std::cout << " name: " << c_green << quote(trackedInfo->name) << c_white << std::endl;
std::cout << " title: " << trackedInfo->title << std::endl;
std::cout << " output path: " << trackedInfo->outputPath << std::endl;
std::cout << " output ext: " << quote(get_output_ext(*trackedInfo)) << std::endl;
std::cout << " content path: " << trackedInfo->contentPath << std::endl;
std::cout << " content ext: " << quote(get_cont_ext(*trackedInfo)) << std::endl;
std::cout << " script ext: " << quote(get_script_ext(*trackedInfo)) << std::endl;
std::cout << "template path: " << trackedInfo->templatePath << std::endl;
}
return 0;
}
int ProjectInfo::info_names()
{
std::cout << c_light_blue << promptStr << c_white << "all tracked names:" << std::endl;
for(auto trackedInfo=trackedAll.begin(); trackedInfo!=trackedAll.end(); trackedInfo++)
std::cout << " " << c_green << trackedInfo->name << c_white << std::endl;
return 0;
}
int ProjectInfo::info_tracking()
{
std::cout << c_light_blue << promptStr << c_white << "all tracked info:" << std::endl;
for(auto trackedInfo=trackedAll.begin(); trackedInfo!=trackedAll.end(); trackedInfo++)
{
if(trackedInfo != trackedAll.begin())
std::cout << std::endl;
std::cout << " name: " << c_green << quote(trackedInfo->name) << c_white << std::endl;
std::cout << " title: " << trackedInfo->title << std::endl;
std::cout << " output path: " << trackedInfo->outputPath << std::endl;
std::cout << " output ext: " << quote(get_output_ext(*trackedInfo)) << std::endl;
std::cout << " content path: " << trackedInfo->contentPath << std::endl;
std::cout << " content ext: " << quote(get_cont_ext(*trackedInfo)) << std::endl;
std::cout << " script ext: " << quote(get_script_ext(*trackedInfo)) << std::endl;
std::cout << "template path: " << trackedInfo->templatePath << std::endl;
}
return 0;
}
int ProjectInfo::info_watching()
{
if(file_exists(".nsm/.watchinfo/watching.list"))
{
WatchList wl;
if(wl.open())
{
start_err(std::cout) << "info-watching: failed to open watch list" << std::endl;
return 1;
}
std::cout << c_light_blue << promptStr << c_white << "all watched directories:" << std::endl;
for(auto wd=wl.dirs.begin(); wd!=wl.dirs.end(); wd++)
{
if(wd != wl.dirs.begin())
std::cout << std::endl;
std::cout << " watch directory: " << c_green << quote(wd->watchDir) << c_white << std::endl;
for(auto ext=wd->contExts.begin(); ext!=wd->contExts.end(); ext++)
{
std::cout << "content extension: " << quote(*ext) << std::endl;
std::cout << " template path: " << wd->templatePaths.at(*ext) << std::endl;
std::cout << " output extension: " << wd->outputExts.at(*ext) << std::endl;
}
}
}
else
{
std::cout << "not watching any directories" << std::endl;
}
return 0;
}
int ProjectInfo::set_incr_mode(const std::string& modeStr)
{
if(modeStr == "mod")
{
if(incrMode == INCR_MOD)
{
start_err(std::cout) << "set_incr_mode: incremental mode is already " << quote(modeStr) << std::endl;
return 1;
}
incrMode = INCR_MOD;
int ret_val = remove_hash_files();
if(ret_val)
return ret_val;
}
else if(modeStr == "hash")
{
if(incrMode == INCR_HASH)
{
start_err(std::cout) << "set_incr_mode: incremental mode is already " << quote(modeStr) << std::endl;
return 1;
}
incrMode = INCR_HASH;
}
else if(modeStr == "hybrid")
{
if(incrMode == INCR_HYB)
{
start_err(std::cout) << "set_incr_mode: incremental mode is already " << quote(modeStr) << std::endl;
return 1;
}
incrMode = INCR_HYB;
}
else
{
start_err(std::cout) << "set_incr_mode: do not recognise incremental mode " << quote(modeStr) << std::endl;
return 1;
}
save_local_config();
std::cout << "successfully changed incremental mode to " << quote(modeStr) << std::endl;
return 0;
}
int ProjectInfo::remove_hash_files()
{
open_tracking(1);
std::cout << "removing hashes.." << std::endl;
Path dep, infoPath;
std::string str;
for(auto cInfo=trackedAll.begin(); cInfo!=trackedAll.end(); ++cInfo)
{
//gets path of info file from last time output file was built
infoPath = cInfo->outputPath.getInfoPath();
//checks whether info path exists
if(file_exists(infoPath.str()))
{
std::ifstream infoStream(infoPath.str());
std::string timeDateLine;
Name prevName;
Title prevTitle;
Path prevTemplatePath;
// reads date/time, name, title and template path
getline(infoStream, str);
getline(infoStream, str);
getline(infoStream, str);
getline(infoStream, str);
while(dep.read_file_path_from(infoStream))
{
if(file_exists(dep.str()))
{
int ret_val = remove_path(dep.getHashPath());
if(ret_val)
return ret_val;
}
}
}
}
return 0;
}
std::string ProjectInfo::get_ext(const TrackedInfo& trackedInfo, const std::string& extType)
{
std::string ext;
//checks for non-default extension
Path extPath = trackedInfo.outputPath.getInfoPath();
extPath.file = extPath.file.substr(0, extPath.file.find_first_of('.')) + extType;
if(file_exists(extPath.str()))
{
std::ifstream ifs(extPath.str());
getline(ifs, ext);