-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathproject-data-service.ts
304 lines (245 loc) · 11.2 KB
/
project-data-service.ts
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import * as path from "path";
import { ProjectData } from "../project-data";
import { exported } from "../common/decorators";
import { NATIVESCRIPT_PROPS_INTERNAL_DELIMITER, AssetConstants, SRC_DIR, RESOURCES_DIR, MAIN_DIR, CLI_RESOURCES_DIR_NAME, ProjectTypes } from "../constants";
interface IProjectFileData {
projectData: any;
projectFilePath: string;
}
export class ProjectDataService implements IProjectDataService {
private static DEPENDENCIES_KEY_NAME = "dependencies";
constructor(private $fs: IFileSystem,
private $staticConfig: IStaticConfig,
private $logger: ILogger,
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
private $androidResourcesMigrationService: IAndroidResourcesMigrationService,
private $injector: IInjector) {
}
public getNSValue(projectDir: string, propertyName: string): any {
return this.getValue(projectDir, this.getNativeScriptPropertyName(propertyName));
}
public setNSValue(projectDir: string, key: string, value: any): void {
this.setValue(projectDir, this.getNativeScriptPropertyName(key), value);
}
public removeNSProperty(projectDir: string, propertyName: string): void {
this.removeProperty(projectDir, this.getNativeScriptPropertyName(propertyName));
}
public removeDependency(projectDir: string, dependencyName: string): void {
const projectFileInfo = this.getProjectFileData(projectDir);
delete projectFileInfo.projectData[ProjectDataService.DEPENDENCIES_KEY_NAME][dependencyName];
this.$fs.writeJson(projectFileInfo.projectFilePath, projectFileInfo.projectData);
}
// TODO: Add tests
// TODO: Remove $projectData and replace it with $projectDataService.getProjectData
@exported("projectDataService")
public getProjectData(projectDir: string): IProjectData {
const projectDataInstance = this.$injector.resolve<IProjectData>(ProjectData);
projectDataInstance.initializeProjectData(projectDir);
return projectDataInstance;
}
@exported("projectDataService")
public getProjectDataFromContent(packageJsonContent: string, nsconfigContent: string, projectDir?: string): IProjectData {
const projectDataInstance = this.$injector.resolve<IProjectData>(ProjectData);
projectDataInstance.initializeProjectDataFromContent(packageJsonContent, nsconfigContent, projectDir);
return projectDataInstance;
}
@exported("projectDataService")
public async getAssetsStructure(opts: IProjectDir): Promise<IAssetsStructure> {
const iOSAssetStructure = await this.getIOSAssetsStructure(opts);
const androidAssetStructure = await this.getAndroidAssetsStructure(opts);
this.$logger.trace("iOS Assets structure:", JSON.stringify(iOSAssetStructure, null, 2));
this.$logger.trace("Android Assets structure:", JSON.stringify(androidAssetStructure, null, 2));
return {
ios: iOSAssetStructure,
android: androidAssetStructure
};
}
@exported("projectDataService")
public async getIOSAssetsStructure(opts: IProjectDir): Promise<IAssetGroup> {
const projectDir = opts.projectDir;
const projectData = this.getProjectData(projectDir);
const basePath = path.join(projectData.appResourcesDirectoryPath, this.$devicePlatformsConstants.iOS, AssetConstants.iOSAssetsDirName);
const pathToIcons = path.join(basePath, AssetConstants.iOSIconsDirName);
const icons = await this.getIOSAssetSubGroup(pathToIcons);
const pathToSplashBackgrounds = path.join(basePath, AssetConstants.iOSSplashBackgroundsDirName);
const splashBackgrounds = await this.getIOSAssetSubGroup(pathToSplashBackgrounds);
const pathToSplashCenterImages = path.join(basePath, AssetConstants.iOSSplashCenterImagesDirName);
const splashCenterImages = await this.getIOSAssetSubGroup(pathToSplashCenterImages);
const pathToSplashImages = path.join(basePath, AssetConstants.iOSSplashImagesDirName);
const splashImages = await this.getIOSAssetSubGroup(pathToSplashImages);
return {
icons,
splashBackgrounds,
splashCenterImages,
splashImages
};
}
@exported("projectDataService")
public async getAndroidAssetsStructure(opts: IProjectDir): Promise<IAssetGroup> {
// TODO: Use image-size package to get the width and height of an image.
// TODO: Parse the splash_screen.xml in nodpi directory and get from it the names of the background and center image.
// TODO: Parse the AndroidManifest.xml to get the name of the icon.
// This way we'll not use the image-definitions.json and the method will return the real android structure.
const projectDir = opts.projectDir;
const projectData = this.getProjectData(projectDir);
const pathToAndroidDir = path.join(projectData.appResourcesDirectoryPath, this.$devicePlatformsConstants.Android);
const hasMigrated = this.$androidResourcesMigrationService.hasMigrated(projectData.appResourcesDirectoryPath);
const basePath = hasMigrated ? path.join(pathToAndroidDir, SRC_DIR, MAIN_DIR, RESOURCES_DIR) : pathToAndroidDir;
const currentStructure = this.$fs.enumerateFilesInDirectorySync(basePath);
const content = this.getImageDefinitions().android;
return {
icons: this.getAndroidAssetSubGroup(content.icons, currentStructure),
splashBackgrounds: this.getAndroidAssetSubGroup(content.splashBackgrounds, currentStructure),
splashCenterImages: this.getAndroidAssetSubGroup(content.splashCenterImages, currentStructure),
splashImages: null
};
}
public getAppExecutableFiles(projectDir: string): string[] {
const projectData = this.getProjectData(projectDir);
let supportedFileExtension = ".js";
if (projectData.projectType === ProjectTypes.NgFlavorName || projectData.projectType === ProjectTypes.TsFlavorName) {
supportedFileExtension = ".ts";
}
const files = this.$fs.enumerateFilesInDirectorySync(
projectData.appDirectoryPath,
(filePath, fstat) => {
if (filePath.indexOf(projectData.appResourcesDirectoryPath) !== -1) {
return false;
}
if (fstat.isDirectory()) {
return true;
}
return path.extname(filePath) === supportedFileExtension;
}
);
return files;
}
private getImageDefinitions(): IImageDefinitionsStructure {
const pathToImageDefinitions = path.join(__dirname, "..", "..", CLI_RESOURCES_DIR_NAME, AssetConstants.assets, AssetConstants.imageDefinitionsFileName);
const imageDefinitions = this.$fs.readJson(pathToImageDefinitions);
return imageDefinitions;
}
private async getIOSAssetSubGroup(dirPath: string): Promise<IAssetSubGroup> {
const pathToContentJson = path.join(dirPath, AssetConstants.iOSResourcesFileName);
const content = this.$fs.exists(pathToContentJson) && <IAssetSubGroup>this.$fs.readJson(pathToContentJson) || { images: [] };
const imageDefinitions = this.getImageDefinitions().ios;
_.each(content && content.images, image => {
// In some cases the image may not be available, it will just be described.
// When this happens, the filename will be empty.
// So we'll keep the path empty as well.
if (image.filename) {
image.path = path.join(dirPath, image.filename);
}
// Find the image size based on the hardcoded values in the image-definitions.json
_.each(imageDefinitions, (assetSubGroup: IAssetItem[]) => {
const assetItem = _.find(assetSubGroup, assetElement =>
assetElement.filename === image.filename && path.basename(assetElement.directory) === path.basename(dirPath)
);
if (image.size) {
// size is basically <width>x<height>
const [width, height] = image.size.toString().split(AssetConstants.sizeDelimiter);
if (width && height) {
image.width = +width;
image.height = +height;
}
}
if (assetItem) {
if (!image.width || !image.height) {
image.width = assetItem.width;
image.height = assetItem.height;
image.size = image.size || `${assetItem.width}${AssetConstants.sizeDelimiter}${assetItem.height}`;
}
image.resizeOperation = image.resizeOperation || assetItem.resizeOperation;
image.overlayImageScale = image.overlayImageScale || assetItem.overlayImageScale;
image.scale = image.scale || assetItem.scale;
// break each
return false;
}
});
});
return content;
}
private getAndroidAssetSubGroup(assetItems: IAssetItem[], realPaths: string[]): IAssetSubGroup {
const assetSubGroup: IAssetSubGroup = {
images: <any>[]
};
const normalizedPaths = _.map(realPaths, p => path.normalize(p));
_.each(assetItems, assetItem => {
_.each(normalizedPaths, currentNormalizedPath => {
const imagePath = path.join(assetItem.directory, assetItem.filename);
if (currentNormalizedPath.indexOf(path.normalize(imagePath)) !== -1) {
assetItem.path = currentNormalizedPath;
assetItem.size = `${assetItem.width}${AssetConstants.sizeDelimiter}${assetItem.height}`;
assetSubGroup.images.push(assetItem);
return false;
}
});
});
return assetSubGroup;
}
private getValue(projectDir: string, propertyName: string): any {
const projectData = this.getProjectFileData(projectDir).projectData;
if (projectData) {
try {
return this.getPropertyValueFromJson(projectData, propertyName);
} catch (err) {
this.$logger.trace(`Error while trying to get property ${propertyName} from ${projectDir}. Error is:`, err);
}
}
return null;
}
private getNativeScriptPropertyName(propertyName: string) {
return `${this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE}${NATIVESCRIPT_PROPS_INTERNAL_DELIMITER}${propertyName}`;
}
private getPropertyValueFromJson(jsonData: any, dottedPropertyName: string): any {
const props = dottedPropertyName.split(NATIVESCRIPT_PROPS_INTERNAL_DELIMITER);
let result = jsonData[props.shift()];
for (const prop of props) {
result = result[prop];
}
return result;
}
private setValue(projectDir: string, key: string, value: any): void {
const projectFileInfo = this.getProjectFileData(projectDir);
const props = key.split(NATIVESCRIPT_PROPS_INTERNAL_DELIMITER);
const data: any = projectFileInfo.projectData;
let currentData = data;
_.each(props, (prop, index: number) => {
if (index === (props.length - 1)) {
currentData[prop] = value;
} else {
currentData[prop] = currentData[prop] || Object.create(null);
}
currentData = currentData[prop];
});
this.$fs.writeJson(projectFileInfo.projectFilePath, data);
}
private removeProperty(projectDir: string, propertyName: string): void {
const projectFileInfo = this.getProjectFileData(projectDir);
const data: any = projectFileInfo.projectData;
let currentData = data;
const props = propertyName.split(NATIVESCRIPT_PROPS_INTERNAL_DELIMITER);
const propertyToDelete = props.splice(props.length - 1, 1)[0];
_.each(props, (prop) => {
currentData = currentData[prop];
});
delete currentData[propertyToDelete];
this.$fs.writeJson(projectFileInfo.projectFilePath, data);
}
private getProjectFileData(projectDir: string): IProjectFileData {
const projectFilePath = path.join(projectDir, this.$staticConfig.PROJECT_FILE_NAME);
const projectFileContent = this.$fs.readText(projectFilePath);
const projectData = projectFileContent ? JSON.parse(projectFileContent) : Object.create(null);
return {
projectData,
projectFilePath
};
}
@exported("projectDataService")
public getNsConfigDefaultContent(data?: Object): string {
const config: INsConfig = {};
Object.assign(config, data);
return JSON.stringify(config);
}
}
$injector.register("projectDataService", ProjectDataService);