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        @NotNull
053        private final BindingTrace trace = new BindingTraceContext();
054    
055        public Config(
056                @NotNull Project project,
057                @NotNull String moduleId,
058                @NotNull EcmaVersion ecmaVersion,
059                boolean sourcemap,
060                boolean inlineEnabled
061        ) {
062            this.project = project;
063            this.target = ecmaVersion;
064            this.moduleId = moduleId;
065            this.sourcemap = sourcemap;
066            this.inlineEnabled = inlineEnabled;
067        }
068    
069        public boolean isSourcemap() {
070            return sourcemap;
071        }
072    
073        public boolean isInlineEnabled() {
074            return inlineEnabled;
075        }
076    
077        @NotNull
078        public Project getProject() {
079            return project;
080        }
081    
082        @NotNull
083        public EcmaVersion getTarget() {
084            return target;
085        }
086    
087        @NotNull
088        public String getModuleId() {
089            return moduleId;
090        }
091    
092        public abstract  boolean checkLibFilesAndReportErrors(@NotNull Function1<String, Unit> report);
093    
094        @NotNull
095        protected abstract List<JetFile> generateLibFiles();
096    
097        @NotNull
098        public final List<JetFile> getLibFiles() {
099            if (libFiles == null) {
100                libFiles = generateLibFiles();
101            }
102            return libFiles;
103        }
104    
105        @Nullable
106        public BindingContext getLibraryContext() {
107            return null;
108        }
109    
110        @NotNull
111        public BindingTrace getTrace() {
112            return trace;
113        }
114    
115        @Nullable
116        public ModuleDescriptor getLibraryModule() {
117            return null;
118        }
119    
120        @NotNull
121        public static Collection<JetFile> withJsLibAdded(@NotNull Collection<JetFile> files, @NotNull Config config) {
122            Collection<JetFile> allFiles = Lists.newArrayList();
123            allFiles.addAll(files);
124            allFiles.addAll(config.getLibFiles());
125            return allFiles;
126        }
127    
128        public boolean isTestConfig() {
129            return false;
130        }
131    }