view src/main/java/org/icedrobot/ika/plugins/scm/IkaInitPlugin.java @ 37:5463492cd2c9

Add new init plugin, also remove old unused plugins.
author Mario Torre <neugens.limasoftware@gmail.com>
date Thu, 01 Sep 2011 23:16:55 +0200
parents
children 987433a90b52
line wrap: on
line source

/*
 * IKA - IcedRobot Kiosk Application
 * Copyright (C) 2011  IcedRobot team
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.icedrobot.ika.plugins.scm;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import joptsimple.OptionParser;

import joptsimple.OptionSet;

import org.icedrobot.ika.plugins.IkaPlugin;
import org.icedrobot.ika.plugins.IkaPluginException;
import org.icedrobot.ika.plugins.IkaPluginResult;
import org.icedrobot.ika.runtime.IkaRuntime;
import org.icedrobot.ika.runtime.scm.GITCommand;

/**
 * Clones an IcedRobot repository.
 */
public class IkaInitPlugin implements IkaPlugin {
    
    private static final String PROPERTIES =
        "/org/icedrobot/ika/plugins/borg/icedrobot.properties";
    
    static enum Options {

        SRC("icedrobot", "source repository"),
        DEST("dest", "target directory"),
        HELP("help", "display a short help");
        final private String option;
        final private String description;

        Options(String option, String description) {
            this.option = option;
            this.description = description;
        }

        public String getOption() {
            return option;
        }

        public String getDescription() {
            return description;
        }
    }
    
    static OptionParser parser;
    static {
        parser = new OptionParser() {
            {
                accepts(Options.HELP.getOption(),
                        Options.HELP.getDescription());
                accepts(Options.DEST.getOption(),
                        Options.DEST.getDescription()).withRequiredArg();
                accepts(Options.SRC.getOption(),
                        Options.SRC.getDescription()).withRequiredArg();
            }
        };
    }

    @Override
    public String getName() {
        return "clone";
    }

    @Override
    public String getDescription() {
        return "clones an icedrobot workspace";
    }
    
    @Override
    public IkaPluginResult execute(String[] args) {
        // clone without option, grabs everything from the known master
        // repository given the --icedrobot option, will grab the code from
        // the given repository, finally the --dest option sets the target
        // directory in all cases, it clones the fill workspace
        if (args == null) {
            displayUsage(parser);
            return IkaPluginResult.OK;
        }
        
        OptionSet options = parser.parse(args);
        if (options.has(Options.HELP.getOption())) {
            displayUsage(parser);
            return IkaPluginResult.OK;
        }
        
        try {
            String source = getSourceFromPropertyFile();
            List<String> commandLine = new ArrayList<String>();
            if (options.has(Options.SRC.getOption())) {
                source = (String) options.valueOf(Options.SRC.getOption());
            }
            commandLine.add(source);
            
            File dest = new File("./icedrobot");
            if (options.has(Options.DEST.getOption())) {
                String _dest = (String) options.valueOf(Options.DEST.getOption());
                commandLine.add(_dest);
                dest = new File(_dest);
            }
            
            // clone the root repository
            GITCommand command = new GITCommand("git", "clone",
                                                commandLine.toArray(new 
                                                   String[commandLine.size()]));
            System.err.println(command);
            IkaRuntime.exec(new File("."), command);
            
            // now we need to load the conf file to grab the subrepo list
            Set subrepos = getSubRepos(dest.getCanonicalPath());
            for (Object repo : subrepos) {
                command = new GITCommand("git", "clone", source + "/" + repo);
                System.err.println(command);
                IkaRuntime.exec(dest, command);
            }
            
        } catch (Exception e) {
            e.printStackTrace();
            displayUsage(parser);
        }
        
        return IkaPluginResult.OK;
    }
   
    private Set getSubRepos(String dest) {
        Properties subrepos = new Properties();
        try {
            System.err.println("reading: " + dest + "/.ika.properties");
            subrepos.load(new FileInputStream(dest + "/.ika.properties"));

        } catch (IOException ex) {
            throw new IkaPluginException("cannot load plugin list", ex);
        }
        
        return subrepos.keySet();
    }
        
    private String getSourceFromPropertyFile() {
        Properties def = new Properties();
        try {
            def.load(getClass().getResourceAsStream(PROPERTIES));

        } catch (IOException ex) {
            throw new IkaPluginException("cannot load plugin list", ex);
        }
        
        return def.getProperty("icedrobot");
    }
    
    private void displayUsage(OptionParser parser) {
        try {
            parser.printHelpOn(System.out);
            
        } catch (IOException ignored) { }
    }
}