commonroutesServer/www/platforms/android/cordova/lib/AndroidProject.js
www/platforms/android/cordova/lib/AndroidProject.jsRaw
1/**
2 Licensed to the Apache Software Foundation (ASF) under one
3 or more contributor license agreements. See the NOTICE file
4 distributed with this work for additional information
5 regarding copyright ownership. The ASF licenses this file
6 to you under the Apache License, Version 2.0 (the
7 "License"); you may not use this file except in compliance
8 with the License. You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing,
13 software distributed under the License is distributed on an
14 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 KIND, either express or implied. See the License for the
16 specific language governing permissions and limitations
17 under the License.
18*/
19
20var fs = require('fs');
21var path = require('path');
22var properties_parser = require('properties-parser');
23var AndroidManifest = require('./AndroidManifest');
24var pluginHandlers = require('./pluginHandlers');
25
26var projectFileCache = {};
27
28function addToPropertyList(projectProperties, key, value) {
29 var i = 1;
30 while (projectProperties.get(key + '.' + i))
31 i++;
32
33 projectProperties.set(key + '.' + i, value);
34 projectProperties.dirty = true;
35}
36
37function removeFromPropertyList(projectProperties, key, value) {
38 var i = 1;
39 var currentValue;
40 while ((currentValue = projectProperties.get(key + '.' + i))) {
41 if (currentValue === value) {
42 while ((currentValue = projectProperties.get(key + '.' + (i + 1)))) {
43 projectProperties.set(key + '.' + i, currentValue);
44 i++;
45 }
46 projectProperties.set(key + '.' + i);
47 break;
48 }
49 i++;
50 }
51 projectProperties.dirty = true;
52}
53
54function getRelativeLibraryPath (parentDir, subDir) {
55 var libraryPath = path.relative(parentDir, subDir);
56 return (path.sep == '\\') ? libraryPath.replace(/\\/g, '/') : libraryPath;
57}
58
59function AndroidProject(projectDir) {
60 this._propertiesEditors = {};
61 this._subProjectDirs = {};
62 this._dirty = false;
63 this.projectDir = projectDir;
64 this.platformWww = path.join(this.projectDir, 'platform_www');
65 this.www = path.join(this.projectDir, 'assets/www');
66}
67
68AndroidProject.getProjectFile = function (projectDir) {
69 if (!projectFileCache[projectDir]) {
70 projectFileCache[projectDir] = new AndroidProject(projectDir);
71 }
72
73 return projectFileCache[projectDir];
74};
75
76AndroidProject.purgeCache = function (projectDir) {
77 if (projectDir) {
78 delete projectFileCache[projectDir];
79 } else {
80 projectFileCache = {};
81 }
82};
83
84/**
85 * Reads the package name out of the Android Manifest file
86 *
87 * @param {String} projectDir The absolute path to the directory containing the project
88 *
89 * @return {String} The name of the package
90 */
91AndroidProject.prototype.getPackageName = function() {
92 return new AndroidManifest(path.join(this.projectDir, 'AndroidManifest.xml')).getPackageId();
93};
94
95AndroidProject.prototype.getCustomSubprojectRelativeDir = function(plugin_id, src) {
96 // All custom subprojects are prefixed with the last portion of the package id.
97 // This is to avoid collisions when opening multiple projects in Eclipse that have subprojects with the same name.
98 var packageName = this.getPackageName();
99 var lastDotIndex = packageName.lastIndexOf('.');
100 var prefix = packageName.substring(lastDotIndex + 1);
101 var subRelativeDir = path.join(plugin_id, prefix + '-' + path.basename(src));
102 return subRelativeDir;
103};
104
105AndroidProject.prototype.addSubProject = function(parentDir, subDir) {
106 var parentProjectFile = path.resolve(parentDir, 'project.properties');
107 var subProjectFile = path.resolve(subDir, 'project.properties');
108 var parentProperties = this._getPropertiesFile(parentProjectFile);
109 // TODO: Setting the target needs to happen only for pre-3.7.0 projects
110 if (fs.existsSync(subProjectFile)) {
111 var subProperties = this._getPropertiesFile(subProjectFile);
112 subProperties.set('target', parentProperties.get('target'));
113 subProperties.dirty = true;
114 this._subProjectDirs[subDir] = true;
115 }
116 addToPropertyList(parentProperties, 'android.library.reference', getRelativeLibraryPath(parentDir, subDir));
117
118 this._dirty = true;
119};
120
121AndroidProject.prototype.removeSubProject = function(parentDir, subDir) {
122 var parentProjectFile = path.resolve(parentDir, 'project.properties');
123 var parentProperties = this._getPropertiesFile(parentProjectFile);
124 removeFromPropertyList(parentProperties, 'android.library.reference', getRelativeLibraryPath(parentDir, subDir));
125 delete this._subProjectDirs[subDir];
126 this._dirty = true;
127};
128
129AndroidProject.prototype.addGradleReference = function(parentDir, subDir) {
130 var parentProjectFile = path.resolve(parentDir, 'project.properties');
131 var parentProperties = this._getPropertiesFile(parentProjectFile);
132 addToPropertyList(parentProperties, 'cordova.gradle.include', getRelativeLibraryPath(parentDir, subDir));
133 this._dirty = true;
134};
135
136AndroidProject.prototype.removeGradleReference = function(parentDir, subDir) {
137 var parentProjectFile = path.resolve(parentDir, 'project.properties');
138 var parentProperties = this._getPropertiesFile(parentProjectFile);
139 removeFromPropertyList(parentProperties, 'cordova.gradle.include', getRelativeLibraryPath(parentDir, subDir));
140 this._dirty = true;
141};
142
143AndroidProject.prototype.addSystemLibrary = function(parentDir, value) {
144 var parentProjectFile = path.resolve(parentDir, 'project.properties');
145 var parentProperties = this._getPropertiesFile(parentProjectFile);
146 addToPropertyList(parentProperties, 'cordova.system.library', value);
147 this._dirty = true;
148};
149
150AndroidProject.prototype.removeSystemLibrary = function(parentDir, value) {
151 var parentProjectFile = path.resolve(parentDir, 'project.properties');
152 var parentProperties = this._getPropertiesFile(parentProjectFile);
153 removeFromPropertyList(parentProperties, 'cordova.system.library', value);
154 this._dirty = true;
155};
156
157AndroidProject.prototype.write = function() {
158 if (!this._dirty) {
159 return;
160 }
161 this._dirty = false;
162
163 for (var filename in this._propertiesEditors) {
164 var editor = this._propertiesEditors[filename];
165 if (editor.dirty) {
166 fs.writeFileSync(filename, editor.toString());
167 editor.dirty = false;
168 }
169 }
170};
171
172AndroidProject.prototype._getPropertiesFile = function (filename) {
173 if (!this._propertiesEditors[filename]) {
174 if (fs.existsSync(filename)) {
175 this._propertiesEditors[filename] = properties_parser.createEditor(filename);
176 } else {
177 this._propertiesEditors[filename] = properties_parser.createEditor();
178 }
179 }
180
181 return this._propertiesEditors[filename];
182};
183
184AndroidProject.prototype.getInstaller = function (type) {
185 return pluginHandlers.getInstaller(type);
186};
187
188AndroidProject.prototype.getUninstaller = function (type) {
189 return pluginHandlers.getUninstaller(type);
190};
191
192
193module.exports = AndroidProject;