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 org.jetbrains.annotations.NotNull;
022    import org.jetbrains.annotations.Nullable;
023    import org.jetbrains.jet.lang.descriptors.ModuleDescriptor;
024    import org.jetbrains.jet.lang.psi.JetFile;
025    import org.jetbrains.jet.lang.resolve.BindingContext;
026    import org.jetbrains.k2js.translate.test.JSTester;
027    import org.jetbrains.k2js.translate.test.QUnitTester;
028    
029    import java.util.Arrays;
030    import java.util.Collection;
031    import java.util.Collections;
032    import java.util.List;
033    
034    /**
035     * Base class representing a configuration of translator.
036     */
037    public abstract class Config {
038        //NOTE: a hacky solution to be able to rerun code samples with lib loaded only once: used by tests and web demo
039        @NotNull
040        public static final String REWRITABLE_MODULE_NAME = "JS_TESTS";
041    
042        @NotNull
043        public static Config getEmptyConfig(@NotNull Project project, @NotNull EcmaVersion ecmaVersion) {
044            return new Config(project, "main", ecmaVersion) {
045                @NotNull
046                @Override
047                protected List<JetFile> generateLibFiles() {
048                    return Collections.emptyList();
049                }
050            };
051        }
052    
053        //NOTE: used by mvn build
054        @SuppressWarnings("UnusedDeclaration")
055        @NotNull
056        public static Config getEmptyConfig(@NotNull Project project) {
057            return getEmptyConfig(project, EcmaVersion.defaultVersion());
058        }
059    
060        @NotNull
061        public static final List<String> LIB_FILES_WITH_DECLARATIONS = Arrays.asList(
062                "/core/annotations.kt",
063                "/core/core.kt",
064                "/core/date.kt",
065                "/core/dom.kt",
066                "/core/javaio.kt",
067                "/core/javalang.kt",
068                "/core/javautil.kt",
069                "/core/javautilCollections.kt",
070                "/core/json.kt",
071                "/core/kotlin.kt",
072                "/core/math.kt",
073                "/core/string.kt",
074                "/core/htmlDom.kt",
075                "/html5/canvas.kt",
076                "/jquery/common.kt",
077                "/jquery/ui.kt",
078                "/junit/core.kt",
079                "/qunit/core.kt",
080                "/stdlib/browser.kt"
081        );
082    
083        @NotNull
084        public static final List<String> LIB_FILES_WITH_CODE = Arrays.asList(
085                "/stdlib/TuplesCode.kt",
086                "/core/javautilCollectionsCode.kt"
087        );
088    
089        @NotNull
090        public static final List<String> LIB_FILE_NAMES = Lists.newArrayList();
091    
092        static {
093            LIB_FILE_NAMES.addAll(LIB_FILES_WITH_DECLARATIONS);
094            LIB_FILE_NAMES.addAll(LIB_FILES_WITH_CODE);
095        }
096    
097        /**
098         * the library files which depend on the STDLIB files to be able to compile
099         */
100        @NotNull
101        public static final List<String> LIB_FILE_NAMES_DEPENDENT_ON_STDLIB = Arrays.asList(
102                "/core/stringsCode.kt",
103                "/stdlib/domCode.kt",
104                "/core/javautilCode.kt",
105                "/stdlib/jutilCode.kt",
106                "/stdlib/testCode.kt"
107        );
108    
109        public static final String LIBRARIES_LOCATION = "js/js.libraries/src";
110        public static final String REFLECTION_LIB_LOCATION = "core/reflection/src/kotlin/reflect";
111    
112        /**
113         * The file names in the standard library to compile
114         */
115        @NotNull
116        public static final List<String> STDLIB_FILE_NAMES = Arrays.asList(
117                "/kotlin/Preconditions.kt",
118                "/kotlin/Functions.kt",
119                "/kotlin/collections/JUtil.kt",
120                "/kotlin/collections/Iterators.kt",
121                "/kotlin/collections/Arrays.kt",
122                "/kotlin/collections/Maps.kt",
123                "/kotlin/collections/Exceptions.kt",
124                "/kotlin/collections/MutableCollections.kt",
125                "/kotlin/collections/Stream.kt",
126                "/kotlin/collections/AbstractIterator.kt",
127                "/generated/_Aggregates.kt",
128                "/generated/_Arrays.kt",
129                "/generated/_DownTo.kt",
130                "/generated/_Elements.kt",
131                "/generated/_Filtering.kt",
132                "/generated/_Generators.kt",
133                "/generated/_Guards.kt",
134                "/generated/_Mapping.kt",
135                "/generated/_Numeric.kt",
136                "/generated/_Ordering.kt",
137                "/generated/_Snapshots.kt",
138                "/generated/_Strings.kt",
139                "/generated/_Streams.kt",
140                "/kotlin/Standard.kt",
141                "/kotlin/Ranges.kt",
142                "/kotlin/Numbers.kt",
143                "/kotlin/text/Strings.kt",
144                "/kotlin/text/StringBuilder.kt",
145                "/kotlin/dom/Dom.kt",
146                "/kotlin/test/Test.kt"
147        );
148    
149        /**
150         * The location of the stdlib sources
151         */
152        public static final String STDLIB_LOCATION = "libraries/stdlib/src";
153    
154        @NotNull
155        private final Project project;
156        @Nullable
157        private List<JetFile> libFiles = null;
158        @NotNull
159        private final EcmaVersion target;
160    
161        @NotNull
162        private final String moduleId;
163    
164        private final boolean sourcemap;
165    
166        public Config(@NotNull Project project, @NotNull String moduleId, @NotNull EcmaVersion ecmaVersion) {
167            this(project, moduleId, ecmaVersion, false);
168        }
169    
170        public Config(@NotNull Project project, @NotNull String moduleId, @NotNull EcmaVersion ecmaVersion, boolean sourcemap) {
171            this.project = project;
172            this.target = ecmaVersion;
173            this.moduleId = moduleId;
174            this.sourcemap = sourcemap;
175        }
176    
177        public boolean isSourcemap() {
178            return sourcemap;
179        }
180    
181        @NotNull
182        public Project getProject() {
183            return project;
184        }
185    
186        @NotNull
187        public EcmaVersion getTarget() {
188            return target;
189        }
190    
191        @NotNull
192        public String getModuleId() {
193            return moduleId;
194        }
195    
196        @NotNull
197        protected abstract List<JetFile> generateLibFiles();
198    
199        @NotNull
200        public final List<JetFile> getLibFiles() {
201            if (libFiles == null) {
202                libFiles = generateLibFiles();
203            }
204            return libFiles;
205        }
206    
207        @Nullable
208        public BindingContext getLibraryContext() {
209            return null;
210        }
211    
212        @Nullable
213        public ModuleDescriptor getLibraryModule() {
214            return null;
215        }
216    
217        @NotNull
218        public static Collection<JetFile> withJsLibAdded(@NotNull Collection<JetFile> files, @NotNull Config config) {
219            Collection<JetFile> allFiles = Lists.newArrayList();
220            allFiles.addAll(files);
221            allFiles.addAll(config.getLibFiles());
222            return allFiles;
223        }
224    
225        //TODO: should be null by default I suppose but we can't communicate it to K2JSCompiler atm
226        @Nullable
227        public JSTester getTester() {
228            return new QUnitTester();
229        }
230    }