Permalink
Browse files

shell - storing parsed representation of module descriptor (with no v…

…ariables/files interpolations).

This parsed module descriptor will be used when writing module descriptor (particularly during 'deps add'), in order to change as few stuff as possible.
  • Loading branch information...
fcamblor committed Aug 27, 2017
1 parent 07876e3 commit ef8517364f740def2207027ab0eff3d9d6f0734c
@@ -69,7 +69,7 @@ public ModuleDescriptor parse(InputStream stream) throws IOException {
}
return new ModuleDescriptor(parent, gav, packaging,
properties, Collections.<String>emptyList(), new HashMap<String,List<ModuleFragment>>(), dependencies);
properties, Collections.<String>emptyList(), new HashMap<String,List<ModuleFragment>>(), dependencies, null);
}
private GAV getGav(JSONObject jsonObject, String typeKey, GAV parentGAV) {
@@ -117,7 +117,7 @@ public void generate(ModuleDescriptor md, Writer w) throws IOException {
w.write(" <dependencies>\n");
for (String scope : md.getDependencyScopes()) {
for (ModuleDependency dependency : md.getDependencies(scope)) {
for (ModuleDependency dependency : md.getParsedModuleDescriptor().getDependencies(scope)) {
w.write(" <dependency>\n");
toMavenGAV(dependency.getGav(), " ", w);
if (!"compile".equals(scope)) {
@@ -160,7 +160,7 @@ public void generate(ModuleDescriptor md, Writer w) throws IOException {
private boolean isVersionPropertyUsed(ModuleDescriptor md, String property) {
for (String scope : md.getDependencyScopes()) {
for (ModuleDependency dependency : md.getDependencies(scope)) {
for (ModuleDependency dependency : md.getParsedModuleDescriptor().getDependencies(scope)) {
if (dependency.getGav().getVersion().indexOf("${" + property + "}") != -1) {
return true;
}
@@ -15,19 +15,22 @@
private final List<String> propertiesFileReferences;
private final Map<String, List<ModuleFragment>> fragments;
private final Map<String, List<ModuleDependency>> dependencies;
private final ModuleDescriptor parsedModuleDescriptor;
public ModuleDescriptor(GAV parent, GAV gav, String packaging,
Map<String, String> properties,
List<String> propertiesFileReferences,
Map<String, List<ModuleFragment>> fragments,
Map<String, List<ModuleDependency>> dependencies) {
Map<String, List<ModuleDependency>> dependencies,
ModuleDescriptor parsedModuleDescriptor) {
this.parent = parent;
this.gav = gav;
this.packaging = packaging;
this.propertiesFileReferences = propertiesFileReferences;
this.fragments = Collections.unmodifiableMap(fragments);
this.properties = Collections.unmodifiableMap(properties);
this.dependencies = Collections.unmodifiableMap(dependencies);
this.parsedModuleDescriptor = parsedModuleDescriptor;
}
public GAV getParent() {
@@ -72,7 +75,7 @@ public ModuleDescriptor concatDependency(String scope, ModuleDependency dep) {
}
newDeps.get(scope).add(dep);
return new ModuleDescriptor(parent, gav, packaging, properties, propertiesFileReferences, fragments, newDeps);
return new ModuleDescriptor(parent, gav, packaging, properties, propertiesFileReferences, fragments, newDeps, parsedModuleDescriptor);
}
public boolean hasClassifier() {
@@ -86,6 +89,10 @@ public boolean hasClassifier() {
return false;
}
public ModuleDescriptor getParsedModuleDescriptor() {
return parsedModuleDescriptor==null?this:parsedModuleDescriptor;
}
public List<String> getPropertiesFileReferences() {
return propertiesFileReferences;
}
@@ -117,23 +117,24 @@ private ModuleDescriptor parse(Path path, InputStream inputStream) throws IOExce
List<String> propertiesFileReferences = new ArrayList<>();
if (jsonObject.has("properties")) {
propertiesJSONObj = jsonObject.getJSONObject("properties");
loadJsonProperties(path == null ? null : path.getParent(), properties, propertiesJSONObj);
for(Object p: propertiesJSONObj.keySet()) {
String key = p.toString();
if("@files".equals(key)) {
JSONArray propertyFiles = propertiesJSONObj.getJSONArray(key);
for (int i = 0; i < propertyFiles.length(); i++) {
propertiesFileReferences.add(propertyFiles.getString(i));
}
} else {
properties.put(key, propertiesJSONObj.getString(key));
}
}
}
GAV parent = null;
if (jsonObject.has("parent")) {
parent = GAV.parse(expandProperties(properties, jsonObject.getString("parent")));
parent = GAV.parse(jsonObject.getString("parent"));
}
GAV gav = GAV.parse(expandProperties(properties, jsonObject.getString("module")));
GAV gav = GAV.parse(jsonObject.getString("module"));
String packaging = jsonObject.has("packaging") ? jsonObject.getString("packaging") : "jar";
@@ -161,15 +162,45 @@ private ModuleDescriptor parse(Path path, InputStream inputStream) throws IOExce
JSONArray array = jsonFragments.getJSONArray(type);
for (int i = 0; i < array.length(); i++) {
String url = expandProperties(properties, array.getString(i));
String url = array.getString(i);
fragmentsForType.add(new ModuleFragment(url));
}
fragments.put(type, fragmentsForType);
}
}
return new ModuleDescriptor(parent, gav, packaging, properties, fragments, dependencies);
ModuleDescriptor parsedModuleDescriptor = new ModuleDescriptor(parent, gav, packaging, properties, propertiesFileReferences, fragments, dependencies, null);
// Interpolating stuff in parsed module descriptor ...
Map<String, String> interpolatedProperties = new LinkedHashMap<>();
if(propertiesJSONObj != null) {
loadJsonProperties(path == null ? null : path.getParent(), interpolatedProperties, propertiesJSONObj);
}
GAV interpolatedParent = parent==null?null:GAV.parse(expandProperties(interpolatedProperties, parent.toParseableString()));
GAV interpolatedGAV = GAV.parse(expandProperties(interpolatedProperties, gav.toParseableString()));
Map<String, List<ModuleDependency>> interpolatedDependencies = new LinkedHashMap<>();
for(Map.Entry<String,List<ModuleDependency>> scopedDependenciesEntry: dependencies.entrySet()) {
List<ModuleDependency> scopedDependencies = new ArrayList<>();
for(ModuleDependency dep: scopedDependenciesEntry.getValue()) {
scopedDependencies.add(new ModuleDependency(GAV.parse(expandProperties(interpolatedProperties, dep.getGav().toParseableString()))));
}
interpolatedDependencies.put(scopedDependenciesEntry.getKey(), scopedDependencies);
}
Map<String, List<ModuleFragment>> interpolatedFragmentsPerModuleType = new LinkedHashMap<>();
for(Map.Entry<String,List<ModuleFragment>> perModuleTypeFragment: fragments.entrySet()) {
List<ModuleFragment> interpolatedFragments = new ArrayList<>();
for(ModuleFragment fragment: perModuleTypeFragment.getValue()) {
interpolatedFragments.add(new ModuleFragment(expandProperties(interpolatedProperties, fragment.getUrl())));
}
interpolatedFragmentsPerModuleType.put(perModuleTypeFragment.getKey(), interpolatedFragments);
}
return new ModuleDescriptor(interpolatedParent, interpolatedGAV, packaging, interpolatedProperties, propertiesFileReferences, interpolatedFragmentsPerModuleType, interpolatedDependencies, parsedModuleDescriptor);
}
private void loadJsonProperties(Path path, Map<String, String> properties, JSONObject props) throws IOException {
@@ -186,10 +217,10 @@ private void loadJsonProperties(Path path, Map<String, String> properties, JSONO
throw new IllegalArgumentException(
"can't resolve property file " + propertyFilePath.toAbsolutePath() + "." +
" Not found." +
(path == null ?
" Note that parsing from mere inputstream resolve files " +
"relative to current directory." :
""));
(path == null ?
" Note that parsing from mere inputstream resolve files " +
"relative to current directory." :
""));
}
try (InputStreamReader reader = new InputStreamReader(Files.newInputStream(propertyFilePath))) {
@@ -239,7 +239,7 @@ public void run(RestxShell shell) throws Exception {
ModuleDescriptor descriptor;
try (FileInputStream is = new FileInputStream(mdFile)) {
descriptor = new RestxJsonSupport().parse(is);
descriptor = new RestxJsonSupport().parse(is).getParsedModuleDescriptor();
for (String s : pluginIds.get()) {
descriptor = descriptor.concatDependency(scope, new ModuleDependency(GAV.parse(s)));

0 comments on commit ef85173

Please sign in to comment.