View Javadoc
1   package com.github.searls.jasmine.io.scripts;
2   
3   import com.github.searls.jasmine.model.ScriptSearch;
4   import org.codehaus.plexus.util.StringUtils;
5   
6   import java.io.File;
7   import java.util.LinkedHashSet;
8   import java.util.List;
9   import java.util.Set;
10  
11  public class BasicScriptResolver implements ScriptResolver {
12  
13    private static final ResolvesLocationOfPreloadSources RESOLVES_PRELOAD_SOURCES = new ResolvesLocationOfPreloadSources();
14    private static final FindsScriptLocationsInDirectory FINDS_SCRIPT_LOCATIONS = new FindsScriptLocationsInDirectory();
15  
16    private final File baseDirectory;
17    private final ScriptSearch sourceScriptSearch;
18    private final ScriptSearch specScriptSearch;
19    private final List<String> preloadList;
20  
21    private Set<String> sources;
22    private Set<String> specs;
23    private Set<String> preloads;
24  
25    public BasicScriptResolver(File baseDirectory,
26                               ScriptSearch sourceScriptSearch,
27                               ScriptSearch specScriptSearch,
28                               List<String> preloadList) {
29      this.baseDirectory = baseDirectory;
30      this.sourceScriptSearch = sourceScriptSearch;
31      this.specScriptSearch = specScriptSearch;
32      this.preloadList = preloadList;
33      resolveScripts();
34    }
35  
36    private void resolveScripts() {
37      this.preloads = new LinkedHashSet<String>(RESOLVES_PRELOAD_SOURCES.resolve(
38        this.preloadList,
39        this.sourceScriptSearch.getDirectory(),
40        this.specScriptSearch.getDirectory()));
41      this.sources = new LinkedHashSet<String>(FINDS_SCRIPT_LOCATIONS.find(this.sourceScriptSearch));
42      this.sources.removeAll(this.preloads);
43  
44      this.specs = new LinkedHashSet<String>(FINDS_SCRIPT_LOCATIONS.find(this.specScriptSearch));
45      this.specs.removeAll(this.preloads);
46    }
47  
48    @Override
49    public String getSourceDirectory() {
50      return directoryToString(this.sourceScriptSearch.getDirectory());
51    }
52  
53    @Override
54    public String getSpecDirectory() {
55      return directoryToString(this.specScriptSearch.getDirectory());
56    }
57  
58    @Override
59    public String getBaseDirectory() {
60      return directoryToString(this.baseDirectory);
61    }
62  
63    @Override
64    public Set<String> getSources() {
65      return sources;
66    }
67  
68    @Override
69    public Set<String> getSpecs() {
70      return specs;
71    }
72  
73    @Override
74    public Set<String> getPreloads() {
75      return preloads;
76    }
77  
78    @Override
79    public Set<String> getAllScripts() {
80      LinkedHashSet<String> allScripts = new LinkedHashSet<String>();
81      allScripts.addAll(this.getPreloads());
82      allScripts.addAll(this.getSources());
83      allScripts.addAll(this.getSpecs());
84      return allScripts;
85    }
86  
87    private String directoryToString(File directory) {
88      return StringUtils.stripEnd(directory.toURI().toString(), "/");
89    }
90  }