Skip to content

Commit cb7e8c7

Browse files
committed
GH-228 - --source-config argument to setup source roots and source viewer using a configuration file
1 parent a485d1e commit cb7e8c7

File tree

7 files changed

+146
-41
lines changed

7 files changed

+146
-41
lines changed

plugins/sources/manifest.mf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ AutoUpdate-Show-In-Client: true
33
AutoUpdate-Essential-Module: false
44
OpenIDE-Module: org.graalvm.visualvm.sources/1
55
OpenIDE-Module-Localizing-Bundle: org/graalvm/visualvm/sources/resources/Bundle.properties
6-
OpenIDE-Module-Specification-Version: 1.0
6+
OpenIDE-Module-Specification-Version: 1.1
77

plugins/sources/src/META-INF/services/org.netbeans.spi.sendopts.OptionProcessor

Lines changed: 0 additions & 2 deletions
This file was deleted.

plugins/sources/src/org/graalvm/visualvm/sources/arguments/Bundle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@
2525

2626
Argument_SourceViewer_ShortDescr=set the source viewer command to be executed by Go To Source
2727
Argument_SourceRoots_ShortDescr=set the source roots to be searched by Go To Source
28+
Argument_SourceConfig_ShortDescr=set the path to sources configuration file containing source roots and viewer definition
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package org.graalvm.visualvm.sources.arguments;
26+
27+
import java.util.Collections;
28+
import java.util.Map;
29+
import java.util.Set;
30+
import org.netbeans.api.sendopts.CommandException;
31+
import org.netbeans.spi.sendopts.Env;
32+
import org.netbeans.spi.sendopts.Option;
33+
import org.netbeans.spi.sendopts.OptionGroups;
34+
import org.netbeans.spi.sendopts.OptionProcessor;
35+
import org.openide.util.lookup.ServiceProvider;
36+
37+
/**
38+
*
39+
* @author Jiri Sedlacek
40+
*/
41+
@ServiceProvider(service=OptionProcessor.class)
42+
public final class SourceArguments extends OptionProcessor {
43+
44+
@Override
45+
protected Set<Option> getOptions() {
46+
return Collections.singleton(OptionGroups.anyOf(SourceRootsArgument.ARGUMENT, SourceViewerArgument.ARGUMENT, SourceConfigArgument.ARGUMENT));
47+
}
48+
49+
@Override
50+
protected void process(Env env, Map<Option, String[]> maps) throws CommandException {
51+
String[] sourceRoots = maps.get(SourceRootsArgument.ARGUMENT);
52+
String[] sourceViewer = maps.get(SourceViewerArgument.ARGUMENT);
53+
String[] sourceConfig = maps.get(SourceConfigArgument.ARGUMENT);
54+
55+
if (sourceConfig != null) SourceConfigArgument.process(sourceConfig, sourceRoots, sourceViewer);
56+
if (sourceRoots != null) SourceRootsArgument.process(sourceRoots);
57+
if (sourceViewer != null) SourceViewerArgument.process(sourceViewer);
58+
}
59+
60+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package org.graalvm.visualvm.sources.arguments;
26+
27+
import java.io.FileInputStream;
28+
import java.io.IOException;
29+
import java.io.InputStreamReader;
30+
import java.util.Properties;
31+
import org.netbeans.api.sendopts.CommandException;
32+
import org.netbeans.spi.sendopts.Option;
33+
34+
/**
35+
* Implementation of the --source-config argument
36+
*
37+
* @author Jiri Sedlacek
38+
*/
39+
class SourceConfigArgument {
40+
41+
static final String LONG_NAME = "source-config"; // NOI18N
42+
43+
static final Option ARGUMENT = Option.shortDescription(Option.requiredArgument(Option.NO_SHORT_NAME, LONG_NAME), "org.graalvm.visualvm.sources.arguments.Bundle", "Argument_SourceConfig_ShortDescr"); // NOI18N
44+
45+
46+
static void process(String[] values, String[] rootsValues, String[] viewerValues) throws CommandException {
47+
if (rootsValues != null || viewerValues != null)
48+
throw new CommandException(0, "--" + LONG_NAME + // NOI18N
49+
" not allowed with --" + SourceRootsArgument.LONG_NAME + // NOI18N
50+
" or --" + SourceViewerArgument.LONG_NAME); // NOI18N
51+
52+
if (values.length == 1) setValue(values[0]);
53+
else throw new CommandException(0, "--" + LONG_NAME + " requires exactly one value"); // NOI18N
54+
}
55+
56+
57+
private static final void setValue(String value) throws CommandException {
58+
try (InputStreamReader isr = new InputStreamReader(new FileInputStream(value), "UTF-8")) { // NOI18N
59+
Properties props = new Properties();
60+
props.load(isr);
61+
62+
String sourceRoots = props.getProperty(SourceRootsArgument.LONG_NAME);
63+
if (sourceRoots != null) SourceRootsArgument.setValue(sourceRoots);
64+
65+
String sourceViewer = props.getProperty(SourceViewerArgument.LONG_NAME);
66+
if (sourceViewer != null) SourceViewerArgument.setValue(sourceViewer);
67+
} catch (IOException e) {
68+
throw new CommandException(0, "--" + LONG_NAME + " failed to read config " + value + ": " + e.getMessage()); // NOI18N
69+
}
70+
}
71+
72+
}

plugins/sources/src/org/graalvm/visualvm/sources/arguments/SourceRootsArgument.java

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,42 +25,29 @@
2525
package org.graalvm.visualvm.sources.arguments;
2626

2727
import java.io.File;
28-
import java.util.Collections;
29-
import java.util.Map;
30-
import java.util.Set;
3128
import org.graalvm.visualvm.sources.impl.SourceRoots;
3229
import org.netbeans.api.sendopts.CommandException;
33-
import org.netbeans.spi.sendopts.Env;
3430
import org.netbeans.spi.sendopts.Option;
35-
import org.netbeans.spi.sendopts.OptionProcessor;
3631

3732
/**
3833
* Implementation of the --source-roots argument
3934
*
4035
* @author Jiri Sedlacek
4136
*/
42-
public final class SourceRootsArgument extends OptionProcessor {
37+
final class SourceRootsArgument {
4338

44-
private static final String ARGUMENT_LONG_NAME = "source-roots"; // NOI18N
39+
static final String LONG_NAME = "source-roots"; // NOI18N
4540

41+
static final Option ARGUMENT = Option.shortDescription(Option.requiredArgument(Option.NO_SHORT_NAME, LONG_NAME), "org.graalvm.visualvm.sources.arguments.Bundle", "Argument_SourceRoots_ShortDescr"); // NOI18N
4642

47-
private final Option argument = Option.shortDescription(Option.requiredArgument(Option.NO_SHORT_NAME, ARGUMENT_LONG_NAME), "org.graalvm.visualvm.sources.arguments.Bundle", "Argument_SourceRoots_ShortDescr"); // NOI18N
48-
49-
50-
@Override
51-
protected Set<Option> getOptions() {
52-
return Collections.singleton(argument);
53-
}
5443

55-
@Override
56-
protected void process(Env env, Map<Option, String[]> maps) throws CommandException {
57-
String[] values = maps.get(argument);
44+
static void process(String[] values) throws CommandException {
5845
if (values.length == 1) setValue(values[0]);
59-
else throw new CommandException(0, "--" + ARGUMENT_LONG_NAME + " requires exactly one value"); // NOI18N
46+
else throw new CommandException(0, "--" + LONG_NAME + " requires exactly one value"); // NOI18N
6047
}
6148

6249

63-
private static final void setValue(String value) {
50+
static final void setValue(String value) {
6451
if (value != null) value = value.trim();
6552
if (value == null || value.isEmpty()) SourceRoots.forceRoots(null);
6653
else SourceRoots.forceRoots(value.split(File.pathSeparator));

plugins/sources/src/org/graalvm/visualvm/sources/arguments/SourceViewerArgument.java

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,42 +24,29 @@
2424
*/
2525
package org.graalvm.visualvm.sources.arguments;
2626

27-
import java.util.Collections;
28-
import java.util.Map;
29-
import java.util.Set;
3027
import org.graalvm.visualvm.sources.impl.SourceViewers;
3128
import org.netbeans.api.sendopts.CommandException;
32-
import org.netbeans.spi.sendopts.Env;
3329
import org.netbeans.spi.sendopts.Option;
34-
import org.netbeans.spi.sendopts.OptionProcessor;
3530

3631
/**
3732
* Implementation of the --external-viewer argument
3833
*
3934
* @author Jiri Sedlacek
4035
*/
41-
public final class SourceViewerArgument extends OptionProcessor {
36+
final class SourceViewerArgument {
4237

43-
private static final String ARGUMENT_LONG_NAME = "source-viewer"; // NOI18N
38+
static final String LONG_NAME = "source-viewer"; // NOI18N
4439

40+
static final Option ARGUMENT = Option.shortDescription(Option.requiredArgument(Option.NO_SHORT_NAME, LONG_NAME), "org.graalvm.visualvm.sources.arguments.Bundle", "Argument_SourceViewer_ShortDescr"); // NOI18N
4541

46-
private final Option argument = Option.shortDescription(Option.requiredArgument(Option.NO_SHORT_NAME, ARGUMENT_LONG_NAME), "org.graalvm.visualvm.sources.arguments.Bundle", "Argument_SourceViewer_ShortDescr"); // NOI18N
4742

48-
49-
@Override
50-
protected Set<Option> getOptions() {
51-
return Collections.singleton(argument);
52-
}
53-
54-
@Override
55-
protected void process(Env env, Map<Option, String[]> maps) throws CommandException {
56-
String[] values = maps.get(argument);
43+
static void process(String[] values) throws CommandException {
5744
if (values.length == 1) setValue(values[0]);
58-
else throw new CommandException(0, "--" + ARGUMENT_LONG_NAME + " requires exactly one value"); // NOI18N
45+
else throw new CommandException(0, "--" + LONG_NAME + " requires exactly one value"); // NOI18N
5946
}
6047

6148

62-
private static final void setValue(String value) {
49+
static final void setValue(String value) {
6350
if (value != null) value = value.trim();
6451
SourceViewers.forceExternalViewer(value);
6552
}

0 commit comments

Comments
 (0)