Skip to content

Commit

Permalink
First cut of mvn goal to generate/update component readme.md file. Sw…
Browse files Browse the repository at this point in the history
…itch to mvel which is easier to use than freemarker.
  • Loading branch information
davsclaus committed Jan 26, 2016
1 parent 4b335a1 commit b3c02c4
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 49 deletions.
Expand Up @@ -112,22 +112,28 @@ private static String decodeJson(String value) {
return value; return value;
} }


public static String getValue(String key, List<Map<String, String>> rows) { /**
* Gets the value with the key in a safe way, eg returning an empty string if there was no value for the key.
*/
public static String getSafeValue(String key, List<Map<String, String>> rows) {
for (Map<String, String> row : rows) { for (Map<String, String> row : rows) {
String value = row.get(key); String value = row.get(key);
if (value != null) { if (value != null) {
return value; return value;
} }
} }
return null; return "";
} }


public static String getValue(String key, Map<String, String> rows) { /**
* Gets the value with the key in a safe way, eg returning an empty string if there was no value for the key.
*/
public static String getSafeValue(String key, Map<String, String> rows) {
String value = rows.get(key); String value = rows.get(key);
if (value != null) { if (value != null) {
return value; return value;
} }
return null; return "";
} }


} }
Expand Up @@ -5,9 +5,9 @@
* The ASF licenses this file to You under the Apache License, Version 2.0 * The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with * (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at * the License. You may obtain a copy of the License at
* <p/> *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* <p/> *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -28,19 +28,21 @@


import org.apache.camel.maven.packaging.model.ComponentModel; import org.apache.camel.maven.packaging.model.ComponentModel;
import org.apache.camel.maven.packaging.model.ComponentOptionModel; import org.apache.camel.maven.packaging.model.ComponentOptionModel;
import org.apache.camel.maven.packaging.model.EndpointOptionModel;
import org.apache.maven.model.Resource; import org.apache.maven.model.Resource;
import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProject;

import org.mvel2.templates.TemplateRuntime; import org.mvel2.templates.TemplateRuntime;
import org.sonatype.plexus.build.incremental.BuildContext; import org.sonatype.plexus.build.incremental.BuildContext;


import static org.apache.camel.maven.packaging.JSonSchemaHelper.getValue; import static org.apache.camel.maven.packaging.JSonSchemaHelper.getSafeValue;
import static org.apache.camel.maven.packaging.PackageHelper.loadText; import static org.apache.camel.maven.packaging.PackageHelper.loadText;


/** /**
* Generate or updates the component readme.md file in the project root directort. * Generate or updates the component readme.md file in the project root directory.
* *
* @goal update-readme * @goal update-readme
*/ */
Expand Down Expand Up @@ -90,8 +92,10 @@ public void execute() throws MojoExecutionException, MojoFailureException {
ComponentModel model = generateComponentModel(componentName, json); ComponentModel model = generateComponentModel(componentName, json);
String header = templateComponentHeader(model); String header = templateComponentHeader(model);
String options = templateComponentOptions(model); String options = templateComponentOptions(model);
String options2 = templateEndpointOptions(model);
getLog().info(header); getLog().info(header);
getLog().info(options); getLog().info(options);
getLog().info(options2);
} }
} }
} }
Expand All @@ -118,33 +122,52 @@ private ComponentModel generateComponentModel(String componentName, String json)
List<Map<String, String>> rows = JSonSchemaHelper.parseJsonSchema("component", json, false); List<Map<String, String>> rows = JSonSchemaHelper.parseJsonSchema("component", json, false);


