package org.jenkinsci.plugins.yamlproject;
import hudson.Launcher;
import hudson.Extension;
import hudson.util.FormValidation;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.AbstractProject;
import hudson.tasks.Builder;
import hudson.tasks.BuildStepDescriptor;
import net.sf.json.JSONObject;

import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.QueryParameter;
import org.yaml.snakeyaml.Yaml;

import javax.servlet.ServletException;

import java.io.IOException;
import java.io.PrintStream;
import java.util.List;

/**
 * Build a project according to .jenkins.yml.
 *
 * <p>
 * When the user configures the project and enables this builder,
 * {@link DescriptorImpl#newInstance(StaplerRequest)} is invoked
 * and a new {@link YamlBuilder} is created. The created
 * instance is persisted to the project configuration XML by using
 * XStream, so this allows you to use instance fields (like {@link #name})
 * to remember the configuration.
 *
 * <p>
 * When a build is performed, the {@link #perform(AbstractBuild, Launcher, BuildListener)}
 * method will be invoked. 
 *
 */
public class YamlBuilder extends Builder {

    // Fields in config.jelly must match the parameter names in the "DataBoundConstructor"
    @DataBoundConstructor
    public YamlBuilder() {
    }

    @Override
    public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) {
        // This is where you 'build' the project.
        PrintStream logger = listener.getLogger();
        
        String document = "\n- Hesperiidae\n- Papilionidae\n- Apatelodidae\n- Epiplemidae";
        
        Yaml yaml = new Yaml();
        @SuppressWarnings("unchecked")
            List<String> list = (List<String>) yaml.load(document);

        logger.println("after load");
        logger.println(list);
        logger.println("after list");

        return true;
    }

    // Overridden for better type safety.
    // If your plugin doesn't really define any property on Descriptor,
    // you don't have to do this.
    @Override
    public DescriptorImpl getDescriptor() {
        return (DescriptorImpl)super.getDescriptor();
    }

    /**
     * Descriptor for {@link YamlBuilder}. Used as a singleton.
     * The class is marked as public so that it can be accessed from views.
     *
     * <p>
     * See <tt>src/main/resources/hudson/plugins/hello_world/YamlBuilder/*.jelly</tt>
     * for the actual HTML fragment for the configuration screen.
     */
    @Extension // This indicates to Jenkins that this is an implementation of an extension point.
    public static final class DescriptorImpl extends BuildStepDescriptor<Builder> {
        /**
         * In order to load the persisted global configuration, you have to 
         * call load() in the constructor.
         */
        public DescriptorImpl() {
            load();
        }

        public boolean isApplicable(Class<? extends AbstractProject> aClass) {
            // Indicates that this builder can be used with all kinds of project types 
            return true;
        }

        /**
         * This human readable name is used in the configuration screen.
         */
        public String getDisplayName() {
            return "Build according to .jenkins.yml";
        }
    }
}


@@ -81,24 +81,48 @@
 .Extension;%0A
+import hudson.FilePath;%0A
 import hudso
@@ -1505,110 +1505,488 @@
 er) 
-%7B%0A        // This is where you 'build' the project.%0A        PrintStream logger = listener.getLogger();
+throws IOException, InterruptedException %7B%0A        // Fetch the YAML file, then build accordingly.%0A%0A        final FilePath workspace = build.getWorkspace();%0A        final FilePath ymlPath = workspace.child(%22.jenkins.yml%22);%0A%0A        PrintStream logger = listener.getLogger();%0A        if (!ymlPath.exists()) %7B%0A            logger.println(%22No .jenkins.yml found in%22 + workspace);%0A            return false;%0A        %7D else %7B%0A            logger.println(%22.jenkins.yml exists!%22);%0A        %7D
 %0A   

Check for a .jenkins.yml file.