package org.leguan.gradle;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import org.leguan.language.file.Module;
import org.leguan.language.file.ModuleReader;
import org.leguan.language.gradle.GradleFile;
import org.leguan.language.gradle.GradleFileStrategies;
import org.leguan.language.gradle.GradleProject;
import org.leguan.refactoring.api.GradleLocation;
import org.leguan.refactoring.api.content.ChangeContentItem;
import org.leguan.refactoring.api.content.ChangeContentRefactoring;

/**
 * demo how to change versions or add dependencies in a gradle file by API
 */
public class ChangeContentGradleProject {

    public static void main(final String args[]) throws IOException {
        File currentPath = new File("").getAbsoluteFile();
        Module module = new ModuleReader(currentPath).getModule();

        GradleProject gradleProject = new GradleProject();
        gradleProject.read(module);

        GradleFile gradleFile = gradleProject.getGradleFileContainer(new GradleFileStrategies()).getSingleItem();

        ChangeContentRefactoring changeContentRefactoring = new ChangeContentRefactoring();

        //add content 'start' at the beginning of each file
        ChangeContentItem item = ChangeContentItem.addContent(GradleLocation.any().start(), "start");

        //add content 'end' at the end of each file
        ChangeContentItem item2 = ChangeContentItem.addContent(GradleLocation.any().end(), "end");

        //add content 'startClosure' at the beginning of closure allprojects in each file
        ChangeContentItem item3 = ChangeContentItem.addContent(GradleLocation.any().closure("allprojects").start(), "startClosure");

        //add content 'endClosure' at the end of closure allprojects in each file
        ChangeContentItem item4 = ChangeContentItem.addContent(GradleLocation.any().closure("allprojects").end(), "endClosure");

        //add content 'beforeBla' before a line which maches bla
        ChangeContentItem item5 = ChangeContentItem.addContent(GradleLocation.any().anchor("bla").start(), "beforeBla");

        //add content 'afterBla' after a line which maches bla
        ChangeContentItem item6 = ChangeContentItem.addContent(GradleLocation.any().anchor("bla").end(), "afterBla");

        changeContentRefactoring.execute(gradleFile, Arrays.asList(item, item2, item3, item4, item5, item6));
    }
}
