-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathindex.js
More file actions
155 lines (115 loc) · 4.77 KB
/
index.js
File metadata and controls
155 lines (115 loc) · 4.77 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
var fs = require('fs');
var spawn = require('child_process').spawn;
var crypto = require('crypto');
var path = require('path');
var ncp = require('ncp').ncp;
var shasum = require('shasum');
var path = require('path');
module.exports = function (req, res, cb) {
console.log('compileServiceCode.dotnet(). just executed'.yellow);
var service = req.service;
// TODO: override hash logic here to include the csproj contents.
// by default handled in compile/index.js and only used the code file
var hash = service.sha1;
console.log('inside compileServiceCode.dotnet()'.yellow)
console.dir(service);
/*
Supported Target: .NET Core 1.1
.NET Core 2.0 (untested)
Uasge:
- Standard .NET Core console app created via `dotnet new console -n <app name>`
- To execute run `microcule ./<path to .NET Core app>/Program.cs`
*/
//console.log('buildDir: ' + service.buildDir);
//console.log('releaseDir: ' + service.releaseDir);
// release is explicityly defaulted to the root of the microcule binary. Not sure why given there is a release folder.
// This may will break if the releaseDir default is overriden in config
//service.releaseDir = service.releaseDir + '/release';
// first, we need to write the file to a temporary location on the disk
// it could be possible to bypass writing to the disk entirely here, but it will be easier to debug user-scripts,
// and also interact with various compilers without having to look up special use cases for stdin / argv code parsing
//console.dir(service, { depth: null, colors: true });
var tmpBuildDir = service.buildDir + '/dotnet/' + hash;
var buildOutputDir = service.releaseDir + '/dotnet/' + hash;
//service.sourceCodeFilePath = '/Users/janaka.abeywardhana/code-projects/microcule/examples/services/hello-world/dotnet-hello/Program.cs';
//console.log('codefilepath = '.yellow, service.originalCodeFilePath);
// Create the folders we need. Because put code in a folders based on the hash we need to create folders it code changes.
// Logic for deciding if we needed re-build is in the shared complile logic.
if (!fs.existsSync(buildOutputDir)) {
fs.mkdirSync(buildOutputDir);
if (!fs.existsSync(tmpBuildDir)) {
fs.mkdirSync(tmpBuildDir);
}
}
console.log('Copy from ', service.originalSourceCodeDir, ' to ', tmpBuildDir);
ncp.limit = 5; // concurrent copies
ncp(service.originalSourceCodeDir, tmpBuildDir, function (err) {
if (err) {
return cb(err);
}
console.log('Successfully copied code to temp build dir');
console.log('starting dependency restore', 'dotnet restore');
var restore = spawn('dotnet', ['restore'], {
cwd: tmpBuildDir // ensure binary compiles to target directory
});
var stderr = '', stdout = '';
restore.on('error', function (data) {
console.log('error', data);
cb(data);
});
restore.on('exit', function (data) {
console.log('exit', data);
console.log('starting compiler dotnet', 'dotnet build -c release -v minimal -o ', buildOutputDir);
// command syntax: dotnet build -f 1.1 -c release -o <output dir> -v minimal
var compiler = spawn('dotnet', ['build', '-c', 'release', '-v','minimal', '-o', buildOutputDir], {
cwd: tmpBuildDir // ensure binary compiles to target directory
});
var stderr = '', stdout = '';
compiler.on('error', function (data) {
console.log('error', data)
cb(data);
});
/*
vm.stdin.error
vm.exit
vm.stdout.end
vm.stderr
*/
compiler.on('data', function (data) {
console.log('got data', data)
});
compiler.on('close', function (data) {
console.log('close', data)
//cb(null, 'close')
});
compiler.stdin.on('error', function (data) {
console.log('stdin.error', data);
// cb(null, 'close')
});
compiler.stdout.on('data', function (data) {
console.log('stdout', data);
stdout += data.toString();
//cb(null, 'close')
});
compiler.stderr.on('data', function (data) {
// console.log('stderr', data);
stderr += data.toString();
//cb(null, 'close')
});
compiler.on('exit', function (data) {
console.log('exit', data);
var result = {
tmpSourceFile: tmpBuildDir,
bin: buildOutputDir,
buildDir: tmpBuildDir,
originalCodeFilePath: service.originalCodeFilePath,
dotnetCsproj: service.dotnetCsproj,
stderr: stderr,
stdout: stdout,
exitCode: data
};
cb(null, result);
});
});
});
};