1
2
3
4
5 package com.qulice.checkstyle;
6
7 import java.io.File;
8 import java.io.IOException;
9 import java.nio.charset.StandardCharsets;
10 import org.apache.commons.io.FileUtils;
11 import org.cactoos.text.IoCheckedText;
12 import org.cactoos.text.Joined;
13
14
15
16
17
18 public final class License {
19
20
21
22
23 private String[] lines;
24
25
26
27
28 private String eol;
29
30
31
32
33 private String pkg = "foo";
34
35
36
37
38 private File directory;
39
40
41
42
43
44
45 public License withEol(final String txt) {
46 this.eol = txt;
47 return this;
48 }
49
50
51
52
53
54
55 public License withLines(final String... lns) {
56 this.lines = new String[lns.length];
57 System.arraycopy(lns, 0, this.lines, 0, lns.length);
58 return this;
59 }
60
61
62
63
64
65
66 public License withPackage(final String name) {
67 this.pkg = name;
68 return this;
69 }
70
71
72
73
74
75
76 public License savePackageInfo(final File dir) {
77 this.directory = dir;
78 return this;
79 }
80
81
82
83
84
85
86 public File file() throws IOException {
87 final File license = File.createTempFile("LICENSE", ".txt");
88 FileUtils.forceDeleteOnExit(license);
89 FileUtils.writeStringToFile(
90 license,
91 new IoCheckedText(new Joined(this.eol, this.lines)).asString(),
92 StandardCharsets.UTF_8
93 );
94 if (this.directory != null) {
95 this.makePackageInfo(this.directory);
96 }
97 return license;
98 }
99
100
101
102
103
104
105 @SuppressWarnings("PMD.AvoidDuplicateLiterals")
106 private void makePackageInfo(final File dir) throws IOException {
107 final File info = new File(dir, "package-info.java");
108 final StringBuilder body = new StringBuilder(100);
109 body.append("/*").append(this.eol);
110 for (final String line : this.lines) {
111 body.append(" *");
112 if (!line.isEmpty()) {
113 body.append(' ').append(line);
114 }
115 body.append(this.eol);
116 }
117 body.append(" */").append(this.eol)
118 .append("/**").append(this.eol)
119 .append(" * Hm...").append(this.eol)
120 .append(" */").append(this.eol)
121 .append("package ").append(this.pkg)
122 .append(';').append(this.eol);
123 FileUtils.writeStringToFile(
124 info,
125 body.toString(),
126 StandardCharsets.UTF_8
127 );
128 }
129
130 }