Skip to content
This repository has been archived by the owner on Apr 3, 2018. It is now read-only.

Commit

Permalink
LANG: refactored ProjectBuildArtifactValidator and related.
Browse files Browse the repository at this point in the history
  • Loading branch information
bruno-medeiros committed Jul 29, 2015
1 parent f028653 commit 0233ad7
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 269 deletions.
@@ -0,0 +1,103 @@
/*******************************************************************************
* Copyright (c) 2015 Bruno Medeiros and other Contributors.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Bruno Medeiros - initial API and implementation
*******************************************************************************/
package melnorme.lang.ide.core.launch;


import static melnorme.utilbox.core.Assert.AssertNamespace.assertNotNull;

import java.nio.file.Path;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;

import melnorme.lang.ide.core.LangCore;
import melnorme.lang.ide.core.operations.build.BuildManager;
import melnorme.lang.ide.core.operations.build.BuildTarget;
import melnorme.lang.ide.core.utils.ProjectValidator;
import melnorme.lang.ide.core.utils.ResourceUtils;
import melnorme.lang.tooling.data.AbstractValidator2;
import melnorme.lang.tooling.data.ValidationMessages;
import melnorme.utilbox.core.CommonException;
import melnorme.utilbox.misc.Location;
import melnorme.utilbox.misc.PathUtil;

public class LaunchExecutableValidator extends AbstractValidator2 {

protected final IProject validProject;
protected final String buildTargetName; // can be null
protected final String artifactPathOverride; // can be null

public LaunchExecutableValidator(ProjectValidator projectValidator, String projectName,
String buildTargetName, String artifactPathOverride)
throws CommonException {
this(projectValidator.getProject(projectName), buildTargetName, artifactPathOverride);
}

public LaunchExecutableValidator(IProject validProject, String buildTargetName, String artifactPathOverride) {
this.validProject = assertNotNull(validProject);
this.buildTargetName = buildTargetName;
this.artifactPathOverride = artifactPathOverride;
}

protected BuildManager getBuildManager() {
return LangCore.getBuildManager();
}

/* ----------------- ----------------- */

public BuildTarget getBuildTarget() throws CoreException, CommonException {
if(buildTargetName == null){
return null;
}
return getValidBuildTarget();
}

public BuildTarget getValidBuildTarget() throws CoreException, CommonException {
return getBuildManager().getValidBuildTarget(validProject, buildTargetName, false);
}

/* ----------------- ----------------- */

public String getDefaultArtifactPath() throws CommonException, CoreException {
BuildTarget buildTarget = getBuildTarget();
if(buildTarget == null) {
return null;
}
return getBuildManager().createBuildTargetValidator(validProject, buildTarget).getArtifactPath();
}

public Location getValidExecutableLocation() throws CoreException, CommonException {
String effectiveArtifactPath = artifactPathOverride != null ? artifactPathOverride : getDefaultArtifactPath();
return getValidExecutableLocation(effectiveArtifactPath);
}

public Location getValidExecutableLocation(String exeFilePathString) throws CommonException, CoreException {
if(exeFilePathString == null || exeFilePathString.isEmpty()) {
throw new CommonException(LaunchMessages.BuildTarget_NoArtifactPathSpecified);
}
Path exeFilePath = PathUtil.createPath(exeFilePathString);

Location exeFileLocation = toAbsolute(exeFilePath);
if(exeFileLocation.toFile().exists() && !exeFileLocation.toFile().isFile()) {
error(ValidationMessages.Location_NotAFile(exeFileLocation));
}
return exeFileLocation;
}

public Location toAbsolute(Path exePath) throws CommonException, CoreException {
if(exePath.isAbsolute()) {
return Location.fromAbsolutePath(exePath);
}
// Otherwise path is relative to project location
return ResourceUtils.loc(validProject.getLocation()).resolve(exePath);
}

}
Expand Up @@ -16,6 +16,7 @@

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.debug.core.DebugPlugin;

import melnorme.lang.ide.core.operations.build.BuildTarget;
import melnorme.utilbox.misc.Location;
Expand Down Expand Up @@ -58,4 +59,8 @@ public BuildTarget getBuildTarget() {
return buildTarget;
}

public String getProgramArgumentsString() {
return DebugPlugin.renderArguments(programArguments, null);
}

}
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2015, 2015 IBM Corporation and others.
* Copyright (c) 2015 Bruno Medeiros and other Contributors.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -17,45 +17,40 @@
import org.eclipse.core.runtime.IPath;
import org.eclipse.debug.core.DebugPlugin;

import melnorme.lang.ide.core.launch.ProjectBuildArtifactValidator.ProjectBuildExecutableSettings;
import melnorme.lang.ide.core.operations.build.BuildTarget;
import melnorme.utilbox.core.CommonException;
import melnorme.utilbox.misc.Location;

