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