RTSDK : Add project clean command
authordaeryong.park <bdragon.park@samsung.com>
Wed, 17 May 2017 06:21:46 +0000 (15:21 +0900)
committerdaeryong.park <bdragon.park@samsung.com>
Wed, 17 May 2017 06:50:44 +0000 (15:50 +0900)
- Add project clean command
- Apply popup menus for build/flash/clean command in ProjectExplorer

Change-Id: Ic692dd030c7146133d9316ad3ee96e5eebbfc4fd
Signed-off-by: daeryong.park <bdragon.park@samsung.com>
rt-ide/tizen.rt.product.plugin/plugin.xml
rt-ide/tizen.rt.product.plugin/src/org/tizen/rt/ide/CleanCommand.java [new file with mode: 0644]
rt-ide/tizen.rt.product.plugin/src/org/tizen/rt/ide/RtosCommandManager.java
rt-ide/tizen.rt.product.plugin/src/org/tizen/rt/ide/handlers/CleanHandler.java [new file with mode: 0644]
rt-ide/tizen.rt.product.plugin/src/org/tizen/rt/ide/util/ResourceUtil.java

index dbbf84a..277d265 100644 (file)
             categoryId="rtosBuild.commands.category"
             id="rtosBuild.commands.buildCommand">
       </command>
-      
       <command
-            name="Flash"
             categoryId="rtosBuild.commands.category"
-            id="rtosBuild.commands.flashCommand">
+            id="rtosBuild.commands.flashCommand"
+            name="Flash">
+      </command>
+      <category
+            id="rtosBuild.commands.clean.category"
+            name="Rtos Clean Category">
+      </category>
+      <command
+            categoryId="rtosBuild.commands.clean.category"
+            id="rtosBuild.commands.cleanCommand"
+            name="Clean">
       </command>
+
       
    </extension>
    
             </reference>
          </enabledWhen>
       </handler>
+      <handler
+            class="org.tizen.rt.ide.handlers.CleanHandler"
+            commandId="rtosBuild.commands.cleanCommand">
+         <enabledWhen>
+            <reference
+                  definitionId="org.tizen.rt.product.hasNature">
+            </reference>
+         </enabledWhen>
+      </handler>
       
       <!-- modify class-->
       <handler
             contextId="org.eclipse.ui.contexts.window"
             sequence="M1+7"
             schemeId="org.eclipse.ui.defaultAcceleratorConfiguration">
+      </key>
+      <key
+            commandId="rtosBuild.commands.cleanCommand"
+            contextId="org.eclipse.ui.contexts.window"
+            schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
+            sequence="M1+8">
       </key>      
    </extension>
    
@@ -72,7 +96,7 @@
       <menuContribution
             locationURI="menu:project?after=additions">
          <separator
-               name="rtosBuild.menus.sampleMenu">
+               name="rtosBuild.menus.sampleMenu1">
          </separator>
          <command
                commandId="rtosBuild.commands.buildCommand"
                </reference>
             </visibleWhen>
          </command>
+         <separator
+               name="rtosBuild.menus.sampleMenu2">
+         </separator>
+         <command
+               commandId="rtosBuild.commands.cleanCommand"
+               icon="icons/tools/tool_build_nor.png"
+               id="rtosBuild.menus.cleanCommand"
+               mnemonic="S">
+            <visibleWhen
+                  checkEnabled="true">
+               <reference
+                     definitionId="org.tizen.rt.product.rt_perspective">
+               </reference>
+            </visibleWhen>
+         </command>
       </menuContribution>
       
       <menuContribution
                   disabledIcon="icons/tools/tool_build_dim.png"
                   icon="icons/tools/tool_build_nor.png"
                   id="rtosBuild.toolbars.buildCommand"
-                  tooltip="build rtos project">
+                  tooltip="Build TizenRT Project">
                <visibleWhen
                      checkEnabled="true">
                   <reference
                   disabledIcon="icons/tools/tool_flash_dim.png"
                   icon="icons/tools/tool_flash_nor.png"
                   id="rtosBuild.toolbars.flsahCommand"
