[Title] Implemented "Import" function of rootstrap view
authordonghee yang <donghee.yang@samsung.com>
Wed, 14 Nov 2012 06:31:38 +0000 (15:31 +0900)
committerdonghee yang <donghee.yang@samsung.com>
Wed, 14 Nov 2012 06:31:38 +0000 (15:31 +0900)
org.tizen.nativeplatform/src/org/tizen/nativeplatform/rootstrap/RootstrapManager.java
org.tizen.nativeplatform/src/org/tizen/nativeplatform/views/ui/RootstrapUIMessages.properties
org.tizen.nativeplatform/src/org/tizen/nativeplatform/views/ui/RootstrapView.java
org.tizen.nativeplatform/src/org/tizen/nativeplatform/wizards/TizenPlatformProjectWizard.java

index f456f5d..5d565e8 100644 (file)
@@ -5,6 +5,7 @@ import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.FileWriter;
+import java.io.FilenameFilter;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.HashSet;
@@ -27,6 +28,7 @@ import org.eclipse.core.runtime.Path;
 import org.tizen.common.core.application.InstallPathConfig;
 import org.tizen.nativecommon.build.SmartBuildInterface;
 import org.tizen.nativecommon.build.exception.SBIException;
+import org.tizen.nativecommon.build.model.Rootstrap;
 import org.tizen.nativecommon.build.model.SBIModel;
 import org.tizen.nativecommon.build.model.Target;
 import org.tizen.nativeplatform.password.SudoPasswdManager;
