-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRelocateJsInclude.js
More file actions
145 lines (131 loc) · 6.17 KB
/
RelocateJsInclude.js
File metadata and controls
145 lines (131 loc) · 6.17 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
//aOldFolder: the old folder in the source file that need to be updated
//aNewFolder: the new fodler that will replace the old folder (if files are found under the new folder)
//aOldFiles: an array of old filenames that are renamed now in your new project
//aNewFiles: an array of current filenames now in your new project
//Replace <aOldFolder>\\123.js to <aNewFolder>\\123.js if <aNewFolder>\\123.js exists
//Replace <aOldFolder>\\<aOldFiles> to <aNewFolder>\\<aNewFiles> if both <aOldFolder>\\<aOldFiles> and <aNewFolder>\\<aNewFiles> exist
function stFileMap( aOldFolder , aNewFolder , aOldFiles , aNewFiles )
{
//For every file exists in aNewFolder, if it's filename is included by aOldFolder
//in the source file, then update the path of aOldFolder to aNewFolder
this.reOldFolder = new RegExp( aOldFolder );
this.strNewFolder = aNewFolder;
//The length of aryOldFiles and aryNewFiles must be the same
this.aryOldFiles = aOldFiles;
this.aryNewFiles = aNewFiles;
}
function TraverseAndRelocateJsInclude( aJsFolderPath , aFileMapAry , aLogFolder )
{
var folder = WshFileSystem.GetFolder( aJsFolderPath );
var enumFolder = new Enumerator( folder.SubFolders );
for ( ; ! enumFolder.atEnd() ; enumFolder.moveNext() )
{
//Avoid the Backup folder
if ( null == enumFolder.item().Name.match(/^Backup[0-9]*$/) )
{
TraverseAndRelocateJsInclude( enumFolder.item().Path , aFileMapAry , aLogFolder );
}
}
CWUtils.DbgMsg( "VERB" , "RelocateJsInclude" , "Checking *.js and *.bat files in \"" + aJsFolderPath + "\"" , aLogFolder );
var enumFile = new Enumerator( folder.Files );
for ( ; ! enumFile.atEnd() ; enumFile.moveNext() )
{
if ( enumFile.item().Name.match(/.+\.(js|bat)/) )
{
CWUtils.DbgMsg( "VERB" , "RelocateJsInclude" , "Updating " + enumFile.item().Name , aLogFolder );
RelocateJsInclude( enumFile.item().Path , aFileMapAry , aLogFolder );
}
}
}
function RelocateJsInclude( aJsPath , aFileMapAry , aLogFolder )
{
CWUtils.DbgMsg( "VERB" , "RelocateJsInclude" , "Enter. aJsPath=\"" + aJsPath + "\"" , aLogFolder );
//Load file
var fileJs = new CWUtils.CWshTextFile();
if ( false == fileJs.Open( aJsPath , CWUtils.CWshTextFile.Mode.ForReading , false , false ) )
{
CWUtils.DbgMsg( "ERRO" , "RelocateJsInclude" , "fileJs.Open() failed. aJsPath=" + aJsPath , aLogFolder );
return false;
}
var strNewFile = "";
//Parse and update the variable in the eval()
var rePtn = /(\s*eval\s*\(\s*LoadJs\s*\(\s*\")(.+)(\"\s*\)\s*\);.*)/;
while ( ! fileJs.AtEndOfStream() )
{
var strLine = fileJs.ReadLine();
var strNewLine;
var aryResult = rePtn.exec( strLine );
if ( null != aryResult && 4 == aryResult.length )
{
var bMatched = false;
var bEverSearchInFolder = false;
var strInclude = aryResult[2];
var strFolder = CWUtils.WshFileSystem.GetParentFolderName( strInclude );
var strFile = CWUtils.WshFileSystem.GetFileName( strInclude );
for ( var j = 0 ; j < aFileMapAry.length ; j++ )
{
//Remap files if the folder name match reOldFolder and the same filename or strNewFiles is found under strNewFolder
if ( 0 < aFileMapAry[j].strNewFolder.length &&
'\\' != aFileMapAry[j].strNewFolder.charAt(aFileMapAry[j].strNewFolder.length - 1))
{
aFileMapAry[j].strNewFolder += "\\";
}
if ( "undefined" !== aFileMapAry[j].reOldFolder &&
strFolder.match(aFileMapAry[j].reOldFolder) )
{
bEverSearchInFolder = true;
if ( CWUtils.WshFileSystem.FileExists( aFileMapAry[j].strNewFolder + strFile ) )
{
strNewLine = CWUtils.ComputeRelativePath( aJsPath , aFileMapAry[j].strNewFolder + strFile );
bMatched = true;
}
else
{
for ( var k = 0 ; k < aFileMapAry[j].aryOldFiles.length ; k++ )
{
if ( "undefined" !== aFileMapAry[j].aryOldFiles[k] &&
"undefined" !== aFileMapAry[j].aryNewFiles[k] &&
aFileMapAry[j].aryOldFiles.length == aFileMapAry[j].aryNewFiles.length &&
strFile.match(aFileMapAry[j].aryOldFiles[k]) &&
CWUtils.WshFileSystem.FileExists(aFileMapAry[j].strNewFolder + aFileMapAry[j].aryNewFiles[k]) )
{
strNewLine = CWUtils.ComputeRelativePath( aJsPath , aFileMapAry[j].strNewFolder + aFileMapAry[j].aryNewFiles[k] );
bMatched = true;
break;
}
}
}
if ( true == bMatched )
{
break;
}
}
}
if ( false == bMatched )
{
if ( true == bEverSearchInFolder )
{
CWUtils.DbgMsg( "ERRO" , "RelocateJsInclude" , aJsPath + " updating eval(). " + strFile + " is not found under any new folder" , aLogFolder );
}
strNewLine = strLine;
}
else
{
strNewLine = aryResult[1] + strNewLine.replace( /\\/g , "\\\\" ) + aryResult[3];
}
CWUtils.DbgMsg( "INFO" , "RelocateJsInclude" , aJsPath + " updating eval(). " + strLine + " => " + strNewLine , aLogFolder );
strNewFile += strNewLine + "\r\n";
}
else
{
strNewFile += strLine + "\r\n";
}
}
//Overwrite the current file
fileJs.Close();
fileJs.Open( aJsPath , CWUtils.CWshTextFile.Mode.ForWriting , false , true );
fileJs.Write( strNewFile );
fileJs.Close();
CWUtils.DbgMsg( "VERB" , "RelocateJsInclude" , "Leave" , aLogFolder );
return true;
}