-                  tooltip="flash rtos project">
+                  tooltip="Flash TizenRT Project">
                <visibleWhen
                      checkEnabled="true">
                   <reference
             </command>
          </toolbar>
       </menuContribution>
+      <menuContribution
+            locationURI="popup:org.tizen.rt.product.RtProjectExplorer?before=group.build">
+         <separator
+               name="rtosClean.menus.sampleMenu1">
+         </separator>
+         <command
+               commandId="rtosBuild.commands.buildCommand">
+         </command>
+         <command
+               commandId="rtosBuild.commands.flashCommand"
+               mnemonic="S">
+         </command>
+         <separator
+               name="rtosClean.menus.sampleMenu1">
+         </separator>
+         <command
+               commandId="rtosBuild.commands.cleanCommand">
+         </command>
+      </menuContribution>
    </extension>
    <extension
          id="product"
    </extension>
    <extension
          point="org.eclipse.ui.navigator.viewer">
+      <viewer
+            helpContext="org.eclipse.ui.project_explorer_context"
+            viewerId="org.eclipse.ui.navigator.ProjectExplorer">
+         <popupMenu
+               allowsPlatformContributions="true"
+               id="org.tizen.rt.product.RtProjectExplorer">
+            <insertionPoint
+                  name="group.new">
+            </insertionPoint>
+            <insertionPoint
+                  name="group.build"
+                  separator="true">
+            </insertionPoint>
+            <insertionPoint
+                  name="group.launch"
+                  separator="true">
+            </insertionPoint>
+            <insertionPoint
+                  name="group.validate"
+                  separator="true">
+            </insertionPoint>
+            <insertionPoint
+                  name="group.edit"
+                  separator="true">
+            </insertionPoint>
+            <insertionPoint
+                  name="group.reorganize">
+            </insertionPoint>
+            <insertionPoint
+                  name="additions"
+                  separator="true">
+            </insertionPoint>
+            <insertionPoint
+                  name="group.open"
+                  separator="true">
+            </insertionPoint>
+            <insertionPoint
+                  name="group.openWith">
+            </insertionPoint>
+            <insertionPoint
+                  name="group.port"
+                  separator="true">
+            </insertionPoint>
+            <insertionPoint
+                  name="group.search"
+                  separator="true">
+            </insertionPoint>
+            <insertionPoint
+                  name="group.properties"
+                  separator="true">
+            </insertionPoint>
+         </popupMenu>
+         <options>
+            <property
+                  name="org.eclipse.ui.navigator.customizeViewDialogHelpContext"
+                  value="org.eclipse.ui.project_explorer_customization_dialog">
+            </property>
+         </options>
+      </viewer>
       <viewerActionBinding
             viewerId="org.tizen.rt.product.RtProjectExplorer">
          <includes>
