1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | return new (P || (P = Promise))(function (resolve, reject) {
|
4 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5 | function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } }
|
6 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
7 | step((generator = generator.apply(thisArg, _arguments)).next());
|
8 | });
|
9 | };
|
10 | const path = require('path');
|
11 | const ast = require('egret-typescript-ast');
|
12 | const fs = require('fs-extra-promise');
|
13 | const util = require('egret-node-utils');
|
14 | const stream = require('stream');
|
15 | const resource = require('./resource');
|
16 | let hook = { name: "generateVersion", execute: null };
|
17 | hook.execute = (env) => __awaiter(this, void 0, void 0, function* () {
|
18 | let result = yield ast.findDecorator(path.join(env.root, "src/Main.ts"));
|
19 | result = result.filter(item => item.name == "RES.mapConfig");
|
20 | yield new GenerateVersion().execute(env, result);
|
21 | });
|
22 | Object.defineProperty(exports, "__esModule", { value: true });
|
23 | exports.default = hook;
|
24 | class GenerateVersion {
|
25 | execute(env, decorators) {
|
26 | return __awaiter(this, void 0, Promise, function* () {
|
27 | let sourceRoot = env.root;
|
28 | let releasePath = env.releaseFolder;
|
29 | console.log(`${process.platform}_${process.arch}`);
|
30 | console.log(sourceRoot, releasePath, decorators);
|
31 | if (!decorators || !decorators.length) {
|
32 | throw new Error("no RES.config found");
|
33 | }
|
34 | let resourceFileNames = decorators.map(item => item.paramters[0]);
|
35 | let firstResourceFileName = resourceFileNames[0];
|
36 | let resourceRoots = ["resource"];
|
37 | let firstResourceRoot = resourceRoots[0];
|
38 | let mapper = resourceRoot => path.join(sourceRoot, resourceRoot);
|
39 | let list = yield util.walk(resourceRoots.map(mapper), (p) => {
|
40 | if (path.basename(p).charAt(0) == ".") {
|
41 | return false;
|
42 | }
|
43 | return true;
|
44 | });
|
45 | var resPath = path.join(sourceRoot, firstResourceFileName);
|
46 |
|
47 | try {
|
48 | var resObj = yield fs.readJSONAsync(resPath);
|
49 | yield resource.execute(resPath, env, firstResourceRoot);
|
50 | }
|
51 | catch (e) {
|
52 | console.log(e);
|
53 | }
|
54 | process.exit();
|
55 | var resourceOutputModeList = ["default"];
|
56 | var length = list.length;
|
57 | let resourceMapper = (element) => __awaiter(this, void 0, void 0, function* () {
|
58 | for (let filePath of list) {
|
59 | var sourceFilePath = path.relative(sourceRoot, filePath);
|
60 |
|
61 | let isResourceFile = false;
|
62 | let isFirstResourceFile = false;
|
63 | let index = resourceFileNames.indexOf(sourceFilePath);
|
64 | if (index == 0) {
|
65 | isFirstResourceFile = true;
|
66 | isResourceFile = true;
|
67 | }
|
68 | else if (index > 0) {
|
69 | isResourceFile = true;
|
70 | }
|
71 | if (isResourceFile) {
|
72 | if (!isFirstResourceFile) {
|
73 |
|
74 |
|
75 | fs.copySync(filePath, path.join(releasePath, sourceFilePath));
|
76 | }
|
77 | continue;
|
78 | }
|
79 | ;
|
80 | var resRoot = element == "default" ? "resource" : "resource-" + element;
|
81 | var buffer = yield fs.readFileAsync(filePath);
|
82 | let sourceStream = fs.createReadStream(filePath);
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | let transform = new GenerateReleaseFileTransform(resObj, sourceFilePath, resRoot, releasePath);
|
95 |
|
96 | transform.on("finish", () => console.log("onFinish", this['sourceFilePath']));
|
97 | transform.on("handled", function () {
|
98 | let sourceFilePath = this['sourceFilePath'];
|
99 | console.log("onHandled", sourceFilePath);
|
100 |
|
101 |
|
102 |
|
103 |
|
104 | var item = getConfig(resObj, sourceFilePath, resRoot);
|
105 | let outputFileName = item.url;
|
106 | let outputFilePath = path.join(releasePath, outputFileName);
|
107 |
|
108 | try {
|
109 | if (fs.existsSync(path.dirname(outputFilePath)) == false) {
|
110 | fs.mkdirSync(path.dirname(outputFilePath));
|
111 | }
|
112 | }
|
113 | catch (e) {
|
114 | console.log(e);
|
115 | }
|
116 | console.log("write 222: " + outputFileName);
|
117 |
|
118 | fs.writeFile(outputFilePath, item.data);
|
119 | });
|
120 | let stream = sourceStream.pipe(transform);
|
121 | }
|
122 | });
|
123 | yield fs.mkdirpAsync(releasePath);
|
124 | yield fs.mkdirpAsync(path.join(releasePath, "resource"));
|
125 | yield Promise.all(resourceOutputModeList.map(resourceMapper));
|
126 | yield fs.writeFileAsync(path.join(releasePath, firstResourceFileName), JSON.stringify(resObj));
|
127 | });
|
128 | }
|
129 | }
|
130 | exports.GenerateVersion = GenerateVersion;
|
131 | function onFinish(invoker) {
|
132 | return new Promise((resolve, reject) => {
|
133 | invoker.on("finish", resolve);
|
134 | invoker.on("error", reject);
|
135 | });
|
136 | }
|
137 | function getConfig(resConfig, sourceFilePath, resRoot) {
|
138 | let resourcesList = resConfig.resources;
|
139 | for (let item of resourcesList) {
|
140 | if (item.virtualUrl) {
|
141 | if (item.virtualUrl == sourceFilePath) {
|
142 | return item;
|
143 | }
|
144 | }
|
145 | if (item.url == sourceFilePath) {
|
146 | return item;
|
147 | }
|
148 | }
|
149 | let name = "$egret$" + GenerateReleaseFileTransform.appendDataToResIndex++;
|
150 | let url = sourceFilePath.replace(resRoot + "/", "");
|
151 | let item = { name: name, url: url };
|
152 | resourcesList.push(item);
|
153 | return item;
|
154 | }
|
155 | class GenerateReleaseFileTransform extends stream.Transform {
|
156 | constructor(resConfig, sourceFilePath, resRoot, releaseRoot) {
|
157 | super();
|
158 | this.resConfig = resConfig;
|
159 | this.sourceFilePath = sourceFilePath;
|
160 | this.resRoot = resRoot;
|
161 | this.releaseRoot = releaseRoot;
|
162 | this.__chunks = [];
|
163 | this.__size = 0;
|
164 | this.on("data", this.onData.bind(this));
|
165 | this.on("end", this.onEnd.bind(this));
|
166 | }
|
167 | onData(data) {
|
168 |
|
169 | }
|
170 | onEnd() {
|
171 | console.log("onEnd", this.sourceFilePath);
|
172 |
|
173 | var data = null;
|
174 | switch (this.__chunks.length) {
|
175 | case 0:
|
176 | data = new Buffer(0);
|
177 | break;
|
178 | case 1:
|
179 | data = this.__chunks[0];
|
180 | break;
|
181 | default:
|
182 | data = new Buffer(this.__size);
|
183 | for (var i = 0, pos = 0, l = this.__chunks.length; i < l; i++) {
|
184 | var chunk = this.__chunks[i];
|
185 | chunk.copy(data, pos);
|
186 | pos += chunk.length;
|
187 | }
|
188 | break;
|
189 | }
|
190 |
|
191 | this.handleData(data);
|
192 | }
|
193 | _transform(chunk, encoding, callback) {
|
194 | console.log("onTransform");
|
195 |
|
196 |
|
197 |
|
198 | this.__chunks.push(chunk);
|
199 | this.__size += chunk.length;
|
200 | this.push(chunk);
|
201 | callback();
|
202 | }
|
203 | _flush(callback) {
|
204 | callback();
|
205 | }
|
206 | handleData(chunk) {
|
207 | console.log(chunk);
|
208 | let resObj = this.resConfig;
|
209 | let found = false;
|
210 | let crc32 = require("../../temp/crc32")(chunk);
|
211 | let size = chunk.length;
|
212 | let resourcesList = resObj.resources;
|
213 | let outputFileBasename = crc32 + "_" + size + path.extname(this.sourceFilePath);
|
214 | let url = path.join(this.resRoot, crc32.substring(0, 2), outputFileBasename);
|
215 | let item = getConfig(this.resConfig, this.sourceFilePath, this.resRoot);
|
216 | item.size = size;
|
217 | item.url = url;
|
218 | item.crc32 = crc32;
|
219 | item.virtualUrl = this.sourceFilePath;
|
220 | item.data = chunk;
|
221 | console.log('transfrom complete : ' + url);
|
222 | this.emit("handled");
|
223 | }
|
224 | }
|
225 | GenerateReleaseFileTransform.appendDataToResIndex = 0;
|
226 | class Publish extends fs.WriteStream {
|
227 | constructor(sourceFilePath) {
|
228 | super();
|
229 | this.sourceFilePath = sourceFilePath;
|
230 | }
|
231 | }
|
232 |
|
\ | No newline at end of file |