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