diff --git a/rt-ide/tizen.rt.product.plugin/src/org/tizen/rt/ide/CleanCommand.java b/rt-ide/tizen.rt.product.plugin/src/org/tizen/rt/ide/CleanCommand.java
new file mode 100644 (file)
index 0000000..20933f8
--- /dev/null
@@ -0,0 +1,140 @@
+/*
+ * Common
+ *
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * Daeryong Park <bdragon.park@samsung.com>
+ * Hyeongseok Heo <harry.heo@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd.
+ *
+ */
+package org.tizen.rt.ide;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.tizen.rt.ide.console.ConsoleManager;
+import org.tizen.rt.ide.util.MacroUtil;
+import org.tizen.rt.ide.util.ProcUtil;
+import org.tizen.rt.ide.util.SDKUtil;
+import org.tizen.rt.ide.util.XMLUtil;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+/**
+ * @since 2017. 5. 17.
+ * @author Daeryong Park {@literal <bdragon.park@samsung.com>}
+ */
+public class CleanCommand {
+
+    private static final String FILE_NAME_BUILD_SCRIPT = "build.xml"; //$NON-NLS-1$
+
+    private List<String> resultBuildExecute = new ArrayList<String>();
+
+    private class BuildScript {
+        public String cmdStr;
+        public String curDir;
+        public boolean isShellCmd;
+        public String shell;
+
+        public BuildScript(String cmdStr, String curDir, boolean isShellCmd, String shell) {
+            this.cmdStr = cmdStr;
+            this.curDir = curDir;
+            this.isShellCmd = isShellCmd;
+            this.shell = shell;
+        }
+    }
+
+    private Map<String, BuildScript> getBuildScripts(File buildScriptFile, Map<String, String> macros) {
+        Document doc = XMLUtil.readXML(buildScriptFile);
+
+        NodeList commands = XMLUtil.getDOMNodes(doc.getDocumentElement(), "/sbi/build/command");
+        if (commands == null || commands.getLength() <= 0) {
+            return null;
+        }
+
+        Map<String, BuildScript> scripts = new HashMap<String, BuildScript>();
+
+        int len = commands.getLength();
+        for (int i = 0; i < len; i++) {
+            Node node = commands.item(i);
+            if (node instanceof Element) {
+                Element elm = (Element) node;
+
+                String name = elm.getAttribute("name");
+                String cmd = elm.getAttribute("command");
+                cmd = MacroUtil.processMacro(cmd, macros);
+                String curdir = elm.getAttribute("curdir");
+                curdir = MacroUtil.processMacro(curdir, macros);
+                String shellcmd = elm.getAttribute("shellcmd");
+                boolean isShellCmd = Boolean.valueOf(shellcmd);
+                String shell = elm.getAttribute("shell");
+
+                scripts.put(name, new BuildScript(cmd, curdir, isShellCmd, shell));
+            }
+        }
+
+        return scripts;
+    }
+
+    protected int executeClean(IProject project, ConsoleManager consoleManager, IProgressMonitor monitor) {
+        String activeProjectName = project.getLocation().toString();
+        String homePath = activeProjectName;
+
+        String sdkPath = SDKUtil.getSdkPath();
+        File buildScriptFile = new File(sdkPath, "rt-ide" + File.separator + "resources" + File.separator + "scripts"
+                + File.separator + FILE_NAME_BUILD_SCRIPT);
+        if (!buildScriptFile.exists()) {
+            buildScriptFile = new File(homePath, FILE_NAME_BUILD_SCRIPT);
+            if (!buildScriptFile.exists()) {
+                return 1;
+            }
+        }
+
+        Map<String, String> macros = new HashMap<String, String>();
+        macros.put("PROJECT_PATH", homePath);
+
+        Map<String, BuildScript> scripts = getBuildScripts(buildScriptFile, macros);
+
+        ProcessBuilder pb = ProcUtil.genProcessBuilder(null);
+
+        String buildCmdName = "clean";
+        BuildScript script = scripts.get(buildCmdName);
+        if (script == null) {
+            consoleManager.println("Cannot found clean command : " + buildCmdName); //$NON-NLS-1$
+            return 1;
+        }
+        int result = ProcUtil.executeProcess(pb, new String[] { script.cmdStr }, script.isShellCmd, script.shell,
+                script.curDir, consoleManager, resultBuildExecute);
+
+        monitor.worked(1);
+        if (result != 0) {
+            return result;
+        }
+
+        return result;
+    }
+
+}
index 8962e7c..4534ef4 100644 (file)
@@ -28,6 +28,7 @@ package org.tizen.rt.ide;
 import java.util.ArrayList;
 import java.util.List;
 
+import org.eclipse.core.resources.IProject;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.graphics.Color;
@@ -176,6 +177,17 @@ public class RtosCommandManager {
         return processResult(result, consoleManager);
     }
 
