@@ -0,0 +1,16 @@ | |||||
package org.cobbzilla.util.main; | |||||
import lombok.Getter; | |||||
import lombok.Setter; | |||||
import java.util.regex.Pattern; | |||||
public class FileHeader { | |||||
@Getter @Setter private String ext; | |||||
@Getter @Setter private String header; | |||||
@Getter @Setter private String regex; | |||||
@Getter(lazy=true) private final Pattern pattern = Pattern.compile(getRegex(), Pattern.MULTILINE); | |||||
} |
@@ -0,0 +1,41 @@ | |||||
package org.cobbzilla.util.main; | |||||
import org.cobbzilla.util.io.FileUtil; | |||||
import org.cobbzilla.util.io.FilesystemWalker; | |||||
import java.util.Map; | |||||
import java.util.regex.Matcher; | |||||
import static org.cobbzilla.util.io.FileUtil.toFileOrDie; | |||||
import static org.cobbzilla.util.io.FileUtil.toStringOrDie; | |||||
public class FileHeaderMain extends BaseMain<FileHeaderOptions> { | |||||
public static void main (String[] args) { main(FileHeaderMain.class, args); } | |||||
@Override protected void run() throws Exception { | |||||
final FileHeaderOptions opts = getOptions(); | |||||
final Map<String, FileHeader> headers = opts.getHeaders(); | |||||
new FilesystemWalker() | |||||
.withDir(opts.getDir()) | |||||
.withVisitor(file -> { | |||||
final String ext = FileUtil.extension(file); | |||||
if (ext.startsWith(".")) { | |||||
final FileHeader header = headers.get(ext.substring(1)); | |||||
if (header != null) { | |||||
String contents = toStringOrDie(file); | |||||
if (contents == null) contents = ""; | |||||
final Matcher matcher = header.getPattern().matcher(contents); | |||||
if (matcher.find()) { | |||||
contents = contents.substring(0, matcher.start()) | |||||
+ header.getHeader() + "\n" + contents.substring(matcher.end()); | |||||
} else { | |||||
contents = header.getHeader() + "\n" + contents; | |||||
} | |||||
toFileOrDie(file, contents); | |||||
} | |||||
} | |||||
}).walk(); | |||||
} | |||||
} |
@@ -0,0 +1,44 @@ | |||||
package org.cobbzilla.util.main; | |||||
import lombok.Getter; | |||||
import lombok.Setter; | |||||
import org.apache.commons.io.IOUtils; | |||||
import org.kohsuke.args4j.Option; | |||||
import java.io.File; | |||||
import java.io.IOException; | |||||
import java.io.InputStream; | |||||
import java.util.Arrays; | |||||
import java.util.Map; | |||||
import java.util.stream.Collectors; | |||||
import static java.util.function.Function.identity; | |||||
import static org.cobbzilla.util.json.JsonUtil.json; | |||||
import static org.cobbzilla.util.string.StringUtil.UTF8cs; | |||||
public class FileHeaderOptions extends BaseMainOptions { | |||||
public static final String USAGE_DIR = "Directory to process"; | |||||
public static final String OPT_DIR = "-d"; | |||||
public static final String LONGOPT_DIR = "--dir"; | |||||
@Option(name=OPT_DIR, aliases=LONGOPT_DIR, usage=USAGE_DIR, required=true) | |||||
@Getter @Setter private File dir; | |||||
public static final String USAGE_HEADERS_JSON = "JSON file with header info, or - to read from stdin"; | |||||
public static final String OPT_HEADERS_JSON = "-H"; | |||||
public static final String LONGOPT_HEADERS_JSON = "--headers"; | |||||
@Option(name=OPT_HEADERS_JSON, aliases=LONGOPT_HEADERS_JSON, usage=USAGE_HEADERS_JSON, required=true) | |||||
@Getter @Setter private String headersJson; | |||||
public Map<String, FileHeader> getHeaders() throws IOException { | |||||
final InputStream input; | |||||
if (headersJson.equals("-")) { | |||||
input = inStream(null); | |||||
} else { | |||||
input = inStream(new File(headersJson)); | |||||
} | |||||
final FileHeader[] headers = json(IOUtils.toString(input, UTF8cs), FileHeader[].class); | |||||
return Arrays.stream(headers).collect(Collectors.toMap(FileHeader::getExt, identity())); | |||||
} | |||||
} |