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