PageRenderTime 57ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/php.composer/src/org/netbeans/modules/php/composer/commands/Composer.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 236 lines | 157 code | 26 blank | 53 comment | 15 complexity | 23063cc489b35c6184b40ac928431479 MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 2012 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * If you wish your version of this file to be governed by only the CDDL
  28. * or only the GPL Version 2, indicate your decision by adding
  29. * "[Contributor] elects to include this software in this distribution
  30. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  31. * single choice of license, a recipient has the option to distribute
  32. * your version of this file under either the CDDL, the GPL Version 2 or
  33. * to extend the choice of license to its licensees as provided above.
  34. * However, if you add GPL Version 2 code and therefore, elected the GPL
  35. * Version 2 license, then the option applies only if the new code is
  36. * made subject to such option by the copyright holder.
  37. *
  38. * Contributor(s):
  39. *
  40. * Portions Copyrighted 2012 Sun Microsystems, Inc.
  41. */
  42. package org.netbeans.modules.php.composer.commands;
  43. import java.util.ArrayList;
  44. import java.util.Arrays;
  45. import java.util.Collections;
  46. import java.util.List;
  47. import java.util.concurrent.Future;
  48. import org.netbeans.api.extexecution.ExecutionDescriptor;
  49. import org.netbeans.modules.php.api.executable.InvalidPhpExecutableException;
  50. import org.netbeans.modules.php.api.executable.PhpExecutable;
  51. import org.netbeans.modules.php.api.executable.PhpExecutableValidator;
  52. import org.netbeans.modules.php.api.phpmodule.PhpModule;
  53. import org.netbeans.modules.php.api.util.StringUtils;
  54. import org.netbeans.modules.php.composer.options.ComposerOptions;
  55. import org.netbeans.modules.php.composer.ui.options.ComposerOptionsPanelController;
  56. import org.openide.DialogDescriptor;
  57. import org.openide.DialogDisplayer;
  58. import org.openide.NotifyDescriptor;
  59. import org.openide.filesystems.FileObject;
  60. import org.openide.filesystems.FileUtil;
  61. import org.openide.util.NbBundle;
  62. /**
  63. * Represents <a href="http://getcomposer.org/">Composer</a> command line tool.
  64. */
  65. public final class Composer {
  66. public static final String NAME = "composer"; // NOI18N
  67. public static final String LONG_NAME = NAME + ".phar"; // NOI18N
  68. private static final String LOCK_FILENAME = "composer.json"; // NOI18N
  69. // commands
  70. private static final String INIT_COMMAND = "init"; // NOI18N
  71. private static final String INSTALL_COMMAND = "install"; // NOI18N
  72. private static final String UPDATE_COMMAND = "update"; // NOI18N
  73. private static final String VALIDATE_COMMAND = "validate"; // NOI18N
  74. private static final String SELF_UPDATE_COMMAND = "self-update"; // NOI18N
  75. // params
  76. private static final List<String> DEFAULT_PARAMS = Arrays.asList(
  77. "--ansi", // NOI18N
  78. "--no-interaction" // NOI18N
  79. );
  80. private static final String NAME_PARAM = "--name=%s"; // NOI18N
  81. private static final String AUTHOR_PARAM = "--author=%s <%s>"; // NOI18N
  82. private static final String DESCRIPTION_PARAM = "--description=%s"; // NOI18N
  83. private static final String DEV_PARAM = "--dev"; // NOI18N
  84. private final String composerPath;
  85. public Composer(String composerPath) {
  86. this.composerPath = composerPath;
  87. }
  88. /**
  89. * Get the default, <b>valid only</b> Composer.
  90. * @return the default, <b>valid only</b> Composer.
  91. * @throws InvalidPhpExecutableException if Composer is not valid.
  92. */
  93. public static Composer getDefault() throws InvalidPhpExecutableException {
  94. String composerPath = ComposerOptions.getInstance().getComposerPath();
  95. String error = validate(composerPath);
  96. if (error != null) {
  97. throw new InvalidPhpExecutableException(error);
  98. }
  99. return new Composer(composerPath);
  100. }
  101. @NbBundle.Messages("Composer.script.label=Composer")
  102. public static String validate(String composerPath) {
  103. return PhpExecutableValidator.validateCommand(composerPath, Bundle.Composer_script_label());
  104. }
  105. @NbBundle.Messages({
  106. "Composer.run.init=Composer (init)",
  107. "Composer.lockFile.exists=Composer lock file already exists - overwrite it?",
  108. "# {0} - project name",
  109. "Composer.init.description=Description of project {0}."
  110. })
  111. public Future<Integer> init(PhpModule phpModule) {
  112. FileObject lockFile = getLockFile(phpModule);
  113. if (lockFile != null && lockFile.isValid()) {
  114. if (!userConfirmation(phpModule.getDisplayName(), Bundle.Composer_lockFile_exists())) {
  115. return null;
  116. }
  117. }
  118. // command params
  119. ComposerOptions options = ComposerOptions.getInstance();
  120. List<String> params = Arrays.asList(
  121. String.format(NAME_PARAM, getInitName(options.getVendor(), phpModule.getName())),
  122. String.format(AUTHOR_PARAM, options.getAuthorName(), options.getAuthorEmail()),
  123. String.format(DESCRIPTION_PARAM, Bundle.Composer_init_description(phpModule.getDisplayName())));
  124. return runCommand(phpModule, INIT_COMMAND, Bundle.Composer_run_init(), params);
  125. }
  126. private String getInitName(String vendor, String projectName) {
  127. StringBuilder name = new StringBuilder(50);
  128. name.append(vendor);
  129. name.append('/'); // NOI18N
  130. name.append(StringUtils.webalize(projectName));
  131. return name.toString();
  132. }
  133. @NbBundle.Messages("Composer.run.install=Composer (install)")
  134. public Future<Integer> install(PhpModule phpModule) {
  135. return runCommand(phpModule, INSTALL_COMMAND, Bundle.Composer_run_install());
  136. }
  137. @NbBundle.Messages("Composer.run.installDev=Composer (install dev)")
  138. public Future<Integer> installDev(PhpModule phpModule) {
  139. return runCommand(phpModule, INSTALL_COMMAND, Bundle.Composer_run_installDev(), Collections.singletonList(DEV_PARAM));
  140. }
  141. @NbBundle.Messages("Composer.run.update=Composer (update)")
  142. public Future<Integer> update(PhpModule phpModule) {
  143. return runCommand(phpModule, UPDATE_COMMAND, Bundle.Composer_run_update());
  144. }
  145. @NbBundle.Messages("Composer.run.updateDev=Composer (update dev)")
  146. public Future<Integer> updateDev(PhpModule phpModule) {
  147. return runCommand(phpModule, UPDATE_COMMAND, Bundle.Composer_run_updateDev(), Collections.singletonList(DEV_PARAM));
  148. }
  149. @NbBundle.Messages("Composer.run.validate=Composer (validate)")
  150. public Future<Integer> validate(PhpModule phpModule) {
  151. return runCommand(phpModule, VALIDATE_COMMAND, Bundle.Composer_run_validate());
  152. }
  153. @NbBundle.Messages("Composer.run.selfUpdate=Composer (self-update)")
  154. public Future<Integer> selfUpdate() {
  155. return runCommand(null, SELF_UPDATE_COMMAND, Bundle.Composer_run_selfUpdate());
  156. }
  157. private Future<Integer> runCommand(PhpModule phpModule, String command, String title) {
  158. return runCommand(phpModule, command, title, Collections.<String>emptyList());
  159. }
  160. private Future<Integer> runCommand(PhpModule phpModule, String command, String title, List<String> commandParams) {
  161. FileObject sourceDirectory = null;
  162. if (phpModule != null) {
  163. sourceDirectory = phpModule.getSourceDirectory();
  164. if (sourceDirectory == null) {
  165. warnNoSources(phpModule.getDisplayName());
  166. return null;
  167. }
  168. }
  169. PhpExecutable composer = new PhpExecutable(composerPath)
  170. .optionsSubcategory(ComposerOptionsPanelController.OPTIONS_SUBPATH)
  171. .displayName(title)
  172. .additionalParameters(getAllParameters(command, commandParams));
  173. if (sourceDirectory != null) {
  174. composer.workDir(FileUtil.toFile(sourceDirectory));
  175. }
  176. return composer.run(getDescriptor());
  177. }
  178. private List<String> getAllParameters(String command, List<String> commandParams) {
  179. List<String> allParams = new ArrayList<String>(DEFAULT_PARAMS.size() + commandParams.size() + 1);
  180. allParams.addAll(DEFAULT_PARAMS);
  181. allParams.add(command);
  182. allParams.addAll(commandParams);
  183. return allParams;
  184. }
  185. private ExecutionDescriptor getDescriptor() {
  186. return PhpExecutable.DEFAULT_EXECUTION_DESCRIPTOR
  187. .optionsPath(ComposerOptionsPanelController.getOptionsPath())
  188. .inputVisible(false);
  189. }
  190. @NbBundle.Messages({
  191. "# {0} - project name",
  192. "Composer.project.noSources=Project {0} has no Source Files."
  193. })
  194. private static void warnNoSources(String projectName) {
  195. DialogDisplayer.getDefault().notifyLater(
  196. new NotifyDescriptor.Message(Bundle.Composer_project_noSources(projectName), NotifyDescriptor.WARNING_MESSAGE));
  197. }
  198. private FileObject getLockFile(PhpModule phpModule) {
  199. FileObject sourceDirectory = phpModule.getSourceDirectory();
  200. if (sourceDirectory == null) {
  201. // broken project
  202. return null;
  203. }
  204. return sourceDirectory.getFileObject(LOCK_FILENAME);
  205. }
  206. private boolean userConfirmation(String title, String question) {
  207. NotifyDescriptor confirmation = new DialogDescriptor.Confirmation(question, title, DialogDescriptor.YES_NO_OPTION);
  208. return DialogDisplayer.getDefault().notify(confirmation) == DialogDescriptor.YES_OPTION;
  209. }
  210. }