ComponentModel component = new ComponentModel(); ComponentModel component = new ComponentModel();
component.setScheme(getValue("scheme", rows)); component.setScheme(JSonSchemaHelper.getSafeValue("scheme", rows));
component.setSyntax(getValue("syntax", rows)); component.setSyntax(JSonSchemaHelper.getSafeValue("syntax", rows));
component.setTitle(getValue("title", rows)); component.setTitle(JSonSchemaHelper.getSafeValue("title", rows));
component.setDescription(getValue("description", rows)); component.setDescription(JSonSchemaHelper.getSafeValue("description", rows));
component.setLabel(getValue("label", rows)); component.setLabel(JSonSchemaHelper.getSafeValue("label", rows));
component.setDeprecated(getValue("deprecated", rows)); component.setDeprecated(JSonSchemaHelper.getSafeValue("deprecated", rows));
component.setConsumerOnly(getValue("consumerOnly", rows)); component.setConsumerOnly(JSonSchemaHelper.getSafeValue("consumerOnly", rows));
component.setProducerOnly(getValue("producerOnly", rows)); component.setProducerOnly(JSonSchemaHelper.getSafeValue("producerOnly", rows));
component.setJavaType(getValue("javaType", rows)); component.setJavaType(JSonSchemaHelper.getSafeValue("javaType", rows));
component.setGroupId(getValue("groupId", rows)); component.setGroupId(JSonSchemaHelper.getSafeValue("groupId", rows));
component.setArtifactId(getValue("artifactId", rows)); component.setArtifactId(JSonSchemaHelper.getSafeValue("artifactId", rows));
component.setVersion(getValue("version", rows)); component.setVersion(JSonSchemaHelper.getSafeValue("version", rows));


rows = JSonSchemaHelper.parseJsonSchema("componentProperties", json, true); rows = JSonSchemaHelper.parseJsonSchema("componentProperties", json, true);

List<ComponentOptionModel> componentOptions = new ArrayList<ComponentOptionModel>();
List<ComponentOptionModel> options = new ArrayList<ComponentOptionModel>();
for (Map<String, String> row : rows) { for (Map<String, String> row : rows) {
ComponentOptionModel option = new ComponentOptionModel(); ComponentOptionModel option = new ComponentOptionModel();
option.setKey(getValue("name", row)); option.setName(getSafeValue("name", row));
option.setKind(getValue("kind", row)); option.setKind(getSafeValue("kind", row));
option.setType(getValue("type", row)); option.setType(getSafeValue("type", row));
option.setJavaType(getValue("javaType", row)); option.setJavaType(getSafeValue("javaType", row));
option.setDeprecated(getValue("javaType", row)); option.setDeprecated(getSafeValue("deprecated", row));
option.setDescription(getValue("description", row)); option.setDescription(getSafeValue("description", row));
options.add(option); componentOptions.add(option);
} }
component.setOptions(options); component.setComponentOptions(componentOptions);

rows = JSonSchemaHelper.parseJsonSchema("properties", json, true);
List<EndpointOptionModel> endpointOptions = new ArrayList<EndpointOptionModel>();
for (Map<String, String> row : rows) {
EndpointOptionModel option = new EndpointOptionModel();
option.setName(getSafeValue("name", row));
option.setKind(getSafeValue("kind", row));
option.setGroup(getSafeValue("group", row));
option.setRequired(getSafeValue("required", row));
option.setType(getSafeValue("type", row));
option.setJavaType(getSafeValue("javaType", row));
option.setEnums(getSafeValue("enum", row));
option.setPrefix(getSafeValue("prefix", row));
option.setMultiValue(getSafeValue("multiValue", row));
option.setDeprecated(getSafeValue("deprecated", row));
option.setDefaultValue(getSafeValue("defaultValue", row));
option.setDescription(getSafeValue("description", row));
endpointOptions.add(option);
}
component.setEndpointOptions(endpointOptions);


return component; return component;
} }
Expand All @@ -169,6 +192,16 @@ private String templateComponentOptions(ComponentModel model) throws MojoExecuti
} }
} }


private String templateEndpointOptions(ComponentModel model) throws MojoExecutionException {
try {
String template = loadText(ReadmeComponentMojo.class.getClassLoader().getResourceAsStream("endpoint-options.mvel"));
String out = (String) TemplateRuntime.eval(template, model);
return out;
} catch (Exception e) {
throw new MojoExecutionException("Error processing mvel template. Reason: " + e, e);
}
}

