1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | 'use strict';
|
12 |
|
13 | import type { Asset } from './parser';
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | function getAssetPathInDrawableFolder(asset : Asset): Array<string> {
|
19 | const paths : Array<string> = [];
|
20 | asset.scales.forEach(scale => {
|
21 | const drawableFolder = getAndroidDrawableFolderName(asset, scale);
|
22 | const fileName = getAndroidResourceIdentifier(asset);
|
23 | paths.push(drawableFolder + '/' + fileName + '.' + asset.type);
|
24 | });
|
25 | return paths;
|
26 | }
|
27 |
|
28 | function getAndroidAssetSuffix(scale : number) {
|
29 | switch (scale) {
|
30 | case 0.75: return 'ldpi';
|
31 | case 1: return 'mdpi';
|
32 | case 1.5: return 'hdpi';
|
33 | case 2: return 'xhdpi';
|
34 | case 3: return 'xxhdpi';
|
35 | case 4: return 'xxxhdpi';
|
36 | }
|
37 | }
|
38 |
|
39 | function getAndroidDrawableFolderName(asset : Asset, scale : number) {
|
40 | const suffix = getAndroidAssetSuffix(scale);
|
41 | if (!suffix) {
|
42 | throw new Error(
|
43 | 'Don\'t know which android drawable suffix to use for asset: ' +
|
44 | JSON.stringify(asset)
|
45 | );
|
46 | }
|
47 | return 'drawable-' + suffix;
|
48 | }
|
49 |
|
50 | function getAndroidResourceIdentifier(asset : Asset) {
|
51 | const folderPath = getBasePath(asset);
|
52 | return (folderPath + '/' + asset.name)
|
53 | .toLowerCase()
|
54 | .replace(/\//g, '_')
|
55 | .replace(/([^a-z0-9_])/g, '')
|
56 | .replace(/^assets_/, '');
|
57 | }
|
58 |
|
59 | function getBasePath(asset : Asset) {
|
60 | let basePath = asset.httpServerLocation;
|
61 | if (basePath[0] === '/') {
|
62 | basePath = basePath.substr(1);
|
63 | }
|
64 | return basePath;
|
65 | }
|
66 |
|
67 | module.exports = {
|
68 | getAssetPathInDrawableFolder
|
69 | }; |
\ | No newline at end of file |