PropertiesTask.java

package org.docascode.ant;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.Property;
import org.docascode.api.DocAsCode;
import org.docascode.api.UpdateCommand;
import org.docascode.api.core.errors.DocAsCodeException;
import org.docascode.api.event.Event;
import org.docascode.api.listener.EventListener;
import java.io.File;
import java.util.Map;

public class PropertiesTask extends Task implements EventListener {
    private File file = null;
    public void setFile(File file){
        this.file=file;
    }

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

    @Override
    public void execute() {
        Map<String, String> map;
        try {
            map = DocAsCode.update()
                    .setAction(UpdateCommand.Action.LIST)
                    .setFile(file)
                    .call();
        } catch (DocAsCodeException e) {
            throw new BuildException(e);
        }
        Property p = new Property();
        p.setProject(getProject());
        for (Map.Entry<String,String> entry : map.entrySet()){
            if (prefix!=null){
                p.setName(prefix+"."+entry.getKey());
            } else {
                p.setName(entry.getKey());
            }
            p.setValue(entry.getValue());
            p.execute();
        }
    }

    @Override
    public void fireEvent(Event event) {
        log(event.getMessage());
    }
}