-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurl.go
More file actions
98 lines (87 loc) · 2.64 KB
/
url.go
File metadata and controls
98 lines (87 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"errors"
"net/url"
"regexp"
"strings"
)
// buildURL constructs the Initializr starter URL from options.
func buildURL(o options) (string, error) {
if o.baseURL == "" {
return "", errors.New("base-url must not be empty")
}
base := strings.TrimRight(o.baseURL, "/") + "/starter."
switch strings.ToLower(o.target) {
case "zip":
base += "zip"
default:
return "", errors.New("unsupported target: " + o.target)
}
q := url.Values{}
add := func(k, v string) {
if strings.TrimSpace(v) != "" {
q.Set(k, v)
}
}
add("type", o.projectType)
add("language", o.language)
// Normalize historical suffix styles (e.g., .RELEASE, .BUILD-SNAPSHOT, .M7, .RC1)
add("bootVersion", normalizeBootVersion(o.bootVersion))
add("baseDir", o.baseDir)
add("groupId", o.groupID)
add("artifactId", o.artifactID)
add("name", o.name)
add("description", o.description)
add("packageName", o.packageName)
add("packaging", o.packaging)
add("javaVersion", o.javaVersion)
add("configurationFileFormat", o.configFileFormat)
deps := strings.TrimSpace(o.dependencies)
if deps != "" {
// normalize: remove whitespace around commas
parts := strings.Split(deps, ",")
for i := range parts {
parts[i] = strings.TrimSpace(parts[i])
}
q.Set("dependencies", strings.Join(parts, ","))
}
return base + "?" + q.Encode(), nil
}
// sanitizePackage normalizes a Java package name string from user inputs.
func sanitizePackage(s string) string {
s = strings.ToLower(s)
replacer := strings.NewReplacer("-", "", " ", "")
s = replacer.Replace(s)
for strings.Contains(s, "..") {
s = strings.ReplaceAll(s, "..", ".")
}
s = strings.Trim(s, ".")
return s
}
// normalizeBootVersion converts historical Spring Boot version notations
// to the forms accepted by modern Initializr servers.
// Examples:
// - 3.5.5.RELEASE -> 3.5.5
// - 2.0.0.BUILD-SNAPSHOT -> 2.0.0-SNAPSHOT
// - 2.0.0.M7 -> 2.0.0-M7
// - 2.0.0.RC1 -> 2.0.0-RC1
func normalizeBootVersion(s string) string {
s = strings.TrimSpace(s)
if s == "" {
return s
}
// RELEASE suffix: drop it (accept both ".RELEASE" and "-RELEASE")
s = strings.TrimSuffix(s, ".RELEASE")
s = strings.TrimSuffix(s, "-RELEASE")
// BUILD-SNAPSHOT: convert to -SNAPSHOT
s = strings.ReplaceAll(s, ".BUILD-SNAPSHOT", "-SNAPSHOT")
s = strings.ReplaceAll(s, "-BUILD-SNAPSHOT", "-SNAPSHOT")
// Rare: handle ".SNAPSHOT" -> "-SNAPSHOT"
if strings.HasSuffix(s, ".SNAPSHOT") {
s = strings.TrimSuffix(s, ".SNAPSHOT") + "-SNAPSHOT"
}
// Convert ".M<digits>" and ".RC<digits>" at the end to hyphenated form.
re := regexp.MustCompile(`\.(M|RC)(\d+)$`)
s = re.ReplaceAllString(s, "-$1$2")
return s
}