Artefact.java
4.2 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* This file is part of LiteLoader.
* Copyright (C) 2012-16 Adam Mummery-Smith
* All Rights Reserved.
*/
package com.mumfrey.liteloader.core.api.repository;
import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Specifier for a mod in the ivy repo shorthand notation
*/
public final class Artefact
{
/**
* Regex for matching valid specifiers
*/
private static final Pattern PATTERN = Pattern.compile("^([^:]+):([^:]+):([^:@]+?)(:([^:@]+?))?(@(zip|jar|litemod))?$");
/**
* Artefact group id
*/
private final String group;
/**
* Artefact name
*/
private final String name;
/**
* Artefact version
*/
private final String version;
/**
* Artefact classifier (can be null)
*/
private final String classifier;
/**
* Artefact type (can be null)
*/
private final String type;
/**
* Resolved file
*/
private File file;
public Artefact(String specifier)
{
if (specifier == null)
{
throw new IllegalArgumentException("Invalid artefact specifier: null");
}
Matcher matcher = Artefact.PATTERN.matcher(specifier);
if (!matcher.matches())
{
throw new IllegalArgumentException("Invalid artefact specifier: " + specifier);
}
this.group = matcher.group(1);
this.name = matcher.group(2);
this.version = matcher.group(3);
this.classifier = matcher.group(5);
this.type = matcher.group(7);
}
/**
* Get the artefact id consisting of the group and name
*
* @return
*/
public String getArtefactId()
{
return String.format("%s:%s", this.getGroup(), this.getName());
}
/**
* Get the path portion of this artefact's resolved location
*
* @return artefact path
*/
public String getPath()
{
return String.format("%s/%s/%s", this.getGroup().replace('.', '/'), this.getName(), this.getVersion());
}
/**
* Get the file name portion of this artefact's resolved location
*
* @return arefact file name
*/
public String getFileName()
{
return String.format("%s-%s%s.%s", this.getName(), this.getVersion(), this.getClassifier("-"), this.getType());
}
/**
* Get the artefact group
*/
public String getGroup()
{
return this.group;
}
/**
* Get the artefact name
*/
public String getName()
{
return this.name;
}
/**
* Get the artefact version
*/
public String getVersion()
{
return this.version;
}
/**
* Get the artefact classifier, returns an empty string if no classifier is
* set
*/
public String getClassifier()
{
return this.getClassifier("");
}
private String getClassifier(String prefix)
{
return this.classifier != null ? prefix + this.classifier : "";
}
/**
* Get the artefact type, defaults to "jar" if no type was specified
*/
public String getType()
{
return this.type != null ? this.type : "jar";
}
/**
* Resolve this artefact beneath the specified repository root
*
* @param repositoryRoot repository root
* @return resolved file
*/
void resolve(File repositoryRoot)
{
this.file = new File(new File(repositoryRoot, this.getPath()), this.getFileName());
}
/**
* Get whether the resolved artefact actually exists
*/
public boolean exists()
{
return this.file != null && this.file.isFile();
}
/**
* After resolution, return the resolved file location
*
* @return resolved location
*/
public File getFile()
{
return this.file;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString()
{
String type = this.type != null && !"jar".equals(this.type) ? "@" + this.type : "";
return String.format("%s:%s:%s%s%s", this.getGroup(), this.getName(), this.getVersion(), this.getClassifier(":"), type);
}
}