001    /*
002     * Copyright 2010-2013 JetBrains s.r.o.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.jetbrains.k2js.config;
018    
019    import com.google.common.collect.Lists;
020    import com.intellij.openapi.project.Project;
021    import com.intellij.openapi.util.io.FileUtil;
022    import org.jetbrains.annotations.NotNull;
023    import org.jetbrains.jet.lang.psi.JetFile;
024    import org.jetbrains.k2js.utils.JetFileUtils;
025    
026    import java.io.File;
027    import java.io.IOException;
028    import java.util.Collections;
029    import java.util.List;
030    
031    public class LibrarySourceDirectoriesConfig extends Config {
032        @NotNull
033        protected final String[] directories;
034    
035        public LibrarySourceDirectoriesConfig(@NotNull Project project, @NotNull String moduleId, @NotNull String[] directories, @NotNull EcmaVersion ecmaVersion) {
036            super(project, moduleId, ecmaVersion);
037            this.directories = directories;
038        }
039    
040        @NotNull
041        @Override
042        public List<JetFile> generateLibFiles() {
043            try {
044                List<JetFile> results = Lists.newArrayList();
045                for (String directory : directories) {
046                    File rootDir = new File(directory);
047                    results.addAll(traverseDirectory(rootDir, rootDir));
048                }
049                return results;
050            }
051            catch (IOException e) {
052                System.out.println("Caught: " + e);
053                e.printStackTrace();
054                return Collections.emptyList();
055            }
056        }
057    
058        @NotNull
059        private List<JetFile> traverseDirectory(@NotNull File rootDir, @NotNull File dir) throws IOException {
060            File[] files = dir.listFiles();
061            File[] children = dir.listFiles();
062            List<JetFile> result = Lists.newArrayList();
063            if (children != null && children.length > 0) {
064                for (File child : children) {
065                    if (child.isDirectory()) {
066                        List<JetFile> childFiles = traverseDirectory(rootDir, child);
067                        result.addAll(childFiles);
068                    }
069                    else {
070                        String name = child.getName();
071                        if (name.toLowerCase().endsWith(".kt")) {
072                            String text = FileUtil.loadFile(child);
073                            //String path = FileUtil.getRelativePath(directoryFile, child);
074                            String path = child.getPath();
075                            JetFile jfile = JetFileUtils.createPsiFile(path, text, getProject());
076                            result.add(jfile);
077                        }
078                    }
079                }
080            }
081            return result;
082        }
083    }