private List<String> findComponentNames() { private List<String> findComponentNames() {
List<String> componentNames = new ArrayList<String>(); List<String> componentNames = new ArrayList<String>();
for (Resource r : project.getBuild().getResources()) { for (Resource r : project.getBuild().getResources()) {
Expand Down
Expand Up @@ -5,9 +5,9 @@
* The ASF licenses this file to You under the Apache License, Version 2.0 * The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with * (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at * the License. You may obtain a copy of the License at
* <p/> *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* <p/> *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -33,7 +33,8 @@ public class ComponentModel {
private String groupId; private String groupId;
private String artifactId; private String artifactId;
private String version; private String version;
private List<ComponentOptionModel> options; private List<ComponentOptionModel> componentOptions;
private List<EndpointOptionModel> endpointOptions;


public String getKind() { public String getKind() {
return kind; return kind;
Expand Down Expand Up @@ -139,11 +140,19 @@ public void setVersion(String version) {
this.version = version; this.version = version;
} }


public List<ComponentOptionModel> getOptions() { public List<ComponentOptionModel> getComponentOptions() {
return options; return componentOptions;
} }


public void setOptions(List<ComponentOptionModel> options) { public void setComponentOptions(List<ComponentOptionModel> componentOptions) {
this.options = options; this.componentOptions = componentOptions;
}

public List<EndpointOptionModel> getEndpointOptions() {
return endpointOptions;
}

public void setEndpointOptions(List<EndpointOptionModel> endpointOptions) {
this.endpointOptions = endpointOptions;
} }
} }
Expand Up @@ -5,9 +5,9 @@
* The ASF licenses this file to You under the Apache License, Version 2.0 * The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with * (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at * the License. You may obtain a copy of the License at
* <p/> *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* <p/> *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -18,19 +18,19 @@


public class ComponentOptionModel { public class ComponentOptionModel {


private String key; private String name;
private String kind; private String kind;
private String type; private String type;
private String javaType; private String javaType;
private String deprecated; private String deprecated;
private String description; private String description;


public String getKey() { public String getName() {
return key; return name;
} }


public void setKey(String key) { public void setName(String name) {
this.key = key; this.name = name;
} }


public String getKind() { public String getKind() {
Expand Down
@@ -0,0 +1,129 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.maven.packaging.model;

public class EndpointOptionModel {

private String name;
private String kind;
private String group;
private String required;
private String type;
private String javaType;
private String enums;
private String prefix;
private String multiValue;
private String deprecated;
private String defaultValue;
private String description;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getKind() {
return kind;
}

public void setKind(String kind) {
this.kind = kind;
}

public String getGroup() {
return group;
}

public void setGroup(String group) {
this.group = group;
}

public String getRequired() {
return required;
}

public void setRequired(String required) {
this.required = required;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getJavaType() {
return javaType;
}

public void setJavaType(String javaType) {
this.javaType = javaType;
}

public String getEnums() {
return enums;
}

public void setEnums(String enums) {
this.enums = enums;
}

public String getPrefix() {
return prefix;
}

public void setPrefix(String prefix) {
this.prefix = prefix;
}

public String getMultiValue() {
return multiValue;
}

public void setMultiValue(String multiValue) {
this.multiValue = multiValue;
}

public String getDeprecated() {
return deprecated;
}

public void setDeprecated(String deprecated) {
this.deprecated = deprecated;
}

public String getDefaultValue() {
return defaultValue;
}

public void setDefaultValue(String defaultValue) {
this.defaultValue = defaultValue;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}
}
@@ -1,8 +1,8 @@
### Component options ### Component options


The @{title} component supports @{options.size()} options which are listed below: The @{title} component supports @{componentOptions.size()} options which are listed below.


| Key | Description | | Key | Description |
| --- | ----------- | | --- | ----------- |
@foreach{row : options}| @{row.key} | @{row.description} | @foreach{row : componentOptions}| @{row.name} | @{row.description} |
@end{} @end{}

0 comments on commit b3c02c4

Please sign in to comment.