Merge pull request #578 from docker-java/issue-551 · docker-java/docker-java@15cb3ac
@@ -31,8 +31,9 @@ public class Dockerfile {
31313232public final File dockerFile;
333334-public Dockerfile(File dockerFile) {
34+private final File baseDirectory;
353536+public Dockerfile(File dockerFile, File baseDirectory) {
3637if (!dockerFile.exists()) {
3738throw new IllegalStateException(String.format("Dockerfile %s does not exist", dockerFile.getAbsolutePath()));
3839 }
@@ -43,6 +44,15 @@ public Dockerfile(File dockerFile) {
43444445this.dockerFile = dockerFile;
454647+if (!baseDirectory.exists()) {
48+throw new IllegalStateException(String.format("Base directory %s does not exist", baseDirectory.getAbsolutePath()));
49+ }
50+51+if (!baseDirectory.isDirectory()) {
52+throw new IllegalStateException(String.format("Base directory %s is not a directory", baseDirectory.getAbsolutePath()));
53+ }
54+55+this.baseDirectory = baseDirectory;
4656 }
47574858private static class LineTransformer implements Function<String, Optional<? extends DockerfileStatement>> {
@@ -76,7 +86,7 @@ public Iterable<DockerfileStatement> getStatements() throws IOException {
76867787public List<String> getIgnores() throws IOException {
7888List<String> ignores = new ArrayList<String>();
79-File dockerIgnoreFile = new File(getDockerFolder(), ".dockerignore");
89+File dockerIgnoreFile = new File(baseDirectory, ".dockerignore");
8090if (dockerIgnoreFile.exists()) {
8191int lineNumber = 0;
8292List<String> dockerIgnoreFileContent = FileUtils.readLines(dockerIgnoreFile);
@@ -102,10 +112,6 @@ public ScannedResult parse() throws IOException {
102112return new ScannedResult();
103113 }
104114105-public File getDockerFolder() {
106-return dockerFile.getParentFile();
107- }
108-109115/**
110116 * Result of scanning / parsing a docker file.
111117 */
@@ -116,7 +122,7 @@ public class ScannedResult {
116122final List<File> filesToAdd = new ArrayList<File>();
117123118124public InputStream buildDockerFolderTar() {
119-return buildDockerFolderTar(getDockerFolder());
125+return buildDockerFolderTar(baseDirectory);
120126 }
121127122128public InputStream buildDockerFolderTar(File directory) {
@@ -180,7 +186,7 @@ public ScannedResult() throws IOException {
180186"Dockerfile is excluded by pattern '%s' in .dockerignore file", matchingIgnorePattern));
181187 }
182188183-Collection<File> filesInBuildContext = FileUtils.listFiles(getDockerFolder(), TrueFileFilter.INSTANCE,
189+Collection<File> filesInBuildContext = FileUtils.listFiles(baseDirectory, TrueFileFilter.INSTANCE,
184190TrueFileFilter.INSTANCE);
185191186192for (File f : filesInBuildContext) {
@@ -217,7 +223,7 @@ private List<String> matchingIgnorePatterns(String fileName) {
217223 * will be respected.
218224 */
219225private String effectiveMatchingIgnorePattern(File file) {
220-String relativeFilename = FilePathUtil.relativize(getDockerFolder(), file);
226+String relativeFilename = FilePathUtil.relativize(baseDirectory, file);
221227222228List<String> matchingPattern = matchingIgnorePatterns(relativeFilename);
223229@@ -242,6 +248,6 @@ private String effectiveMatchingIgnorePattern(File file) {
242248 }
243249244250return lastMatchingPattern;
245- }
251+ }
246252 }
247253}