1 package org.kuali.common.jute.kfs; 2 3 import static com.google.common.base.Charsets.UTF_8; 4 import static com.google.common.collect.Iterables.concat; 5 import static com.google.common.collect.Ordering.natural; 6 import static com.google.common.io.Files.readLines; 7 import static org.apache.commons.lang3.StringUtils.substringBetween; 8 import static org.kuali.common.jute.base.Exceptions.illegalState; 9 10 import java.io.File; 11 import java.io.IOException; 12 import java.util.List; 13 14 import org.kuali.common.jute.env.Environment; 15 import org.kuali.common.jute.kfs.annotation.Basedir; 16 import org.kuali.common.jute.kfs.annotation.DepFragment; 17 import org.kuali.common.jute.kfs.annotation.DepVersions; 18 import org.kuali.common.jute.kfs.annotation.Deps; 19 import org.kuali.common.jute.kfs.annotation.DepsContainer; 20 import org.kuali.common.jute.kfs.annotation.ExistingProperties; 21 import org.kuali.common.jute.kfs.annotation.NewProperties; 22 import org.kuali.common.jute.kfs.annotation.PomContent; 23 import org.kuali.common.jute.kfs.annotation.PomLines; 24 import org.kuali.common.jute.system.User; 25 26 import com.google.common.base.Joiner; 27 import com.google.common.base.Splitter; 28 import com.google.inject.AbstractModule; 29 import com.google.inject.Provides; 30 import com.google.inject.TypeLiteral; 31 32 public class KfsDepModule extends AbstractModule { 33 34 @Override 35 protected void configure() { 36 bind(new TypeLiteral<List<String>>() {}).annotatedWith(Deps.class).toProvider(DepsProvider.class); 37 bind(new TypeLiteral<List<List<String>>>() {}).annotatedWith(DepsContainer.class).toProvider(DepsContainerProvider.class); 38 bind(new TypeLiteral<List<String>>() {}).annotatedWith(DepVersions.class).toProvider(DepVersionsProvider.class); 39 bind(new TypeLiteral<String>() {}).annotatedWith(DepFragment.class).toProvider(DepFragmentProvider.class); 40 } 41 42 @Provides 43 @Basedir 44 protected File basedir(Environment env, User user) { 45 try { 46 String basedir = env.getProperty("kfs.basedir", user.getHome() + "/git/kfs"); 47 return new File(basedir).getCanonicalFile(); 48 } catch (IOException e) { 49 throw illegalState(e); 50 } 51 } 52 53 @Provides 54 @PomLines 55 protected List<String> pomLines(@Basedir File basedir) { 56 try { 57 File pom = new File(basedir, "pom.xml"); 58 return readLines(pom, UTF_8); 59 } catch (IOException e) { 60 throw illegalState(e); 61 } 62 } 63 64 @Provides 65 @PomContent 66 protected String pomContent(@PomLines List<String> lines) { 67 return Joiner.on('\n').join(lines); 68 } 69 70 @Provides 71 @ExistingProperties 72 protected List<String> existingProperties(@PomContent String content) { 73 return Splitter.on('\n').trimResults().omitEmptyStrings().splitToList(substringBetween(content, "<properties>", "</properties>")); 74 } 75 76 @Provides 77 @NewProperties 78 protected List<String> newProperties(@ExistingProperties List<String> existing, @DepVersions List<String> deps) { 79 return natural().immutableSortedCopy(concat(existing, deps)); 80 } 81 82 }