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