Skip to content

Commit

Permalink
Changes suggested in PR#5567
Browse files Browse the repository at this point in the history
  • Loading branch information
vieiro committed Mar 1, 2023
1 parent fc82932 commit 2d7a271
Show file tree
Hide file tree
Showing 56 changed files with 1,040 additions and 1,639 deletions.
9 changes: 9 additions & 0 deletions rust/rust.cargo/nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@
<specification-version>1.109</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.modules.projectuiapi.base</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<release-version>1</release-version>
<specification-version>1.106</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.modules.rust.project.api</code-name-base>
<build-prerequisite/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
OpenIDE-Module-Display-Category=Rust
OpenIDE-Module-Long-Description=\
rust.cargo contains all cargo-related details for Rust support.
OpenIDE-Module-Name=rust.cargo - Rust Cargo support
OpenIDE-Module-Name=Rust Cargo support
OpenIDE-Module-Short-Description=Cargo related stuff for Rust support.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public String getDisplayName() {

public String getDescription() {
return NbBundle.getMessage(CargoCommand.class, name() + "_DESC"); // NOI18N
// NOI18N
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,55 @@ public final class RustPackage {
private final SemVer semver;
private final String description;
private final boolean optional;
private final String git;
private final String branch;

public RustPackage(CargoTOML cargotoml, String name, String version) {
private RustPackage(CargoTOML cargotoml, String name, String version) {
this(cargotoml, name, version, false);
}

public RustPackage(CargoTOML cargotoml, String name, String version, Boolean optional) {
this(cargotoml, name, version, optional, null);
private RustPackage(CargoTOML cargotoml, String name, String version, Boolean optional) {
this(cargotoml, name, version, optional, null, null, null);
}

public RustPackage(CargoTOML cargotoml, String name, String version, String description) {
this(cargotoml, name, version, false, description);

private RustPackage(CargoTOML cargotoml, String name, String version, String description) {
this(cargotoml, name, version, false, description, null, null);
}
public RustPackage(CargoTOML cargotoml, String name, String version, Boolean optional, String description) {

private RustPackage(CargoTOML cargotoml, String name, String version, Boolean optional, String description, String git, String branch) {
this.cargotoml = cargotoml;
this.name = name;
this.version = version;
this.semver = new SemVer(version);
this.description = description;
this.optional = optional == null ? false : optional;
this.git = git;
this.branch = branch;
// TODO: We set a "SemVer" to "0.0.0" if this comes from git.
this.semver = version == null ? new SemVer("0.0.0") : new SemVer(version);
}

public static final RustPackage withNameAndVersion(CargoTOML cargotoml, String name, String version) {
return withNameAndVersion(cargotoml, name, version, false);
}

public static final RustPackage withNameAndVersion(CargoTOML cargotoml, String name, String version, Boolean optional) {
return new RustPackage(cargotoml, name, version, optional, null, null, null);
}

public static RustPackage withNameVersionAndDescription(CargoTOML cargotoml, String name, String version, String description) {
return new RustPackage(cargotoml, name, version, false, description, null, null);
}

public static final RustPackage withGit(CargoTOML cargotoml, String name, String git, String branch) {
return new RustPackage(cargotoml, name, null, false, null, git, branch);
}

public String getGit() {
return git;
}

public String getBranch() {
return branch;
}

public CargoTOML getCargotoml() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.netbeans.modules.rust.cargo.api.CargoTOML;
import org.netbeans.modules.rust.cargo.impl.nodes.RustProjectDependenciesNode;
import org.netbeans.modules.rust.cargo.impl.nodes.RustWorkspaceNode;
import org.openide.nodes.Node;

/**
Expand All @@ -39,4 +40,13 @@ public static final Node newCargoDependenciesNode(CargoTOML cargotoml) {
return new RustProjectDependenciesNode(cargotoml);
}

/**
* Returns a Node that shows the members of the workspace in a project.
* @param cargotoml The CargoTOML in question.
* @return The node containing the workspace members.
*/
public static Node newWorkspaceNode(CargoTOML cargotoml) {
return new RustWorkspaceNode(cargotoml);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.netbeans.api.extexecution.ExecutionDescriptor;
import org.netbeans.api.extexecution.ExecutionService;
Expand Down Expand Up @@ -86,6 +84,7 @@ public Process call() throws Exception {
File workingDirectory = FileUtil.toFile(cargotoml.getFileObject()).getParentFile();
pb.setWorkingDirectory(workingDirectory.getAbsolutePath());
pb.setRedirectErrorStream(false);
// TODO: Parametrize the "cargo" path
pb.setExecutable("cargo"); // NOI18N
pb.setArguments(arguments);

Expand Down Expand Up @@ -117,6 +116,7 @@ public Integer call() throws Exception {
String commandNames = Arrays.stream(commands).map(CargoCommand::getDisplayName).collect(Collectors.joining(",")); // NOI18N
String ioName = String.format("%s (%s)", projectName, commandNames); // NOI18N
InputOutput console = IOProvider.getDefault().getIO(ioName, false);
console.select();

ExecutionDescriptor ed = new ExecutionDescriptor()
.inputOutput(IOProvider.getDefault().getIO(ioName, false))
Expand Down Expand Up @@ -184,7 +184,8 @@ public List<RustPackage> call() throws Exception {
File workingDirectory = new File(System.getProperty("user.home")); // NOI18N
pb.setWorkingDirectory(workingDirectory.getAbsolutePath());
pb.setRedirectErrorStream(false);
pb.setExecutable("cargo");
// TODO: Parametrize the "cargo" path
pb.setExecutable("cargo"); // NOI18N
String[] arguments = {
"search", // NOI18N
text, // TODO: What happens with spaces?
Expand Down Expand Up @@ -244,7 +245,7 @@ static List<RustPackage> filterLines(CargoTOML cargotoml, List<String> lines) {
description = line.substring(i + 1);
description = description.replace("\n", "");

RustPackage rustPackage = new RustPackage(cargotoml, name, version, description);
RustPackage rustPackage = RustPackage.withNameVersionAndDescription(cargotoml, name, version, description);
packages.add(rustPackage);
}
return packages;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,28 @@ private static final List<RustPackage> getDependencies(CargoTOML cargotoml, Toml
Object value = declaredDependency.getValue();
if (value instanceof String) {
String stringValue = (String) value;
packages.add(new RustPackage(cargotoml, key, stringValue));
packages.add(RustPackage.withNameAndVersion(cargotoml, key, stringValue));
} else if (value instanceof TomlTable) {
String dependencyName = key.replaceAll("^dependencies\\.", ""); // NOI18N
TomlTable dependencyInfo = (TomlTable) value;
String version = dependencyInfo.getString("version"); // NOI18N
Boolean optional = dependencyInfo.getBoolean("optional"); // NOI18N
RustPackage rp = new RustPackage(cargotoml, dependencyName, version, optional);
packages.add(new RustPackage(cargotoml, dependencyName, version));
String git = dependencyInfo.getString("git"); // NOI18N
if (version != null) {
// Examples:
// [dependencies]
// some-crate = { version = "1.0", registry = "my-registry" }
Boolean optional = dependencyInfo.getBoolean("optional"); // NOI18N
RustPackage rp = RustPackage.withNameAndVersion(cargotoml, dependencyName, version, optional);
packages.add(rp);
} else if (git != null) {
// Examples:
// [dependencies]
// regex = { git = "https://github.com/rust-lang/regex" }
// regex = { git = "https://github.com/rust-lang/regex", branch = "next" }
String branch = dependencyInfo.getString("branch");
RustPackage rp = RustPackage.withGit(cargotoml, dependencyName, git, branch);
packages.add(rp);
}
} else {
// TODO: Add support for github dependencies and registry dependencies https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html
LOG.warning(String.format("Unrecognized cargo dev-dependency on file %s with key '%s', value '%s'",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,16 @@
"MINOR=Minor version",
"PATCH=Patch version",
"DESCRIPTION=Description",
"GIT=Git repository",
"BRANCH=Git repository branch",
"NAME_DESC=The name of this package",
"VERSION_DESC=The version of this package",
"MAJOR_DESC=The major version of this package",
"MINOR_DESC=The minor version of this package",
"PATCH_DESC=The patch version of this package",
"DESCRIPTION_DESC=The description of this package"
"DESCRIPTION_DESC=The description of this package",
"GIT_DESC=A git repository where this package is downloaded from",
"BRANCH_DESC=The branch used to fetch this package source from git",
})
public final class RustPackageNode extends AbstractNode {

Expand All @@ -59,7 +63,9 @@ public enum PROPS {
MAJOR,
MINOR,
PATCH,
DESCRIPTION;
DESCRIPTION,
GIT,
BRANCH;
}

private final RustPackage rustPackage;
Expand Down Expand Up @@ -101,6 +107,8 @@ protected Sheet createSheet() {
set.put(getMinorProperty());
set.put(getPatchProperty());
set.put(getDescriptionProperty());
set.put( getGitProperty());
set.put( getBranchProperty());

// Add the set of properties to the sheet
sheet.put(set);
Expand Down Expand Up @@ -132,6 +140,14 @@ public Property<String> getDescriptionProperty() {
return getStringProperty(PROPS.DESCRIPTION, RustPackage::getDescription);
}

public Property<String> getGitProperty() {
return getStringProperty(PROPS.GIT, RustPackage::getGit);
}

public Property<String> getBranchProperty() {
return getStringProperty(PROPS.BRANCH, RustPackage::getBranch);
}

private Property<String> getStringProperty(PROPS props, Function<RustPackage, String> value) {
Property<String> property = new PropertySupport.ReadOnly<String>(props.name(), String.class, I18N(props.name()), I18N(props.name() + "_DESC")) {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.awt.Image;
import javax.swing.Action;
import org.netbeans.modules.rust.cargo.api.CargoTOML;
import org.netbeans.modules.rust.cargo.impl.nodes.actions.dependencies.RustAddDependencyAction;
import org.netbeans.modules.rust.project.api.RustIconFactory;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
Expand All @@ -31,13 +32,34 @@
/**
* The "Dependencies" node in a Rust project.
*
* @author antonio
*/
public final class RustProjectDependenciesNode extends AbstractNode {

/**
* The kind of children in a "Dependencies" node in a Rust project.
*
* @see
* <a href="https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#specifying-dependencies>Specifying
* dependencies</a>
* @see
* <a href="https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#development-dependencies>Development
* dependencies.</a>
* @see
* <a href="https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#build-dependencies>Build
* dependencies</a>
*/
public enum DependencyType {
/**
* Normal Rust project runtime dependencies.
*/
DEPENDENCY,
/**
* Development time dependencies.
*/
DEV_DEPENDENCY,
/**
* Build time dependencies.
*/
BUILD_DEPENDENCY,
}

Expand Down Expand Up @@ -105,9 +127,18 @@ Image getOpenedIcon(int type) {
return RustIconFactory.getDependenciesFolderIcon(true);
}

/**
* Let users add dependencies, development dependencies and build dependencies
* from the main "Dependencies node".
* @param context
* @return
*/
@Override
public Action[] getActions(boolean context) {
return new Action[0];
return new Action[]{
new RustAddDependencyAction(cargotoml, DependencyType.DEPENDENCY),
new RustAddDependencyAction(cargotoml, DependencyType.DEV_DEPENDENCY),
new RustAddDependencyAction(cargotoml, DependencyType.BUILD_DEPENDENCY),};
}

}
Loading

0 comments on commit 2d7a271

Please sign in to comment.