From: shingil.kang Date: Tue, 12 Nov 2013 01:47:16 +0000 (+0900) Subject: CLI : support for auto completion of template name and add utility for creating web... X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=7d5971a0cad1fa4a54f4943b5ffcbb58c7f90039;p=sdk%2Ftools%2Fcli.git CLI : support for auto completion of template name and add utility for creating web project support for built-in template libraries add the option for output directory on which the project is created and the processing logic of project configuration. Change-Id: I63c89ed76c95c2d88170b29bcde6950738d7b8b5 Signed-off-by: shingil.kang --- diff --git a/org.tizen.ncli.ide/src/org/tizen/ncli/ide/autocomplete/TizenAutoComplete.java b/org.tizen.ncli.ide/src/org/tizen/ncli/ide/autocomplete/TizenAutoComplete.java index e13fb34..228e9ba 100644 --- a/org.tizen.ncli.ide/src/org/tizen/ncli/ide/autocomplete/TizenAutoComplete.java +++ b/org.tizen.ncli.ide/src/org/tizen/ncli/ide/autocomplete/TizenAutoComplete.java @@ -29,6 +29,7 @@ import java.util.List; import org.tizen.nativecommon.build.SmartBuildInterface; import org.tizen.nativecommon.build.exception.SBIException; +import org.tizen.ncli.ide.subcommands.CreateProjectCommandData; public class TizenAutoComplete { @@ -60,7 +61,7 @@ public class TizenAutoComplete { }; public enum casesForInputSuggestion { - platform("--platform"), toolchain("--toolchain"), buildtarget("--build-target"); + platform("--platform"), toolchain("--toolchain"), buildtarget("--build-target"), name("-n"); private String option; @@ -260,6 +261,10 @@ public class TizenAutoComplete { case buildtarget: listResult = sbi.getTargetList(); break; + case name: + listResult = CreateProjectCommandData.getTemplateList(); + break; + } for (String result : listResult) { suggestion = suggestion + " " + result; diff --git a/org.tizen.ncli.ide/src/org/tizen/ncli/ide/shell/CreateProjectCLI.java b/org.tizen.ncli.ide/src/org/tizen/ncli/ide/shell/CreateProjectCLI.java index 595285c..be096d4 100644 --- a/org.tizen.ncli.ide/src/org/tizen/ncli/ide/shell/CreateProjectCLI.java +++ b/org.tizen.ncli.ide/src/org/tizen/ncli/ide/shell/CreateProjectCLI.java @@ -26,6 +26,7 @@ package org.tizen.ncli.ide.shell; import org.kohsuke.args4j.Option; +import org.kohsuke.args4j.spi.StringOptionHandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; //import org.tizen.ncli.ide.subcommands.CreateNativeProjectCommand; @@ -38,12 +39,15 @@ public class CreateProjectCLI extends AbstractCLI { private Logger logger = LoggerFactory.getLogger(getClass()); - @Option(name = "-t", aliases = { "--type" }) + @Option(name = "-t", aliases = { "--type" }, required = true, usage = "Specify template type") private TizenAppType appType; - @Option(name = "-n", aliases = "--name", required = true) + @Option(name = "-n", aliases = "--name", required = true, usage = "Specify template name") private String projectName; + @Option(name = "-out", aliases = {"--output"} ,handler=StringOptionHandler.class, usage = "Specify output directory name") + public String outputName; + /* * (non-Javadoc) * @@ -65,6 +69,8 @@ public class CreateProjectCLI extends AbstractCLI CreateWebProjectCommand webCommand = new CreateWebProjectCommand(); webCommand.setAppType(appType); webCommand.setProjectName(projectName); + if(outputName != null) + webCommand.setOutputName(outputName); webCommand.runCommand(); break; default: diff --git a/org.tizen.ncli.ide/src/org/tizen/ncli/ide/subcommands/CreateProjectCommandData.java b/org.tizen.ncli.ide/src/org/tizen/ncli/ide/subcommands/CreateProjectCommandData.java index 2a379f2..9474ef1 100644 --- a/org.tizen.ncli.ide/src/org/tizen/ncli/ide/subcommands/CreateProjectCommandData.java +++ b/org.tizen.ncli.ide/src/org/tizen/ncli/ide/subcommands/CreateProjectCommandData.java @@ -27,12 +27,19 @@ package org.tizen.ncli.ide.subcommands; import static org.tizen.web.common.WebConstant.WIDGET_CONFIGURATION_FILE; import java.io.File; +import java.io.FileNotFoundException; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.regex.Pattern; import org.tizen.common.AppIdGenerator; import org.tizen.common.core.application.InstallPathConfig; +import org.tizen.common.util.FileUtil; import org.tizen.ncli.ide.shell.TizenAppType; +import org.tizen.web.model.TizenAppTemplate; +import org.tizen.web.model.TizenModelFactory; /** * @author Shingil Kang{@literal } (S-core) @@ -51,31 +58,53 @@ public class CreateProjectCommandData private TizenAppType appType; private String projectName; - private String targetAbsoultePath = System.getProperty("user.dir") + File.separator + "te"; + private String outputName = System.getProperty("user.dir") + File.separator; + + private static TizenAppTemplate tizenAppTemplate; private String APPID = AppIdGenerator.getInstance().create(); - static + public static List getTemplateList() + { + File root = new File(PLATFORM_PATH + File.separator + WEB_PROJECT_PATH); + + List fileArray = new ArrayList(); + List fileNameArray = new ArrayList(); + + try + { + fileArray = FileUtil.findFiles(root, TIZEN_WEB_APP_TEMPLATE_FILE, true); + + for (File f : fileArray) + { + tizenAppTemplate = TizenModelFactory.unmarshalAppTemplate(f); + String templateName = tizenAppTemplate.getTemplateId(); + fileNameArray.add(templateName); + webProjects.put(templateName, f.getParentFile().getAbsolutePath()); + } + + } catch (FileNotFoundException e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return fileNameArray; + } + + public void setOutputName(String outputName) { - webProjects.put("tizen-basic", "Basic" + File.separator + "Tizen_Blank"); - webProjects.put("tizenwebuifw-masterdetail", "Tizen Web UI Framework + File.separator + Tizen_Web_UI_FW_MasterDetail "); - webProjects.put("tizenwebuifw-multipage", "Tizen Web UI Framework + File.separator + Tizen_Web_UI_FW_MultiPage"); - webProjects.put("tizenwebuifw-navigation", "Tizen Web UI Framework + File.separator + Tizen_Web_UI_FW_NavigationView"); - webProjects.put("tizenwebuifw-singlepage", "Tizen Web UI Framework" + File.separator + "Tizen_Web_UI_FW_SinglePage"); - webProjects.put("jqm-masterdetail", "jQuery Mobile" + File.separator + "MasterDetail"); - webProjects.put("jqm-multipage", "jQuery Mobile" + File.separator + "MultiPage"); - webProjects.put("jqm-navigatio", "jQuery Mobile" + File.separator + "NavigationView"); - webProjects.put("jqm-singlepage", "jQuery Mobile" + File.separator + "SinglePage"); + this.outputName = this.outputName + File.separator + outputName + File.separator; } - public String getTargetAbsoultePath() + public String getOutputName() { - return targetAbsoultePath; + return outputName; } public void setTargetAbsoultePath(File targetAbsoultePath) { - this.targetAbsoultePath = targetAbsoultePath.getAbsolutePath(); + this.outputName = targetAbsoultePath.getAbsolutePath(); } public void setAppType(TizenAppType appType) @@ -111,9 +140,7 @@ public class CreateProjectCommandData // get web template root path public String getTizenWebTemplateRootPath(String id) { - String TemplatePath = PLATFORM_PATH + File.separator + WEB_PROJECT_PATH + File.separator + webProjects.get(id); - - return TemplatePath; + return webProjects.get(id); } // get web project root path @@ -143,12 +170,13 @@ public class CreateProjectCommandData // get web project configuration destination path public String getDestTizenWebProjectConfigFile(String id) { - return targetAbsoultePath + File.separator + WIDGET_CONFIGURATION_FILE; + return outputName + File.separator + WIDGET_CONFIGURATION_FILE; } - // get web project configuration file - public String getTizenWebConfigTemplatePath() - { - return PLATFORM_PATH + File.separator + WEB_PROJECT_PATH + File.separator + TIZEN_PROJECT_CONFIG_PATH; - } + // // get web project configuration file + // public String getTizenWebConfigTemplatePath() + // { + // return PLATFORM_PATH + File.separator + WEB_PROJECT_PATH + File.separator + // + TIZEN_PROJECT_CONFIG_PATH; + // } } \ No newline at end of file diff --git a/org.tizen.ncli.ide/src/org/tizen/ncli/ide/subcommands/CreateWebProjectCommand.java b/org.tizen.ncli.ide/src/org/tizen/ncli/ide/subcommands/CreateWebProjectCommand.java index 8aa096a..c0f1392 100644 --- a/org.tizen.ncli.ide/src/org/tizen/ncli/ide/subcommands/CreateWebProjectCommand.java +++ b/org.tizen.ncli.ide/src/org/tizen/ncli/ide/subcommands/CreateWebProjectCommand.java @@ -31,19 +31,28 @@ import static org.tizen.web.common.WebConstant.WIDGET_BASE_IDENTIFIER; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStreamWriter; import java.net.URI; import java.net.URISyntaxException; +import java.net.URL; +import java.util.Enumeration; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Scanner; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import org.kohsuke.args4j.Option; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.tizen.common.Factory; import org.tizen.common.core.application.InstallPathConfig; import org.tizen.common.core.application.ProfileInfo; @@ -95,38 +104,47 @@ import freemarker.template.Configuration; public class CreateWebProjectCommand extends AbstractSubCommand { private CreateProjectCommandData data = new CreateProjectCommandData(); - + // Freemarker variables private Map root = new HashMap(); private Configuration cfg; - + private static String temporaryFileAdditionalName = ".template"; //$NON-NLS-1$ private static String optionGroupSeparator = "_"; //$NON-NLS-1$ - + TizenAppTemplate tizenAppTemplate; - + + private ProjectGenUtil pg = new ProjectGenUtil(); + private TizenTemplateLibrariesPool libraryPool; - - /* 1. copy web project template - * 2. copy the dependent library - * 3. copy configuration - * 4. generate HTML and XML file from template + + /* + * create web project from template. + * + * 1. get template list + * 2. copy web project template + * 3. copy the dependent library + * 4. copy configuration + * 5. generate HTML and XML file from template */ @Override protected CreateProjectCommandData call() { - //1. copy web project template + // 1. get template list + data.getTemplateList(); + + // 2. copy web project template copyWebProjectTemplate(); - - //2. copy the dependent library + + // 3. copy the dependent library copyLibrary(); - - //3. generate configuration + + // 4. generate configuration copyConfiguration(); - - //4. generate file from template + + // 5. generate file from template processOption(); - + return data; } @@ -139,31 +157,38 @@ public class CreateWebProjectCommand extends AbstractSubCommand groupList = libraryPool.getLibraryGroupWithDepends(sl); - + // copy libraries - for (TizenTemplateLibraryGroup group : groupList) { + for (TizenTemplateLibraryGroup group : groupList) + { /* - if ( isSampleGroup( template ) ) { - group.setSelectedMinifiedWithDependsUpdate( true ); - } - */ - if ( group.getSelectedElement().getKindsOfLibrary() == TizenTemplateKindsOfLibraries.TIZENWEBUIFW ) { - copyTizenWebUIFWLibrary( group ); + * if ( isSampleGroup( template ) ) { + * group.setSelectedMinifiedWithDependsUpdate( true ); } + */ + if (group.getSelectedElement().getKindsOfLibrary() == TizenTemplateKindsOfLibraries.TIZENWEBUIFW) + { + copyTizenWebUIFWLibrary(group); } - else { - copyNormalLibrary( group); + else + { + copyNormalLibrary(group); } } } } - + private void copyTizenWebUIFWLibrary(TizenTemplateLibraryGroup group) { String libraryPath = group.getSelectedLibraryPath(); - if (libraryPath == null) { - return ; + if (libraryPath == null) + { + return; } // cannot find library directory (source) File source = new File(libraryPath); - if ( !source.exists() ) { - String errorMsg = "Cannot find template library resource : " + libraryPath; // TODO : externalize + if (!source.exists()) + { + String errorMsg = "Cannot find template library resource : " + libraryPath; // TODO + // : + // externalize } // create target directory TizenWebUIFWType type = null; String elementKey = group.getSelectedElementKey(); - try { - type = TizenWebUIFWType.valueOf( elementKey ); - } catch (IllegalArgumentException e) { + try + { + type = TizenWebUIFWType.valueOf(elementKey); + } catch (IllegalArgumentException e) + { return; } URI destinationURI = null; - switch ( type ) { + switch (type) { case TizenWebUIFramework_Theme: case TizenWebUIFramework_Theme_Images: - String themePaths[] = libraryPath.split( "Theme/" ); - // destinationURI = URI.create(project.getLocationURI() + type.getDestinationPath() + themePaths[1]); + String themePaths[] = libraryPath.split("Theme/"); + // destinationURI = URI.create(project.getLocationURI() + + // type.getDestinationPath() + themePaths[1]); break; default: - // destinationURI = URI.create(project.getLocationURI() + type.getDestinationPath()); + // destinationURI = URI.create(project.getLocationURI() + + // type.getDestinationPath()); break; } // decide target directory - if ( type != TizenWebUIFWType.TizenWebUIFramework_Theme ) { - destinationURI = URI.create( destinationURI + group.getSelectedElementValue() ); + if (type != TizenWebUIFWType.TizenWebUIFramework_Theme) + { + destinationURI = URI.create(destinationURI + group.getSelectedElementValue()); } // copy must pre-create directory - if ( source.isDirectory() ) { - if ( !FileUtil.isExist( destinationURI.getPath() ) ) { - FileUtil.createDirectory( new File(destinationURI) ); + if (source.isDirectory()) + { + if (!FileUtil.isExist(destinationURI.getPath())) + { + FileUtil.createDirectory(new File(destinationURI)); } - } else { + } + else + { File parentFile = new File(destinationURI).getParentFile(); - if ( !FileUtil.isExist( parentFile.getPath() ) ) { - FileUtil.createDirectory( parentFile ); + if (!FileUtil.isExist(parentFile.getPath())) + { + FileUtil.createDirectory(parentFile); } } // copyLibrary(source.toURI(), destinationURI); } - - private void copyNormalLibrary(TizenTemplateLibraryGroup group) + + private void copyNormalLibrary(TizenTemplateLibraryGroup group) { String libraryPath = group.getSelectedLibraryPath(); - if (libraryPath == null) { + if (libraryPath == null) + { return; } // cannot find library directory (source) File source = new File(libraryPath); - if (!source.exists() || !source.isDirectory()) { - String errorMsg = "Cannot find template library resource directory : " + libraryPath; + if (!source.exists() || !source.isDirectory()) + { + String errorMsg = "Cannot find template library resource directory : " + libraryPath; } - + // decide target directory TizenTemplateKindsOfLibraries kindOfLibrary = group.getSelectedElement().getKindsOfLibrary(); - + String destDir; - + switch (kindOfLibrary) { - case JAVASCRIPT : - destDir = data.getTargetAbsoultePath() + "js"; + case JAVASCRIPT: + destDir = data.getOutputName() + "js"; break; - case CSS : - destDir = data.getTargetAbsoultePath() + "css"; + case CSS: + destDir = data.getOutputName() + "css"; break; - case OTHERS : // copy to root folder basically - default : - destDir = data.getTargetAbsoultePath(); + case OTHERS: // copy to root folder basically + default: + destDir = data.getOutputName(); break; } - + try { FileUtil.copyRecursively(libraryPath, destDir, true); @@ -301,24 +348,25 @@ public class CreateWebProjectCommand extends AbstractSubCommand groupList = libraryPool.getLibraryGroupWithDepends(supportLibraries); - - for (TizenTemplateLibraryGroup group : groupList) { + + for (TizenTemplateLibraryGroup group : groupList) + { // get key String key = group.getKey(); - + // get value LibraryElement selectedElement = group.getSelectedElement(); - String value = group.isSelectedMinified() ? - selectedElement.getMinifiedValue() : selectedElement.getOriginalValue(); - + String value = group.isSelectedMinified() ? selectedElement.getMinifiedValue() : selectedElement.getOriginalValue(); + // put key and value - if (value != null) { + if (value != null) + { root.put(key, value); } } } } - - private void buildModelForOptionGroup(OptionGroup optionGroup, String key) { - for (OptionElement optionElement : optionGroup.getOptionElement()) { + + private void buildModelForOptionGroup(OptionGroup optionGroup, String key) + { + for (OptionElement optionElement : optionGroup.getOptionElement()) + { boolean isDefault = (optionElement.isDefaultElement() != null) ? optionElement.isDefaultElement() : false; - String addedKey = key + optionGroupSeparator + optionElement.getKey(); // key ex) group1_element + String addedKey = key + optionGroupSeparator + optionElement.getKey(); // key + // ex) + // group1_element String value = optionElement.getValue(); - - switch(optionGroup.getOptionType()) { - case MANDATORY : + + switch (optionGroup.getOptionType()) { + case MANDATORY: root.put(addedKey, value); break; - case MULTI_SELECTION : + case MULTI_SELECTION: root.put(addedKey, isDefault ? value : ""); break; - case SINGLE_SELECTION : - if (isDefault) { + case SINGLE_SELECTION: + if (isDefault) + { root.put(key, value); } break; - default : + default: } } - + // process inner group - for (OptionGroup innerOptionGroup : optionGroup.getOptionGroup()) { - String addedKey = key + optionGroupSeparator + innerOptionGroup.getKey(); // key ex) group1_group2 + for (OptionGroup innerOptionGroup : optionGroup.getOptionGroup()) + { + String addedKey = key + optionGroupSeparator + innerOptionGroup.getKey(); // key + // ex) + // group1_group2 buildModelForOptionGroup(innerOptionGroup, addedKey); } } - - private void buildModelForConfigFile(){ + + private void buildModelForConfigFile() + { root.put("APP_ID", data.getAppID()); root.put("WIDGET_ID", data.getProjectName()); } - - private void generateFileFromTemplate(File srcFile){ - + + private void generateFileFromTemplate(File srcFile) + { + // rename source file to template file String originalFilePath = srcFile.getAbsolutePath(); String templateFilePath = originalFilePath + temporaryFileAdditionalName; File templateFile = new File(templateFilePath); srcFile.renameTo(templateFile); - - try { + + try + { // generate file from template file FreeMarkerUtil.generateDocument(root, cfg, templateFile.getName(), originalFilePath); } catch (IOException e) @@ -456,98 +532,99 @@ public class CreateWebProjectCommand extends AbstractSubCommand"; + private final String CONFIG_EXTRA_NAME_INFO = " ${WIDGET_ID}"; + + public void copyConfigFile(String destConfigPath) + { + InputStream ips = null; + FileOutputStream fos = null; + + try + { + ips = getClass().getResource(CONFIG_FILE).openStream(); + fos = new FileOutputStream(destConfigPath); + FileUtil.redirectStream(ips, fos); + } catch (IOException e) + { + // logger.error(e.getMessage()); + + } finally + { + IOUtil.tryClose(ips, fos); + } + + addExtraInfo2Config(destConfigPath); + } + + public void addExtraInfo2Config(String destConfigPath) + { + OutputStreamWriter out = null; + Scanner scanner = null; + + String newLine = System.getProperty("line.separator"); + + try + { + scanner = new Scanner(new FileInputStream(destConfigPath)); + out = new OutputStreamWriter(new FileOutputStream(destConfigPath + ".output")); + + String line; + while (scanner.hasNextLine()) + { + line = scanner.nextLine(); + + out.write(line); + out.write(newLine); + + if (line.startsWith(" 0) + { // write contents of 'is' to 'fos' + fos.write(is.read()); + } + fos.close(); + is.close(); + } + } catch (FileNotFoundException e) + { + logger.error(e.getMessage()); + } catch (IOException e) + { + logger.error(e.getMessage()); + } + return templateLibPath; + } +}