-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
252 lines (229 loc) · 8.04 KB
/
index.js
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
var https = require('https'),
spinner = require('cli-spinner').Spinner,
path = require('path'),
walker = require('walker'),
fse = require('fs-extra'),
replace = require('replace'),
solution = {};
if(process.argv.length < 3 || process.argv[2].length === 0)
{
console.error("Please provide a valid path to your source files.");
process.exit(1);
}
var isPathValid = function (dirPath) {
return fse.existsSync(dirPath);
};
var isValidScriptFile = function (filePath) {
return (filePath.indexOf('.cs') === filePath.length - 3);
};
var isValidReferenceFile = function (filePath) {
return (filePath.indexOf('.dll') === filePath.length - 4);
};
var isEditorScript = function (pathReference, scriptFile) {
for(var i = 0; i < pathReference.length; i++)
{
if(scriptFile.indexOf(pathReference[i]) > -1)
{
return true;
}
}
return false;
};
var invertSlashes = function (string) {
return string.replace(/\//ig, '\\');
};
var preprocessPath = function (element, index, array) {
array[index] = '<Compile Include="' + invertSlashes(element) + '" />';
};
var placeSolution = function (sourceCodePath) {
var targetPath = path.resolve(sourceCodePath + '/..');
fse.copySync(path.join(__dirname, 'template'), targetPath);
return targetPath;
};
var writeScripts = function (solutionPath, scripts) {
replace({
regex: '{{SCRIPTITEMS}}',
replacement: scripts.join('\n'),
paths: [solutionPath],
recursive: false,
silent: true,
preview: false
});
};
var writeReferences = function (solutionPath, references) {
for(var i = 0; i < references.length; i++)
{
references[i] = '<Reference Include="' + path.basename(references[i], '.dll') + '">' +
' <HintPath>' + invertSlashes(references[i]) + '</HintPath>' +
'</Reference>';
}
replace({
regex: '{{REFERENCEITEMS}}',
replacement: references.join('\n'),
paths: [solutionPath],
recursive: false,
silent: true,
preview: false
});
};
var writeSymbols = function (solutionPath, platform) {
var symbols = require(path.join(__dirname, 'data/platformSymbols.json'));
var replacement = '';
if(solutionPath.indexOf('Editor') > -1)
{
replacement = symbols[platform].Editor;
} else
{
replacement = symbols[platform].Player;
}
replace({
regex: '{{DEFINEDSYMBOLS}}',
replacement: replacement,
paths: [solutionPath],
recursive: false,
silent: true
});
replace({
regex: '{{PLATFORM}}',
replacement: platform,
paths: [solutionPath],
recursive: false,
silent: true
});
};
var setReferencePath = function (solutionPath) {
console.log(' -> Linking unity references...');
replace({
regex: '{{REFERENCEPATH}}',
replacement: invertSlashes(path.resolve(path.join(solutionPath, 'References'))),
paths: [solutionPath],
recursive: true,
silent: true
});
};
var prepareReferences = function(solutionPath) {
var refPath = path.resolve(path.join(solutionPath, 'References'));
if(fse.existsSync(refPath))
{
setReferencePath(solutionPath);
} else {
var spi = new spinner(' -> Downloading references...');
spi.setSpinnerString(10);
spi.start();
// TODO more readability
var tempFile = fse.createOutputStream(path.resolve('./tempfile.zip'), {'flags': 'a'});
https.get(
{
host: 'codeload.github.com',
path: '/malud/unity-libs/zip/master',
headers: { 'User-Agent': 'unity-solution node app',
'Cache-Control': 'no-cache'}
},
function (response) {
if(response.statusCode !== 200)
{
console.error('\nGot statusCode:', response.statusCode);
process.exit(1);
}
response.pipe(tempFile);
tempFile.on('finish', function () {
tempFile.close(function () {
spi.stop(true);
// finish cb
var yauzl = require('yauzl');
yauzl.open(path.resolve('./tempfile.zip'), function (err, zipfile) {
if(err) throw err;
zipfile.on('entry', function (entry) {
if (/\/$/.test(entry.fileName) || entry.fileName.indexOf('.') === 0) {
// directory file names end with '/'
// dotfiles
return;
}
zipfile.openReadStream(entry, function (err, readStream) {
if(err) throw err;
readStream.pipe(fse.createOutputStream(path.resolve('./' + entry.fileName.replace('unity-libs-master',''))));
});
});
zipfile.once('end', function () {
console.log('Solution is prepared now.');
setReferencePath(solutionPath);
});
});
});
});
tempFile.on('error', function (err) {
fse.unlink('tempfile.zip');
throw err;
});
});
}
};
solution.create = function (pathParam) {
if(!isPathValid(pathParam))
{
console.error('Path does not exist.');
return false;
}
var unityEditorRelated = [];
var playerFiles = [];
var editorFiles = [];
var playerRefs = [];
var editorRefs = [];
walker(fullPath)
.on('dir', function (dir, stat) {
if(dir.indexOf('Editor') > -1)
{
var isChildEditorPath = false;
for(var i = 0; i < unityEditorRelated.length; i++)
{
if(dir.indexOf(unityEditorRelated[i]) === 0)
{
isChildEditorPath = true;
break;
}
}
if(!isChildEditorPath) unityEditorRelated.push(dir);
}
})
.on('file', function (file, stat) {
if(isValidScriptFile(file))
{
if(isEditorScript(unityEditorRelated, file))
{
editorFiles.push(file);
} else
{
playerFiles.push(file);
}
} else if (isValidReferenceFile(file))
{
if(isEditorScript(unityEditorRelated, file))
{
editorRefs.push(file);
} else
{
playerRefs.push(file);
}
}
})
.on('end', function() {
playerFiles.forEach(preprocessPath);
editorFiles.forEach(preprocessPath);
slnPath = placeSolution(pathParam);
writeScripts(path.join(slnPath, 'Assembly-CSharp.csproj'), playerFiles);
writeScripts(path.join(slnPath, 'Assembly-CSharp-Editor.csproj'), editorFiles);
writeReferences(path.join(slnPath, 'Assembly-CSharp.csproj'), playerRefs);
writeReferences(path.join(slnPath, 'Assembly-CSharp-Editor.csproj'), editorRefs);
var platform = process.argv[3];
if(platform)
{
writeSymbols(path.join(slnPath, 'Assembly-CSharp.csproj'), platform);
writeSymbols(path.join(slnPath, 'Assembly-CSharp-Editor.csproj'), platform);
}
prepareReferences(slnPath);
})
;
};
var fullPath = path.resolve(process.argv[2]);
solution.create(fullPath);
module.exports = solution;