Skip to content
Permalink
Browse files

Git: project share

  • Loading branch information...
serge-rider committed Nov 7, 2019
1 parent bf7f408 commit bfc6c7ac35cefcb572c968c402a966e9899bc799
@@ -23,7 +23,6 @@
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.util.Date;

/**
* Some IO helper functions
@@ -272,25 +271,4 @@ public static String readToString(Reader is) throws IOException {
return result.toString();
}

public static void makeFileBackup(File file) throws IOException {
if (!file.exists()) {
return;
}
String backupFileName = file.getName() + ".bak";
if (!backupFileName.startsWith(".")) {
backupFileName = "." + backupFileName;
}
File backupFile = new File(file.getParent(), backupFileName);
if (backupFile.exists()) {
Date backupTime = new Date(backupFile.lastModified());
if (CommonUtils.isSameDay(backupTime, new Date())) {
return;
}
}
try (FileInputStream fis = new FileInputStream(file)) {
try (FileOutputStream fos = new FileOutputStream(backupFile)) {
fastCopy(fis, fos);
}
}
}
}
@@ -20,6 +20,8 @@
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
@@ -41,6 +43,7 @@
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.text.MessageFormat;
import java.util.Date;
import java.util.Locale;

/**
@@ -486,4 +489,30 @@ public static void checkFolderExists(IFolder folder, DBRProgressMonitor monitor)
}
}
}

public static void makeFileBackup(IFile file) throws IOException {
if (!file.exists()) {
return;
}
String backupFileName = file.getName() + ".bak";
if (!backupFileName.startsWith(".")) {
backupFileName = "." + backupFileName;
}
IFile backupFile = file.getParent().getFile(new Path(backupFileName));
if (backupFile.exists()) {
Date backupTime = new Date(backupFile.getModificationStamp());
if (CommonUtils.isSameDay(backupTime, new Date())) {
return;
}
}
try (InputStream fis = file.getContents()) {
if (!backupFile.exists()) {
backupFile.create(fis, IResource.HIDDEN | IResource.TEAM_PRIVATE, new NullProgressMonitor());
} else {
backupFile.setContents(fis, IResource.HIDDEN | IResource.TEAM_PRIVATE, new NullProgressMonitor());
}
} catch (CoreException e) {
throw new IOException("Error creating backup copy of " + file.getFullPath(), e);
}
}
}
@@ -39,12 +39,11 @@
import org.jkiss.dbeaver.registry.driver.DriverDescriptor;
import org.jkiss.dbeaver.runtime.DBWorkbench;
import org.jkiss.dbeaver.runtime.resource.DBeaverNature;
import org.jkiss.dbeaver.utils.ContentUtils;
import org.jkiss.dbeaver.utils.RuntimeUtils;
import org.jkiss.utils.ArrayUtils;
import org.jkiss.utils.CommonUtils;
import org.jkiss.utils.IOUtils;

import java.io.File;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
@@ -681,8 +680,7 @@ private void saveDataSources() {
}
}
try {
File plainConfigFile = configFile.getLocation().toFile();
IOUtils.makeFileBackup(plainConfigFile);
ContentUtils.makeFileBackup(configFile);

if (localDataSources.isEmpty()) {
if (configFile.exists()) {
@@ -31,6 +31,7 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.equinox.security.storage.ISecurePreferences;
import org.eclipse.equinox.security.storage.SecurePreferencesFactory;
import org.jkiss.code.NotNull;
@@ -40,8 +41,8 @@
import org.jkiss.dbeaver.model.app.DBPWorkspace;
import org.jkiss.dbeaver.model.task.DBTTaskManager;
import org.jkiss.dbeaver.registry.task.TaskManagerImpl;
import org.jkiss.dbeaver.utils.ContentUtils;
import org.jkiss.utils.CommonUtils;
import org.jkiss.utils.IOUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;
@@ -370,9 +371,8 @@ private void flushMetadata() {
// Nothing to save and metadata file doesn't exist
return;
}
getMetadataFolder(true);
try {
IOUtils.makeFileBackup(mdFile);
ContentUtils.makeFileBackup(getMetadataFolder(true).getFile(new Path(METADATA_STORAGE_FILE)));

try (Writer mdWriter = new OutputStreamWriter(new FileOutputStream(mdFile), StandardCharsets.UTF_8)) {
try (JsonWriter jsonWriter = METADATA_GSON.newJsonWriter(mdWriter)) {
@@ -31,8 +31,8 @@
import org.jkiss.dbeaver.model.runtime.DefaultProgressMonitor;
import org.jkiss.dbeaver.model.task.*;
import org.jkiss.dbeaver.registry.ProjectMetadata;
import org.jkiss.dbeaver.utils.ContentUtils;
import org.jkiss.dbeaver.utils.GeneralUtils;
import org.jkiss.utils.IOUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;
@@ -239,7 +239,7 @@ private void saveConfiguration() {
IFile configFile = getConfigFile(true);
try {
if (configFile.exists()) {
IOUtils.makeFileBackup(configFile.getLocation().toFile());
ContentUtils.makeFileBackup(configFile);
}
if (tasks.isEmpty()) {
configFile.delete(true, false, monitor);
@@ -0,0 +1,28 @@
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2019 Serge Rider (serge@jkiss.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jkiss.dbeaver.team.git.ui.handlers;

public class GITCommandIds {

public static final String CMD_SHARE = "org.jkiss.dbeaver.git.commands.share";
public static final String CMD_CREATE_PROJECT = "org.jkiss.dbeaver.git.commands.projectFromGit";
public static final String CMD_COMMIT = "org.jkiss.dbeaver.git.commands.commit";
public static final String CMD_UPDATE = "org.jkiss.dbeaver.git.commands.update";

public static final String EGIT_CMD_SHARE = "org.eclipse.egit.ui.command.shareProject";
public static final String EGIT_CMD_ADD_TO_INDEX = "org.eclipse.egit.ui.team.AddToIndex";
}
@@ -18,22 +18,35 @@

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.egit.ui.internal.sharing.SharingWizard;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.commands.IElementUpdater;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.menus.UIElement;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.DBPMessageType;
import org.jkiss.dbeaver.model.app.DBPDataSourceRegistry;
import org.jkiss.dbeaver.model.app.DBPProject;
import org.jkiss.dbeaver.runtime.DBWorkbench;
import org.jkiss.dbeaver.runtime.DBeaverNotifications;
import org.jkiss.dbeaver.team.git.ui.utils.GitUIUtils;
import org.jkiss.dbeaver.ui.ActionUtils;
import org.jkiss.dbeaver.ui.UIUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ProjectShareHandler extends AbstractHandler implements IElementUpdater {

private static final String CMD_SHARE = "org.eclipse.egit.ui.command.shareProject";
private static final Log log = Log.getLog(ProjectShareHandler.class);

@Override
public Object execute(ExecutionEvent event) {
@@ -45,20 +58,74 @@ public Object execute(ExecutionEvent event) {
"Select a project or resource to share");
return null;
}
//IHandlerService handlerService = window.getService(IHandlerService.class);
//ICommandService commandService = window.getService(ICommandService.class);

IWorkbench workbench = HandlerUtil.getActiveWorkbenchWindow(event).getWorkbench();
SharingWizard wizard = new SharingWizard();
wizard.init(workbench, project);
Shell shell = HandlerUtil.getActiveShell(event);
WizardDialog wizardDialog = new WizardDialog(shell, wizard);
wizardDialog.setHelpAvailable(false);
wizardDialog.open();
if (wizardDialog.open() == IDialogConstants.OK_ID) {
// Add content
addProjectContentsToRepository(event, project);

DBeaverNotifications.showNotification(
"git",
"Project added to Git",
"Project '" + project.getName() + "' has been added to Git repository",
DBPMessageType.INFORMATION,
() -> ActionUtils.runCommand(GITCommandIds.CMD_COMMIT, workbench));
}

return null;
}

private void addProjectContentsToRepository(ExecutionEvent event, IProject project) {
List<IResource> resources = new ArrayList<>();
try {
addFolderToIndex(project, resources);
} catch (CoreException e) {
log.error(e);
}

IStructuredSelection selection = new StructuredSelection(resources);
ActionUtils.runCommand(GITCommandIds.EGIT_CMD_ADD_TO_INDEX, selection, UIUtils.getActiveWorkbenchWindow());
}

private void addFolderToIndex(IContainer container, List<IResource> resources) throws CoreException {
for (IResource resource : container.members(IContainer.INCLUDE_HIDDEN)) {
if (container instanceof IProject && resource instanceof IFolder && resource.getName().equals(DBPProject.METADATA_FOLDER)) {
// Add dbeaver configs
for (IResource cfgResource : ((IFolder) resource).members(IContainer.INCLUDE_HIDDEN)) {
if (cfgResource instanceof IFile) {
if (cfgResource.getFileExtension().equals("bak")) {
continue;
}
resources.add(cfgResource);
}
}
continue;
} else if (resource.isHidden() || resource.isTeamPrivateMember() || resource.isLinked() || resource.isVirtual() || !resource.exists()) {
continue;
}
if (container instanceof IProject) {
if (resource instanceof IFolder && resource.getName().equals(".settings")) {
continue;
}
if (resource instanceof IFile) {
if (resource.getName().startsWith(DBPDataSourceRegistry.LEGACY_CONFIG_FILE_PREFIX) && resource.getName().endsWith(DBPDataSourceRegistry.LEGACY_CONFIG_FILE_EXT)) {
continue;
}
}
}
if (resource instanceof IFolder) {
addFolderToIndex((IFolder) resource, resources);
} else {
resources.add(resource);
}
}
}

@Override
public void updateElement(UIElement element, Map parameters) {
IProject project = GitUIUtils.extractActiveProject(element.getServiceLocator());

0 comments on commit bfc6c7a

Please sign in to comment.
You can’t perform that action at this time.