forked from xdarklight/cm-update-server
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadd-build.js
161 lines (144 loc) · 5.04 KB
/
add-build.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
var Troll = require('troll-opt').Troll;
var models = require('./models/');
var fs = require('fs');
var utils = require('./utils.js');
var buildInfo = (new Troll()).options(function(troll) {
troll.banner('Adds a new build to the database.');
troll.opt('device', 'The device ID.', { type: 'string', required: true });
troll.opt('timestamp', 'The build\'s timestamp as "unixepoch" timestamp ("ro.build.date.utc").', { type: 'integer', required: true });
troll.opt('md5sum', 'The build\'s md5sum.', { type: 'string', required: true });
troll.opt('filename', 'The resulting filename.', { type: 'string', required: true });
troll.opt('channel', 'The update-channel.', { type: 'string', required: true });
troll.opt('api_level', 'The SDK API-level of the ROM ("ro.build.version.sdk").', { type: 'integer', required: true });
troll.opt('subdirectory', 'The subdirectory from which the file can be downloaded.', { type: 'string' });
troll.opt('active', 'Marks the build as active (available for download) or not.', { type: 'boolean', required: true });
troll.opt('sourcecode_timestamp', 'The ("unixepoch") timestamp when the source code was updated.', { type: 'integer' });
troll.opt('filesize', 'The size (in bytes) of the ZIP file.', { type: 'integer' });
troll.opt('incrementalid', 'The build\s incremental ID ("ro.build.version.incremental").', { type: 'string' });
troll.opt('changelogfile', 'A path to a file which contains the changelog (utf-8 encoded) for the build.', { type: 'string' });
troll.opt('targetfileszip', 'The name of the "target files" ZIP archive (useful for generating incremental updates).', { type: 'string' });
});
// TODO: Remove this once sequelize 2 is ready.
var validateUniqueActiveRomPerSubdirectory = function(romVariant, parentRomId, successCallback) {
// Find all existing active roms for this filename in the given subdirectory.
models.Rom.count({
where: {
isActive: true,
filename: buildInfo.filename,
},
include: [
{
model: models.RomVariant,
where: {
id: romVariant.id,
}
}
]
}).then(function(totalExisting) {
if (totalExisting > 0) {
throw new Error('There are already ' + totalExisting + ' active ROMs for ' + JSON.stringify(romVariant) + ' with filename ' + buildInfo.filename);
} else {
successCallback(romVariant, parentRomId);
}
});
}
function createNewRomVariantFor(device) {
var variantName = device.name + '_' + new Date().getTime();
var romVariant = models.RomVariant.build({
DeviceId: device.id,
name: variantName,
subdirectory: buildInfo.subdirectory,
});
romVariant.save().then(function() {
console.log('Successfully created new rom variant ' + JSON.stringify(romVariant));
validateUniqueActiveRomPerSubdirectory(romVariant, null, createNewRomFor);
});
}
function createNewRomFor(romVariant, parentRomId) {
var buildTimestamp = utils.toDate(buildInfo.timestamp);
var parsedUpdateChannel = new String(buildInfo.channel);
if (parsedUpdateChannel.toUpperCase() == "RC") {
parsedUpdateChannel = "RC";
} else {
parsedUpdateChannel = parsedUpdateChannel.toLowerCase();
}
var sourceCodeTimestamp = null;
if (buildInfo.sourcecode_timestamp && buildInfo.sourcecode_timestamp > 0) {
sourceCodeTimestamp = utils.toDate(buildInfo.sourcecode_timestamp);
}
var filesize = null;
if (buildInfo.filesize && !isNaN(buildInfo.filesize)) {
filesize = buildInfo.filesize;
}
if (isNaN(parentRomId)) {
parentRomId = null;
}
models.Rom.build({
RomVariantId: romVariant.id,
timestamp: buildTimestamp,
md5sum: buildInfo.md5sum,
filename: buildInfo.filename,
updateChannel: parsedUpdateChannel,
changelog: changelog,
apiLevel: buildInfo.api_level,
isActive: buildInfo.active,
sourceCodeTimestamp: sourceCodeTimestamp,
incrementalId: buildInfo.incrementalid,
parentRomId: parentRomId,
targetFilesZipName: buildInfo.targetfileszip,
fileSize: filesize,
}).save().then(function(newRom) {
console.log('Successfully created new rom: ' + JSON.stringify(newRom));
});
}
var changelog = null;
if (buildInfo.changelogfile) {
changelog = fs.readFileSync(buildInfo.changelogfile, 'utf-8');
}
models.sequelize.sync().then(function() {
models.RomVariant.find({
include: [
{
model: models.Device,
where: {
name: buildInfo.device,
}
},
],
where: {
subdirectory: buildInfo.subdirectory
}
}).then(function(romVariant) {
if (romVariant) {
models.Rom.find({
include: [
{
model: models.RomVariant,
where: {
id: romVariant.id,
}
}
],
order: 'timestamp DESC'
}).then(function(parentRom) {
validateUniqueActiveRomPerSubdirectory(romVariant, parentRom.id, createNewRomFor);
});
} else {
models.Device.find({
where: {
name: buildInfo.device
}
}).then(function(device) {
if (device) {
createNewRomVariantFor(device);
} else {
var device = models.Device.build({ name: buildInfo.device });
device.save().then(function() {
console.log('Successfully created new device ' + JSON.stringify(device));
createNewRomVariantFor(device);
});
}
});
}
});
});