+    public static boolean runClean(IProject project, ConsoleManager consoleManager, IProgressMonitor monitor) {
+        if (consoleManager == null) {
+            logger.error("ConsoleManager is null"); //$NON-NLS-1$
+            return false;
+        }
+        CleanCommand cleanCommand = new CleanCommand();
+        int result = cleanCommand.executeClean(project, consoleManager, monitor);
+
+        return processResult(result, consoleManager);
+    }
+
     /**
      * Print success status to the Console and if failed print error messages to the Console
      * @param result
@@ -185,11 +197,11 @@ public class RtosCommandManager {
     private static boolean processResult(int result, ConsoleManager consoleManager) {
         boolean success = result == 0;
         if (consoleManager != null) {
-            consoleManager.print("[STATUS] ", SWT.BOLD, ResourceManager.BLACK); //$NON-NLS-1$
+            consoleManager.print("[STATUS] ", SWT.BOLD, ResourceManager.INTENSE_BLACK); //$NON-NLS-1$
             if (success) { // success
-                consoleManager.println(SUCCEED, SWT.BOLD, ResourceManager.GREEN);
+                consoleManager.println(SUCCEED, SWT.BOLD, ResourceManager.INTENSE_GREEN);
             } else { // fail
-                consoleManager.println(FAILED, SWT.BOLD, ResourceManager.RED);
+                consoleManager.println(FAILED, SWT.BOLD, ResourceManager.INTENSE_RED);
             }
             // consoleManager.println("[RESULT]" + result.toString(), SWT.BOLD,
             // ResourceManager.BLACK);
diff --git a/rt-ide/tizen.rt.product.plugin/src/org/tizen/rt/ide/handlers/CleanHandler.java b/rt-ide/tizen.rt.product.plugin/src/org/tizen/rt/ide/handlers/CleanHandler.java
new file mode 100644 (file)
index 0000000..b614210
--- /dev/null
@@ -0,0 +1,104 @@
+/*
+ * Common
+ *
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * Daeryong Park <bdragon.park@samsung.com>
+ * Hyeongseok Heo <harry.heo@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd.
+ *
+ */
+package org.tizen.rt.ide.handlers;
+
+import java.lang.reflect.InvocationTargetException;
+
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.operation.IRunnableWithProgress;
+import org.eclipse.ui.IWorkbench;
+import org.eclipse.ui.progress.IProgressService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.tizen.rt.ide.Activator;
+import org.tizen.rt.ide.RtosCommandManager;
+import org.tizen.rt.ide.console.ConsoleManager;
+import org.tizen.rt.ide.util.ResourceUtil;
+import org.tizen.rt.ide.util.Util;
+
+/**
+ * @since 2017. 5. 16.
+ * @author Daeryong Park {@literal <bdragon.park@samsung.com>}
+ */
+public class CleanHandler extends AbstractHandler {
+
+    private final Logger logger = LoggerFactory.getLogger(CleanHandler.class);
+
+    /* (non-Javadoc)
+     * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
+     */
+    @Override
+    public Object execute(ExecutionEvent event) throws ExecutionException {
+        final IProject project = ResourceUtil.getCurrentProject();
+        if (project == null) {
+            return null;
+        }
+
+        boolean ok = MessageDialog.openQuestion(Util.getDisplay().getActiveShell(),
+                "Clean Project", "Are you sure you want to clean selected project?"); //$NON-NLS-1$ //$NON-NLS-2$
+        if (!ok) {
+            return null;
+        }
+
+        IWorkbench workbench = Activator.getDefault().getWorkbench();
+        IProgressService progressService = workbench.getProgressService();
+        IRunnableWithProgress runnable = new IRunnableWithProgress() {
+            @Override
+            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
+                monitor.beginTask("Clean Project", 3); //$NON-NLS-1$
+
+                RtosCommandManager.runClean(project, new ConsoleManager(true), monitor);
+                monitor.worked(1);
+
+                try {
+                    project.refreshLocal(IResource.DEPTH_INFINITE, monitor);
+                    monitor.worked(1);
+                } catch (CoreException e) {
+                    logger.error(e.getMessage(), e);
+                }
+
+                monitor.done();
+            }
+        };
+        try {
+            progressService.busyCursorWhile(runnable);
+        } catch (InvocationTargetException e) {
+            logger.error(e.getMessage(), e);
+        } catch (InterruptedException e) {
+            logger.error(e.getMessage(), e);
+        }
+
+        return null;
+    }
+
+}
index 3b82fe0..8bfe7d7 100644 (file)
@@ -45,6 +45,9 @@ public class ResourceUtil {
 
         IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
 
+        if (window == null) {
+            return null;
+        }
         ISelectionService selectionService = window.getSelectionService();
         if (selectionService != null) {
             ISelection selection = selectionService.getSelection();