Search in sources :

Example 1 with NewModelRepoDialog

use of org.archicontribs.modelrepository.dialogs.NewModelRepoDialog in project archi-modelrepository-plugin by archi-contribs.

the class CreateRepoFromModelAction method run.

@Override
public void run() {
    NewModelRepoDialog dialog = new NewModelRepoDialog(fWindow.getShell());
    if (dialog.open() != Window.OK) {
        return;
    }
    final String repoURL = dialog.getURL();
    final String userName = dialog.getUsername();
    final String userPassword = dialog.getPassword();
    if (!StringUtils.isSet(repoURL) && !StringUtils.isSet(userName) && !StringUtils.isSet(userPassword)) {
        return;
    }
    File localRepoFolder = new File(ModelRepositoryPlugin.INSTANCE.getUserModelRepositoryFolder(), GraficoUtils.getLocalGitFolderName(repoURL));
    // Folder is not empty
    if (localRepoFolder.exists() && localRepoFolder.isDirectory() && localRepoFolder.list().length > 0) {
        MessageDialog.openError(fWindow.getShell(), Messages.CreateRepoFromModelAction_1, Messages.CreateRepoFromModelAction_2 + " " + // $NON-NLS-1$
        localRepoFolder.getAbsolutePath());
        return;
    }
    setRepository(new ArchiRepository(localRepoFolder));
    /**
     * Wrapper class to handle progress monitor
     */
    class CreateRepoProgressHandler extends ProgressHandler {

        @Override
        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            super.run(monitor);
            try {
                monitor.beginTask(Messages.CreateRepoFromModelAction_3, IProgressMonitor.UNKNOWN);
                // Proxy check
                ProxyAuthenticater.update(repoURL);
                // Create a new repo
                try (Git git = getRepository().createNewLocalGitRepository(repoURL)) {
                }
                // TODO: If the model has not been saved yet this is fine but if the model already exists
                // We should tell the user this is the case
                // Set new file location
                fModel.setFile(getRepository().getTempModelFile());
                // And Save it
                IEditorModelManager.INSTANCE.saveModel(fModel);
                // Export to Grafico
                getRepository().exportModelToGraficoFiles();
                monitor.subTask(Messages.CreateRepoFromModelAction_4);
                // Commit changes
                getRepository().commitChanges(Messages.CreateRepoFromModelAction_5, false);
                monitor.subTask(Messages.CreateRepoFromModelAction_6);
                // Push
                getRepository().pushToRemote(userName, userPassword, null);
                // Store repo credentials if option is set
                if (ModelRepositoryPlugin.INSTANCE.getPreferenceStore().getBoolean(IPreferenceConstants.PREFS_STORE_REPO_CREDENTIALS)) {
                    SimpleCredentialsStorage sc = new SimpleCredentialsStorage(new File(getRepository().getLocalGitFolder(), IGraficoConstants.REPO_CREDENTIALS_FILE));
                    sc.store(userName, userPassword);
                }
                // Save the checksum
                getRepository().saveChecksum();
            } catch (GitAPIException | IOException | NoSuchAlgorithmException | URISyntaxException ex) {
                displayErrorDialog(Messages.CreateRepoFromModelAction_7, ex);
            } finally {
                monitor.done();
            }
        }
    }
    Display.getCurrent().asyncExec(new Runnable() {

        @Override
        public void run() {
            try {
                ProgressMonitorDialog pmDialog = new ProgressMonitorDialog(fWindow.getShell());
                pmDialog.run(false, true, new CreateRepoProgressHandler());
            } catch (InvocationTargetException | InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    });
}
Also used : ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) NewModelRepoDialog(org.archicontribs.modelrepository.dialogs.NewModelRepoDialog) ArchiRepository(org.archicontribs.modelrepository.grafico.ArchiRepository) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) URISyntaxException(java.net.URISyntaxException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Git(org.eclipse.jgit.api.Git) SimpleCredentialsStorage(org.archicontribs.modelrepository.authentication.SimpleCredentialsStorage) File(java.io.File)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 SimpleCredentialsStorage (org.archicontribs.modelrepository.authentication.SimpleCredentialsStorage)1 NewModelRepoDialog (org.archicontribs.modelrepository.dialogs.NewModelRepoDialog)1 ArchiRepository (org.archicontribs.modelrepository.grafico.ArchiRepository)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 ProgressMonitorDialog (org.eclipse.jface.dialogs.ProgressMonitorDialog)1 Git (org.eclipse.jgit.api.Git)1 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)1