@@ -42,7 +44,9 @@ public class RootstrapManager {
        private static PlatformRootstrap selectedRootstrap;
        public static Set<IRootstrapChangedListener> changedListener = new HashSet<IRootstrapChangedListener>();
        
-       private static boolean init;    
+       private static boolean init;
+       private static final String URL_SEP = "|";
+       private static final String URL_SEP_REGEXP = "[|]";
        
        private RootstrapManager() {
        }
@@ -63,8 +67,8 @@ public class RootstrapManager {
                                String path = sbi.getRootstrapPathFromRootstrapID(id);
                                ArrayList<String> reposURLs = new ArrayList<String>();
                                String rootstrapPath = sbi.getRootstrapPropertyValue(id, "REPOSITORY_URLS");
-                               if ( rootstrapPath != null && rootstrapPath.split("[|]").length >= 1 ) {
-                                       for ( String url : rootstrapPath.split("[|]") ) {
+                               if ( rootstrapPath != null && rootstrapPath.split(URL_SEP_REGEXP).length >= 1 ) {
+                                       for ( String url : rootstrapPath.split(URL_SEP_REGEXP) ) {
                                                reposURLs.add( url );
                                        }
                                }
@@ -112,32 +116,122 @@ public class RootstrapManager {
 
                // make temp directory
                String tempDirPath = makeTempDirectory();
-               
-               // zip file-system
-               String basePath = rootstrap.getPath().removeLastSegments(1).toOSString();
-               String rootstrapID = rootstrap.getId();
-               if( !commandWithSudo( String.format("cd %s;sudo -S tar cvzf %s/rootstrap.tar.gz %s",
-                               basePath, tempDirPath, rootstrapID), "Platform Internal" ) ) {
-                       return false;
+
+               try {
+                       // zip file-system
+                       String basePath = rootstrap.getPath().removeLastSegments(1).toOSString();
+                       String rootstrapID = rootstrap.getId();
+                       if( !commandWithSudo( String.format("cd %s;sudo -S tar cvzf %s/rootstrap.tar.gz %s",
+                                       basePath, tempDirPath, rootstrapID), "Platform Internal" ) ) {
+                               return false;
+                       }
+                       
+                       // copy xml
+                       String pluginFileName = rootstrap.getId() + ".xml";
+                       String pluginPath = new Path(SmartBuildInterface.getInstance().getPluginPath())
+                               .append(pluginFileName).toOSString();
+                       if ( !commandWithSudo( String.format("cp %s %s/", pluginPath, tempDirPath ), null) )  {
+                               return false;
+                       }
+                       
+                       // zip them together
+                       if ( !commandWithSudo( String.format("cd %s;tar cvf %s rootstrap.tar.gz %s",
+                                       tempDirPath, exportFilePath, pluginFileName), "Platform Internal" ) )  {
+                               return false;
+                       }
+                       
+                       return true;
                }
-               
-               // copy xml
-               String pluginFileName = rootstrap.getId() + ".xml";
-               String pluginPath = new Path(SmartBuildInterface.getInstance().getPluginPath())
-                       .append(pluginFileName).toOSString();
-               if ( !commandWithSudo( String.format("cp %s %s/", pluginPath, tempDirPath ), null) )  {
-                       return false;
+               finally {
+                       // remove temporary directory
+                       commandWithSudo( String.format("sudo -S rm -rf %s",tempDirPath), null );
                }
+       }
+
+       
+       public static PlatformRootstrap importRootstrap( String importFilePath) throws Exception {
+               PlatformRootstrap result = null;
+               String pluginPath = null;
+               String rootstrapPath = null;
                
-               // zip them together
-               if ( !commandWithSudo( String.format("cd %s;tar cvf %s rootstrap.tar.gz %s",
-                               tempDirPath, exportFilePath, pluginFileName), "Platform Internal" ) )  {
-                       return false;
+               // make temp directory
+               String tempDirPath = makeTempDirectory();
+
+               try {
+                       // extract "tar" archive
+                       if( !commandWithSudo( String.format("cd %s;tar xvf %s",
+                                       tempDirPath, importFilePath), "Platform Internal" ) ) {
+                               throw new Exception("Extracting rootsrap failed!");
+                       }
+                       
+                       // find rootstrap id
+                       File tempDir = new File(tempDirPath);
+                       File[] xmlFiles = tempDir.listFiles( new FilenameFilter() {
+                               @Override
+                               public boolean accept(File dir, String name) {
+                                       if( name.endsWith(".xml") ) {
+                                               return true;
+                                       }
+                                       else {
+                                               return false;
+                                       }
+                               }
+                               
+                       } );
+                       if ( xmlFiles.length == 0 ) {
+                               throw new Exception("There is no SBI plugin file(*.xml)");
+                       }
+                       String pluginFilePath = xmlFiles[0].getPath().toString();
+                       String pluginFileName = new File(pluginFilePath).getName();
+                       String pluginId = pluginFileName.split("\\.")[0];
+                       
+                       // check already exist?
+                       if( RootstrapManager.getRootstrap(pluginId) != null ) {
+                               throw new Exception("There already exists the rootrap with same id!");
+                       }
+                       
+                       // copy xml
+                       pluginPath = new Path(SmartBuildInterface.getInstance().getPluginPath())
+                               .append(pluginFileName).toOSString();
+                       commandWithSudo( String.format("cp %s %s", pluginFilePath, pluginPath), null);
+                       
+                       // extracting file system
+                       String basePath = SmartBuildInterface.getInstance().getBaseRootstrapPath()+"/platform";
+                       rootstrapPath = basePath + "/" + pluginId;
+                       File baseDirDir = new File(basePath);
+                       if ( !(baseDirDir.exists()) ) {
+                               baseDirDir.mkdir();
+                       }
+                       if ( !commandWithSudo( String.format("cd %s;sudo -S tar xvf %s/rootstrap.tar.gz",
+                                       basePath, tempDirPath), "Platform Internal" ) )  {
+                               throw new Exception("Extracting rootsrap failed!");
+                       }
+                       
+                       SBIModel.reinitialize();
+                       result = createRootstrapFromSBIPlugin( pluginId );
+                       if ( result == null ) {
+                               throw new Exception("Loading SBI plugin failed!");
+                       }
+
+                       return result;
+               } 
+               catch (Exception e) {
+                       if ( pluginPath != null ) {
+                               commandWithSudo( String.format("rm -rf %s", pluginPath), null );
+                               SBIModel.reinitialize();
+                       }
+                       if ( rootstrapPath != null ) {
+                               commandWithSudo( String.format("sudo -S rm -rf %s", rootstrapPath), null );
+                       }
+                       
+                       throw e;
+               }
+               finally {
+                       // remove temporary directory
+                       commandWithSudo( String.format("sudo -S rm -rf %s",tempDirPath), null );
                }
-               
-               return true;
        }
-
+       
        
        private static String makeTempDirectory() {
                final File temp;
@@ -460,7 +554,7 @@ public class RootstrapManager {
                        String reposURLString = "";
                        for ( String URL: reposURLs ) {
                                if ( !reposURLString.isEmpty() ) {
-                                       reposURLString += "|";
+                                       reposURLString += URL_SEP;
                                }
                                reposURLString += URL;
                        }
@@ -521,11 +615,30 @@ public class RootstrapManager {
                }
                
                // remove model/file
-               //SBIModel.removeRootstrap(rootstrap);
                new File(getPluginXML(rootstrap.getId())).delete();
        }
 
        
+       private static PlatformRootstrap createRootstrapFromSBIPlugin( String id ) {
+               Rootstrap rootstrap = SBIModel.getRootstrapById(id);
+               if ( rootstrap == null ) {
+                       return null;
+               }
+               
+               ArrayList<String> reposURLs = new ArrayList<String>();
+               String urlStr = rootstrap.getPropertyValue("REPOSITORY_URLS");
+               if ( urlStr != null ) {
+                       for( String url : urlStr.split(URL_SEP_REGEXP) ) {
+                               reposURLs.add(url);
+                       }
+               }
+               
+               return new PlatformRootstrap(id, rootstrap.getName(), rootstrap.getArch(),
+                               rootstrap.getPath().toOSString(), 
+                               reposURLs, true);
+       }
+       
+       
        private static String getPluginXML( String id ) {
                IPath pluginPath = new Path(SmartBuildInterface.getInstance().getPluginPath());
                return pluginPath.append(id + ".xml").toOSString();
index ecbac23..72f4aac 100644 (file)
@@ -1,6 +1,5 @@
 View.Add.Rootstrap.ToolTip = Create New Rootstrap
-View.Import.Rootstrap.ToolTip = Create New Rootstrap
-View.Refresh.Rootstrap.ToolTip = Create New Rootstrap
+View.Import.Rootstrap.ToolTip = Import Rootstrap
 
 View.Error.Init.SBIModel = Failed to initialize SBI model
 
@@ -42,11 +41,14 @@ SelectRootDlg.Msg.LastOne = "%s [%s]" rootstrap is the last one. You can not rem
 SelectRootDlg.Label.Desc = Follow projects use "%s" rootstrap.\nYou should set other rootstraps if you want to remove "%s" rootstrap
 
 Action.Msg.Error.RemoveDefault = Cannot remove basic rootstrap!  
-Action.Msg.Progress.Zipping = Zipping rootstrap...
+Action.Msg.Progress.Exporting = Exporting rootstrap...
 Action.Msg.Error.ExportUninitialzed = Cannot export uninitialized rootstrap!  
-Action.Msg.Info.ExportOK = Exporting rootstrap is successful.
+Action.Msg.Info.ExportOK = Exporting rootstrap is successful!
 Action.Msg.Error.ExportFailed = Exporting rootstrap failed!  
 Action.Msg.Info.NotInit = This rootstrap is not initialize.
+Action.Msg.Progress.Importing = Importing rootstrap...
+Action.Msg.Info.ImportOK = Importing rootstrap is successful!
+Action.Msg.Error.ImportFailed = Importing rootstrap failed!
 
-FileDlg.Title.Export = Export Rootstrap
-FileDlg.Title.Import = Import Rootstrap
\ No newline at end of file
+FileDlg.Title.Export = Export rootstrap
+FileDlg.Title.Import = Import rootstrap
\ No newline at end of file
index 368dfbb..3a3631b 100644 (file)
@@ -15,16 +15,13 @@ import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IWorkspace;
 import org.eclipse.core.resources.ResourcesPlugin;
-import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.Path;
 import org.eclipse.jface.action.Action;
 import org.eclipse.jface.action.IContributionItem;
 import org.eclipse.jface.action.IMenuListener;
 import org.eclipse.jface.action.IMenuManager;
 import org.eclipse.jface.action.MenuManager;
 import org.eclipse.jface.action.Separator;
-import org.eclipse.jface.dialogs.IDialogBlockedHandler;
 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
 import org.eclipse.jface.operation.IRunnableWithProgress;
 import org.eclipse.jface.viewers.ArrayContentProvider;
@@ -250,19 +247,16 @@ public class RootstrapView extends ViewPart {
         addRootstrap.setImage(addIcon);
         addRootstrap.setToolTipText(resources.getString("View.Add.Rootstrap.ToolTip"));
         
-        addRootstrap.addSelectionListener(new SelectionAdapter()
-        {
+        addRootstrap.addSelectionListener(new SelectionListener() {
             @Override
-            public void widgetSelected(SelectionEvent e)
-            {
-                try
-                {
-                    showAddDialog();
-                } catch (Exception e1)
-                {
-                    e1.printStackTrace();
-                }
+            public void widgetSelected(SelectionEvent e) {
+               showAddDialog();
             }
+            
+                       @Override
+                       public void widgetDefaultSelected(SelectionEvent e) {
+                               // Nothing to do
+                       }
         });
         addRootstrap.setEnabled(true);
         
@@ -272,42 +266,23 @@ public class RootstrapView extends ViewPart {
         Image editIcon = ImageUtil.getImage(Activator.PLUGIN_ID, "icons/rootstrap/import.gif");
         importRootstrap.setImage(editIcon);
         importRootstrap.setToolTipText(resources.getString("View.Import.Rootstrap.ToolTip"));
+        importRootstrap.addSelectionListener( new SelectionListener() {
+
+                       @Override
+                       public void widgetSelected(SelectionEvent e) {
+                               showImportDialog();
+                       }
+
+                       @Override
+                       public void widgetDefaultSelected(SelectionEvent e) {
+                               // Nothing to do
+                       }
+               
+        });
         
     }
 
     
-    private void showAddDialog() {
-       AddRootstrapDialog dlg = new AddRootstrapDialog(shell);
-       if ( dlg.open() == AddRootstrapDialog.OK ) {
-               final String rootstrapName = dlg.getRootstrapName();
-               final String arch = dlg.getArchitecture();
-               final ArrayList<String> repositoryURLs = dlg.getRepositoryURLs();
-               final boolean immediateGen = dlg.isImmediateGenerateChecked();
-               
-               // generate rootstrap here
-               ProgressMonitorDialog dialog = new ProgressMonitorDialog(shell);
-               try {
-                       dialog.run(true, true, new IRunnableWithProgress() {
-                               @Override
-                               public void run(IProgressMonitor monitor) throws InvocationTargetException,
-                                       InterruptedException {
-                                       monitor.beginTask(resources.getString("GenRootDlg.Progress.Msg.Generating"), -1);
-                                       generatedRootstrap = null;
-                                       generatedRootstrap = RootstrapManager.generate(rootstrapName, arch, repositoryURLs, immediateGen);
-                                       if ( generatedRootstrap == null ) {
-                                               throw new InterruptedException();
-                                       }
-                               }
-                       });                                     
-               } catch (Exception e1) {
-                       e1.printStackTrace();
-                       DialogUtil.openErrorDialog(resources.getString("GenRootDlg.Error.Gen.Rootstrap.Failed") );
-                       return;
-               }
-
-               RootstrapManager.addRootstrap( generatedRootstrap );
-       }
-    }
     
     
        private void createRootstrapTableComposite(Composite parent) {
@@ -487,6 +462,83 @@ public class RootstrapView extends ViewPart {
         actionExport.setText(resources.getString("View.Contextmenu.Export"));
     }
        
+       
+    private void showAddDialog() {
+       AddRootstrapDialog dlg = new AddRootstrapDialog(shell);
+       if ( dlg.open() == AddRootstrapDialog.OK ) {
+               final String rootstrapName = dlg.getRootstrapName();
+               final String arch = dlg.getArchitecture();
+               final ArrayList<String> repositoryURLs = dlg.getRepositoryURLs();
+               final boolean immediateGen = dlg.isImmediateGenerateChecked();
+               
+               // generate rootstrap here
+               ProgressMonitorDialog dialog = new ProgressMonitorDialog(shell);
+               try {
+                       dialog.run(true, true, new IRunnableWithProgress() {
+                               @Override
+                               public void run(IProgressMonitor monitor) throws InvocationTargetException,
+                                       InterruptedException {
+                                       monitor.beginTask(resources.getString("GenRootDlg.Progress.Msg.Generating"), -1);
+                                       generatedRootstrap = null;
+                                       generatedRootstrap = RootstrapManager.generate(rootstrapName, arch, repositoryURLs, immediateGen);
+                                       if ( generatedRootstrap == null ) {
+                                               throw new InterruptedException();
+                                       }
+                               }
+                       });                                     
+               } catch (Exception e1) {
+                       e1.printStackTrace();
+                       DialogUtil.openErrorDialog(resources.getString("GenRootDlg.Error.Gen.Rootstrap.Failed") );
+                       return;
+               }
+
+               RootstrapManager.addRootstrap( generatedRootstrap );
+       }
+    }
+
+    
+    private void showImportDialog() {
+       FileDialog fd = new FileDialog( shell, SWT.OPEN);
+        fd.setText(resources.getString("FileDlg.Title.Import"));
+        String[] extensions = {"*.tar"};
+        fd.setFilterExtensions(extensions);
+        String resultFilePath = fd.open();
+        if ( !resultFilePath.endsWith(".tar")) {
+               resultFilePath += ".tar";
+        }
+        final String selectedFilePath =  resultFilePath;
+        if (selectedFilePath != null) {
+               // generate rootstrap here
+               ProgressMonitorDialog dialog = new ProgressMonitorDialog(shell);
+               try {
+                       dialog.run(true, false, new IRunnableWithProgress() {
+                               @Override
+                               public void run(IProgressMonitor monitor) throws InvocationTargetException,
+                                               InterruptedException {
+                                       
+                                       monitor.beginTask(resources.getString("Action.Msg.Progress.Importing"), -1);
+                                       generatedRootstrap = null;
+                                       try {
+                                                       generatedRootstrap = RootstrapManager.importRootstrap( selectedFilePath );
+                                               } catch (Exception e) {
+                                               throw new InterruptedException(e.getMessage());
+                                               }
+                               }
+                       });                                     
+               } catch (Exception e1) {
+                       e1.printStackTrace();
+                       DialogUtil.openErrorDialog(
+                                       String.format("%s\n * %s", resources.getString("Action.Msg.Error.ImportFailed"),
+                                                       e1.getMessage() ) );
+                       return;
+               }
+        }
+        
+        RootstrapManager.addRootstrap( generatedRootstrap );
+               DialogUtil.openMessageDialog(shell,resources.getString("Action.Msg.Info.ImportOK"));
+    }
+
+    
     public void selectionBuildRoot()
     {
         doActionForEntry(new IActionForEntry()
@@ -633,7 +685,7 @@ public class RootstrapView extends ViewPart {
                                        dialog.run(true, false, new IRunnableWithProgress() {
                                                @Override
                                                public void run(IProgressMonitor monitor) {
-                                                       monitor.beginTask(resources.getString("Action.Msg.Progress.Zipping"), -1);
+                                                       monitor.beginTask(resources.getString("Action.Msg.Progress.Exporting"), -1);
                                                        boolean result = RootstrapManager.exportRootstrap( selectedRootstrap, selectedFilePath );
                                                        if ( result ) {
                                                        DialogUtil.openMessageDialog(shell,resources.getString("Action.Msg.Info.ExportOK"));
index 8c9bfc6..b3cabae 100644 (file)
@@ -124,4 +124,11 @@ public class TizenPlatformProjectWizard extends TizenProjectWizard {
         // TODO Auto-generated method stub
         
     }
+
+       @Override
+       protected void createCDTDescriptionTemplate(TemplateCore selectedTemplate,
+                       IPath projectPath, String projectType) throws IOException {
+               // TODO Auto-generated method stub
+               
+       }
 }