PageRenderTime 31ms CodeModel.GetById 18ms app.highlight 11ms 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 */
 42package org.netbeans.modules.php.composer.commands;
 43
 44import java.util.ArrayList;
 45import java.util.Arrays;
 46import java.util.Collections;
 47import java.util.List;
 48import java.util.concurrent.Future;
 49import org.netbeans.api.extexecution.ExecutionDescriptor;
 50import org.netbeans.modules.php.api.executable.InvalidPhpExecutableException;
 51import org.netbeans.modules.php.api.executable.PhpExecutable;
 52import org.netbeans.modules.php.api.executable.PhpExecutableValidator;
 53import org.netbeans.modules.php.api.phpmodule.PhpModule;
 54import org.netbeans.modules.php.api.util.StringUtils;
 55import org.netbeans.modules.php.composer.options.ComposerOptions;
 56import org.netbeans.modules.php.composer.ui.options.ComposerOptionsPanelController;
 57import org.openide.DialogDescriptor;
 58import org.openide.DialogDisplayer;
 59import org.openide.NotifyDescriptor;
 60import org.openide.filesystems.FileObject;
 61import org.openide.filesystems.FileUtil;
 62import org.openide.util.NbBundle;
 63
 64/**
 65 * Represents <a href="http://getcomposer.org/">Composer</a> command line tool.
 66 */
 67public final class Composer {
 68
 69    public static final String NAME = "composer"; // NOI18N
 70    public static final String LONG_NAME = NAME + ".phar"; // NOI18N
 71
 72    private static final String LOCK_FILENAME = "composer.json"; // NOI18N
 73
 74    // commands
 75    private static final String INIT_COMMAND = "init"; // NOI18N
 76    private static final String INSTALL_COMMAND = "install"; // NOI18N
 77    private static final String UPDATE_COMMAND = "update"; // NOI18N
 78    private static final String VALIDATE_COMMAND = "validate"; // NOI18N
 79    private static final String SELF_UPDATE_COMMAND = "self-update"; // NOI18N
 80    // params
 81    private static final List<String> DEFAULT_PARAMS = Arrays.asList(
 82        "--ansi", // NOI18N
 83        "--no-interaction" // NOI18N
 84    );
 85    private static final String NAME_PARAM = "--name=%s"; // NOI18N
 86    private static final String AUTHOR_PARAM = "--author=%s <%s>"; // NOI18N
 87    private static final String DESCRIPTION_PARAM = "--description=%s"; // NOI18N
 88    private static final String DEV_PARAM = "--dev"; // NOI18N
 89
 90    private final String composerPath;
 91
 92
 93    public Composer(String composerPath) {
 94        this.composerPath = composerPath;
 95    }
 96
 97    /**
 98     * Get the default, <b>valid only</b> Composer.
 99     * @return the default, <b>valid only</b> Composer.
100     * @throws InvalidPhpExecutableException if Composer is not valid.
101     */
102    public static Composer getDefault() throws InvalidPhpExecutableException {
103        String composerPath = ComposerOptions.getInstance().getComposerPath();
104        String error = validate(composerPath);
105        if (error != null) {
106            throw new InvalidPhpExecutableException(error);
107        }
108        return new Composer(composerPath);
109    }
110
111    @NbBundle.Messages("Composer.script.label=Composer")
112    public static String validate(String composerPath) {
113        return PhpExecutableValidator.validateCommand(composerPath, Bundle.Composer_script_label());
114    }
115
116    @NbBundle.Messages({
117        "Composer.run.init=Composer (init)",
118        "Composer.lockFile.exists=Composer lock file already exists - overwrite it?",
119        "# {0} - project name",
120        "Composer.init.description=Description of project {0}."
121    })
122    public Future<Integer> init(PhpModule phpModule) {
123        FileObject lockFile = getLockFile(phpModule);
124        if (lockFile != null && lockFile.isValid()) {
125            if (!userConfirmation(phpModule.getDisplayName(), Bundle.Composer_lockFile_exists())) {
126                return null;
127            }
128        }
129        // command params
130        ComposerOptions options = ComposerOptions.getInstance();
131        List<String> params = Arrays.asList(
132                String.format(NAME_PARAM, getInitName(options.getVendor(), phpModule.getName())),
133                String.format(AUTHOR_PARAM, options.getAuthorName(), options.getAuthorEmail()),
134                String.format(DESCRIPTION_PARAM, Bundle.Composer_init_description(phpModule.getDisplayName())));
135        return runCommand(phpModule, INIT_COMMAND, Bundle.Composer_run_init(), params);
136    }
137
138    private String getInitName(String vendor, String projectName) {
139        StringBuilder name = new StringBuilder(50);
140        name.append(vendor);
141        name.append('/'); // NOI18N
142        name.append(StringUtils.webalize(projectName));
143        return name.toString();
144    }
145
146    @NbBundle.Messages("Composer.run.install=Composer (install)")
147    public Future<Integer> install(PhpModule phpModule) {
148        return runCommand(phpModule, INSTALL_COMMAND, Bundle.Composer_run_install());
149    }
150
151    @NbBundle.Messages("Composer.run.installDev=Composer (install dev)")
152    public Future<Integer> installDev(PhpModule phpModule) {
153        return runCommand(phpModule, INSTALL_COMMAND, Bundle.Composer_run_installDev(), Collections.singletonList(DEV_PARAM));
154    }
155
156    @NbBundle.Messages("Composer.run.update=Composer (update)")
157    public Future<Integer> update(PhpModule phpModule) {
158        return runCommand(phpModule, UPDATE_COMMAND, Bundle.Composer_run_update());
159    }
160
161    @NbBundle.Messages("Composer.run.updateDev=Composer (update dev)")
162    public Future<Integer> updateDev(PhpModule phpModule) {
163        return runCommand(phpModule, UPDATE_COMMAND, Bundle.Composer_run_updateDev(), Collections.singletonList(DEV_PARAM));
164    }
165
166    @NbBundle.Messages("Composer.run.validate=Composer (validate)")
167    public Future<Integer> validate(PhpModule phpModule) {
168        return runCommand(phpModule, VALIDATE_COMMAND, Bundle.Composer_run_validate());
169    }
170
171    @NbBundle.Messages("Composer.run.selfUpdate=Composer (self-update)")
172    public Future<Integer> selfUpdate() {
173        return runCommand(null, SELF_UPDATE_COMMAND, Bundle.Composer_run_selfUpdate());
174    }
175
176    private Future<Integer> runCommand(PhpModule phpModule, String command, String title) {
177        return runCommand(phpModule, command, title, Collections.<String>emptyList());
178    }
179
180    private Future<Integer> runCommand(PhpModule phpModule, String command, String title, List<String> commandParams) {
181        FileObject sourceDirectory = null;
182        if (phpModule != null) {
183            sourceDirectory = phpModule.getSourceDirectory();
184            if (sourceDirectory == null) {
185                warnNoSources(phpModule.getDisplayName());
186                return null;
187            }
188        }
189        PhpExecutable composer = new PhpExecutable(composerPath)
190                .optionsSubcategory(ComposerOptionsPanelController.OPTIONS_SUBPATH)
191                .displayName(title)
192                .additionalParameters(getAllParameters(command, commandParams));
193        if (sourceDirectory != null) {
194            composer.workDir(FileUtil.toFile(sourceDirectory));
195        }
196        return composer.run(getDescriptor());
197    }
198
199    private List<String> getAllParameters(String command, List<String> commandParams) {
200        List<String> allParams = new ArrayList<String>(DEFAULT_PARAMS.size() + commandParams.size() + 1);
201        allParams.addAll(DEFAULT_PARAMS);
202        allParams.add(command);
203        allParams.addAll(commandParams);
204        return allParams;
205    }
206
207    private ExecutionDescriptor getDescriptor() {
208        return PhpExecutable.DEFAULT_EXECUTION_DESCRIPTOR
209                .optionsPath(ComposerOptionsPanelController.getOptionsPath())
210                .inputVisible(false);
211    }
212
213    @NbBundle.Messages({
214        "# {0} - project name",
215        "Composer.project.noSources=Project {0} has no Source Files."
216    })
217    private static void warnNoSources(String projectName) {
218        DialogDisplayer.getDefault().notifyLater(
219                new NotifyDescriptor.Message(Bundle.Composer_project_noSources(projectName), NotifyDescriptor.WARNING_MESSAGE));
220    }
221
222    private FileObject getLockFile(PhpModule phpModule) {
223        FileObject sourceDirectory = phpModule.getSourceDirectory();
224        if (sourceDirectory == null) {
225            // broken project
226            return null;
227        }
228        return sourceDirectory.getFileObject(LOCK_FILENAME);
229    }
230
231    private boolean userConfirmation(String title, String question) {
232        NotifyDescriptor confirmation = new DialogDescriptor.Confirmation(question, title, DialogDescriptor.YES_NO_OPTION);
233        return DialogDisplayer.getDefault().notify(confirmation) == DialogDescriptor.YES_OPTION;
234    }
235
236}