001/**
002 * Copyright 2013 John Ericksen
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 */
016package org.androidtransfuse.processor;
017
018import org.androidtransfuse.config.TransfuseAndroidModule;
019import org.androidtransfuse.model.manifest.Application;
020import org.androidtransfuse.model.manifest.Manifest;
021import org.androidtransfuse.transaction.AbstractCompletionTransactionWorker;
022import org.androidtransfuse.util.Logger;
023import org.androidtransfuse.util.ManifestSerializer;
024
025import javax.inject.Inject;
026import javax.inject.Named;
027import java.io.File;
028import java.util.Collections;
029
030/**
031 * @author John Ericksen
032 */
033public class GenerateModuleProcessor extends AbstractCompletionTransactionWorker<Void, Void> {
034
035    public static final String MANIFEST_PROCESSING_OPTION = "transfuseManifestProcessing";
036
037    private final ManifestManager manifestManager;
038    private final Merger merger;
039    private final Manifest originalManifest;
040    private final Logger logger;
041    private final File manifestFile;
042    private ManifestSerializer manifestParser;
043    private final String manifestProcessingOption;
044
045    @Inject
046    public GenerateModuleProcessor(ManifestManager manifestManager,
047                                   Merger merger,
048                                   @Named(TransfuseAndroidModule.ORIGINAL_MANIFEST)
049                                   Manifest originalManifest,
050                                   Logger logger,
051                                   @Named(TransfuseAndroidModule.MANIFEST_FILE)
052                                   File manifestFile,
053                                   ManifestSerializer manifestParser,
054                                   @Named(MANIFEST_PROCESSING_OPTION)
055                                   String manifestProcessingOption) {
056        this.manifestManager = manifestManager;
057        this.merger = merger;
058        this.originalManifest = originalManifest;
059        this.logger = logger;
060        this.manifestFile = manifestFile;
061        this.manifestParser = manifestParser;
062        this.manifestProcessingOption = manifestProcessingOption;
063    }
064
065    @Override
066    public Void innerRun(Void value) {
067
068        //assembling generated code
069        Manifest updatedManifest = buildManifest();
070
071        if(!"off".equals(manifestProcessingOption)){
072            if("log".equals(manifestProcessingOption)){
073                logger.info("Manifest generation piped to log");
074                manifestParser.writeManifest(updatedManifest, System.out);
075            }
076            else{
077                //write manifest back out, updating from processed classes
078                manifestParser.writeManifest(updatedManifest, manifestFile);
079            }
080        }
081        else{
082            logger.info("Manifest generation disabled");
083        }
084
085        return null;
086    }
087
088    private Manifest buildManifest() {
089        try {
090
091            Manifest manifest = manifestManager.getManifest();
092            Manifest mergedManifest = merger.merge(Manifest.class, originalManifest, manifest);
093
094            for (Application application : mergedManifest.getApplications()) {
095                Collections.sort(application.getActivities());
096            }
097
098            mergedManifest.updatePackages();
099
100            return mergedManifest;
101        } catch (MergerException e) {
102            logger.error("InstantiationException while merging manifest", e);
103            return originalManifest;
104        }
105    }
106}