Java Developer

ot-clean, Refactoring and cleaning DSL (4/4)

We haven’t changed the logic and still have the same code to use. This way we’ve separated behaviour and definition of cleaning from the logic of cleaning itself. It’s very clear and easy to add new definitions.

Summary

Maven definition before refactoring.

final class Maven implements Cleanable {
    private final Delete delete;

    public Maven(final Delete cdelete) {
        this.delete = cdelete;
    }

    @Override
    public void clean(final Path path) {
        if (this.match(path)) {
            this.delete.file(path.resolve("target"));
        }
    }

    @Override
    public boolean match(final Path path) {
        return path.resolve("pom.xml").toFile().exists();
    }

    @Override
    public void display(final Path path, final Console console) {
        console.print(
            String.format(
                "[Maven]: %s", path
            )
        );
    }
}

Maven definition after refactoring.

new Definition(
    Type.MAVEN,
    If.fileExists("pom.xml"),
    Then.delete("target")
),

List of all definitions.

List<Definition> DEFAULT = Lists.newArrayList(
    new Definition(
        Type.MAVEN,
        If.fileExists("pom.xml"),
        Then.delete("target")
    ),
    new Definition(
        Type.GRAILS_2,
        If.fileExistsWithRegExp(
            "application.properties",
            "app.grails.version"
        ),
        Then.delete("target", "**/*.log")
    ),
    new Definition(
        Type.GRAILS_3,
        If.fileExistsWithRegExp(
            "build.gradle",
            "apply plugin:.*org.grails"
        ),
        Then.delete("build", "**/*.log")
    ),
    new Definition(
        Type.OT_CLEAN,
        If.fileExists(".clean.yml"),
        Then.useYmlConfig()
    ),
    new Definition(
        Type.PLAYFRAMEWORK_2,
        If.fileExistsWithRegExp(
            "build.sbt",
            "enablePlugins\\(PlayJava\\)"
        ),
        Then.delete(
            "logs",
            "target",
            "project/target",
            "project/project/target",
            ".sbtserver",
            "**/*.log"
        )
    )
);

Is it better that way? :)

The ot-clean series

This blog post is one of the series about the ot-clean. Simple open source tool that maintains your projects directories cleaned from all the garbage. Feel free to look at the source code or readme page for more information. You can also submit pull request or post suggestion. This text participates in open source blogging competition.

Project status

Build Status

All posts from the ot-clean series


Like it? Share it on , , ,

About the author

Grzegorz Gajos, Software Architect with international consulting and programming background. Co-founder of Open Tangerine Software House. Quality evangelist. An experienced entrepreneur, out of the box thinker and problem solver.