public class ProcessLaunchInfoValidator {

public final ProcessLaunchInfoSettings settings;
protected final ProjectBuildArtifactValidator buildExecutableValidator;

public ProcessLaunchInfoValidator(ProcessLaunchInfoSettings settings) {
this.settings = settings;
this.buildExecutableValidator = init_ProjectBuildExecutableFileValidator(settings);
}
protected final LaunchExecutableValidator launchExecutableValidator;

protected ProjectBuildArtifactValidator init_ProjectBuildExecutableFileValidator(
ProcessLaunchInfoSettings settings) {
return new ProjectBuildArtifactValidator(settings);
}
protected final String programArguments;
protected final String workingDirectory;
protected final Map<String, String> environmentVars;
protected final boolean appendEnvironmentVars;

public static interface ProcessLaunchInfoSettings extends ProjectBuildExecutableSettings {

public abstract String getProgramArguments_Attribute() throws CoreException;
public abstract String getWorkingDirectory_Attribute() throws CoreException;
public abstract Map<String, String> getEnvironmentVars() throws CoreException;
public abstract boolean getAppendEnvironmentVars() throws CoreException;

public ProcessLaunchInfoValidator(LaunchExecutableValidator launchExecutableValidator, String programArguments,
String workingDirectory, Map<String, String> environmentVars, boolean appendEnvironmentVars) {
this.launchExecutableValidator = launchExecutableValidator;
this.programArguments = programArguments;
this.workingDirectory = workingDirectory;
this.environmentVars = environmentVars;
this.appendEnvironmentVars = appendEnvironmentVars;
}

/* ----------------- ----------------- */

protected IProject getProject() throws CommonException, CoreException {
return buildExecutableValidator.getValidProject();
return launchExecutableValidator.validProject;
}

protected BuildTarget getBuildTarget() throws CoreException, CommonException {
return buildExecutableValidator.getBuildTarget();
return launchExecutableValidator.getBuildTarget();
}

protected Location getValidExecutableFileLocation() throws CoreException, CommonException {
return buildExecutableValidator.getValidExecutableFileLocation();
return launchExecutableValidator.getValidExecutableLocation();
}

/* ----------------- ----------------- */
Expand All @@ -69,8 +64,7 @@ public IPath getWorkingDirectory() throws CommonException, CoreException {
}

public IPath getDefinedWorkingDirectory() throws CoreException {
String path = settings.getWorkingDirectory_Attribute();
return path.isEmpty() ? null : new org.eclipse.core.runtime.Path(path);
return workingDirectory.isEmpty() ? null : new org.eclipse.core.runtime.Path(workingDirectory);
}

public IPath getDefaultWorkingDirectory() throws CommonException, CoreException {
Expand All @@ -80,21 +74,25 @@ public IPath getDefaultWorkingDirectory() throws CommonException, CoreException
/* ----------------- ----------------- */

public String[] getProgramArguments() throws CoreException {
return DebugPlugin.parseArguments(settings.getProgramArguments_Attribute());
return DebugPlugin.parseArguments(programArguments);
}

/* ----------------- ----------------- */

public Map<String, String> getValidEnvironmentVars() throws CoreException {
return environmentVars;
}

/* ====================== ====================== */

public ProcessLaunchInfo getValidatedProcessLaunchInfo() throws CoreException, CommonException {
public ProcessLaunchInfo getValidProcessLaunchInfo() throws CommonException, CoreException {

IProject project = getProject();
BuildTarget buildTarget = getBuildTarget();
Location programLoc = getValidExecutableFileLocation();
String[] processArgs = getProgramArguments();
IPath workingDirectory = getWorkingDirectory();
Map<String, String> configEnv = settings.getEnvironmentVars();
boolean appendEnv = settings.getAppendEnvironmentVars();
Map<String, String> configEnv = getValidEnvironmentVars();

return new ProcessLaunchInfo(
project,
Expand All @@ -103,7 +101,7 @@ public ProcessLaunchInfo getValidatedProcessLaunchInfo() throws CoreException, C
processArgs,
workingDirectory,
configEnv,
appendEnv);
appendEnvironmentVars);
}

}

This file was deleted.

Expand Up @@ -12,18 +12,14 @@

import static melnorme.utilbox.core.Assert.AssertNamespace.assertNotNull;

import java.nio.file.Path;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;

import melnorme.lang.ide.core.LangCore;
import melnorme.lang.ide.core.launch.LaunchMessages;
import melnorme.lang.ide.core.operations.build.BuildManager.BuildConfiguration;
import melnorme.lang.ide.core.operations.build.BuildManager.BuildType;
import melnorme.lang.tooling.data.AbstractValidator2;
import melnorme.utilbox.core.CommonException;
import melnorme.utilbox.misc.PathUtil;

public class BuildTargetValidator extends AbstractValidator2 {

Expand Down Expand Up @@ -88,16 +84,4 @@ public String getArtifactPath() throws CommonException, CoreException {
return getBuildType().getArtifactPath(this);
}

public Path getValidArtifactPath(String artifactPathStr) throws CommonException {
if(artifactPathStr == null || artifactPathStr.isEmpty()) {
throw new CommonException(LaunchMessages.BuildTarget_NoArtifactPathSpecified);
}
return PathUtil.createPath(artifactPathStr);
}

public Path getValidArtifactPath3(String artifactPathOverride) throws CommonException, CoreException {
String effectiveArtifactPath = artifactPathOverride != null ? artifactPathOverride : getArtifactPath();
return getValidArtifactPath(effectiveArtifactPath);
}

}

0 comments on commit 0233ad7

Please sign in to comment.