@@ -60,22 +60,44 @@ private record IdAndDefinition(TransportVersionId id, TransportVersionDefinition
6060
6161 @ TaskAction
6262 public void validateTransportVersions () throws IOException {
63+ TransportVersionResourcesService resources = getResources ().get ();
6364 Set <String > referencedNames = TransportVersionReference .collectNames (getReferencesFiles ());
64- Map <String , TransportVersionDefinition > definitions = getResources ().get ().getNamedDefinitions ();
65- Map <Integer , List <IdAndDefinition >> idsByBase = collectIdsByBase (definitions .values ());
66- Map <String , TransportVersionLatest > latestByReleaseBranch = getResources ().get ().getLatestByReleaseBranch ();
65+ Map <String , TransportVersionDefinition > namedDefinitions = resources .getNamedDefinitions ();
66+ Map <String , TransportVersionDefinition > unreferencedDefinitions = resources .getUnreferencedDefinitions ();
67+ Map <String , TransportVersionDefinition > allDefinitions = collectAllDefinitions (namedDefinitions , unreferencedDefinitions );
68+ Map <Integer , List <IdAndDefinition >> idsByBase = collectIdsByBase (allDefinitions .values ());
69+ Map <String , TransportVersionLatest > latestByReleaseBranch = resources .getLatestByReleaseBranch ();
70+
71+ for (var definition : namedDefinitions .values ()) {
72+ validateNamedDefinition (definition , referencedNames );
73+ }
6774
68- for (var definition : definitions .values ()) {
69- validateDefinition (definition , referencedNames );
75+ for (var definition : unreferencedDefinitions .values ()) {
76+ validateUnreferencedDefinition (definition );
7077 }
7178
7279 for (var entry : idsByBase .entrySet ()) {
7380 validateBase (entry .getKey (), entry .getValue ());
7481 }
7582
7683 for (var latest : latestByReleaseBranch .values ()) {
77- validateLatest (latest , definitions , idsByBase );
84+ validateLatest (latest , allDefinitions , idsByBase );
85+ }
86+ }
87+
88+ private Map <String , TransportVersionDefinition > collectAllDefinitions (
89+ Map <String , TransportVersionDefinition > namedDefinitions ,
90+ Map <String , TransportVersionDefinition > unreferencedDefinitions
91+ ) {
92+ Map <String , TransportVersionDefinition > allDefinitions = new HashMap <>(namedDefinitions );
93+ for (var entry : unreferencedDefinitions .entrySet ()) {
94+ TransportVersionDefinition existing = allDefinitions .put (entry .getKey (), entry .getValue ());
95+ if (existing != null ) {
96+ Path unreferencedPath = getResources ().get ().getUnreferencedDefinitionRepositoryPath (entry .getValue ());
97+ throwDefinitionFailure (existing , "has same name as unreferenced definition [" + unreferencedPath + "]" );
98+ }
7899 }
100+ return allDefinitions ;
79101 }
80102
81103 private Map <Integer , List <IdAndDefinition >> collectIdsByBase (Collection <TransportVersionDefinition > definitions ) {
@@ -97,23 +119,17 @@ private Map<Integer, List<IdAndDefinition>> collectIdsByBase(Collection<Transpor
97119 return idsByBase ;
98120 }
99121
100- private void validateDefinition (TransportVersionDefinition definition , Set <String > referencedNames ) {
122+ private void validateNamedDefinition (TransportVersionDefinition definition , Set <String > referencedNames ) {
101123
102124 // validate any modifications
103125 Map <Integer , TransportVersionId > existingIdsByBase = new HashMap <>();
104126 TransportVersionDefinition originalDefinition = getResources ().get ().getNamedDefinitionFromMain (definition .name ());
105127 if (originalDefinition != null ) {
106-
107- int primaryId = definition .ids ().get (0 ).complete ();
108- int originalPrimaryId = originalDefinition .ids ().get (0 ).complete ();
109- if (primaryId != originalPrimaryId ) {
110- throwDefinitionFailure (definition , "has modified primary id from " + originalPrimaryId + " to " + primaryId );
111- }
112-
128+ validateIdenticalPrimaryId (definition , originalDefinition );
113129 originalDefinition .ids ().forEach (id -> existingIdsByBase .put (id .base (), id ));
114130 }
115131
116- if (referencedNames .contains (definition .name ()) == false && definition . name (). startsWith ( "initial_" ) == false ) {
132+ if (referencedNames .contains (definition .name ()) == false ) {
117133 throwDefinitionFailure (definition , "is not referenced" );
118134 }
119135 if (NAME_FORMAT .matcher (definition .name ()).matches () == false ) {
@@ -129,9 +145,7 @@ private void validateDefinition(TransportVersionDefinition definition, Set<Strin
129145 TransportVersionId id = definition .ids ().get (ndx );
130146
131147 if (ndx == 0 ) {
132- // TODO: initial versions will only be applicable to a release branch, so they won't have an associated
133- // main version. They will also be loaded differently in the future, but until they are separate, we ignore them here.
134- if (id .patch () != 0 && definition .name ().startsWith ("initial_" ) == false ) {
148+ if (id .patch () != 0 ) {
135149 throwDefinitionFailure (definition , "has patch version " + id .complete () + " as primary id" );
136150 }
137151 } else {
@@ -148,6 +162,30 @@ private void validateDefinition(TransportVersionDefinition definition, Set<Strin
148162 }
149163 }
150164
165+ private void validateUnreferencedDefinition (TransportVersionDefinition definition ) {
166+ TransportVersionDefinition originalDefinition = getResources ().get ().getUnreferencedDefinitionFromMain (definition .name ());
167+ if (originalDefinition != null ) {
168+ validateIdenticalPrimaryId (definition , originalDefinition );
169+ }
170+ if (definition .ids ().isEmpty ()) {
171+ throwDefinitionFailure (definition , "does not contain any ids" );
172+ }
173+ if (definition .ids ().size () > 1 ) {
174+ throwDefinitionFailure (definition , " contains more than one id" );
175+ }
176+ // note: no name validation, anything that is a valid filename is ok, this allows eg initial_8.9.1
177+ }
178+
179+ private void validateIdenticalPrimaryId (TransportVersionDefinition definition , TransportVersionDefinition originalDefinition ) {
180+ assert definition .name ().equals (originalDefinition .name ());
181+
182+ int primaryId = definition .ids ().get (0 ).complete ();
183+ int originalPrimaryId = originalDefinition .ids ().get (0 ).complete ();
184+ if (primaryId != originalPrimaryId ) {
185+ throwDefinitionFailure (definition , "has modified primary id from " + originalPrimaryId + " to " + primaryId );
186+ }
187+ }
188+
151189 private void validateLatest (
152190 TransportVersionLatest latest ,
153191 Map <String , TransportVersionDefinition > definitions ,
@@ -158,7 +196,7 @@ private void validateLatest(
158196 throwLatestFailure (latest , "contains transport version name [" + latest .name () + "] which is not defined" );
159197 }
160198 if (latestDefinition .ids ().contains (latest .id ()) == false ) {
161- Path relativePath = getResources ().get ().getRepositoryPath (latestDefinition );
199+ Path relativePath = getResources ().get ().getNamedDefinitionRepositoryPath (latestDefinition );
162200 throwLatestFailure (latest , "has id " + latest .id () + " which is not in definition [" + relativePath + "]" );
163201 }
164202
@@ -196,7 +234,7 @@ private void validateBase(int base, List<IdAndDefinition> ids) {
196234 IdAndDefinition current = ids .get (ndx );
197235
198236 if (previous .id ().equals (current .id ())) {
199- Path existingDefinitionPath = getResources ().get ().getRepositoryPath (previous .definition );
237+ Path existingDefinitionPath = getResources ().get ().getNamedDefinitionRepositoryPath (previous .definition );
200238 throwDefinitionFailure (
201239 current .definition (),
202240 "contains id " + current .id + " already defined in [" + existingDefinitionPath + "]"
@@ -213,12 +251,12 @@ private void validateBase(int base, List<IdAndDefinition> ids) {
213251 }
214252
215253 private void throwDefinitionFailure (TransportVersionDefinition definition , String message ) {
216- Path relativePath = getResources ().get ().getRepositoryPath (definition );
254+ Path relativePath = getResources ().get ().getNamedDefinitionRepositoryPath (definition );
217255 throw new IllegalStateException ("Transport version definition file [" + relativePath + "] " + message );
218256 }
219257
220258 private void throwLatestFailure (TransportVersionLatest latest , String message ) {
221- Path relativePath = getResources ().get ().getRepositoryPath (latest );
259+ Path relativePath = getResources ().get ().getLatestRepositoryPath (latest );
222260 throw new IllegalStateException ("Latest transport version file [" + relativePath + "] " + message );
223261 }
224262}
0 commit comments