Skip to content

Commit

Permalink
basic tools validation code
Browse files Browse the repository at this point in the history
  • Loading branch information
alirana01 committed Sep 14, 2023
1 parent 05da2b9 commit 389bf4f
Show file tree
Hide file tree
Showing 12 changed files with 299 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

public class DefaultSystemWrapper implements SystemWrapper
{
private static final String PATH = "PATH"; //$NON-NLS-1$
private static final String PATHEXT = "PATHEXT"; //$NON-NLS-1$

@Override
public String getPathEnv()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

public interface SystemWrapper
{
String PATH = "PATH"; //$NON-NLS-1$
String PATHEXT = "PATHEXT"; //$NON-NLS-1$

public String getPathEnv();
public String getEnvExecutables();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*******************************************************************************
* Copyright 2023 Espressif Systems (Shanghai) PTE LTD. All rights reserved.
* Use is subject to license terms.
*******************************************************************************/
package com.espressif.idf.core.tools;

import com.espressif.idf.core.SystemWrapper;

/**
* Tools System wrapper to make sure to avoid the
* system path when verifying for validation after tools installation
* @author Ali Azam Rana
*
*/
public class ToolsSystemWrapper implements SystemWrapper
{
private String path;

public ToolsSystemWrapper(String path)
{
this.path = path;
}

@Override
public String getPathEnv()
{
return path;
}

@Override
public String getEnvExecutables()
{
return System.getenv(PATHEXT);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Platform;
import org.tukaani.xz.XZInputStream;

import com.espressif.idf.core.IDFEnvironmentVariables;
import com.espressif.idf.core.SystemExecutableFinder;
import com.espressif.idf.core.logging.Logger;
import com.espressif.idf.core.tools.ToolsSystemWrapper;
import com.espressif.idf.core.tools.vo.ToolsVO;
import com.espressif.idf.core.util.FileUtil;
import com.espressif.idf.core.util.StringUtil;

/**
* Utility class for Tools Management operations
Expand Down Expand Up @@ -303,4 +307,25 @@ public static String getFileChecksum(MessageDigest digest, File file) throws IOE
}
return sb.toString();
}

/**
* Gets the absolute path for the tool from the given path
* @param toolName tool to find absolute path
* @param path the path to variable to look into, if null System.getenv() will be used
* @return absolute path to the tool
*/
public static IPath findAbsoluteToolPath(String toolName, String path)
{
if (StringUtil.isEmpty(path))
{
Map<String, String> env = System.getenv();
if (env.containsKey(IDFEnvironmentVariables.PATH))
path = env.get(IDFEnvironmentVariables.PATH);
else
path = env.get("Path"); //$NON-NLS-1$
}

SystemExecutableFinder systemExecutableFinder = new SystemExecutableFinder(new ToolsSystemWrapper(path));
return systemExecutableFinder.find(toolName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.eclipse.core.runtime.Platform;

Expand Down Expand Up @@ -226,4 +227,33 @@ public void setVersionRegex(String versionRegex)
{
this.versionRegex = versionRegex;
}

@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null || getClass() != obj.getClass())
return false;
ToolsVO other = (ToolsVO) obj;
return Objects.equals(description, other.description)
&& Objects.equals(exportPaths, other.exportPaths)
&& Objects.equals(exportVars, other.exportVars)
&& Objects.equals(infoUrl, other.infoUrl)
&& Objects.equals(installType, other.installType)
&& Objects.equals(licesnse, other.licesnse)
&& Objects.equals(name, other.name)
&& Objects.equals(supportedTargets, other.supportedTargets)
&& Objects.equals(versionCmd, other.versionCmd)
&& Objects.equals(versionRegex, other.versionRegex)
&& Objects.equals(versionVOs, other.versionVOs)
&& Objects.equals(version, other.version)
&& installed == other.installed;
}

@Override
public int hashCode()
{
return Objects.hash(description, exportPaths, exportVars, infoUrl, installType, licesnse, name, supportedTargets, versionCmd, versionRegex, versionVOs, version, installed);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package com.espressif.idf.core.tools.vo;

import java.text.DecimalFormat;
import java.util.Objects;

/**
* Version details vo for the versions class
Expand Down Expand Up @@ -70,4 +71,24 @@ public void setSelected(boolean selected)
{
this.selected = selected;
}

@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null || getClass() != obj.getClass())
return false;
VersionDetailsVO other = (VersionDetailsVO) obj;
return Double.compare(size, other.size) == 0
&& selected == other.selected
&& Objects.equals(sha256, other.sha256)
&& Objects.equals(url, other.url);
}

@Override
public int hashCode()
{
return Objects.hash(sha256, size, url, selected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package com.espressif.idf.core.tools.vo;

import java.util.Map;
import java.util.Objects;

/**
* Versions class for versions information in tools json
Expand Down Expand Up @@ -73,4 +74,25 @@ public void setAvailablePath(String availablePath)
{
this.availablePath = availablePath;
}

@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null || getClass() != obj.getClass())
return false;
VersionsVO other = (VersionsVO) obj;
return isAvailable == other.isAvailable
&& Objects.equals(name, other.name)
&& Objects.equals(status, other.status)
&& Objects.equals(versionOsMap, other.versionOsMap)
&& Objects.equals(availablePath, other.availablePath);
}

@Override
public int hashCode()
{
return Objects.hash(name, status, versionOsMap, isAvailable, availablePath);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*******************************************************************************
* Copyright 2023 Espressif Systems (Shanghai) PTE LTD. All rights reserved.
* Use is subject to license terms.
*******************************************************************************/
package com.espressif.idf.ui.dialogs;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Shell;

import com.espressif.idf.core.logging.Logger;

/**
* URL Dialog class that can show multiple external web URLs in the text and
* make them click able and launches the default program to open them
* @author Ali Azam Rana
*
*/
public class URLDialog extends Dialog
{
private static final String URL_REGEX = "\\b((http|https)://)?[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b([-a-zA-Z0-9()@:%_\\+.~#?&//=]*)"; //$NON-NLS-1$

private String text;
private String title;

public URLDialog(Shell parentShell, String title, String text)
{
super(parentShell);
this.text = text;
this.title = title;
}

@Override
protected Control createDialogArea(Composite parent)
{
Composite container = new Composite((Composite) super.createDialogArea(parent), SWT.NONE);
container.setLayout(new GridLayout());
Link link = new Link(container, SWT.NONE);
link.setText(getTextForLinkControl());
link.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, true));
link.addListener(SWT.Selection, e -> Program.launch(e.text));
return container;
}

@Override
protected void configureShell(Shell newShell)
{
super.configureShell(newShell);
newShell.setText(title);
}

private String getTextForLinkControl()
{
StringBuilder stringBuilder = new StringBuilder();
Pattern pattern = Pattern.compile(URL_REGEX);
Matcher matcher = pattern.matcher(text);
int startIndex = 0, endIndex = text.length();
while (matcher.find())
{
String url = matcher.group();
Logger.log("URL Found: " + url);
endIndex = text.indexOf(url);
stringBuilder.append(text.substring(startIndex, endIndex));
stringBuilder.append("<a>"); //$NON-NLS-1$
stringBuilder.append(url);
stringBuilder.append("</a>"); //$NON-NLS-1$
startIndex = endIndex + url.length();
endIndex = text.length();
}
if (stringBuilder.toString().isEmpty())
{
stringBuilder.append(text);
}
return stringBuilder.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ public class Messages extends NLS
public static String FilterTargetBoxToolTip;
public static String ShowAvailableVersionsOnlyToolTip;
public static String DeleteToolsTextToolTip;
public static String MissingToolsValidationMessage_A;
public static String MissingToolsValidationMessage_B;
public static String MissingToolsValidationLink;

static
{
Expand Down
Loading

0 comments on commit 389bf4f

Please sign in to comment.