Code Open
[sdk/tools/ide-enventor-eplugin.git] / org.tizen.nativecore.enventor / src / org / tizen / nativecore / enventor / executer / EDCEditorExecuter.java
1 /*
2  * Enventor
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:
7  * Hyeongseok Heo <hyeongseok.heo@samsung.com>
8  * Gun Kim <gune.kim@samseung.com>
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  * Contributors:
23  * - S-Core Co., Ltd
24  *
25  */
26 package org.tizen.nativecore.enventor.executer;
27
28 import java.io.File;
29
30 import org.eclipse.cdt.managedbuilder.core.IConfiguration;
31 import org.eclipse.core.resources.IFile;
32 import org.eclipse.core.resources.IProject;
33 import org.eclipse.core.resources.ResourcesPlugin;
34 import org.eclipse.core.runtime.IPath;
35 import org.eclipse.core.runtime.Path;
36 import org.eclipse.osgi.util.NLS;
37 import org.tizen.common.core.application.InstallPathConfig;
38 import org.tizen.common.core.application.TizenProjectDescription;
39 import org.tizen.common.util.FileUtil;
40 import org.tizen.common.util.HostUtil;
41 import org.tizen.nativecommon.ProjectUtil;
42 import org.tizen.nativecore.build.NativeCoreConfigurationManager;
43 import org.tizen.nativecore.enventor.Messages;
44
45 public class EDCEditorExecuter {
46     // The format is "environment executable <edc file>".
47     private static final String cmdFormat = "%s %s %s %s";
48     private static final String templateWizardArg = "--to";
49
50     public static void executeTool(final IPath path, final boolean withTemplateWizard) {
51         new Thread(new Runnable() {
52             @Override
53             public void run() {
54                 HostUtil.returnExecute(getCommand(path, withTemplateWizard), null, false);
55                 /*
56                  * String result = HostUtil.returnExecute(getCommand(path) +
57                  * TizenPlatformConstants.CMD_SUFFIX, null, true);
58                  * 
59                  * if ( !result.endsWith(TizenPlatformConstants.CMD_SUCCESS) ) {
60                  * SWTUtil.asyncExec(new Runnable() {
61                  * 
62                  * @Override public void run() {
63                  * DialogUtil.openErrorDialog(Messages.CANNOT_EXECUTE_TOOL); } }); logger.error(
64                  * "Enventor's result: " + result); }
65                  */
66             }
67         }).start();
68
69     }
70
71     public static String checkTool() {
72         String msg = "";
73
74         String tool = getFullpathOfTool();
75         if (!isInstallation(tool)) {
76             return NLS.bind(Messages.CANNOT_FIND_TOOL, tool);
77         }
78
79         tool = getFullpathOfEflTool();
80         if (!isInstallation(tool)) {
81             return NLS.bind(Messages.CANNOT_FIND_TOOL, tool);
82         }
83
84         return msg;
85     }
86
87     private static boolean isInstallation(String tool) {
88         if (new File(tool).exists()) {
89             return true;
90         } else {
91             return false;
92         }
93     }
94
95     private static String getCommand(IPath path, boolean withTemplateWizard) {
96         final String fullpathOfTool = getFullpathOfTool();
97         final String env = getEnv();
98         final IPath edj_path = path.removeFileExtension().addFileExtension("edj");
99         String redirectStream = null;
100
101         if (System.getProperty("os.name").startsWith("Windows")) {
102             redirectStream = " 1> NUL 2> NUL";
103         } else {
104             redirectStream = " 1> /dev/null 2> /dev/null";
105         }
106
107         if (withTemplateWizard) {
108             return String.format(cmdFormat, env, fullpathOfTool, path.toOSString(), edj_path.toOSString()) + " " + getOptions(path) + " " + templateWizardArg
109                     + redirectStream;
110         } else {
111             return String.format(cmdFormat, env, fullpathOfTool, path.toOSString(), edj_path.toOSString()) + " " + getOptions(path) + redirectStream;
112         }
113     }
114
115     private static String[] trimOption(String[] option) {
116         String[] trimOptions = new String[option.length];
117
118         for (int i = 0; i < option.length; i++) {
119             trimOptions[i] = option[i].trim();
120         }
121
122         return trimOptions;
123     }
124
125     private static String getOptions(IPath path) {
126         String option = "";
127         IFile projectFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
128         if (projectFile == null) return null;
129
130         IProject project = projectFile.getProject();
131
132         // Set workspace path option with "-w <workspace path>".
133         String workspaceDir = project.getLocation().toOSString();
134         if (!workspaceDir.isEmpty())
135             option += "-w " + workspaceDir + " ";
136
137         IConfiguration config = ProjectUtil.getDefaultConfiguration(project);
138
139         IFile file = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
140         IPath projectRelativePath = null;
141         if (file != null) projectRelativePath = file.getProjectRelativePath();
142
143         if ((config != null) && (projectRelativePath != null)) {
144             String[] fontDirs = trimOption(NativeCoreConfigurationManager.getEdcFontDirs(config, projectRelativePath));
145             String[] imageDirs = trimOption(NativeCoreConfigurationManager.getEdcImageDirs(config, projectRelativePath));
146             String[] soundDirs = trimOption(NativeCoreConfigurationManager.getEdcSoundDirs(config, projectRelativePath));
147
148             for (int i = 0; i < fontDirs.length; i++) {
149                 if (!fontDirs[i].isEmpty()) {
150                     if (System.getProperty("os.name").startsWith("Windows")) {
151                         if ((fontDirs[i].charAt(0) == '\\') ||
152                                 ((fontDirs[i].charAt(0) == '"') && (fontDirs[i].charAt(1) == '\\')) ||
153                                 ((fontDirs[i].length() >= 3) && (fontDirs[i].charAt(1) == ':') && (fontDirs[i].charAt(2) == '\\')) ||
154                                 ((fontDirs[i].length() >= 4) && (fontDirs[i].charAt(0) == '"') && (fontDirs[i].charAt(2) == ':')
155                                         && (fontDirs[i].charAt(3) == '\\')))
156                             option += "--fd " + fontDirs[i] + " ";
157                         else
158                             option += "--fd " + path.removeLastSegments(1) + "\\" + fontDirs[i] + " ";
159                     } else {
160                         if ((fontDirs[i].charAt(0) == '/') ||
161                                 ((fontDirs[i].charAt(0) == '"') && (fontDirs[i].charAt(1) == '/')))
162                             option += "--fd " + fontDirs[i] + " ";
163                         else
164                             option += "--fd " + path.removeLastSegments(1) + "/" + fontDirs[i] + " ";
165                     }
166                 }
167             }
168             for (int i = 0; i < imageDirs.length; i++) {
169                 if (!imageDirs[i].isEmpty()) {
170                     if (System.getProperty("os.name").startsWith("Windows")) {
171                         if ((imageDirs[i].charAt(0) == '\\') ||
172                                 ((imageDirs[i].charAt(0) == '"') && (imageDirs[i].charAt(1) == '\\')) ||
173                                 ((imageDirs[i].length() >= 3) && (imageDirs[i].charAt(1) == ':') && (imageDirs[i].charAt(2) == '\\')) ||
174                                 ((imageDirs[i].length() >= 4) && (imageDirs[i].charAt(0) == '"') && (imageDirs[i].charAt(2) == ':')
175                                         && (imageDirs[i].charAt(3) == '\\')))
176                             option += "--id " + imageDirs[i] + " ";
177                         else
178                             option += "--id " + path.removeLastSegments(1) + "\\" + imageDirs[i] + " ";
179                     } else {
180                         if ((imageDirs[i].charAt(0) == '/') ||
181                                 ((imageDirs[i].charAt(0) == '"') && (imageDirs[i].charAt(1) == '/')))
182                             option += "--id " + imageDirs[i] + " ";
183                         else
184                             option += "--id " + path.removeLastSegments(1) + "/" + imageDirs[i] + " ";
185                     }
186                 }
187             }
188             for (int i = 0; i < soundDirs.length; i++) {
189                 if (!soundDirs[i].isEmpty()) {
190                     if (System.getProperty("os.name").startsWith("Windows")) {
191                         if ((soundDirs[i].charAt(0) == '\\') ||
192                                 ((soundDirs[i].charAt(0) == '"') && (soundDirs[i].charAt(1) == '\\')) ||
193                                 ((soundDirs[i].length() >= 3) && (soundDirs[i].charAt(1) == ':') && (soundDirs[i].charAt(2) == '\\')) ||
194                                 ((soundDirs[i].length() >= 4) && (soundDirs[i].charAt(0) == '"') && (soundDirs[i].charAt(2) == ':')
195                                         && (soundDirs[i].charAt(3) == '\\')))
196                             option += "--sd " + soundDirs[i] + " ";
197                         else
198                             option += "--sd " + path.removeLastSegments(1) + "\\" + soundDirs[i] + " ";
199                     } else {
200                         if ((soundDirs[i].charAt(0) == '/') ||
201                                 ((soundDirs[i].charAt(0) == '"') && (soundDirs[i].charAt(1) == '/')))
202                             option += "--sd " + soundDirs[i] + " ";
203                         else
204                             option += "--sd " + path.removeLastSegments(1) + "/" + soundDirs[i] + " ";
205                     }
206                 }
207             }
208         }
209
210         // Get edje_cc path of platform efl-tools
211         TizenProjectDescription tizenDesc = org.tizen.common.util.ProjectUtil.getTizenProjectDescription(project);
212         if (tizenDesc == null) return null;
213
214         String platformToolsPath = InstallPathConfig.getPlatformToolsPath(tizenDesc.getProfileName(), tizenDesc.getVersion());
215         if (platformToolsPath == null) return null;
216
217         String edje_ccDirPath = FileUtil.appendPath(platformToolsPath, "efl-tool/efl-tools/bin");
218         String edje_ccPath = "";
219         if (System.getProperty("os.name").startsWith("Windows"))
220             edje_ccPath = FileUtil.appendPath(edje_ccDirPath, "edje_cc.exe");
221         else
222             edje_ccPath = FileUtil.appendPath(edje_ccDirPath, "edje_cc");
223
224         // Set edje_cc path option
225         if (!edje_ccPath.isEmpty())
226             option += "-e" + new Path(edje_ccPath).toOSString() + " ";
227
228         // Get autocomp eet path of platform version
229         String autocompDirPath = FileUtil.appendPath(getFullpathOfEnventor(), "share/enventor/autocomp");
230         String autocompEet = tizenDesc.getVersion() + "-autocomp.eet";
231         String autocompEetPath = FileUtil.appendPath(autocompDirPath, autocompEet);
232
233         // Set autocomp eet path option
234         if (!autocompEetPath.isEmpty())
235             option += "-a" + new Path(autocompEetPath).toOSString() + " ";
236
237         // Set Tizen version
238         if ((tizenDesc.getVersion() != null) && (!tizenDesc.getVersion().isEmpty()))
239             option += "-z" + tizenDesc.getVersion() + " ";
240
241         return option;
242     }
243
244     private static String getDirectoryOfTool() {
245         String dir = FileUtil.appendPath(InstallPathConfig.getToolsPath(), "edc-editor/bin");
246         IPath path = new Path(dir);
247         return path.toOSString();
248     }
249
250     private static String getFullpathOfTool() {
251         if (System.getProperty("os.name").startsWith("Windows"))
252             return FileUtil.appendPath(getDirectoryOfTool(), "enventor.exe");
253         else
254             return FileUtil.appendPath(getDirectoryOfTool(), "enventor");
255     }
256
257     private static String getFullpathOfEflTool() {
258         String dir = FileUtil.appendPath(InstallPathConfig.getToolsPath(), "efl-tools");
259         IPath path = new Path(dir);
260         return path.toOSString();
261     }
262
263     private static String getFullpathOfEnventor() {
264         String dir = FileUtil.appendPath(InstallPathConfig.getToolsPath(), "edc-editor");
265         IPath path = new Path(dir);
266         return path.toOSString();
267     }
268
269     private static String getEnv() {
270         String fullpathOfEflTool = getFullpathOfEflTool();
271         String fullpathOfEnventor = getFullpathOfEnventor();
272
273         if (System.getProperty("os.name").startsWith("Windows")) {
274             String envFormat = "set path=%s;%s;%s && set FONTCONFIG_PATH=%s &&";
275             String eflToolBin = FileUtil.appendPath(fullpathOfEflTool, "bin");
276             String enventorBin = FileUtil.appendPath(fullpathOfEnventor, "bin");
277             String fontconfigDir = FileUtil.appendPath(fullpathOfEflTool, "etc/fonts");
278             return String.format(envFormat, eflToolBin, enventorBin, "%path%", fontconfigDir);
279         } else if (System.getProperty("os.name").contains("OS X")) {
280             String envFormat = "PATH=%s:$PATH DYLD_LIBRARY_PATH=%s:%s:$DYLD_LIBRARY_PATH FONTCONFIG_PATH=%s";
281             String eflToolBin = FileUtil.appendPath(fullpathOfEflTool, "bin");
282             String eflToolLib = FileUtil.appendPath(fullpathOfEflTool, "lib");
283             String enventorLib = FileUtil.appendPath(fullpathOfEnventor, "lib");
284             String fontconfigDir = FileUtil.appendPath(fullpathOfEflTool, "etc/fonts");
285             return String.format(envFormat, eflToolBin, eflToolLib, enventorLib, fontconfigDir);
286         } else {
287             String envFormat = "PATH=%s:$PATH LD_LIBRARY_PATH=%s:%s:$LD_LIBRARY_PATH";
288             String eflToolBin = FileUtil.appendPath(fullpathOfEflTool, "bin");
289             String eflToolLib = FileUtil.appendPath(fullpathOfEflTool, "lib");
290             String enventorLib = FileUtil.appendPath(fullpathOfEnventor, "lib");
291             return String.format(envFormat, eflToolBin, eflToolLib, enventorLib);
292         }
293     }
294 }