|
1 | 1 | package pl.koder95.cmf;
|
2 | 2 |
|
3 | 3 | import org.apache.commons.cli.*;
|
| 4 | +import pl.koder95.cmf.format.JSON; |
4 | 5 |
|
| 6 | +import java.io.IOException; |
| 7 | +import java.nio.charset.StandardCharsets; |
| 8 | +import java.nio.file.Files; |
| 9 | +import java.nio.file.Path; |
| 10 | +import java.nio.file.Paths; |
| 11 | +import java.time.LocalDate; |
| 12 | +import java.time.LocalDateTime; |
| 13 | +import java.time.format.DateTimeFormatter; |
5 | 14 | import java.util.Arrays;
|
| 15 | +import java.util.Collections; |
| 16 | +import java.util.List; |
| 17 | +import java.util.stream.Collectors; |
| 18 | +import java.util.stream.IntStream; |
6 | 19 |
|
7 | 20 | public class Main {
|
8 | 21 |
|
| 22 | + private Main() {} |
| 23 | + |
9 | 24 | public static void main(String[] args) {
|
10 | 25 | System.out.println("Catholic Moveable Feasts by Kamil Jan Mularski [Koder95]. See more: https://koder95.pl/");
|
11 | 26 | System.out.println("- It is starting with arguments: " + Arrays.deepToString(args));
|
12 | 27 | System.out.println();
|
13 | 28 |
|
14 | 29 | Options options = new Options();
|
15 | 30 | options.addOption("h", "help", false, "wyświetla pomoc programu");
|
| 31 | + options.addOption("d", "dir", true, "określa katalog dla zapisania pliku"); |
| 32 | + options.addOption("n", "filename", true, "określa nazwę pliku"); |
| 33 | + options.addOption(Option.builder("f") |
| 34 | + .longOpt("format") |
| 35 | + .hasArg() |
| 36 | + .desc("określa format danych wyjściowych") |
| 37 | + .build()); |
| 38 | + options.addOption(Option.builder("y") |
| 39 | + .longOpt("years") |
| 40 | + .hasArg() |
| 41 | + .type(int[].class) |
| 42 | + .converter(string -> { |
| 43 | + System.out.println("Converting: " + string); |
| 44 | + String digits = "(\\d)+"; |
| 45 | + if (string.matches(digits)) { |
| 46 | + return new int[] { Integer.parseInt(string) }; |
| 47 | + } |
| 48 | + int dash = string.indexOf('-'); |
| 49 | + if (dash > 0) { |
| 50 | + int begin = Integer.parseInt(string.substring(0, dash)); |
| 51 | + int end = Integer.parseInt(string.substring(dash + 1)); |
| 52 | + return IntStream.rangeClosed(begin, end).toArray(); |
| 53 | + } else { |
| 54 | + String[] years = string.split("\\."); |
| 55 | + System.out.println("Parts: " + Arrays.deepToString(years)); |
| 56 | + return Arrays.stream(years).filter(s -> s.matches(digits)).mapToInt(Integer::parseInt).toArray(); |
| 57 | + } |
| 58 | + }) |
| 59 | + .build()); |
16 | 60 | CommandLineParser parser = new DefaultParser(false);
|
17 | 61 | try {
|
18 | 62 | CommandLine cl = parser.parse(options, args);
|
19 | 63 | if (cl.hasOption("h")) {
|
20 | 64 | HelpFormatter hf = new HelpFormatter();
|
21 | 65 | hf.printHelp("cmf", options);
|
| 66 | + } else { |
| 67 | + String dir = cl.hasOption("dir")? cl.getOptionValue("dir") : "./"; |
| 68 | + String filename = cl.hasOption("filename")? cl.getOptionValue("filename") : defaultFilename(); |
| 69 | + String format = cl.hasOption("format")? cl.getOptionValue("format") : "json"; |
| 70 | + int[] years = cl.hasOption("years")? cl.getParsedOptionValue("years") : new int[] { LocalDate.now().getYear() }; |
| 71 | + List<FinalMFR> result = generate(years); |
| 72 | + save(result, dir, filename, format); |
22 | 73 | }
|
23 |
| - } catch (ParseException e) { |
| 74 | + } catch (ParseException | IOException e) { |
24 | 75 | throw new RuntimeException(e);
|
25 | 76 | }
|
26 | 77 | }
|
| 78 | + |
| 79 | + private static String defaultFilename() { |
| 80 | + LocalDateTime now = LocalDateTime.now(); |
| 81 | + String formatted = now.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME); |
| 82 | + String adapted = formatted.replace('T', '_').replace(":", ""); |
| 83 | + String clipped = adapted.substring(0, adapted.indexOf('.')); |
| 84 | + return clipped + ".cmf"; |
| 85 | + } |
| 86 | + |
| 87 | + private static List<FinalMFR> generate(int... years) { |
| 88 | + System.out.println("Generating for years: " + Arrays.toString(years)); |
| 89 | + return IntStream.of(years).sorted().mapToObj(MFR_Basic::new).map(MFR_Basic::toStable).map(MFR_Stable::toFinal).collect(Collectors.toList()); |
| 90 | + } |
| 91 | + |
| 92 | + private static void save(List<FinalMFR> list, String dir, String filename, String format) throws IOException { |
| 93 | + if (format.endsWith("json")) { |
| 94 | + String json = JSON.toJson(list, format.replace("json", "").equals("pretty")); |
| 95 | + Path destin = Paths.get(dir, filename + ".json"); |
| 96 | + Files.write(destin, Collections.singleton(json), StandardCharsets.UTF_8); |
| 97 | + } |
| 98 | + } |
27 | 99 | }
|
0 commit comments