001    /*
002     * Copyright 2010-2015 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.kotlin.js.config;
018    
019    import com.google.common.collect.Lists;
020    import com.intellij.openapi.project.Project;
021    import kotlin.Function1;
022    import kotlin.Unit;
023    import org.jetbrains.annotations.NotNull;
024    import org.jetbrains.annotations.Nullable;
025    import org.jetbrains.kotlin.descriptors.ModuleDescriptor;
026    import org.jetbrains.kotlin.psi.JetFile;
027    import org.jetbrains.kotlin.resolve.BindingContext;
028    import org.jetbrains.kotlin.resolve.BindingTrace;
029    import org.jetbrains.kotlin.resolve.BindingTraceContext;
030    
031    import java.util.Collection;
032    import java.util.List;
033    
034    /**
035     * Base class representing a configuration of translator.
036     */
037    public abstract class Config {
038        private final boolean inlineEnabled;
039    
040        @NotNull
041        private final Project project;
042        @Nullable
043        private List<JetFile> libFiles = null;
044        @NotNull
045        private final EcmaVersion target;
046    
047        @NotNull
048        private final String moduleId;
049    
050        private final boolean sourcemap;
051    
052        public Config(
053                @NotNull Project project,
054                @NotNull String moduleId,
055                @NotNull EcmaVersion ecmaVersion,
056                boolean sourcemap,
057                boolean inlineEnabled
058        ) {
059            this.project = project;
060            this.target = ecmaVersion;
061            this.moduleId = moduleId;
062            this.sourcemap = sourcemap;
063            this.inlineEnabled = inlineEnabled;
064        }
065    
066        public boolean isSourcemap() {
067            return sourcemap;
068        }
069    
070        public boolean isInlineEnabled() {
071            return inlineEnabled;
072        }
073    
074        @NotNull
075        public Project getProject() {
076            return project;
077        }
078    
079        @NotNull
080        public EcmaVersion getTarget() {
081            return target;
082        }
083    
084        @NotNull
085        public String getModuleId() {
086            return moduleId;
087        }
088    
089        public abstract  boolean checkLibFilesAndReportErrors(@NotNull Function1<String, Unit> report);
090    
091        @NotNull
092        protected abstract List<JetFile> generateLibFiles();
093    
094        @NotNull
095        public final List<JetFile> getLibFiles() {
096            if (libFiles == null) {
097                libFiles = generateLibFiles();
098            }
099            return libFiles;
100        }
101    
102        @Nullable
103        public BindingContext getLibraryContext() {
104            return null;
105        }
106    
107        @Nullable
108        public ModuleDescriptor getLibraryModule() {
109            return null;
110        }
111    
112        @NotNull
113        public static Collection<JetFile> withJsLibAdded(@NotNull Collection<JetFile> files, @NotNull Config config) {
114            Collection<JetFile> allFiles = Lists.newArrayList();
115            allFiles.addAll(files);
116            allFiles.addAll(config.getLibFiles());
117            return allFiles;
118        }
119    
120        public boolean isTestConfig() {
121            return false;
122        }
123    }