• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jreleaser / jreleaser / #477

04 Apr 2025 05:53PM UTC coverage: 35.124% (-5.1%) from 40.183%
#477

push

github

aalmiray
fix(deploy): Add missing Forgejo messages

Related to #1842

18210 of 51845 relevant lines covered (35.12%)

0.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.5
/api/jreleaser-model-api/src/main/java/org/jreleaser/mustache/MustacheUtils.java
1
/*
2
 * SPDX-License-Identifier: Apache-2.0
3
 *
4
 * Copyright 2020-2025 The JReleaser authors.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     https://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
package org.jreleaser.mustache;
19

20
import com.github.mustachejava.DefaultMustacheFactory;
21
import com.github.mustachejava.Mustache;
22
import com.github.mustachejava.MustacheException;
23
import com.github.mustachejava.MustacheFactory;
24
import com.github.mustachejava.TemplateFunction;
25
import org.jreleaser.bundle.RB;
26
import org.jreleaser.extensions.api.ExtensionManagerHolder;
27
import org.jreleaser.extensions.api.mustache.MustacheExtensionPoint;
28

29
import java.io.IOException;
30
import java.io.Reader;
31
import java.io.StringReader;
32
import java.io.StringWriter;
33
import java.io.Writer;
34
import java.util.LinkedHashMap;
35
import java.util.LinkedHashSet;
36
import java.util.Map;
37
import java.util.UUID;
38

39
import static org.jreleaser.util.StringUtils.isNotBlank;
40

41
/**
42
 * @author Andres Almiray
43
 * @since 0.1.0
44
 */
45
@org.jreleaser.infra.nativeimage.annotations.NativeImage
46
public final class MustacheUtils {
47
    private MustacheUtils() {
48
        //noop
49
    }
50

51
    private static Map<String, String> envVars() {
52
        Map<String, String> vars = new LinkedHashMap<>();
1✔
53
        System.getenv().forEach((k, v) -> {
1✔
54
            if (!k.startsWith("JRELEASER_")) {
1✔
55
                vars.put("Env." + k, v);
1✔
56
            }
57
        });
1✔
58
        return vars;
1✔
59
    }
60

61
    public static String applyTemplate(Reader reader, TemplateContext context, String templateName) {
62
        StringWriter input = new StringWriter();
1✔
63
        MustacheFactory mf = new MyMustacheFactory();
1✔
64
        Mustache mustache = mf.compile(reader, templateName);
1✔
65
        context.setAll(envVars());
1✔
66
        applyFunctions(context);
1✔
67
        mustache.execute(input, decorate(context.asMap()));
1✔
68
        input.flush();
1✔
69
        return input.toString();
1✔
70
    }
71

72
    private static Map<String, Object> decorate(Map<String, Object> context) {
73
        for (Map.Entry<String, Object> e : new LinkedHashSet<>(context.entrySet())) {
1✔
74
            Object value = e.getValue();
1✔
75

76
            if (value instanceof CharSequence) {
1✔
77
                String val = String.valueOf(value);
1✔
78
                if (val.contains("{{")) {
1✔
79
                    context.put(e.getKey(), (TemplateFunction) s -> val);
1✔
80
                }
81
            }
82
        }
1✔
83
        return context;
1✔
84
    }
85

86
    public static String applyTemplate(Reader reader, TemplateContext context) {
87
        return applyTemplate(reader, context, UUID.randomUUID().toString()).trim();
×
88
    }
89

90
    public static String applyTemplate(String template, TemplateContext context, String templateName) {
91
        return applyTemplate(new StringReader(template), context, templateName);
×
92
    }
93

94
    public static String applyTemplate(String template, TemplateContext context) {
95
        return applyTemplate(new StringReader(template), context, UUID.randomUUID().toString()).trim();
1✔
96
    }
97

98
    public static void applyTemplates(Map<String, Object> props, TemplateContext templates) {
99
        applyTemplates(new TemplateContext(props), templates);
×
100
    }
×
101

102
    public static void applyTemplates(TemplateContext props, Map<String, Object> templates) {
103
        for (Map.Entry<String, Object> e : new LinkedHashSet<>(templates.entrySet())) {
1✔
104
            Object value = e.getValue();
1✔
105

106
            if (value instanceof CharSequence) {
1✔
107
                String val = String.valueOf(value);
1✔
108
                if (val.contains("{{") && val.contains("}}")) {
1✔
109
                    value = applyTemplate(val, props);
×
110
                }
111
            }
112

113
            props.set(e.getKey(), value);
1✔
114
        }
1✔
115
    }
1✔
116

117
    public static void applyTemplates(TemplateContext props, TemplateContext templates) {
118
        for (Map.Entry<String, Object> e : new LinkedHashSet<>(templates.entries())) {
1✔
119
            Object value = e.getValue();
1✔
120

121
            if (value instanceof CharSequence) {
1✔
122
                String val = String.valueOf(value);
1✔
123
                if (val.contains("{{") && val.contains("}}")) {
1✔
124
                    value = applyTemplate(val, props);
1✔
125
                }
126
            }
127

128
            props.set(e.getKey(), value);
1✔
129
        }
1✔
130
    }
1✔
131

132
    public static String passThrough(String str) {
133
        return isNotBlank(str) ? "!!" + str + "!!" : str;
1✔
134
    }
135

136
    private static void applyFunctions(TemplateContext props) {
137
        ExtensionManagerHolder.get().findExtensionPoints(MustacheExtensionPoint.class)
1✔
138
            .forEach(ep -> ep.apply(props));
1✔
139
    }
1✔
140

141
    private static class MyMustacheFactory extends DefaultMustacheFactory {
142
        @Override
143
        public void encode(String value, Writer writer) {
144
            if (value.startsWith("!!") && value.endsWith("!!")) {
1✔
145
                try {
146
                    writer.write(value.substring(2, value.length() - 2));
1✔
147
                } catch (IOException e) {
×
148
                    throw new MustacheException(RB.$("ERROR_mustache_write_value", value), e);
×
149
                }
1✔
150
            } else {
151
                super.encode(value, writer);
1✔
152
            }
153
        }
1✔
154
    }
155
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc