Home | History | Annotate | Download | only in srcgen
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.icu4j.srcgen;
     17 
     18 import com.google.common.collect.Lists;
     19 import com.google.currysrc.Main;
     20 import com.google.currysrc.api.Rules;
     21 import com.google.currysrc.api.input.InputFileGenerator;
     22 import com.google.currysrc.api.output.OutputSourceFileGenerator;
     23 import com.google.currysrc.api.process.Rule;
     24 import com.google.currysrc.processors.ReplaceTextCommentScanner;
     25 
     26 import java.io.File;
     27 import java.util.List;
     28 
     29 import static com.android.icu4j.srcgen.Icu4jTransformRules.createOptionalRule;
     30 
     31 /**
     32  * Applies Android's ICU4J source code transformation rules to test code, adds @RunWith annotations
     33  * to test so that they can be run with JUnit and fixes up the jcite start/end tags.
     34  *
     35  * <p>Intended for use when transforming test code.
     36  */
     37 public class Icu4jTestsTransform {
     38 
     39   private static final boolean DEBUG = false;
     40 
     41   private Icu4jTestsTransform() {
     42   }
     43 
     44   /**
     45    * Usage:
     46    * java com.android.icu4j.srcgen.Icu4jSampleTransform {source files/directories} {target dir}
     47    */
     48   public static void main(String[] args) throws Exception {
     49     new Main(DEBUG).execute(new Icu4jBasicRules(args));
     50   }
     51 
     52   private static class Icu4jBasicRules implements Rules {
     53 
     54     private final InputFileGenerator inputFileGenerator;
     55 
     56     private final List<Rule> rules;
     57 
     58     private final OutputSourceFileGenerator outputSourceFileGenerator;
     59 
     60     public Icu4jBasicRules(String[] args) {
     61       if (args.length < 2) {
     62         throw new IllegalArgumentException("At least 2 arguments required.");
     63       }
     64 
     65       String[] inputDirNames = new String[args.length - 1];
     66       System.arraycopy(args, 0, inputDirNames, 0, args.length - 1);
     67       inputFileGenerator = Icu4jTransformRules.createInputFileGenerator(inputDirNames);
     68       rules = createTransformRules();
     69       outputSourceFileGenerator = Icu4jTransformRules.createOutputFileGenerator(
     70           args[args.length - 1]);
     71     }
     72 
     73     @Override
     74     public List<Rule> getRuleList(File ignored) {
     75       return rules;
     76     }
     77 
     78     @Override
     79     public InputFileGenerator getInputFileGenerator() {
     80       return inputFileGenerator;
     81     }
     82 
     83     @Override
     84     public OutputSourceFileGenerator getOutputSourceFileGenerator() {
     85       return outputSourceFileGenerator;
     86     }
     87 
     88     private static List<Rule> createTransformRules() {
     89       List<Rule> rules =
     90               Lists.newArrayList(Icu4jTransform.Icu4jRules.getRepackagingRules());
     91 
     92       // Switch all embedded comment references from com.ibm.icu to android.icu.
     93       rules.add(
     94           createOptionalRule(new ReplaceTextCommentScanner(
     95               Icu4jTransform.ORIGINAL_ICU_PACKAGE, Icu4jTransform.ANDROID_ICU_PACKAGE)));
     96 
     97       // Change sample jcite begin / end tags ---XYZ to Androids 'BEGIN(XYZ)' / 'END(XYZ)'
     98       rules.add(createOptionalRule(new TranslateJcite.BeginEndTagsHandler()));
     99 
    100       return rules;
    101     }
    102   }
    103 }
    104