-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetBuildVersion.cpp
500 lines (500 loc) · 21.4 KB
/
SetBuildVersion.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
#include<iostream>
#include<stdio.h>
#include<conio.h>
#include<fstream>
#include<sstream>
#include <windows.h>
using namespace std;
string CurrentDirectory() {
char Buffer[MAX_PATH];
GetModuleFileName( NULL, Buffer, MAX_PATH );
string::size_type Position = string(Buffer).find_last_of( "\\/" );
return string(Buffer).substr(0,Position);
}
#define MAXSWITCHES 9
#define MAXSUBSWITCHES 5
#define MAXSWITCHINPUT 6
string Switches[MAXSWITCHES][MAXSUBSWITCHES] = {
{"-h","-help","NULL","NULL","-NULL"}, // 1
{"-d","-default","NULL","NULL","NULL"}, // 2
{"-f","-force","NULL","NULL","NULL"}, // 3
{"-a","-auto","NULL","NULL","NULL"}, // 4
{"-as","-autos","-autosub","NULL","NULL"}, // 5
{"-am","-autom","-automajor","NULL","NULL"}, // 6
{"-fmv","-forcemv","-forcemaxv","-forcemaxversion","NULL"}, // 7
{"-fmsv","-forcemsv","-forcemaxsv","-forcemaxsubv","-forcemaxsubversion"}, // 8
{"-n","-new","NULL","NULL","-NULL"} // 9
};
int SwitchFlags[MAXSWITCHES] = {1,1,1,1,1,1,1,1,1};
int CurrentVersion[3] = {1,0,0};
int MaxVersion = 9;
int MaxSubVersion = 9;
string AssemblyInfoVbFilePath = "";
string SetupAipFilePath = "";
int GetSwitchIndex(char* Switch) {
for(int i=0; i<MAXSWITCHES; i++) {
for(int j=0; j<MAXSUBSWITCHES; j++) {
if(strcmp(Switches[i][j].c_str(),(const char*)Switch)==0) {
return i+1;
}
}
}
return 0;
}
void ToggleSwitch(int SwitchID) {
if(SwitchFlags[SwitchID-1]==0) SwitchFlags[SwitchID-1] = 1;
else if(SwitchFlags[SwitchID-1]==1) SwitchFlags[SwitchID-1] = 0;
}
void SetSwitch(int SwitchID,int State) {
SwitchFlags[SwitchID-1] = State;
}
int GetSwitchState(int SwitchID) {
return SwitchFlags[SwitchID-1];
}
int CheckFileType(string FilePath,int FileType = 0) {
int FileExtensionPos = 0;
for(int i=0; i<strlen(FilePath.c_str()); i++) {
if(FilePath[i]=='.') {
FileExtensionPos = i;
break;
}
}
string Ext = "";
if(FileType==0) Ext = ".vb";
else Ext = ".aip";
if(strcmp(FilePath.substr(FileExtensionPos,strlen(Ext.c_str())).c_str(),Ext.c_str())==0) {
return 1;
} else {
return 0;
}
}
string _GetVersion() {
stringstream VersionStringStream;
VersionStringStream<<CurrentVersion[0]<<"."<<CurrentVersion[1]<<"."<<CurrentVersion[2];
return VersionStringStream.str();
}
int InitCurrentVersion(string FilePath) {
string VersionString = "<Assembly: AssemblyVersion(\"";
string VersionValidatorString = "";
fstream VersionFile(FilePath.c_str(),ios::in);
if(VersionFile.is_open()) {
if(CheckFileType(FilePath,0)) {
string InputLine;
while(getline(VersionFile,InputLine)) {
size_t CommentPosition = InputLine.find("'");
size_t TagOpenPosition = InputLine.find("<");
if(CommentPosition!=string::npos) {
if(TagOpenPosition!=string::npos) {
if(TagOpenPosition<CommentPosition) {
size_t AssemblyVersionFound = InputLine.find(VersionString);
if(AssemblyVersionFound!=string::npos) {
VersionString = InputLine.substr(AssemblyVersionFound+strlen(VersionString.c_str()),strlen(InputLine.c_str())-AssemblyVersionFound);
size_t NextQuotePos = VersionString.find("\")>");
if(NextQuotePos!=string::npos) {
VersionString = VersionString.substr(0,NextQuotePos);
stringstream VersionStringStream(VersionString);
string Version;
int i=0;
while(getline(VersionStringStream,Version,'.')) {
CurrentVersion[i] = atoi(Version.c_str());
i++;
}
break;
} else {
return 0;
}
}
} else {
continue;
}
} else {
continue;
}
} else {
size_t AssemblyVersionFound = InputLine.find(VersionString);
if(AssemblyVersionFound!=string::npos) {
VersionString = InputLine.substr(AssemblyVersionFound+strlen(VersionString.c_str()),strlen(InputLine.c_str())-AssemblyVersionFound);
size_t NextQuotePos = VersionString.find("\")>");
if(NextQuotePos!=string::npos) {
VersionString = VersionString.substr(0,NextQuotePos);
stringstream VersionStringStream(VersionString);
string Version;
int i=0;
while(getline(VersionStringStream,Version,'.')) {
CurrentVersion[i] = atoi(Version.c_str());
i++;
}
break;
} else {
return 0;
}
}
}
}
} else {
cout<<"\n [Error::Provided file is not of correct type.]";
return 0;
}
} else {
cout<<"\n [Error::Provided file is inaccessible.]";
return 0;
}
VersionFile.close();
return 1;
}
/*
int SetBuildVersionSetup(){
string FilePath = SetupAipFilePath;
string VersionString = " <ROW Property=\"ProductVersion\" Value=\"";
string VersionValidatorString = "";
fstream VersionFile(FilePath.c_str(),ios::in);
string FileWritePath = FilePath;
FileWritePath+=".temp";
fstream VersionFileWrite(FileWritePath.c_str(),ios::out);
if(VersionFile.is_open()){
if(CheckFileType(FilePath,1)){
string InputLine;
while(getline(VersionFile,InputLine)){
size_t AssemblyVersionFound = InputLine.find(VersionString);
if(AssemblyVersionFound!=string::npos){
VersionFileWrite<<VersionString<<_GetVersion()<<"\" Type=\"32\"/>";
}else{
VersionFileWrite<<InputLine;
}
VersionFileWrite<<"\n";
}
}else{
cout<<"\n [Error::Provided file is not of correct type.]";
return 0;
}
}else{
cout<<"\n [Error::Provided file is inaccessible.]";
return 0;
}
VersionFile.close();
VersionFileWrite.close();
if(remove(FilePath.c_str())==0){
if(rename(FileWritePath.c_str(),FilePath.c_str())==0){
cout<<" Done!\n Do not forget to create new Product ID Code for the setup!";
return 1;
}else{
cout<<"\n [Error::The file cannot be renamed or is inaccessible.]";
return 0;
}
}else{
cout<<"\n [Error::Provided file is inaccessible.]";
return 0;
}
}
*/
int SetBuildVersion(string FilePath) {
string VersionString = "<Assembly: AssemblyVersion(\"";
string FileVersionString = "<Assembly: AssemblyFileVersion(\"";
string VersionValidatorString = "";
fstream VersionFile(FilePath.c_str(),ios::in);
string FileWritePath = FilePath;
FileWritePath+=".temp";
fstream VersionFileWrite(FileWritePath.c_str(),ios::out);
if(VersionFile.is_open()) {
if(CheckFileType(FilePath,0)) {
string InputLine;
while(getline(VersionFile,InputLine)) {
size_t CommentPosition = InputLine.find("'");
size_t TagOpenPosition = InputLine.find("<");
if(CommentPosition!=string::npos) {
if(TagOpenPosition!=string::npos) {
if(TagOpenPosition<CommentPosition) {
size_t AssemblyVersionFound = InputLine.find(VersionString);
size_t AssemblyFileVersionFound = InputLine.find(FileVersionString);
if(AssemblyVersionFound!=string::npos) {
VersionFileWrite<<VersionString<<_GetVersion()<<"\")>";
} else if(AssemblyFileVersionFound!=string::npos) {
VersionFileWrite<<FileVersionString<<_GetVersion()<<"\")>";
}
} else {
VersionFileWrite<<InputLine;
}
} else {
VersionFileWrite<<InputLine;
}
} else {
size_t AssemblyVersionFound = InputLine.find(VersionString);
size_t AssemblyFileVersionFound = InputLine.find(FileVersionString);
if(AssemblyVersionFound!=string::npos) {
VersionFileWrite<<VersionString<<_GetVersion()<<"\")>";
} else if(AssemblyFileVersionFound!=string::npos) {
VersionFileWrite<<FileVersionString<<_GetVersion()<<"\")>";
} else {
VersionFileWrite<<InputLine;
}
}
VersionFileWrite<<"\n";
}
} else {
cout<<"\n [Error::Provided file is not of correct type.]";
return 0;
}
} else {
cout<<"\n [Error::Provided file is inaccessible.]";
return 0;
}
VersionFile.close();
VersionFileWrite.close();
if(remove(FilePath.c_str())==0) {
if(rename(FileWritePath.c_str(),FilePath.c_str())==0) {
fstream CurrentVersionFile("CURRENT.VER",ios::trunc|ios::out);
if(CurrentVersionFile.is_open()) {
CurrentVersionFile<<_GetVersion();
}
CurrentVersionFile.close();
cout<<" Done!\n Do not forget to update version of setup too!";
return 1;
} else {
cout<<"\n [Error::The file cannot be renamed or is inaccessible.]";
return 0;
}
} else {
cout<<"\n [Error::Provided file is inaccessible.]";
return 0;
}
}
string* Arguments;
void ClearArguments() {
delete[] Arguments;
}
bool StringDigitOnly(const string &InputString) {
return InputString.find_first_not_of("0123456789") == string::npos;
}
bool StringVersionValidator(string InputString) {
int VersionCount = 0;
stringstream InputStringStream(InputString);
string InputStringSplit;
while(getline(InputStringStream,InputStringSplit,'.')) {
if(VersionCount>2) {
return false;
} else {
if(!StringDigitOnly(InputStringSplit)) {
return false;
}
}
VersionCount++;
}
return true;
}
void StringVersionSet(string InputString) {
int VersionCount = 0;
stringstream InputStringStream(InputString);
string InputStringSplit;
while(getline(InputStringStream,InputStringSplit,'.')) {
CurrentVersion[VersionCount] = atoi(InputStringSplit.c_str());
VersionCount++;
}
}
void PrintHelp(const char *name) {
cout<<"\n Help for "<<name<<":"
<<"\n Available switches are:"
<<"\n [-h][-help]"
<<"\n "<<char(192)<<char(26)<<" For Help"
<<"\n [-d][-default]"
<<"\n "<<char(192)<<char(26)<<" To take AssemblyInfo.vb file path as default."
<<"\n [-f][-force]"
<<"\n "<<char(192)<<char(26)<<" To force to take AssemblyInfo.vb file path as provided."
<<"\n [-a][-auto]"
<<"\n "<<char(192)<<char(26)<<" To automatically generate next version. {1.0.x}"
<<"\n [-as][-autos][-autosub]"
<<"\n "<<char(192)<<char(26)<<" For automatically generate next sub-version. {1.x.0}"
<<"\n [-am][-autom][-automajor]"
<<"\n "<<char(192)<<char(26)<<" For automatically generate next major-version. {x.0.0}"
<<"\n [-fmv][-forcemv][-forcemaxv][-forcemaxversion]"
<<"\n "<<char(192)<<char(26)<<" To force to take max version value as provided."
<<"\n [-fmsv][-forcemsv][-forcemaxsv][-forcemaxsubv][-forcemaxsubversion]"
<<"\n "<<char(192)<<char(26)<<" To force to take max sub-version value as provided."
<<"\n [-n][-new]"
<<"\n "<<char(192)<<char(26)<<" To change the version to the one given with the switch."
<<"\n How to use:"
<<"\n "<<name<<" [Switches] [FilePath{If Required}] [Version{If Required}]"
<<"\n";
}
int main(int argc,char *argv[],char* envp[]) {
if (argc > 1) {
AssemblyInfoVbFilePath = CurrentDirectory();
AssemblyInfoVbFilePath+= "\\Pokemon Sprite Inserter\\My Project\\AssemblyInfo.vb";
//SetupAipFilePath = CurrentDirectory();
//SetupAipFilePath+= "\\Pokemon Sprite Inserter.aip";
atexit(ClearArguments);
int SwitchCount = 0;
for(int i=1; i<argc; i++) {
if(argv[i][0]=='-') SwitchCount++;
}
int ArgumentCount = 0;
Arguments = new string[argc-SwitchCount];
for(int i=1; i<argc; i++) {
if(argv[i][0]!='-') {
Arguments[ArgumentCount] = (const char*)argv[i];
ArgumentCount++;
}
}
int CurrentArgument = 0;
if(GetSwitchIndex(argv[1])==1) {
PrintHelp(argv[0]);
return 0;
}
if(SwitchCount<=MAXSWITCHINPUT) {
int k = 1;
for(int i=1; i<=SwitchCount; i++) {
if(argv[k][0]!='-') {
i--;
k++;
continue;
}
switch(GetSwitchIndex(argv[k])) {
case 1:
PrintHelp(argv[0]);
return 0;
break;
case 2:
if(GetSwitchState(2)) {
AssemblyInfoVbFilePath = CurrentDirectory();
AssemblyInfoVbFilePath+= "\\Pokemon Sprite Inserter\\My Project\\AssemblyInfo.vb";
cout<<"\n Using default path for AssemblyInfo.vb file.";
if(InitCurrentVersion(AssemblyInfoVbFilePath)) {
cout<<"\n Current Version is : "<<_GetVersion();
} else {
return 0;
}
ToggleSwitch(3);
} else {
cout<<"\n [Warning::Force switch has overridden the Default switch. Please place Default switch before Force.]";
}
break;
case 3:
if(GetSwitchState(3)) {
if((ArgumentCount>0)&&(ArgumentCount>=CurrentArgument)) {
AssemblyInfoVbFilePath = Arguments[CurrentArgument];
cout<<"\n Using ["<<AssemblyInfoVbFilePath<<"] as path for AssemblyInfo.vb file.";
if(InitCurrentVersion(AssemblyInfoVbFilePath)) {
cout<<"\n Current Version is : "<<_GetVersion();
} else {
return 0;
}
CurrentArgument++;
if(CurrentArgument>ArgumentCount) CurrentArgument = ArgumentCount;
} else {
cout<<"\n [Error::No argument was passed to Force switch.]";
return 0;
}
} else {
cout<<"\n [Warning::Default switch has overridden the Force switch. Please place Force switch before Default.]";
}
break;
case 4:
if(GetSwitchState(4)) {
ToggleSwitch(7);
cout<<"\n Auto-incrementing version.";
CurrentVersion[2]++;
if(CurrentVersion[2]>MaxVersion) {
cout<<"\n [Warning::Incremented version was greater than the max version. Please use Force Max Version switch to increase the limit.]";
CurrentVersion[2]--;
} else {
cout<<" [Incremented version : "<<_GetVersion()<<"]";
SetSwitch(9,0);
}
}
break;
case 5:
if(GetSwitchState(5)) {
ToggleSwitch(8);
cout<<"\n Auto-incrementing sub-version.";
CurrentVersion[1]++;
if(CurrentVersion[1]>MaxSubVersion) {
cout<<"\n [Warning::Incremented sub-version was greater than the max sub-version. Please use Force Max Sub-Version switch to increase the limit.]";
CurrentVersion[1]--;
} else {
cout<<" [Incremented version : "<<_GetVersion()<<"]";
SetSwitch(9,0);
}
}
break;
case 6:
if(GetSwitchState(6)) {
cout<<"\n Auto-incrementing major-version.";
CurrentVersion[0]++;
cout<<" [Incremented version : "<<_GetVersion()<<"]";
SetSwitch(9,0);
}
break;
case 7:
if(GetSwitchState(7)) {
if((ArgumentCount>0)&&(ArgumentCount>=CurrentArgument)) {
if(StringDigitOnly(Arguments[CurrentArgument])) {
cout<<"\n Forcing to take max version value as "<<Arguments[CurrentArgument]<<".";
MaxVersion = atoi(Arguments[CurrentArgument].c_str());
} else {
cout<<"\n [Error::Argument passed to Force Max Version switch is not an integer]";
return 0;
}
CurrentArgument++;
if(CurrentArgument>ArgumentCount) CurrentArgument = ArgumentCount;
} else {
cout<<"\n [Error::No argument was passed to Force Max Version switch.]";
return 0;
}
} else {
cout<<"\n [Warning::Auto Version switch has overridden the Force Max Version switch. Please place Force Max Version switch before Auto Version.]";
}
break;
case 8:
if(GetSwitchState(8)) {
if((ArgumentCount>0)&&(ArgumentCount>=CurrentArgument)) {
if(StringDigitOnly(Arguments[CurrentArgument])) {
cout<<"\n Forcing to take max sub-version value as "<<Arguments[CurrentArgument]<<".";
MaxSubVersion = atoi(Arguments[CurrentArgument].c_str());
} else {
cout<<"\n [Error::Argument passed to Force Max Sub-Version switch is not an integer]";
return 0;
}
CurrentArgument++;
if(CurrentArgument>ArgumentCount) CurrentArgument = ArgumentCount;
} else {
cout<<"\n [Error::No argument was passed to Force Max Sub-Version switch.]";
return 0;
}
} else {
cout<<"\n [Warning::Auto Sub-Version switch has overridden the Force Max Sub-Version switch. Please place Force Max Sub-Version switch before Auto Sub-Version.]";
}
break;
case 9:
if(GetSwitchState(9)) {
if((ArgumentCount>0)&&(ArgumentCount>=CurrentArgument)) {
ToggleSwitch(4);
ToggleSwitch(5);
ToggleSwitch(6);
if(StringVersionValidator(Arguments[CurrentArgument])) {
StringVersionSet(Arguments[CurrentArgument]);
cout<<"\n New Version is : "<<_GetVersion();
} else {
cout<<"\n [Error::Argument passed to New Version switch is not a valid version format.]";
return 0;
}
CurrentArgument++;
if(CurrentArgument>ArgumentCount) CurrentArgument = ArgumentCount;
} else {
cout<<"\n [Error::No argument was passed to New Version switch.]";
return 0;
}
} else {
cout<<"\n [Warning::Auto Increment switch(es) has overridden the New Version switch. Please place New Version switch before Auto Increment Switch(es).]";
}
break;
}
k++;
}
}
cout<<"\n Finalizing Version Setting.";
SetBuildVersion(AssemblyInfoVbFilePath);
cout<<"\n";
} else {
PrintHelp(argv[0]);
}
return 1;
}