Permalink
Browse files

dist dependency to check that document properties

  • Loading branch information...
1 parent a317894 commit 4e85abe331e09768dc12281086577b3e50b5aa01 @jhinkey jhinkey committed Apr 13, 2012
@@ -0,0 +1,7 @@
+##
+## Markdown Metadata
+##
+
+ product.abbrev=lp
+ product.version=6.1
+ doc.abbrev=qs
View
@@ -44,6 +44,17 @@ directory to convert the userGuide into complete English markdown, html, odt, an
<!-- taskdefs -->
+ <taskdef name="checkdocprops" classname="com.liferay.documentation.util.CheckDocPropsTask">
+ <classpath>
+ <fileset dir="${project.dir}/lib" includes="*.jar" />
+ <!-- Use below instead of above fileset when testing changes to classes
+ <pathelement path="${project.dir}/code/liferay-doc-utils/classes"/>
+ -->
+
+ <fileset dir="${project.dir}/code/liferay-doc-utils/lib" includes="*.jar" />
+ </classpath>
+ </taskdef>
+
<taskdef name="numberheaders" classname="com.liferay.documentation.util.NumberHeadersTask">
<classpath>
<fileset dir="${project.dir}/lib" includes="*.jar" />
@@ -78,6 +89,11 @@ directory to convert the userGuide into complete English markdown, html, odt, an
<echo message="pandoc.app -> ${pandoc.app}"/>
</target>
+ <target name="check-doc-props" description="checks that all document dirs have a valid doc.properties file">
+ <checkdocprops lang="${lang}" basedir="${project.dir}">
+ </checkdocprops>
+ </target>
+
<target name="clean-all" description="deletes residual directories">
<echo message="... deleting build directory"/>
<delete dir="${build.dir}"/>
@@ -284,7 +300,7 @@ directory to convert the userGuide into complete English markdown, html, odt, an
<target name="markdown-to-all-win" depends="markdown-to-html-win,markdown-to-odt-win,markdown-to-epub-win" description="converts markdown file to html, odt, and epub files on windows" />
- <target name="number-headers-dir" description="numbers the headers of markdown files found in ${lang}/chapters/">
+ <target name="number-headers-dir" depends="check-doc-props" description="numbers the headers of markdown files found in ${lang}/chapters/">
<numberheaders lang="${lang}" docDir="${doc.dir}">
</numberheaders>
</target>
@@ -0,0 +1,167 @@
+package com.liferay.documentation.util;
+
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Properties;
+import java.util.Set;
+import java.util.TreeMap;
+import java.util.TreeSet;
+
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.Task;
+
+public class CheckDocPropsTask extends Task {
+
+ @Override
+ public void execute() throws BuildException {
+ File basedirFile = new File(_basedir);
+
+ File[] basedirFiles = basedirFile.listFiles();
+
+ // Build a list of files in the base directory
+
+ List<File> dirs = new ArrayList<File>();
+ for (File file : basedirFiles) {
+ if (file.isDirectory()) {
+ dirs.add(file);
+ }
+ }
+
+
+ // Start a product / document
+
+ Set <String> prodDocSet = new TreeSet<String>();
+
+ // Keep track of invalid doc directories
+
+ Map<String, String> docDirInvalidations = new TreeMap<String, String>();
+
+ for (File dir : dirs) {
+ File[] files = dir.listFiles();
+
+ File langDir = null;
+ for (File file : files) {
+ if (file.isDirectory() && file.getName().equals(_lang)) {
+ langDir = file;
+ break;
+ }
+ }
+
+ File docPropsFile = null;
+ for (File file : files) {
+ if (file.getName().equals(DOC_PROPERTIES)) {
+ docPropsFile = file;
+ break;
+ }
+ }
+
+ if (docPropsFile == null) {
+ if (langDir == null) {
+ continue;
+ }
+ else {
+ docDirInvalidations.put(dir.getName(), " missing file " +
+ DOC_PROPERTIES);
+ continue;
+ }
+ }
+
+ // Load properties file
+
+ Properties props = new Properties();
+ try {
+ props.load(new FileReader(docPropsFile));
+ }
+ catch (IOException io) {
+ throw new BuildException(io);
+ }
+
+ // Check for missing properties
+
+ List<String> missingProps = new ArrayList<String>();
+ checkProperty(missingProps, props, PRODUCT_ABBREV);
+ checkProperty(missingProps, props, PRODUCT_VERSION);
+ checkProperty(missingProps, props, DOC_ABBREV);
+
+ String prod = props.getProperty(PRODUCT_ABBREV);
+ String doc = props.getProperty(DOC_ABBREV);
+ if (prod != null && doc != null) {
+ String prodDoc = prod + " " + doc;
+ if (prodDocSet.contains(prodDoc)) {
+ StringBuffer sb = new StringBuffer();
+ sb.append("Prod and doc property combo ");
+ sb.append(prodDoc);
+ sb.append(" for ");
+ sb.append(dir.getName());
+ sb.append("/");
+ sb.append(DOC_PROPERTIES);
+ sb.append(" conflicts with that of another doc dir's ");
+ sb.append(DOC_PROPERTIES);
+ sb.append(". Resolve by deconflicting the doc.abbrev property of the offending doc dirs.");
+ throw new BuildException(sb.toString());
+ }
+ else {
+ prodDocSet.add(prodDoc);
+ }
+ }
+
+ // Compile message of missing properties for the current doc directory
+
+ if (!missingProps.isEmpty()) {
+ StringBuffer errorMsg = new StringBuffer();
+ for (String prop : missingProps) {
+ errorMsg.append(prop);
+ errorMsg.append(" ");
+ }
+
+ docDirInvalidations.put(dir.getName(), errorMsg.toString());
+ }
+ }
+
+ if (!docDirInvalidations.isEmpty()) {
+
+ // Assemble error message
+
+ StringBuffer invalidations = new StringBuffer();
+ invalidations.append("Invalid doc directories: \n");
+ for (Entry<String, String> entry : docDirInvalidations.entrySet()) {
+ invalidations.append("\t");
+ invalidations.append(entry.getKey());
+ invalidations.append("/doc.properties missing: ");
+ invalidations.append(entry.getValue());
+ invalidations.append("\n");
+ }
+
+ throw new BuildException(invalidations.toString());
+ }
+ }
+
+ private void checkProperty(List<String> missingProps,
+ Properties props, String propName) {
+ if (props.getProperty(propName) == null) {
+ missingProps.add(propName);
+ }
+ }
+
+ public void setLang(String lang) {
+ _lang = lang;
+ }
+
+ public void setBasedir(String basedir) {
+ _basedir = basedir;
+ }
+
+ private static final String DOC_PROPERTIES = "doc.properties";
+
+ private static final String DOC_ABBREV = "doc.abbrev";
+ private static final String PRODUCT_ABBREV = "product.abbrev";
+ private static final String PRODUCT_VERSION = "product.version";
+
+ private String _basedir;
+ private String _lang;
+}
@@ -8,6 +8,7 @@
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.HashMap;
+import java.util.Properties;
import java.util.regex.Pattern;
import org.apache.commons.io.FileUtils;
@@ -42,6 +43,18 @@ public boolean accept(File file, String name) {
throw new BuildException("FAILURE - no markdown files found in " + chDir);
}
+ File docPropsFile = new File("../" + docDir + "/" + DOC_PROPERTIES);
+
+ // Load properties file
+
+ Properties props = new Properties();
+ try {
+ props.load(new FileReader(docPropsFile));
+ }
+ catch (IOException io) {
+ throw new BuildException(io);
+ }
+
// Process each file
for (int i = 0; i < files.length; i++) {
@@ -59,7 +72,8 @@ public boolean accept(File file, String name) {
if (line.startsWith("#")) {
- String newHeadingLine = handleHeaderLine(line, filename, in.getLineNumber());
+ String newHeadingLine = handleHeaderLine(line, filename,
+ in.getLineNumber(), props);
if (newHeadingLine != null) {
line = newHeadingLine;
}
@@ -122,7 +136,7 @@ private String extractChapterNumber(String fileName) {
return chapter;
}
- private String handleHeaderLine(String line, String filename, int lineNum) {
+ private String handleHeaderLine(String line, String filename, int lineNum, Properties props) {
String newHeadingLine = null;
// Check if the header contains an ID
@@ -174,56 +188,71 @@ private String handleHeaderLine(String line, String filename, int lineNum) {
int headingLen = heading.length();
int idCount = 0;
- String newHeading2 = null;
- StringBuffer headingSb;
+ String newHeading = null;
while (true) {
- headingSb = new StringBuffer(heading);
- headingSb.append("-");
- headingSb.append(chapter);
- headingSb.append("-");
- headingSb.append(idCount);
+
+ newHeading = assembleId(heading, props, chapter, idCount);
- int lenDiff = headingSb.length() - MAX_ID_LEN;
+ int lenDiff = newHeading.length() - MAX_ID_LEN;
if (lenDiff > 0) {
// Trim heading
heading = heading.substring(0, headingLen - lenDiff);
}
- headingSb = new StringBuffer(heading);
- headingSb.append("-");
- headingSb.append(chapter);
- headingSb.append("-");
- headingSb.append(idCount);
+ newHeading = assembleId(heading, props, chapter, idCount);
- if (IDS.get(headingSb.toString()) == null) {
+ if (IDS.get(newHeading) == null) {
// Heading is unique
// Add the ID
- IDS.put(headingSb.toString(), filename);
+ IDS.put(newHeading, filename);
- newHeading2 = headingSb.toString();
break;
}
idCount++;
}
- newHeadingLine = line + " [](id=" + newHeading2 + ")";
+ newHeadingLine = line + " [](id=" + newHeading + ")";
}
return newHeadingLine;
}
+ private String assembleId(String heading, Properties props, String chapter,
+ int idCount) {
+ StringBuffer headingSb = new StringBuffer();
+ headingSb.append(props.getProperty(PRODUCT_ABBREV));
+ headingSb.append("-");
+ headingSb.append(props.getProperty(PRODUCT_VERSION).replace('.', '-'));
+ headingSb.append("-");
+ headingSb.append(props.getProperty(DOC_ABBREV));
+ headingSb.append(lang);
+ headingSb.append(chapter);
+ headingSb.append("-");
+ headingSb.append(heading);
+ headingSb.append("-");
+ headingSb.append(idCount);
+ return headingSb.toString();
+ }
+
public void setLang(String lang) {
this.lang = lang;
}
+
public void setDocDir(String docDir) {
this.docDir = docDir;
}
+ private static final String DOC_PROPERTIES = "doc.properties";
+
private static final int MAX_ID_LEN = 75;
+ private static final String DOC_ABBREV = "doc.abbrev";
+ private static final String PRODUCT_ABBREV = "product.abbrev";
+ private static final String PRODUCT_VERSION = "product.version";
+
private static HashMap<String, String> IDS = new HashMap<String, String>();
private static Pattern headerIdPattern;
View
@@ -0,0 +1,7 @@
+##
+## Markdown Metadata
+##
+
+ product.abbrev=lp
+ product.version=6.1
+ doc.abbrev=dg
View
Binary file not shown.
View
@@ -0,0 +1,7 @@
+##
+## Markdown Metadata
+##
+
+ product.abbrev=lp
+ product.version=2.0
+ doc.abbrev=so
View
@@ -0,0 +1,7 @@
+##
+## Markdown Metadata
+##
+
+ product.abbrev=lp
+ product.version=6.1
+ doc.abbrev=ug

0 comments on commit 4e85abe

Please sign in to comment.